/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.laughingpanda.halo.io; import java.io.File; import java.io.FileFilter; import java.util.Arrays; import java.util.Collections; import java.util.List; import junit.framework.TestCase; import org.laughingpanda.halo.io.DirectoryScanner; import org.laughingpanda.halo.stub.io.StubDirectory; import org.laughingpanda.halo.stub.io.StubEmptyFile; import org.laughingpanda.halo.stub.io.StubFile; /** * @author Pekka Enberg * @author Markus Hjort */ public final class DirectoryScannerTest extends TestCase { private DirectoryScanner scannerThatAcceptsAll; private DirectoryScanner scannerThatAcceptsJavaOnly; private List files; private StubEmptyFile directoryWithSubDirectories; @Override protected void setUp() throws Exception { scannerThatAcceptsAll = new DirectoryScanner(new FileFilter() { public boolean accept(@SuppressWarnings("unused") File pathname) { return true; } }); FileFilter endsWithFilter = new FileFilter() { public boolean accept(File file) { return file.getAbsolutePath().endsWith(".java"); } }; scannerThatAcceptsJavaOnly = new DirectoryScanner(endsWithFilter); files = Arrays.asList(new StubFile("/usr/src/Hello.java"), new StubFile("/usr/src/World.java")); List directories = Arrays.asList(new File[]{new StubDirectory("/usr/src", files)}); directoryWithSubDirectories = new StubDirectory("/usr", directories); } public void testScanEmptyDirectory() throws Exception { assertEquals(Collections.EMPTY_LIST, scannerThatAcceptsAll.scan(new StubDirectory("/usr/src"))); } public void testScanDirectoryWithOnlyFiles() { List actual = scannerThatAcceptsAll.scan(new StubDirectory("/usr/src/", files)); assertEquals(files, actual); } public void testScanDirectoryWithFilter() { List actual = scannerThatAcceptsAll.scan(directoryWithSubDirectories); assertEquals(files, actual); } public void testScanDirectoryRecursively() { List actual = scannerThatAcceptsJavaOnly.scan(directoryWithSubDirectories); assertEquals(files, actual); } public void testScanFileFails() { try { scannerThatAcceptsJavaOnly.scan(new StubFile("Hello.java")); fail("did not throw exception"); } catch (IllegalArgumentException expected) { } } }