TransformationOperation extends TransformationUtility<T> { protected final File getOrCreateReadFile(File transformedAppFolder, TransformationContext transformationContext) throws IOException { if (readFile == null) { File originalFile = getAbsoluteFile(transformedAppFolder, transformationContext); if (!originalFile.exists()) { throw new IOException("Specified file does not exist: " + originalFile.getAbsolutePath()); } if (originalFile.isDirectory()) { throw new IOException("Specified file is a directory: " + originalFile.getAbsolutePath()); } readFile = File.createTempFile(READ_FILE_PREFIX, null); FileUtils.copyFile(originalFile, readFile); readFile.setReadOnly(); } return readFile; } TransformationOperation(); @Override final PerformResult perform(File transformedAppFolder, TransformationContext transformationContext);  }
@Test public void readFileTest() throws IOException { TransformationOperation transformationOperation = getNewTestTransformationOperation().relative("pom.xml"); File readFile = transformationOperation.getOrCreateReadFile(transformedAppFolder, transformationContext); FileUtils.contentEquals(new File(transformedAppFolder, "pom.xml"), readFile); }
@Test public void nonexistentReadFileTest() { try { TransformationOperation transformationOperation = getNewTestTransformationOperation().relative("foo"); transformationOperation.getOrCreateReadFile(transformedAppFolder, transformationContext); fail("transformationOperation.getOrCreateReadFile was supposed to have thrown an exception"); } catch (IOException e) { assertEquals(e.getMessage(), "Specified file does not exist: " + new File(transformedAppFolder, "foo").getAbsolutePath()); } }
@Test public void dirReadFileTest() { try { TransformationOperation transformationOperation = getNewTestTransformationOperation().relative(""); transformationOperation.getOrCreateReadFile(transformedAppFolder, transformationContext); fail("transformationOperation.getOrCreateReadFile was supposed to have thrown an exception"); } catch (IOException e) { assertEquals(e.getMessage(), "Specified file is a directory: " + transformedAppFolder.getAbsolutePath()); } }