ExecutionRequestChecker implements Runnable { @Override public void run() { Collection<File> files = FileUtils.listFiles(configuration.getTetrisHomeDir(), new String[]{"war"}, false); if (files.isEmpty()) { return; } File warFile = files.iterator().next(); String userName = warFile.getName().replace(".war", "").split("@")[0]; try { gameExecutorService.runGame(userName, warFile); } catch (Exception e) { logger.error("Unable to execute game for user: " + userName + ", app:" + warFile.getAbsolutePath(), e); } finally { try { FileUtils.moveFile(warFile, new File(configuration.getArchiveDir(), warFile.getName())); } catch (IOException e) { logger.error("Unable to archive app file for user: " + userName + ", app:" + warFile.getAbsolutePath(), e); } } } ExecutionRequestChecker(ServiceConfiguration configuration, GameExecutorService gameExecutorService);  ExecutionRequestChecker(); @PostConstruct void init(); @Override void run();  }
@Test public void shouldExecuteWhenOneRequest() throws IOException { File warFile = createAppfile("vasya@2012-10-11 123456.war"); checker.run(); verify(executorService).runGame(userCaptor.capture(), appCaptor.capture()); assertExecutedApp("vasya", warFile); }
@Test public void shouldArchiveExecutedApps() throws IOException { createAppfile("vasya@2012-10-11 123456.war", "bla-bla"); checker.run(); assertEquals("bla-bla", readFileToString(new File(archiveFolder, "vasya@2012-10-11 123456.war"))); }
@Test public void shouldNotFailWhenException() throws IOException { createAppfile("petya.war", "something"); doThrow(new RuntimeException()).when(executorService).runGame(anyString(), any(File.class)); checker.run(); assertEquals("something", readFileToString(new File(archiveFolder, "petya.war"))); }
@Test public void shouldExecuteOnce() throws IOException { File warFile = createAppfile("vasya.war"); checker.run(); checker.run(); verify(executorService, times(1)).runGame(userCaptor.capture(), appCaptor.capture()); assertExecutedApp("vasya", warFile); }