public void testIsExpired() throws Exception { // test that removing a file, or modifying it, expires the cached item File basedir = new File(testdir, "testIsExpired"); basedir.mkdirs(); final FileCache<ProviderLoader> loaderFileCache = new FileCache<ProviderLoader>(); test scanner = new test(basedir, loaderFileCache, 0); File testfile1 = new File(basedir, "service_provider"); assertTrue(testfile1.createNewFile()); File testfile2 = new File(basedir, "service2_provider"); assertTrue(testfile2.createNewFile()); final ProviderIdent ident = new ProviderIdent("service", "provider"); final ProviderIdent ident2 = new ProviderIdent("service2", "provider"); assertEquals(testfile1, scanner.scanForFile(ident)); assertEquals(testfile2, scanner.scanForFile(ident2)); assertFalse(scanner.isExpired(ident, testfile1)); assertFalse(scanner.isExpired(ident2, testfile2)); // test delete assertTrue(testfile1.delete()); assertTrue(scanner.isExpired(ident, testfile1)); assertFalse(scanner.isExpired(ident2, testfile2)); // modify file final FileOutputStream fileOutputStream = new FileOutputStream(testfile2); fileOutputStream.write("blah".getBytes()); fileOutputStream.close(); assertTrue(scanner.isExpired(ident2, testfile2)); assertEquals(testfile2, scanner.scanForFile(ident2)); assertFalse(scanner.isExpired(ident2, testfile2)); }
public void testshouldRescanInterval() throws Exception { // set scan interval to 60 seconds, shouldRescan should now return false File basedir = new File(testdir, "testshouldRescan"); basedir.mkdirs(); final FileCache<ProviderLoader> loaderFileCache = new FileCache<ProviderLoader>(); // scan interval set to 60 seconds test scanner = new test(basedir, loaderFileCache, 60 * 1000); File testfile1 = new File(basedir, "service_provider"); assertTrue(testfile1.createNewFile()); File testfile2 = new File(basedir, "service2_provider"); assertTrue(testfile2.createNewFile()); final ProviderIdent ident = new ProviderIdent("service", "provider"); final ProviderIdent ident2 = new ProviderIdent("service2", "provider"); // initial pass should require scan assertTrue(scanner.shouldRescan()); // cause initial scan assertEquals(testfile1, scanner.scanForFile(ident)); assertFalse(scanner.shouldRescan()); // modify a file final FileOutputStream fileOutputStream = new FileOutputStream(testfile2); fileOutputStream.write("blah".getBytes()); fileOutputStream.close(); assertFalse(scanner.shouldRescan()); // rescan assertEquals(testfile2, scanner.scanForFile(ident2)); assertFalse(scanner.shouldRescan()); // delete a file testfile1.delete(); assertFalse(scanner.shouldRescan()); // rescan assertNull(scanner.scanForFile(ident)); assertFalse(scanner.shouldRescan()); }
public void testshouldRescan() throws Exception { // test rescan should return true if any file has been modified, and we are over the rescan // interval File basedir = new File(testdir, "testshouldRescan"); basedir.mkdirs(); final FileCache<ProviderLoader> loaderFileCache = new FileCache<ProviderLoader>(); test scanner = new test(basedir, loaderFileCache, 0); File testfile1 = new File(basedir, "service_provider"); assertTrue(testfile1.createNewFile()); File testfile2 = new File(basedir, "service2_provider"); assertTrue(testfile2.createNewFile()); final ProviderIdent ident = new ProviderIdent("service", "provider"); final ProviderIdent ident2 = new ProviderIdent("service2", "provider"); // initial pass should require scan assertTrue(scanner.shouldRescan()); // cause initial scan assertEquals(testfile1, scanner.scanForFile(ident)); assertFalse(scanner.shouldRescan()); // modify a file final FileOutputStream fileOutputStream = new FileOutputStream(testfile2); fileOutputStream.write("blah".getBytes()); fileOutputStream.close(); assertTrue(scanner.shouldRescan()); // rescan assertEquals(testfile2, scanner.scanForFile(ident2)); assertFalse(scanner.shouldRescan()); // delete a file testfile1.delete(); assertTrue(scanner.shouldRescan()); // rescan assertNull(scanner.scanForFile(ident)); assertFalse(scanner.shouldRescan()); }
public void testscanForFile() throws Exception { File basedir = new File(testdir, "testscanForFile"); final FileCache<ProviderLoader> loaderFileCache = new FileCache<ProviderLoader>(); test scanner = new test(basedir, loaderFileCache, 0); assertFalse(basedir.isDirectory()); assertFalse(basedir.exists()); // directory dne final File file = scanner.scanForFile(new ProviderIdent("test1", "test")); assertNull(file); assertTrue(basedir.mkdirs()); assertTrue("basedir does not exist: " + basedir.getAbsolutePath(), basedir.isDirectory()); assertTrue(basedir.exists()); // empty directory final File file2 = scanner.scanForFile(new ProviderIdent("test1", "test")); assertNull(file2); // create test file File testfile1 = new File(basedir, "service_provider"); assertTrue(testfile1.createNewFile()); // file not matched final File test3 = scanner.scanForFile(new ProviderIdent("test1", "test")); assertNull(test3); assertTrue(testfile1.exists()); assertTrue(testfile1.isFile()); // matched file final File test4 = scanner.scanForFile(new ProviderIdent("service", "provider")); assertNotNull(test4); assertEquals(testfile1, test4); testfile1.delete(); // delete file assertNull(scanner.scanForFile(new ProviderIdent("service", "provider"))); // create invalid file File testfile2 = new File(basedir, "service-provider"); assertTrue(testfile2.createNewFile()); assertNull(scanner.scanForFile(new ProviderIdent("service", "provider"))); }