package org.eclipse.gmf.tests.samples; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.Path; import org.eclipse.gmf.codegen.gmfgen.GenEditorGenerator; import org.eclipse.gmf.tests.setup.RuntimeBasedGeneratorConfiguration; public abstract class LinksSampleTestBase extends BaseSampleTest { public LinksSampleTestBase(String name, RuntimeBasedGeneratorConfiguration genConfig) { super(name, genConfig); } protected void doTestAspects(GenEditorGenerator editorGen) throws Exception { genAndCompile(editorGen, getGenerationConfiguration()); final String foundCommentExtendedCommon = "* @author generated by aspects.CommonExtended"; final String foundCommentBaseCommon = "@generated"; Map<String, Boolean> fragment2found = new HashMap<String, Boolean>(); fragment2found.put(foundCommentExtendedCommon, false); fragment2found.put(foundCommentBaseCommon, false); findLineFragments(editorGen, "/edit/parts/ChildNodeBEditPart.java", fragment2found); assertTrue("Aspects code was not invoked.", fragment2found.get(foundCommentExtendedCommon)); assertTrue("@generated should be kept to allow merge of generation iterations", fragment2found.get(foundCommentBaseCommon)); } protected void findLineFragments(GenEditorGenerator editorGen, String srcPath, Map<String, Boolean> fragment2found) throws Exception { IProject diagramProject = ResourcesPlugin.getWorkspace().getRoot().getProject(editorGen.getPluginDirectory().split("/")[1]); assertTrue(diagramProject.exists()); IFile file = diagramProject.getFile(new Path("src/" + diagramProject.getName().replace(".", "/") + srcPath)); assertTrue(file.exists()); BufferedReader classToTest = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset())); String line; while ((line = classToTest.readLine()) != null) { line = line.trim(); for (String fragment : fragment2found.keySet()) { if (line.contains(fragment)) { fragment2found.put(fragment, true); } } } } protected void doTestCustomExtension(GenEditorGenerator editorGen, String path) throws Exception { genAndCompile(editorGen, getGenerationConfiguration()); IProject aspectsProject = ResourcesPlugin.getWorkspace().getRoot().getProject(editorGen.getPluginDirectory().split("/")[1]); assertTrue(aspectsProject.exists()); IFile file = aspectsProject.getFile(new Path(path)); assertTrue(file.exists()); BufferedReader classToTest = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset())); String pluginID = null; String line; while ((line = classToTest.readLine()) != null) { if (line.trim().startsWith("private static String SOME_VAR = \"TEST_PLUGIN_NAME = ")) { pluginID = line.trim().replaceFirst("private static String SOME_VAR = \"TEST_PLUGIN_NAME = ", "").replace("\";", ""); } } assertEquals(pluginID, editorGen.getPlugin().getName()); } }