@Test(expected = IllegalStateException.class) public void renameFile_removingOrphanedFileTest() throws MalformedURLException, IOException, SecurityException, NoSuchMethodException { final File from = folder.newFile(); // cache file does not exist final IMockBuilder<File> builder = EasyMock.createMockBuilder(File.class); builder.withConstructor(URI.class); builder.withArgs(from.toURI()); builder.addMockedMethod(File.class.getMethod("exists")); builder.addMockedMethod(File.class.getMethod("renameTo", File.class)); final File fileMock = builder.createMock(); // final File fileMock = builder.addMockedMethod("exists").addMockedMethod("renameTo", // File.class).createMock(); EasyMock.expect(fileMock.exists()).andReturn(true).anyTimes(); EasyMock.expect(fileMock.renameTo(EasyMock.anyObject(File.class))).andReturn(false).anyTimes(); EasyMock.replay(fileMock); CachingXmlDataStore.renameFile(fileMock, folder.newFile()); EasyMock.verify(fileMock); }
@Test public void deleteFile_successful() throws IOException, SecurityException, NoSuchMethodException { final File file = folder.newFile(); // cache file does not exist final IMockBuilder<File> builder = EasyMock.createMockBuilder(File.class); builder.withConstructor(URI.class); builder.withArgs(file.toURI()); builder.addMockedMethod(File.class.getMethod("exists")); builder.addMockedMethod(File.class.getMethod("delete")); final File fileMock = builder.createMock(); EasyMock.expect(fileMock.exists()).andReturn(true).anyTimes(); EasyMock.expect(fileMock.delete()).andReturn(true).anyTimes(); EasyMock.replay(fileMock); CachingXmlDataStore.deleteFile(fileMock); EasyMock.verify(fileMock); }
@Test public void readAndSave_renamingFailsTest() throws MalformedURLException, IOException { final File cache = folder.newFile(); // cache file does not exist final IMockBuilder<File> builder = EasyMock.createMockBuilder(File.class); builder.withConstructor(URI.class); builder.withArgs(cache.toURI()); final File fileMock = builder.addMockedMethod("renameTo", File.class).createMock(); EasyMock.expect(fileMock.renameTo(EasyMock.anyObject(File.class))).andReturn(false).anyTimes(); EasyMock.replay(fileMock); // Assert.assertTrue(temp.length() == 0); // does not work on any system Assert.assertTrue(FileUtil.isEmpty(fileMock, DataStore.DEFAULT_CHARSET)); // file will be created CachingXmlDataStore.readAndSave(DATA_URL, fileMock, CHARSET); Assert.assertTrue(fileMock.length() >= 722015); // file will be overwritten (delete and rename) CachingXmlDataStore.readAndSave(DATA_URL, fileMock, CHARSET); Assert.assertTrue(fileMock.length() >= 722015); }