FileSourceFactory implements SourceFactory { @Override public List<FileSource> createSources(String srcName) { final File dirOrFile = new File(srcName); final File[] files = dirOrFile.listFiles(FILTER); if (files != null) { final List<FileSource> sources = new ArrayList<FileSource>(files.length); for (File file : files) { sources.add(new FileSource(file)); } return sources; } else { return Util.list(new FileSource(dirOrFile)); } }  @Override List<FileSource> createSources(String srcName);  }
@Test public void testValidFile() throws IOException { final File temp = File.createTempFile("testFactory", "java"); try { final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(temp), "UTF-8")); try { writer.write("hello"); } finally { writer.close(); } final SourceFactory factory = new FileSourceFactory(); final List<? extends Source> sources = factory.createSources(temp.getAbsolutePath()); assertEquals(1, sources.size()); final Source source = sources.get(0); final BufferedReader reader = source.createReader(); try { assertEquals("hello", reader.readLine()); assertEquals(temp.getAbsolutePath(), source.getSrcInfo()); } finally { reader.close(); } } finally { temp.delete(); } }
@Test public void testInValidFile() throws IOException { final File temp = File.createTempFile("testFactory", "java"); temp.delete(); final SourceFactory factory = new FileSourceFactory(); final List<? extends Source> sources = factory.createSources(temp.getAbsolutePath()); final Source source = sources.get(0); try { final BufferedReader reader = source.createReader(); try { assertEquals("hello", reader.readLine()); assertEquals(temp.getAbsolutePath(), source.getSrcInfo()); } finally { reader.close(); } fail("Did not get an exception"); } catch (RuntimeException e) { } }
@Test public void testDir() throws IOException { final File tempDir = TestUtil.createTmpDir(); try { final List<File> tempFiles = new ArrayList<File>(); try { for (int i=0; i<5; i++) { tempFiles.add(createTempFile(tempDir, i)); } final SourceFactory factory = new FileSourceFactory(); final List<? extends Source> sources = factory.createSources(tempDir.getAbsolutePath()); assertEquals(5, sources.size()); Collections.sort(sources, new SourceComparator()); for (int i=0; i<5; i++) { checkSource(i, tempDir, sources.get(i)); } } finally { for (File tempFile : tempFiles) { tempFile.delete(); } } } finally { tempDir.delete(); } }