ZipDataChecker implements Checker<File> { @Override public void check(File file) throws AppException { AppExceptionUtil.assertNotNull(file); File[] files = file.listFiles(); if (files == null) { throw new AppException("Data file is invalid."); } Set<String> fileSet = new HashSet<>(); List<String> outputFileList = new LinkedList<>(); for (File current : files) { if (current.isDirectory()) { throw new AppException("Problem information contains sub-directory."); } if (current.getName().endsWith(".in")) { fileSet.add(FileUtil.getFileName(current)); } else if (current.getName().endsWith(".out")) { outputFileList.add(FileUtil.getFileName(current)); } else if (current.getName().equals("spj.cc")) { } else if (current.getName().equals("problemInfo.xml")) { } else { throw new AppException("Problem information directory contains unknown file type."); } } if (outputFileList.size() != fileSet.size()) { throw new AppException("Some data files has not input file or output file."); } if (fileSet.size() == 0) { throw new AppException("No test data."); } for (String outputFile : outputFileList) { if (!fileSet.contains(outputFile)) { throw new AppException("Some data files has not input file or output file."); } } }  @Override void check(File file);  }
@Test public void testCheck_withSpjFile_oneCase() throws AppException { when(directory.listFiles()).thenReturn(new File[]{dataInput1, dataOutput1, spjFile}); checker.check(directory); }
@Test public void testCheck_withSpjFile_moreCases() throws AppException { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataOutput1, spjFile, dataInput2, dataOutput2, dataInput3, dataOutput3}); checker.check(directory); }
@Test public void testCheck_withInvalidDataName() { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataOutput1, spjFile, otherFile}); try { checker.check(directory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(new AppException("Problem information directory contains unknown file type."), e); } }
@Test public void testCheck_withDirectory() { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataOutput1, spjFile, otherDirectory}); try { checker.check(directory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(e, new AppException("Problem information contains sub-directory.")); } }
@Test public void testCheck_withNullDirectory() throws AppException { try { checker.check(null); Assert.fail(); } catch (AppException expected) { LOGGER.info("expected", expected); } }
@Test public void testCheck_invalidDataDirectory() { try { checker.check(otherDirectory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(e, new AppException("Data file is invalid.")); } }
@Test public void testCheck_notSameInputsAndOutputs() { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataOutput1, dataInput2}); try { checker.check(directory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(e, new AppException("Some data files has not input file or output file.")); } }
@Test public void testCheck_missMatchInputAndOutput() { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataOutput1, dataInput2, dataOutput3}); try { checker.check(directory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(e, new AppException("Some data files has not input file or output file.")); } }
@Test public void testCheck_noTestData() { when(directory.listFiles()).thenReturn(new File[] {}); try { checker.check(directory); Assert.fail(); } catch (AppException e) { Assert.assertEquals(e, new AppException("No test data.")); } }
@Test public void testCheck_withoutSpjFile_oneCase() throws AppException { when(directory.listFiles()).thenReturn(new File[]{dataInput1, dataOutput1}); checker.check(directory); }
@Test public void testCheck_withoutSpjFile_moreCases() throws AppException { when(directory.listFiles()).thenReturn( new File[]{dataInput1, dataInput2, dataOutput1, dataOutput2}); checker.check(directory); }