コード例 #1
0
 public void testCustomPreferences() throws Exception {
   DiaGenSource s = createLibraryGen(false);
   final GenDiagram gd = s.getGenDiagram();
   GenCustomPreferencePage pp = GMFGenFactory.eINSTANCE.createGenCustomPreferencePage();
   if (gd.getPreferencePages().isEmpty()) {
     gd.getPreferencePages().add(pp);
   } else {
     gd.getPreferencePages().get(0).getChildren().add(pp);
   }
   pp.setGenerateBoilerplate(true);
   pp.setName("Page Name");
   pp.setQualifiedClassName(
       gd.getEditorGen().getEditor().getPackageName() + ".CustomPreferencePage");
   GenPreference p1 = GMFGenFactory.eINSTANCE.createGenPreference();
   p1.setName("PREF_XXX_ONE");
   p1.setDefaultValue("\"XXX_ONE_DEFAULT\"");
   GenPreference p2 = GMFGenFactory.eINSTANCE.createGenPreference();
   p2.setName("NO_PREFIX_XXX_TWO");
   p2.setKey("KEY.XXX.TWO");
   pp.getPreferences().add(p1);
   pp.getPreferences().add(p2);
   //
   generateAndCompile(s);
   //
   IProject generatedProject =
       ResourcesPlugin.getWorkspace().getRoot().getProject(gd.getEditorGen().getPlugin().getID());
   IFile file_pp =
       generatedProject.getFile("/src/" + pp.getQualifiedClassName().replace('.', '/') + ".java");
   assertTrue(file_pp.exists());
   ICompilationUnit cuPage = (ICompilationUnit) JavaCore.create(file_pp);
   assertNotNull(cuPage);
   IType mainClass = cuPage.getTypes()[0];
   assertNotNull(mainClass);
   assertEquals(2, mainClass.getFields().length);
   final IField p1field = mainClass.getField(p1.getName());
   final IField p2field = mainClass.getField(p2.getName());
   assertTrue(Flags.isPublic(p1field.getFlags()));
   assertTrue(Flags.isStatic(p1field.getFlags()));
   assertTrue(Flags.isPublic(p2field.getFlags()));
   assertTrue(Flags.isStatic(p2field.getFlags()));
   assertEquals('"' + p1.getKey() + '"', p1field.getConstant());
   assertEquals('"' + p2.getKey() + '"', p2field.getConstant());
   IMethod initMethod =
       mainClass.getMethod(
           "initDefaults", new String[] {"Q" + IPreferenceStore.class.getSimpleName() + ";"});
   assertNotNull(initMethod);
   String methodText = initMethod.getSource();
   assertTrue(methodText.indexOf(p1.getName()) != -1);
   assertTrue(methodText.indexOf(p1.getDefaultValue()) != -1);
   assertTrue(methodText.indexOf(p2.getName()) == -1);
 }
コード例 #2
0
 public void testPreferencePages() throws Exception {
   DiaGenSource s = createLibraryGen(false);
   final GenDiagram gd = s.getGenDiagram();
   // Part 1: compile all standard
   GenStandardPreferencePage[] p =
       new GenStandardPreferencePage[StandardPreferencePages.values().length];
   assertTrue("sanity", p.length > 5);
   for (int i = 0; i < p.length; i++) {
     p[i] = GMFGenFactory.eINSTANCE.createGenStandardPreferencePage();
     p[i].setClassName("Page" + i);
     p[i].setKind(StandardPreferencePages.values()[i]);
     if (i > 0) {
       p[i - 1].getChildren().add(p[i]);
     }
   }
   gd.getPreferencePages().clear();
   gd.getPreferencePages().add(p[0]);
   generateAndCompile(s);
   GenPlugin gp = gd.getEditorGen().getPlugin();
   IProject generatedProject = ResourcesPlugin.getWorkspace().getRoot().getProject(gp.getID());
   IFile generatedManifest = generatedProject.getFile("plugin.xml");
   assertTrue(generatedManifest.exists());
   XPathFactory xf = XPathFactory.newInstance();
   XPathExpression xe =
       xf.newXPath().compile("/plugin/extension[@point = 'org.eclipse.ui.preferencePages']/page");
   NodeList result =
       (NodeList)
           xe.evaluate(new InputSource(generatedManifest.getContents()), XPathConstants.NODESET);
   assertEquals(p.length, result.getLength());
   //
   // Part 2: generateBoilerplate and subset of pages
   gp.setID(gp.getID() + ".boilerplateprefpage");
   gd.getPreferencePages().clear();
   GenCustomPreferencePage cp1 = GMFGenFactory.eINSTANCE.createGenCustomPreferencePage();
   cp1.setQualifiedClassName(gd.getPreferencesPackageName() + ".CustomPageNoCodeGenerated");
   cp1.setGenerateBoilerplate(false);
   GenCustomPreferencePage cp2 = GMFGenFactory.eINSTANCE.createGenCustomPreferencePage();
   cp2.setQualifiedClassName(gd.getPreferencesPackageName() + ".CustomPageWithBoilerplateCode");
   cp2.setGenerateBoilerplate(true);
   GenStandardPreferencePage onePage = GMFGenFactory.eINSTANCE.createGenStandardPreferencePage();
   onePage.setKind(StandardPreferencePages.CONNECTIONS_LITERAL);
   onePage.getChildren().add(cp1);
   onePage.getChildren().add(cp2);
   gd.getPreferencePages().add(onePage);
   generateAndCompile(s);
   generatedProject = ResourcesPlugin.getWorkspace().getRoot().getProject(gp.getID());
   generatedManifest = generatedProject.getFile("plugin.xml");
   assertTrue(generatedManifest.exists());
   // check all three have been registered
   result =
       (NodeList)
           xe.evaluate(new InputSource(generatedManifest.getContents()), XPathConstants.NODESET);
   assertEquals(3, result.getLength());
   HashSet<String> names = new HashSet<String>();
   names.add(onePage.getQualifiedClassName());
   names.add(cp1.getQualifiedClassName());
   names.add(cp2.getQualifiedClassName());
   assertTrue("sanity", names.size() == 3);
   for (int i = 0; i < result.getLength(); i++) {
     String className = result.item(i).getAttributes().getNamedItem("class").getNodeValue();
     assertTrue(className, names.remove(className));
   }
   assertTrue(names.isEmpty());
   // check real files
   IFile file_sp =
       generatedProject.getFile(
           "/src/" + onePage.getQualifiedClassName().replace('.', '/') + ".java");
   IFile file_cp1 =
       generatedProject.getFile("/src/" + cp1.getQualifiedClassName().replace('.', '/') + ".java");
   IFile file_cp2 =
       generatedProject.getFile("/src/" + cp2.getQualifiedClassName().replace('.', '/') + ".java");
   assertTrue(file_sp.exists());
   assertTrue(file_cp2.exists());
   assertFalse(file_cp1.exists());
 }