/** * Creates a dummy file block containing a single string literal as its body. * * @return Dummy file block. */ private FileBlock getDummyFileBlock() { final FileBlock mtlFileBlock = MtlFactory.eINSTANCE.createFileBlock(); mtlFileBlock.setOpenMode(OpenModeKind.OVER_WRITE); mtlFileBlock.getBody().add(createOCLExpression('\'' + OUTPUT + '\'')); getDummyTemplate().getBody().add(mtlFileBlock); return mtlFileBlock; }
/** Tests the files containing "@generated" JMerge are properly merged. */ public void testFileBlockJMergeContent() { final FileBlock mtlFileBlock = getDummyFileBlock(); final String fileName = "validFile.java"; // $NON-NLS-1$ mtlFileBlock.setFileUrl(createOCLExpression('\'' + fileName + '\'')); mtlFileBlock.getBody().clear(); final String packageDeclaration = "package test;"; // $NON-NLS-1$ final String classDeclaration = "class Class {"; // $NON-NLS-1$ String generatedTag = "/** @generated */"; // $NON-NLS-1$ String methodDeclaration = "public void test() { } }"; // $NON-NLS-1$ mtlFileBlock.getBody().add(createOCLExpression('\'' + packageDeclaration + '\'')); mtlFileBlock.getBody().add(createOCLLineSeparator()); mtlFileBlock.getBody().add(createOCLExpression('\'' + classDeclaration + '\'')); mtlFileBlock.getBody().add(createOCLLineSeparator()); mtlFileBlock.getBody().add(createOCLExpression('\'' + generatedTag + '\'')); mtlFileBlock.getBody().add(createOCLLineSeparator()); mtlFileBlock.getBody().add(createOCLExpression('\'' + methodDeclaration + '\'')); evaluationVisitor.visitExpression(getParentTemplate(mtlFileBlock)); assertSame("Expecting a single preview", 1, getPreview().size()); // $NON-NLS-1$ Map.Entry<String, String> entry = getPreview().entrySet().iterator().next(); assertEquals( "Unexpected file URL.", //$NON-NLS-1$ generationRoot.getAbsolutePath() + File.separatorChar + fileName, entry.getKey()); assertEquals( "File hasn't been created as expected.", packageDeclaration + LINE_SEPARATOR //$NON-NLS-1$ + classDeclaration + LINE_SEPARATOR + generatedTag + LINE_SEPARATOR + methodDeclaration, entry.getValue().toString()); methodDeclaration = methodDeclaration.replace("test()", "changedMethodName()"); // $NON-NLS-1$ //$NON-NLS-2$ mtlFileBlock.getBody().remove(6); mtlFileBlock.getBody().add(createOCLExpression('\'' + methodDeclaration + '\'')); String expected = packageDeclaration + LINE_SEPARATOR + classDeclaration + LINE_SEPARATOR + generatedTag + LINE_SEPARATOR + methodDeclaration; /* * We've changed the method name but didn't alter the "@generated" tag. We then expect the generated * file to be altered as well. */ evaluationVisitor.visitExpression(getParentTemplate(mtlFileBlock)); assertSame("Expecting a single preview", 1, getPreview().size()); // $NON-NLS-1$ entry = getPreview().entrySet().iterator().next(); assertEquals( "Unexpected file URL.", //$NON-NLS-1$ generationRoot.getAbsolutePath() + File.separatorChar + fileName, entry.getKey()); assertEquals( "File hasn't been modified as expected.", expected, entry.getValue().toString()); // $NON-NLS-1$ methodDeclaration = methodDeclaration.replace( "changedMethodName()", "notGenerated()"); // $NON-NLS-1$ //$NON-NLS-2$ generatedTag = generatedTag.replace("@generated", "@generated not"); // $NON-NLS-1$ //$NON-NLS-2$ mtlFileBlock.getBody().remove(4); mtlFileBlock.getBody().add(4, createOCLExpression('\'' + generatedTag + '\'')); mtlFileBlock.getBody().remove(6); mtlFileBlock.getBody().add(createOCLExpression('\'' + methodDeclaration + '\'')); /* * Both the method name and "@generated" tag have changed. The generated file shouldn't be modified * when generating again. */ expected = packageDeclaration + LINE_SEPARATOR + classDeclaration + LINE_SEPARATOR + generatedTag + LINE_SEPARATOR + methodDeclaration; evaluationVisitor.visitExpression(getParentTemplate(mtlFileBlock)); assertSame("Expecting a single preview", 1, getPreview().size()); // $NON-NLS-1$ entry = getPreview().entrySet().iterator().next(); assertEquals( "Unexpected file URL.", //$NON-NLS-1$ generationRoot.getAbsolutePath() + File.separatorChar + fileName, entry.getKey()); assertEquals( "File shouldn't have been modified.", expected, //$NON-NLS-1$ entry.getValue().toString()); }