@Test public void testGetMemoFilePermissionsDirectory() throws Exception { String uuid = UUID.randomUUID().toString(); File directory = new File(System.getProperty("java.io.tmpdir"), uuid); memoizer = new Memoizer(reader, 0, directory); // Check non-existing memo directory returns null assertEquals(memoizer.getMemoFile(id), null); // Create memoizer directory and memoizer reader directory.mkdirs(); memoizer = new Memoizer(reader, 0, directory); // Check existing non-writeable memo directory returns null if (File.separator.equals("/")) { // File.setWritable() does not work properly on Windows directory.setWritable(false); assertEquals(memoizer.getMemoFile(id), null); } // Check existing writeable memo diretory returns a memo file directory.setWritable(true); String memoDir = idDir.getAbsolutePath(); memoDir = memoDir.substring(memoDir.indexOf(File.separator) + 1); File memoFile = new File(directory, memoDir); memoFile = new File(memoFile, "." + TEST_FILE + ".bfmemo"); assertEquals(memoizer.getMemoFile(id).getAbsolutePath(), memoFile.getAbsolutePath()); }
@Test public void testConstructorReaderTimeElapsedDirectory() throws Exception { String uuid = UUID.randomUUID().toString(); File directory = new File(System.getProperty("java.io.tmpdir"), uuid); memoizer = new Memoizer(reader, 0, directory); // Check non-existing memo directory returns null assertEquals(memoizer.getMemoFile(id), null); // Create memoizer directory and memoizer reader directory.mkdirs(); String memoDir = idDir.getAbsolutePath(); memoDir = memoDir.substring(memoDir.indexOf(File.separator) + 1); File memoFile = new File(directory, memoDir); memoFile = new File(memoFile, "." + TEST_FILE + ".bfmemo"); File f = memoizer.getMemoFile(id); assertEquals(f.getAbsolutePath(), memoFile.getAbsolutePath()); // Test multiple setId invocations memoizer.setId(id); assertFalse(memoizer.isLoadedFromMemo()); assertTrue(memoizer.isSavedToMemo()); memoizer.close(); memoizer.setId(id); assertTrue(memoizer.isLoadedFromMemo()); assertFalse(memoizer.isSavedToMemo()); memoizer.close(); }
@Test public void testConstructorReaderTimeElapsedNull() throws Exception { memoizer = new Memoizer(reader, 0, null); // Check null memo directory returns null assertEquals(memoizer.getMemoFile(id), null); // Test setId invocation memoizer.setId(id); assertFalse(memoizer.isLoadedFromMemo()); assertFalse(memoizer.isSavedToMemo()); memoizer.close(); }
@Test public void testGetMemoFilePermissionsInPlace() throws Exception { memoizer = new Memoizer(reader); // Check non-writeable file directory returns null for in-place caching if (File.separator.equals("/")) { // File.setWritable() does not work properly on Windows idDir.setWritable(false); assertEquals(memoizer.getMemoFile(id), null); } // Check writeable file directory returns memo file beside file idDir.setWritable(true); File memoFile = new File(idDir, "." + TEST_FILE + ".bfmemo"); assertEquals(memoizer.getMemoFile(id).getAbsolutePath(), memoFile.getAbsolutePath()); }
@Test public void testSimple() throws Exception { memoizer = new Memoizer(reader); File f = memoizer.getMemoFile(id); if (f != null && f.exists()) { f.delete(); } // At this point we're sure that there's no memo file. reader.setId(id); reader.close(); memoizer.setId(id); memoizer.close(); memoizer.setId(id); memoizer.close(); }
@Test public void testConstructorReader() throws Exception { memoizer = new Memoizer(reader); File f = memoizer.getMemoFile(id); File memoFile = new File(idDir, "." + TEST_FILE + ".bfmemo"); assertEquals(f.getAbsolutePath(), memoFile.getAbsolutePath()); }
@Test public void testConstructorReaderTimeElapsed() throws Exception { memoizer = new Memoizer(reader, 0); File f = memoizer.getMemoFile(id); File memoFile = new File(idDir, "." + TEST_FILE + ".bfmemo"); assertEquals(f.getAbsolutePath(), memoFile.getAbsolutePath()); // Test multiple setId invocations memoizer.setId(id); assertFalse(memoizer.isLoadedFromMemo()); assertTrue(memoizer.isSavedToMemo()); memoizer.close(); memoizer.setId(id); assertTrue(memoizer.isLoadedFromMemo()); assertFalse(memoizer.isSavedToMemo()); memoizer.close(); }
@Test public void testRelocate() throws Exception { // Create an in-place memo file memoizer = new Memoizer(reader, 0); memoizer.setId(id); memoizer.close(); assertFalse(memoizer.isLoadedFromMemo()); assertTrue(memoizer.isSavedToMemo()); // Rename the directory (including the file and the memo file) String uuid = UUID.randomUUID().toString(); File newidDir = new File(System.getProperty("java.io.tmpdir"), uuid); idDir.renameTo(newidDir); File newtempFile = new File(newidDir, TEST_FILE); String newid = newtempFile.getAbsolutePath(); // Try to reopen the file with the Memoizer memoizer.setId(newid); memoizer.close(); assertFalse(memoizer.isLoadedFromMemo()); assertTrue(memoizer.isSavedToMemo()); }
@AfterMethod public void tearDown() throws Exception { memoizer.close(); reader.close(); }