PomFile extends AddElementTO<PomFile> { @Override protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { if (attribute != null) { if (!transformationContext.contains(attribute)) { return TOExecutionResult.error(this, new TransformationOperationException("Transformation context attribute " + attribute + " does not exist")); } Object modelObj = transformationContext.get(attribute); if (modelObj == null) { return TOExecutionResult.error(this, new TransformationOperationException("Transformation context attribute " + attribute + " is null")); } if (!(modelObj instanceof Model)) { return TOExecutionResult.error(this, new TransformationOperationException("Transformation context attribute " + attribute + " is not a Model object, but " + modelObj.getClass().getName())); } model = (Model) modelObj; } else if (model == null) { return TOExecutionResult.error(this, new TransformationOperationException("Neither transformation context attribute nor Model object were specified")); } File pomFile = new File(getAbsoluteFile(transformedAppFolder, transformationContext), "pom.xml"); TOExecutionResult errorResult; if (pomFile.exists()) { switch (ifPresent) { case Fail: return TOExecutionResult.error(this, new TransformationOperationException("There is already a pom file at " + getRelativePath())); case WarnNotAdd: return TOExecutionResult.warning(this, new TransformationOperationException("There is already a pom file at " + getRelativePath())); case NoOp: return TOExecutionResult.noOp(this, "There is already a pom file at " + getRelativePath()); case WarnButAdd: errorResult = writePomFile(pomFile); if (errorResult != null) return errorResult; return TOExecutionResult.warning(this, new TransformationOperationException("There is already a pom file at " + getRelativePath()), "Pom file overwritten at " + getRelativePath()); case Overwrite: errorResult = writePomFile(pomFile); if (errorResult != null) return errorResult; return TOExecutionResult.success(this, "Pom file overwritten at " + getRelativePath()); } } else { try { pomFile.createNewFile(); } catch (IOException e) { return TOExecutionResult.error(this, new TransformationOperationException("There was an error when creating pom.xml file at " + getRelativePath() + ", double check if that directory exists")); } } errorResult = writePomFile(pomFile); if (errorResult != null) return errorResult; return TOExecutionResult.success(this, "Pom file written at " + getRelativePath()); } PomFile();  PomFile(String attribute);  PomFile(Model model); PomFile setAttribute(String attribute); PomFile setModel(Model model); String getAttribute(); Model getModel(); @Override String getDescription();  }
@Test public void noModelNoAttributeFailTest() { TOExecutionResult executionResult = new PomFile().execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Neither transformation context attribute nor Model object were specified"); }
@Test public void nonExistentAttributeFailTest() { TOExecutionResult executionResult = new PomFile("ATT").execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Transformation context attribute ATT does not exist"); }
@Test public void nullAttributeFailTest() { Mockito.when(transformationContext.contains("ATT")).thenReturn(true); Mockito.when(transformationContext.get("ATT")).thenReturn(null); TOExecutionResult executionResult = new PomFile("ATT").execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Transformation context attribute ATT is null"); }
@Test public void invalidAttributeFailTest() { Mockito.when(transformationContext.contains("ATT")).thenReturn(true); Mockito.when(transformationContext.get("ATT")).thenReturn(""); TOExecutionResult executionResult = new PomFile("ATT").execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Transformation context attribute ATT is not a Model object, but java.lang.String"); }
@Test public void invalidFolderFailTest() { TOExecutionResult executionResult = new PomFile(model).relative("gigs").execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "There was an error when creating pom.xml file at gigs, double check if that directory exists"); }
@Test public void existentErrorFailTest() { TOExecutionResult executionResult = new PomFile(model).relative("/").execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "There is already a pom file at /"); }
@Test public void existentNoOpTest() { TOExecutionResult executionResult = new PomFile(model).relative("/").noOpIfPresent().execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.NO_OP); assertNull(executionResult.getException()); assertEquals(executionResult.getDetails(), "There is already a pom file at /"); }
@Test public void existentWarnNotAddTest() { TOExecutionResult executionResult = new PomFile(model).relative("/").warnNotAddIfPresent().execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.WARNING); assertNull(executionResult.getException()); assertEquals(executionResult.getWarnings().get(0).getClass(), TransformationOperationException.class); assertEquals(executionResult.getWarnings().get(0).getMessage(), "There is already a pom file at /"); }
@Test public void existentWarnButAddTest() { assertTrue(new File(transformedAppFolder, "pom.xml").exists()); TOExecutionResult executionResult = new PomFile(model).relative("/").warnButAddIfPresent().execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.WARNING); assertNull(executionResult.getException()); assertEquals(executionResult.getWarnings().get(0).getClass(), TransformationOperationException.class); assertEquals(executionResult.getWarnings().get(0).getMessage(), "There is already a pom file at /"); assertEquals(executionResult.getDetails(), "Pom file overwritten at /"); checkCreatedPomFile("pom.xml"); }
@Test public void existentOverwriteTest() { assertTrue(new File(transformedAppFolder, "pom.xml").exists()); TOExecutionResult executionResult = new PomFile(model).relative("/").overwriteIfPresent().execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertNull(executionResult.getException()); assertEquals(executionResult.getWarnings().size(), 0); assertEquals(executionResult.getDetails(), "Pom file overwritten at /"); checkCreatedPomFile("pom.xml"); }