diff --git a/org.junit.tools/classes/.DS_Store b/org.junit.tools/classes/.DS_Store deleted file mode 100644 index fe8f8a8..0000000 Binary files a/org.junit.tools/classes/.DS_Store and /dev/null differ diff --git a/org.junit.tools/classes/.gitignore b/org.junit.tools/classes/.gitignore index 571ee51..cf1db2e 100644 --- a/org.junit.tools/classes/.gitignore +++ b/org.junit.tools/classes/.gitignore @@ -1 +1 @@ -/org +/org/ diff --git a/org.junit.tools/classes/org/junit/tools/generator/IGeneratorConstants.class b/org.junit.tools/classes/org/junit/tools/generator/IGeneratorConstants.class index b44af21..767873f 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/IGeneratorConstants.class and b/org.junit.tools/classes/org/junit/tools/generator/IGeneratorConstants.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/TestClassGenerator.class b/org.junit.tools/classes/org/junit/tools/generator/TestClassGenerator.class index 5d4f6eb..bcced49 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/TestClassGenerator.class and b/org.junit.tools/classes/org/junit/tools/generator/TestClassGenerator.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$1.class b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$1.class index d80f38e..c3de885 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$1.class and b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$1.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$2.class b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$2.class index 3cd8b3b..23bb1d6 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$2.class and b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$2.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$TestSuiteDeclaration.class b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$TestSuiteDeclaration.class index a3fc166..69c1f3d 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$TestSuiteDeclaration.class and b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator$TestSuiteDeclaration.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator.class b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator.class index 12dd96c..e4850f7 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator.class and b/org.junit.tools/classes/org/junit/tools/generator/TestSuitesGenerator.class differ diff --git a/org.junit.tools/classes/org/junit/tools/generator/utils/GeneratorUtils.class b/org.junit.tools/classes/org/junit/tools/generator/utils/GeneratorUtils.class index ee1426a..1f7889b 100644 Binary files a/org.junit.tools/classes/org/junit/tools/generator/utils/GeneratorUtils.class and b/org.junit.tools/classes/org/junit/tools/generator/utils/GeneratorUtils.class differ diff --git a/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements$1.class b/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements$1.class index 7bcb273..dd3ac5d 100644 Binary files a/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements$1.class and b/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements$1.class differ diff --git a/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements.class b/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements.class index 46b76fe..ad234aa 100644 Binary files a/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements.class and b/org.junit.tools/classes/org/junit/tools/refactoring/DeleteTestElements.class differ diff --git a/org.junit.tools/src/org/junit/tools/generator/IGeneratorConstants.java b/org.junit.tools/src/org/junit/tools/generator/IGeneratorConstants.java index c01d653..f38dac3 100755 --- a/org.junit.tools/src/org/junit/tools/generator/IGeneratorConstants.java +++ b/org.junit.tools/src/org/junit/tools/generator/IGeneratorConstants.java @@ -31,7 +31,7 @@ public interface IGeneratorConstants { public final static String QUOTES = "\""; - public static final String VERSION = "org.junit-tools-1.0.4"; + public static final String VERSION = "org.junit-tools-1.0.5"; public static final String ANNO_GENERATED_NAME = "Generated"; public static final String ANNO_GENERATED = "@" + ANNO_GENERATED_NAME; diff --git a/org.junit.tools/src/org/junit/tools/generator/TestClassGenerator.java b/org.junit.tools/src/org/junit/tools/generator/TestClassGenerator.java index 6ef8aa0..9ace18c 100755 --- a/org.junit.tools/src/org/junit/tools/generator/TestClassGenerator.java +++ b/org.junit.tools/src/org/junit/tools/generator/TestClassGenerator.java @@ -170,7 +170,9 @@ public ICompilationUnit generate(GeneratorModel model, List te // create the test-source-folder and -package IPackageFragment testPackage = model.getJUTElements().getClassesAndPackages().getTestPackage(); - testClass.createPackageDeclaration(testPackage.getElementName(), null); + if (!testPackage.isDefaultPackage()) { + testClass.createPackageDeclaration(testPackage.getElementName(), null); + } // create static standard-imports createStandardStaticImports(testClass); diff --git a/org.junit.tools/src/org/junit/tools/generator/TestSuitesGenerator.java b/org.junit.tools/src/org/junit/tools/generator/TestSuitesGenerator.java index 76e9320..59f85d4 100755 --- a/org.junit.tools/src/org/junit/tools/generator/TestSuitesGenerator.java +++ b/org.junit.tools/src/org/junit/tools/generator/TestSuitesGenerator.java @@ -33,599 +33,564 @@ */ public class TestSuitesGenerator implements ITestSuitesGenerator { - private static final String TESTSUITE_PREFIX = "TestSuite"; - private ArrayList generatedTestSuites = new ArrayList(); - - /** - * Generates the test-suites after a test-class generation depend on the - * utmElements (bottom-up) - * @throws JUTWarning - */ - @Override - public boolean generateTestSuites(JUTElements jutElements) - throws CoreException, JUTWarning { - init(); - - IJavaProject testProject = jutElements.getProjects().getTestProject(); - IPackageFragment testPackage = jutElements.getClassesAndPackages() - .getTestPackage(true); - String testClassName = jutElements.getClassesAndPackages() - .getTestClassName(); - - HashSet childTestSuites = new HashSet(); - - boolean testSuiteExists; - - testSuiteExists = generateTestSuites(testProject, testPackage, - testClassName, childTestSuites); - - if (testSuiteExists) { - return true; + private static final String TESTSUITE_PREFIX = "TestSuite"; + private ArrayList generatedTestSuites = new ArrayList(); + + /** + * Generates the test-suites after a test-class generation depend on the + * utmElements (bottom-up) + * + * @throws JUTWarning + */ + @Override + public boolean generateTestSuites(JUTElements jutElements) throws CoreException, JUTWarning { + init(); + + IJavaProject testProject = jutElements.getProjects().getTestProject(); + IPackageFragment testPackage = jutElements.getClassesAndPackages().getTestPackage(true); + String testClassName = jutElements.getClassesAndPackages().getTestClassName(); + + HashSet childTestSuites = new HashSet(); + + boolean testSuiteExists; + + testSuiteExists = generateTestSuites(testProject, testPackage, testClassName, childTestSuites); + + if (testSuiteExists) { + return true; + } + + IJavaElement parent = testPackage.getParent(); + IPackageFragmentRoot packageRoot; + if (parent instanceof IPackageFragmentRoot) { + packageRoot = (IPackageFragmentRoot) parent; + + IPath parentPackage = new Path(testPackage.getElementName().replace(".", "/")); + if ("test".equals(parentPackage.lastSegment())) { + parentPackage = parentPackage.removeLastSegments(2); + while ((parentPackage != null && !parentPackage.isEmpty() && !parentPackage.isRoot())) { + parentPackage = parentPackage.append("test"); + testPackage = packageRoot.getPackageFragment(parentPackage.toString().replace("/", ".")); + generateTestSuites(testProject, testPackage, null, childTestSuites); + parentPackage = parentPackage.removeLastSegments(2); + } + + } else { + parentPackage = parentPackage.removeLastSegments(1); + while ((parentPackage != null && !parentPackage.isEmpty() && !parentPackage.isRoot())) { + testPackage = packageRoot.getPackageFragment(parentPackage.toString().replace("/", ".")); + generateTestSuites(testProject, testPackage, null, childTestSuites); + parentPackage = parentPackage.removeLastSegments(1); + } + } + + if (parentPackage.isEmpty()) { + parentPackage = parentPackage.append("base").append("test"); + + if (parentPackage != null) { + testPackage = packageRoot.getPackageFragment(parentPackage.toString().replace("/", ".")); + generateTestSuites(testProject, testPackage, null, childTestSuites); + } + } + } + + return false; } - IJavaElement parent = testPackage.getParent(); - IPackageFragmentRoot packageRoot; - if (parent instanceof IPackageFragmentRoot) { - packageRoot = (IPackageFragmentRoot) parent; - - IPath parentPackage = new Path(testPackage.getElementName() - .replace(".", "/")); - if ("test".equals(parentPackage.lastSegment())) { - parentPackage = parentPackage.removeLastSegments(2); - while ((parentPackage != null && !parentPackage.isEmpty() && !parentPackage - .isRoot())) { - parentPackage = parentPackage.append("test"); - testPackage = packageRoot.getPackageFragment(parentPackage - .toString().replace("/", ".")); - generateTestSuites(testProject, testPackage, null, - childTestSuites); - parentPackage = parentPackage.removeLastSegments(2); + private class TestSuiteDeclaration { + + private TestSuiteDeclaration parent = null; + + private String name; + + private IPackageFragment packageFragment; + + private ICompilationUnit[] cuList = null; + + private List childTestSuiteDeclarations = new ArrayList(); + + public String getName() { + return name; } - } else { - parentPackage = parentPackage.removeLastSegments(1); - while ((parentPackage != null && !parentPackage.isEmpty() && !parentPackage - .isRoot())) { - testPackage = packageRoot.getPackageFragment(parentPackage - .toString().replace("/", ".")); - generateTestSuites(testProject, testPackage, null, - childTestSuites); - parentPackage = parentPackage.removeLastSegments(1); + public void setName(String name) { + this.name = name; } - } - if (parentPackage.isEmpty()) { - parentPackage = parentPackage.append("base").append("test"); + public String getPackageName() { + return packageFragment.getElementName(); + } - if (parentPackage != null) { - testPackage = packageRoot.getPackageFragment(parentPackage - .toString().replace("/", ".")); - generateTestSuites(testProject, testPackage, null, - childTestSuites); + public ICompilationUnit[] getCuList() { + return cuList; } - } - } - return false; - } + public void setCuList(ICompilationUnit[] cuList) { + this.cuList = cuList; + } - private class TestSuiteDeclaration { + public List getChildTestSuiteDeclarations() { + return childTestSuiteDeclarations; + } - private TestSuiteDeclaration parent = null; + public TestSuiteDeclaration getParent() { + return parent; + } - private String name; + public void setParent(TestSuiteDeclaration parent) { + this.parent = parent; + } - private IPackageFragment packageFragment; + public void setPackageFragment(IPackageFragment pf) { + this.packageFragment = pf; + } - private ICompilationUnit[] cuList = null; + public IPackageFragment getPackageFragment() { + return packageFragment; + } - private List childTestSuiteDeclarations = new ArrayList(); + public HashSet createChildTestSuiteNameList() { + HashSet childTestSuiteNameList = new HashSet(); + for (TestSuiteDeclaration tsd : childTestSuiteDeclarations) { + childTestSuiteNameList.add(tsd.getPackageName() + "." + tsd.getName() + ".class"); + } + return childTestSuiteNameList; + } - public String getName() { - return name; } - public void setName(String name) { - this.name = name; + private HashSet createRootTestSuiteNameList(List tsdList) { + HashSet rootTestSuiteNameList = new HashSet(); + for (TestSuiteDeclaration tsd : tsdList) { + rootTestSuiteNameList.add(tsd.getPackageName() + "." + tsd.getName() + ".class"); + } + return rootTestSuiteNameList; } - public String getPackageName() { - return packageFragment.getElementName(); - } + /** + * Generates all necessary test-suites (top-down) + * + * @throws CoreException + * @throws OperationCanceledException + */ + @Override + public boolean generateTestSuites(final IJavaProject testProject) throws OperationCanceledException, CoreException { + init(); + + JavaCore.run(new IWorkspaceRunnable() { + + @Override + public void run(IProgressMonitor monitor) throws CoreException { + String testSourceFolderName = JUTPreferences.getTestSourceFolderName(); + + for (IPackageFragmentRoot fragmentRoot : testProject.getPackageFragmentRoots()) { + if (testSourceFolderName.equals(fragmentRoot.getPath().lastSegment())) { + generateTestSuites(monitor, fragmentRoot); + } + } - public ICompilationUnit[] getCuList() { - return cuList; - } + } - public void setCuList(ICompilationUnit[] cuList) { - this.cuList = cuList; - } + private void generateTestSuites(IProgressMonitor monitor, IPackageFragmentRoot testFragmentRoot) + throws JavaModelException, OperationCanceledException, CoreException { + List testSuiteDeclarations = new ArrayList(); + + TestSuiteDeclaration tsdTmp, tsdBefore = null; + String packageBefore = ""; + IPackageFragment baseTestSuitePackage = null; + ICompilationUnit[] baseTestSuiteCuList = null; + + for (IJavaElement javaElement : testFragmentRoot.getChildren()) { + + if (javaElement instanceof IPackageFragment) { + IPackageFragment pf = (IPackageFragment) javaElement; + if (pf.getKind() == IPackageFragmentRoot.K_SOURCE) { + + if (pf.getElementName().equals(pf.getJavaProject().getElementName() + ".all")) { + baseTestSuitePackage = pf; + baseTestSuiteCuList = pf.getCompilationUnits(); + continue; + } else if (pf.getElementName().equals("all")) { + boolean baseTestSuiteFound = false; + for (ICompilationUnit cu : pf.getCompilationUnits()) { + if (cu.getElementName().startsWith("TestSuiteAll")) { + baseTestSuitePackage = pf; + baseTestSuiteCuList = pf.getCompilationUnits(); + baseTestSuiteFound = true; + break; + } + } + + if (baseTestSuiteFound) { + continue; + } + } + + if (pf.getCompilationUnits().length == 0) { + continue; + } + + // TestSuiteDeclaration erzeugen + tsdTmp = new TestSuiteDeclaration(); + tsdTmp.setName(TESTSUITE_PREFIX); + tsdTmp.setPackageFragment(pf); + tsdTmp.setCuList(pf.getCompilationUnits()); + + // zuordnen + if (tsdBefore == null || "".equals(packageBefore)) { + testSuiteDeclarations.add(tsdTmp); + } else if (JDTUtils.isParentPackage(packageBefore, pf.getElementName())) { + tsdTmp.setParent(tsdBefore); + tsdBefore.getChildTestSuiteDeclarations().add(tsdTmp); + } else if (tsdBefore.getParent() != null && JDTUtils + .isParentPackage(tsdBefore.getParent().getPackageName(), pf.getElementName())) { + tsdTmp.setParent(tsdBefore.getParent()); + tsdBefore.getParent().getChildTestSuiteDeclarations().add(tsdTmp); + } else { + testSuiteDeclarations.add(tsdTmp); + } + + tsdBefore = tsdTmp; + packageBefore = pf.getElementName(); + } + } + + for (TestSuiteDeclaration tsd : testSuiteDeclarations) { + processTestSuiteDeclaration(tsd); + } + + if (baseTestSuitePackage == null) { + try { + baseTestSuitePackage = JDTUtils.getPackage(testProject, testFragmentRoot, + testProject.getElementName() + ".all", true); + } catch (JavaModelException ex) { + // the package-name is not allowed, so we take only + // the package-name 'all' + baseTestSuitePackage = JDTUtils.getPackage(testProject, testFragmentRoot, "all", true); + } + baseTestSuiteCuList = new ICompilationUnit[0]; + } + + // create base-test-suite + processTestSuiteDeclaration("TestSuiteAll", baseTestSuitePackage, baseTestSuiteCuList, + createRootTestSuiteNameList(testSuiteDeclarations)); + } - public List getChildTestSuiteDeclarations() { - return childTestSuiteDeclarations; - } + } - public TestSuiteDeclaration getParent() { - return parent; - } + }, null); - public void setParent(TestSuiteDeclaration parent) { - this.parent = parent; + return true; } - public void setPackageFragment(IPackageFragment pf) { - this.packageFragment = pf; - } + private void processTestSuiteDeclaration(TestSuiteDeclaration tsd) + throws OperationCanceledException, CoreException { + IPackageFragment testSuitePackage = tsd.getPackageFragment(); - public IPackageFragment getPackageFragment() { - return packageFragment; - } + processTestSuiteDeclaration(tsd.getName(), testSuitePackage, tsd.getCuList(), + tsd.createChildTestSuiteNameList()); + + // create child test-suites + for (TestSuiteDeclaration childTsd : tsd.getChildTestSuiteDeclarations()) { + processTestSuiteDeclaration(childTsd); + } - public HashSet createChildTestSuiteNameList() { - HashSet childTestSuiteNameList = new HashSet(); - for (TestSuiteDeclaration tsd : childTestSuiteDeclarations) { - childTestSuiteNameList.add(tsd.getPackageName() + "." - + tsd.getName() + ".class"); - } - return childTestSuiteNameList; } - } + private void processTestSuiteDeclaration(String testSuiteName, IPackageFragment testSuitePackage, + ICompilationUnit[] cuList, HashSet childTestSuiteNameList) + throws OperationCanceledException, CoreException { + ICompilationUnit testSuite = testSuitePackage.getCompilationUnit(testSuiteName + ".java"); - private HashSet createRootTestSuiteNameList( - List tsdList) { - HashSet rootTestSuiteNameList = new HashSet(); - for (TestSuiteDeclaration tsd : tsdList) { - rootTestSuiteNameList.add(tsd.getPackageName() + "." - + tsd.getName() + ".class"); - } - return rootTestSuiteNameList; - } - - /** - * Generates all necessary test-suites (top-down) - * - * @throws CoreException - * @throws OperationCanceledException - */ - @Override - public boolean generateTestSuites(final IJavaProject testProject) - throws OperationCanceledException, CoreException { - init(); - - JavaCore.run(new IWorkspaceRunnable() { - - @Override - public void run(IProgressMonitor monitor) throws CoreException { - String testSourceFolderName = JUTPreferences - .getTestSourceFolderName(); - - for (IPackageFragmentRoot fragmentRoot : testProject - .getPackageFragmentRoots()) { - if (testSourceFolderName.equals(fragmentRoot.getPath() - .lastSegment())) { - generateTestSuites(monitor, fragmentRoot); - } + // create new source + IType testSuiteFrame = createTestSuiteFrame(testSuiteName, testSuite, cuList, null, childTestSuiteNameList, + true); + + if (testSuiteFrame == null) { + return; } - } + // save compilation-unit + testSuite.save(null, false); + testSuite.makeConsistent(null); + if (testSuite.hasUnsavedChanges()) { + testSuite.commitWorkingCopy(true, null); + } - private void generateTestSuites(IProgressMonitor monitor, - IPackageFragmentRoot testFragmentRoot) - throws JavaModelException, OperationCanceledException, - CoreException { - List testSuiteDeclarations = new ArrayList(); + generatedTestSuites.add(testSuite); + } - TestSuiteDeclaration tsdTmp, tsdBefore = null; - String packageBefore = ""; - IPackageFragment baseTestSuitePackage = null; - ICompilationUnit[] baseTestSuiteCuList = null; + /** + * Generates the test suites. + * + * @param testProject + * @param testPackage + * @param newTestSuiteElementName + * @param childTestSuites + * @return true if test test suite already exists + * @throws CoreException + */ + private boolean generateTestSuites(IJavaProject testProject, IPackageFragment testPackage, + String newTestSuiteElementName, HashSet childTestSuites) throws CoreException { + boolean exists = false; + + if (testPackage == null || !testPackage.exists()) { + return exists; + } - for (IJavaElement javaElement : testFragmentRoot.getChildren()) { + String testSuiteName = TESTSUITE_PREFIX; - if (javaElement instanceof IPackageFragment) { - IPackageFragment pf = (IPackageFragment) javaElement; - if (pf.getKind() == IPackageFragmentRoot.K_SOURCE) { + ICompilationUnit testSuite; + ICompilationUnit[] testClasses; - if (pf.getElementName().equals(pf.getJavaProject().getElementName() + ".all")) { - baseTestSuitePackage = pf; - baseTestSuiteCuList = pf.getCompilationUnits(); - continue; - } + testSuite = getTestSuite(testPackage); + testClasses = testPackage.getCompilationUnits(); - if (pf.getCompilationUnits().length == 0) { - continue; - } - - // TestSuiteDeclaration erzeugen - tsdTmp = new TestSuiteDeclaration(); - tsdTmp.setName(TESTSUITE_PREFIX); - tsdTmp.setPackageFragment(pf); - tsdTmp.setCuList(pf.getCompilationUnits()); - - // zuordnen - if (tsdBefore == null || "".equals(packageBefore)) { - testSuiteDeclarations.add(tsdTmp); - } else if (JDTUtils.isParentPackage(packageBefore, - pf.getElementName())) { - tsdTmp.setParent(tsdBefore); - tsdBefore.getChildTestSuiteDeclarations().add( - tsdTmp); - } else if (tsdBefore.getParent() != null - && JDTUtils.isParentPackage(tsdBefore - .getParent().getPackageName(), pf - .getElementName())) { - tsdTmp.setParent(tsdBefore.getParent()); - tsdBefore.getParent() - .getChildTestSuiteDeclarations() - .add(tsdTmp); - } else { - testSuiteDeclarations.add(tsdTmp); - } - - tsdBefore = tsdTmp; - packageBefore = pf.getElementName(); - } - } - - for (TestSuiteDeclaration tsd : testSuiteDeclarations) { - processTestSuiteDeclaration(tsd); - } - - if (baseTestSuitePackage == null) { - baseTestSuitePackage = JDTUtils.getPackage(testProject, - testFragmentRoot, testProject.getElementName() - + ".all", true); - baseTestSuiteCuList = new ICompilationUnit[0]; - } - - // create base-test-suite - processTestSuiteDeclaration("TestSuiteAll", - baseTestSuitePackage, baseTestSuiteCuList, - createRootTestSuiteNameList(testSuiteDeclarations)); + if (testClasses.length == 0 && !"base".equals(testPackage.getPath().lastSegment())) { + return exists; } - } + // create new source + IType testSuiteFrame = createTestSuiteFrame(testSuiteName, testSuite, testClasses, newTestSuiteElementName, + childTestSuites, false); + if (testSuiteFrame == null) { + return true; + } - }, null); + // create new compilation-unit + testSuite.save(null, false); + testSuite.makeConsistent(null); + if (testSuite.hasUnsavedChanges()) { + testSuite.commitWorkingCopy(true, null); + } - return true; - } + generatedTestSuites.add(testSuite); - private void processTestSuiteDeclaration(TestSuiteDeclaration tsd) - throws OperationCanceledException, CoreException { - IPackageFragment testSuitePackage = tsd.getPackageFragment(); + // clear child test-suites + childTestSuites.clear(); + // add generated test-suite + String testSuitePath = ""; - processTestSuiteDeclaration(tsd.getName(), testSuitePackage, - tsd.getCuList(), tsd.createChildTestSuiteNameList()); + if (!"".equals(testPackage.getElementName())) { + testSuitePath = testPackage.getElementName() + "."; + } - // create child test-suites - for (TestSuiteDeclaration childTsd : tsd - .getChildTestSuiteDeclarations()) { - processTestSuiteDeclaration(childTsd); - } + childTestSuites.add(testSuitePath + TESTSUITE_PREFIX + ".class"); - } + return exists; + } - private void processTestSuiteDeclaration(String testSuiteName, - IPackageFragment testSuitePackage, ICompilationUnit[] cuList, - HashSet childTestSuiteNameList) - throws OperationCanceledException, CoreException { - ICompilationUnit testSuite = testSuitePackage - .getCompilationUnit(testSuiteName + ".java"); + /** + * Creates the test suite frame. + * + * @param testSuiteName + * @param testSuite + * @param testClasses + * @param newTestSuiteElementName + * @param generatedTestSuites + * @param createNew + * @return the test-suite + * @throws JavaModelException + */ + protected IType createTestSuiteFrame(String testSuiteName, ICompilationUnit testSuite, + ICompilationUnit[] testClasses, String newTestSuiteElementName, HashSet generatedTestSuites, + boolean createNew) throws JavaModelException { + IType type = testSuite.getType(testSuiteName); + + String testElementList; + + if (type.exists()) { + if (!createNew) { + for (IAnnotation annotation : type.getAnnotations()) { + if ("Suite.SuiteClasses".equals(annotation.getElementName())) { + for (IMemberValuePair memberValuePairs : annotation.getMemberValuePairs()) { + Object value = memberValuePairs.getValue(); + String suiteClassName; + + if (value instanceof Object[]) { + for (Object testSuiteObject : (Object[]) value) { + suiteClassName = testSuiteObject.toString(); + if (suiteClassName.startsWith(TESTSUITE_PREFIX)) { + generatedTestSuites.add(suiteClassName + ".class"); + } + if (suiteClassName.equals(newTestSuiteElementName)) { + return null; + } + } + } + } + + } + } + } - // create new source - IType testSuiteFrame = createTestSuiteFrame(testSuiteName, testSuite, - cuList, null, childTestSuiteNameList, true); + } - if (testSuiteFrame == null) { - return; - } + testElementList = createCommaSeparatedClassList(testClasses, generatedTestSuites); - // save compilation-unit - testSuite.save(null, false); - testSuite.makeConsistent(null); - if (testSuite.hasUnsavedChanges()) { - testSuite.commitWorkingCopy(true, null); + return refreshTestSuiteElements(testSuite, testSuiteName, testElementList); } - generatedTestSuites.add(testSuite); - } - - /** - * Generates the test suites. - * - * @param testProject - * @param testPackage - * @param newTestSuiteElementName - * @param childTestSuites - * @return true if test test suite already exists - * @throws CoreException - */ - private boolean generateTestSuites(IJavaProject testProject, - IPackageFragment testPackage, String newTestSuiteElementName, - HashSet childTestSuites) throws CoreException { - boolean exists = false; - - if (testPackage == null || !testPackage.exists()) { - return exists; + /** + * Creates a comma separated class list. + * + * @param testClasses + * @param generatedTestSuites + * @return separated class list + */ + protected String createCommaSeparatedClassList(ICompilationUnit[] testClasses, + HashSet generatedTestSuites) { + return createCommaSeparatedClassList(testClasses, generatedTestSuites, ""); } - String testSuiteName = TESTSUITE_PREFIX; - - ICompilationUnit testSuite; - ICompilationUnit[] testClasses; + protected String createCommaSeparatedClassList(ICompilationUnit[] testClasses, HashSet generatedTestSuites, + String... exceptionClasses) { + boolean first = true; + StringBuilder sb = new StringBuilder(); + String testClassName; + String testClassNameWithClassSuffix; - testSuite = getTestSuite(testPackage); - testClasses = testPackage.getCompilationUnits(); + for (ICompilationUnit testClass : testClasses) { + if (!testClass.exists()) { + continue; + } - if (testClasses.length == 0 - && !"base".equals(testPackage.getPath().lastSegment())) { - return exists; - } + testClassNameWithClassSuffix = testClass.getElementName().replace(".java", ".class"); + testClassName = testClassNameWithClassSuffix.replace(".class", ""); - // create new source - IType testSuiteFrame = createTestSuiteFrame(testSuiteName, testSuite, - testClasses, newTestSuiteElementName, childTestSuites, false); - if (testSuiteFrame == null) { - return true; - } + if (testClassName.equals(TESTSUITE_PREFIX) || !isValidTestClassName(testClassName)) { + continue; + } - // create new compilation-unit - testSuite.save(null, false); - testSuite.makeConsistent(null); - if (testSuite.hasUnsavedChanges()) { - testSuite.commitWorkingCopy(true, null); - } + if (isExceptionClass(testClassName, exceptionClasses)) { + continue; + } - generatedTestSuites.add(testSuite); + if (!first) { + sb.append(", "); + } else { + first = false; + } - // clear child test-suites - childTestSuites.clear(); - // add generated test-suite - String testSuitePath = ""; + sb.append(testClassNameWithClassSuffix); + } - if (!"".equals(testPackage.getElementName())) { - testSuitePath = testPackage.getElementName() + "."; - } + if (generatedTestSuites != null) { + for (String generatedTestSuite : generatedTestSuites) { + if (generatedTestSuite.startsWith(".")) { + continue; + } - childTestSuites.add(testSuitePath + TESTSUITE_PREFIX + ".class"); - - return exists; - } - - /** - * Creates the test suite frame. - * - * @param testSuiteName - * @param testSuite - * @param testClasses - * @param newTestSuiteElementName - * @param generatedTestSuites - * @param createNew - * @return the test-suite - * @throws JavaModelException - */ - protected IType createTestSuiteFrame(String testSuiteName, - ICompilationUnit testSuite, ICompilationUnit[] testClasses, - String newTestSuiteElementName, - HashSet generatedTestSuites, boolean createNew) - throws JavaModelException { - IType type = testSuite.getType(testSuiteName); - - String testElementList; - - if (type.exists()) { - if (!createNew) { - for (IAnnotation annotation : type.getAnnotations()) { - if ("Suite.SuiteClasses" - .equals(annotation.getElementName())) { - for (IMemberValuePair memberValuePairs : annotation - .getMemberValuePairs()) { - Object value = memberValuePairs.getValue(); - String suiteClassName; - - if (value instanceof Object[]) { - for (Object testSuiteObject : (Object[]) value) { - suiteClassName = testSuiteObject.toString(); - if (suiteClassName - .startsWith(TESTSUITE_PREFIX)) { - generatedTestSuites.add(suiteClassName - + ".class"); - } - if (suiteClassName - .equals(newTestSuiteElementName)) { - return null; - } + if (!first) { + sb.append(", "); + } else { + first = false; } - } - } - } + sb.append(generatedTestSuite); + } } - } + return sb.toString(); } - testElementList = createCommaSeparatedClassList(testClasses, - generatedTestSuites); - - return refreshTestSuiteElements(testSuite, testSuiteName, - testElementList); - } - - /** - * Creates a comma separated class list. - * - * @param testClasses - * @param generatedTestSuites - * @return separated class list - */ - protected String createCommaSeparatedClassList( - ICompilationUnit[] testClasses, HashSet generatedTestSuites) { - return createCommaSeparatedClassList(testClasses, generatedTestSuites, - ""); - } - - protected String createCommaSeparatedClassList( - ICompilationUnit[] testClasses, - HashSet generatedTestSuites, String... exceptionClasses) { - boolean first = true; - StringBuilder sb = new StringBuilder(); - String testClassName; - String testClassNameWithClassSuffix; - - for (ICompilationUnit testClass : testClasses) { - if (!testClass.exists()) { - continue; - } - - testClassNameWithClassSuffix = testClass.getElementName().replace( - ".java", ".class"); - testClassName = testClassNameWithClassSuffix.replace(".class", ""); - - if (testClassName.equals(TESTSUITE_PREFIX) - || !isValidTestClassName(testClassName)) { - continue; - } - - if (isExceptionClass(testClassName, exceptionClasses)) { - continue; - } - - if (!first) { - sb.append(", "); - } else { - first = false; - } - - sb.append(testClassNameWithClassSuffix); - } + private boolean isValidTestClassName(String testClassName) { + String testClassPrefix = JUTPreferences.getTestClassPrefix(); + String testClassPostfix = JUTPreferences.getTestClassPostfix(); - if (generatedTestSuites != null) { - for (String generatedTestSuite : generatedTestSuites) { - if (generatedTestSuite.startsWith(".")) { - continue; + if (!"".equals(testClassPrefix)) { + if (!testClassName.startsWith(testClassPrefix)) { + return false; + } } - if (!first) { - sb.append(", "); - } else { - first = false; + if (!"".equals(testClassPostfix)) { + if (!testClassName.endsWith(testClassPostfix)) { + return false; + } } - sb.append(generatedTestSuite); - } + return true; } - return sb.toString(); - } - - private boolean isValidTestClassName(String testClassName) { - String testClassPrefix = JUTPreferences.getTestClassPrefix(); - String testClassPostfix = JUTPreferences.getTestClassPostfix(); + private boolean isExceptionClass(String testClassName, String[] exceptionClasses) { + for (String exceptionClass : exceptionClasses) { + if (exceptionClass.equals(testClassName)) { + return true; + } + } - if (!"".equals(testClassPrefix)) { - if (!testClassName.startsWith(testClassPrefix)) { return false; - } } - if (!"".equals(testClassPostfix)) { - if (!testClassName.endsWith(testClassPostfix)) { - return false; - } + private void init() { + generatedTestSuites = new ArrayList(); } - return true; - } + @Override + public boolean deleteTestSuiteElement(final IPackageFragment packageOfDeletedClass, + final ICompilationUnit deletedClass) throws CoreException { + init(); - private boolean isExceptionClass(String testClassName, - String[] exceptionClasses) { - for (String exceptionClass : exceptionClasses) { - if (exceptionClass.equals(testClassName)) { - return true; - } - } + if (packageOfDeletedClass == null) { + return true; + } - return false; - } + JavaCore.run(new IWorkspaceRunnable() { - private void init() { - generatedTestSuites = new ArrayList(); - } + @Override + public void run(IProgressMonitor monitor) throws CoreException { - @Override - public boolean deleteTestSuiteElement( - final IPackageFragment packageOfDeletedClass, - final ICompilationUnit deletedClass) throws CoreException { - init(); + ICompilationUnit testSuite = getTestSuite(packageOfDeletedClass); - if (packageOfDeletedClass == null) { - return true; - } + String testElementList = createCommaSeparatedClassList(packageOfDeletedClass.getCompilationUnits(), + null, deletedClass.getElementName().replace(".java", "")); - JavaCore.run(new IWorkspaceRunnable() { + IType testSuiteType = refreshTestSuiteElements(testSuite, TESTSUITE_PREFIX, testElementList); - @Override - public void run(IProgressMonitor monitor) throws CoreException { - - ICompilationUnit testSuite = getTestSuite(packageOfDeletedClass); + // if only the test-class and the test-suite is in the package, + // then delete the complete package + if (testSuiteType == null && packageOfDeletedClass.getCompilationUnits().length == 2) { + // delete empty packages, if no class is inside + JDTUtils.deletePackages(packageOfDeletedClass); + } - String testElementList = createCommaSeparatedClassList( - packageOfDeletedClass.getCompilationUnits(), null, - deletedClass.getElementName().replace(".java", "")); + } - IType testSuiteType = refreshTestSuiteElements(testSuite, - TESTSUITE_PREFIX, testElementList); + }, null); - // if only the test-class and the test-suite is in the package, - // then delete the complete package - if (testSuiteType == null - && packageOfDeletedClass.getCompilationUnits().length == 2) { - // delete empty packages, if no class is inside - JDTUtils.deletePackages(packageOfDeletedClass); - } + return true; + } - } + protected ICompilationUnit getTestSuite(IPackageFragment p) throws JavaModelException { + return p.getCompilationUnit(TESTSUITE_PREFIX + ".java"); + } - }, null); + private IType refreshTestSuiteElements(ICompilationUnit testSuite, String testSuiteName, String testElementList) + throws JavaModelException { + IType testSuiteType = testSuite.getType(testSuiteName); - return true; - } + if (testSuiteType.exists()) { + testSuiteType.delete(true, null); + } - protected ICompilationUnit getTestSuite(IPackageFragment p) - throws JavaModelException { - return p.getCompilationUnit(TESTSUITE_PREFIX + ".java"); - } + if (testElementList.length() > 0) { + String testAnnotation = "@RunWith(Suite.class)\n@Suite.SuiteClasses(\n\n{" + testElementList + "}\n)"; - private IType refreshTestSuiteElements(ICompilationUnit testSuite, - String testSuiteName, String testElementList) - throws JavaModelException { - IType testSuiteType = testSuite.getType(testSuiteName); + testSuiteType = testSuite.createType( + testAnnotation + "\npublic class " + testSuiteType.getElementName() + "{ // nothing\n}", null, true, + null); + } else { + if (testSuite.exists()) { + testSuite.delete(true, null); + } + return null; + } - if (testSuiteType.exists()) { - testSuiteType.delete(true, null); + return testSuiteType; } - if (testElementList.length() > 0) { - String testAnnotation = "@RunWith(Suite.class)\n@Suite.SuiteClasses(\n\n{" - + testElementList + "}\n)"; - - testSuiteType = testSuite.createType(testAnnotation - + "\npublic class " + testSuiteType.getElementName() - + "{ // nothing\n}", null, true, null); - } else { - if (testSuite.exists()) { - testSuite.delete(true, null); - } - return null; + public ArrayList getGeneratedTestSuites() { + return generatedTestSuites; } - - return testSuiteType; - } - - public ArrayList getGeneratedTestSuites() { - return generatedTestSuites; - } } diff --git a/org.junit.tools/src/org/junit/tools/generator/utils/GeneratorUtils.java b/org.junit.tools/src/org/junit/tools/generator/utils/GeneratorUtils.java index a2d4747..000bb7d 100755 --- a/org.junit.tools/src/org/junit/tools/generator/utils/GeneratorUtils.java +++ b/org.junit.tools/src/org/junit/tools/generator/utils/GeneratorUtils.java @@ -695,6 +695,10 @@ public static Method getClosestMethod(IMethod method, * @return true if it is a test-class */ public static boolean isTestClass(IType type) { + if (type == null) { + return false; + } + String elementName = type.getElementName(); String projectPostfix = JUTPreferences.getTestProjectPostfix(); diff --git a/org.junit.tools/src/org/junit/tools/refactoring/DeleteTestElements.java b/org.junit.tools/src/org/junit/tools/refactoring/DeleteTestElements.java index af13e18..0e6e82c 100755 --- a/org.junit.tools/src/org/junit/tools/refactoring/DeleteTestElements.java +++ b/org.junit.tools/src/org/junit/tools/refactoring/DeleteTestElements.java @@ -6,6 +6,7 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IType; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.CompositeChange; import org.eclipse.ltk.core.refactoring.PerformChangeOperation; @@ -15,6 +16,7 @@ import org.junit.tools.Activator; import org.junit.tools.generator.ITestSuitesGenerator; import org.junit.tools.generator.model.JUTElements; +import org.junit.tools.generator.utils.GeneratorUtils; import org.junit.tools.generator.utils.JDTUtils; public class DeleteTestElements extends DeleteParticipant { @@ -97,6 +99,13 @@ public Change perform(IProgressMonitor pm2) throws CoreException { return null; } } + + if (!utmElements.getProjects().isBaseProjectSelected() && utmElements.getProjects() + .isTestProjectFound()) { + if (!GeneratorUtils.isTestClass(deletedCu.findPrimaryType())) { + return null; + } + } // actualize the test-suites for (ITestSuitesGenerator testSuiteGenerator : Activator