Skip to content

Commit

Permalink
Implementation of #1251 (create multiple types at once).
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian authored and Christian committed Sep 16, 2024
1 parent c9a4731 commit 1c59008
Show file tree
Hide file tree
Showing 3 changed files with 1,759 additions and 1,360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,16 @@ private void internalSetJUnit4(boolean isJUnit4) {
}

@Override
protected String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
protected String constructCUContent(String typeName, ICompilationUnit cu, String typeContent, String lineDelimiter)
throws CoreException {

if (isJUnit4()) {
typeContent= appendAnnotations(typeContent, lineDelimiter);
} else if (isJUnit5()) {
typeContent= appendAnnotations5(typeContent, lineDelimiter);
}

return super.constructCUContent(cu, typeContent, lineDelimiter);
return super.constructCUContent(typeName, cu, typeContent, lineDelimiter);
}

private String appendAnnotations(String typeContent, String lineDelimiter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void testCreateClass1() throws Exception {
* File
*/
package test1;
/**
* Type
*/
Expand Down Expand Up @@ -187,9 +188,9 @@ public void testCreateClass2() throws Exception {
* File
*/
package test1;
import java.util.ArrayList;
/**
* Type
*/
Expand Down Expand Up @@ -241,14 +242,14 @@ public class A<T> {
* File
*/
package test1;
import pack.A;
/**
* Type
*/
public class E extends A<String> {
/**
* Overridden
*/
Expand Down Expand Up @@ -303,27 +304,27 @@ public class A<T> {
* File
*/
package test1;
import pack.A;
/**
* Type
*/
public class E extends A<String> {
/**
* Constructor
*/
public E(String t) {
super(t);
}
/* class body */
/**
* Method
*/
public static void main(String[] args) {
}
}
""";
Expand Down Expand Up @@ -370,17 +371,80 @@ public class A<T> {

String expected= """
package pack;
import java.util.ArrayList;
public class A<T> {
/**
* Type
*/
public class E<S> extends ArrayList<S> {
/* class body */
}
public abstract void foo(T t);
}
""";

StringAsserts.assertEqualStringIgnoreDelim(actual, expected);
}

@Test
public void testCreateMultipleInnerClasses1() throws Exception {

IPackageFragment pack0= fSourceFolder.createPackageFragment("pack", false, null);
String str= """
package pack;
public class A<T> {
public abstract void foo(T t);
}
""";
ICompilationUnit outer= pack0.createCompilationUnit("A.java", str, false, null);

IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);

NewClassWizardPage wizardPage= new NewClassWizardPage();
wizardPage.setPackageFragmentRoot(fSourceFolder, true);
wizardPage.setPackageFragment(pack1, true);
wizardPage.setEnclosingTypeSelection(true, true);
wizardPage.setEnclosingType(outer.findPrimaryType(), true);
wizardPage.setTypeName("E<S>;F<S>", true);

wizardPage.setSuperClass("java.util.ArrayList<S>", true);

List<String> interfaces= new ArrayList<>();
wizardPage.setSuperInterfaces(interfaces, true);

wizardPage.setMethodStubSelection(false, false, true, true);
wizardPage.setAddComments(true, true);
wizardPage.enableCommentControl(true);

FussyProgressMonitor testMonitor= new FussyProgressMonitor();
wizardPage.createType(testMonitor);
testMonitor.assertUsedUp();

String actual= wizardPage.getCreatedType().getCompilationUnit().getSource();

String expected= """
package pack;
import java.util.ArrayList;
public class A<T> {
/**
* Type
*/
public class F<S> extends ArrayList<S> {
/* class body */
}
/**
* Type
*/
public class E<S> extends ArrayList<S> {
/* class body */
}
public abstract void foo(T t);
}
""";
Expand Down Expand Up @@ -423,11 +487,11 @@ public void testCreateClassExtraImports1() throws Exception {
* File
*/
package test1;
import java.io.File;
import java.util.List;
import java.util.Map;
/**
* Type
*/
Expand Down Expand Up @@ -486,16 +550,16 @@ public static class Inner {
* File
*/
package test1;
import java.util.Map;
import pack.A;
/**
* Type
*/
public class E extends A {
/**
* Overridden
*/
Expand Down Expand Up @@ -526,9 +590,9 @@ public static class Inner {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str1= """
package test1;
import java.util.Map;
public class B {
}
""";
Expand Down Expand Up @@ -558,18 +622,18 @@ public class B {

String expected= """
package test1;
import java.util.Map;
import pack.A;
public class B {
/**
* Type
*/
public class E extends A {
/**
* Overridden
*/
Expand Down Expand Up @@ -612,9 +676,9 @@ public void testCreateInterface() throws Exception {
* File
*/
package test1;
import java.util.List;
/**
* Type
*/
Expand Down Expand Up @@ -652,7 +716,7 @@ public void testCreateEnum() throws Exception {
* File
*/
package test1;
/**
* Type
*/
Expand Down Expand Up @@ -690,7 +754,7 @@ public void testCreateAnnotation() throws Exception {
* File
*/
package test1;
/**
* Type
*/
Expand Down Expand Up @@ -811,9 +875,9 @@ public void testAttemptCreateExistingClass() throws Exception
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
public class Foo1 {
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("Foo1.java", str, false, null);
Expand All @@ -823,9 +887,9 @@ public class Foo1 {
pack1= fSourceFolder.createPackageFragment("test2", false, null);
String str1= """
package test2;
public class Foo3 {
}
""";
pack1.createCompilationUnit("Foo3.java", str1, false, null);
Expand All @@ -849,7 +913,7 @@ public void testAddFinalSuperClassError1() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String test= """
package test1;
public final class A{
}
""";
Expand Down Expand Up @@ -877,6 +941,76 @@ public final class A{
assertTrue(expected.equals(status.getMessage()));
}

@Test
public void testCreateMultipleClasses1() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);

NewClassWizardPage wizardPage= new NewClassWizardPage();
wizardPage.setPackageFragmentRoot(fSourceFolder, true);
wizardPage.setPackageFragment(pack1, true);
wizardPage.setEnclosingTypeSelection(false, true);
wizardPage.setTypeName("E;F", true);

wizardPage.setSuperClass("", true);

List<String> interfaces= new ArrayList<>();
wizardPage.setSuperInterfaces(interfaces, true);

wizardPage.setMethodStubSelection(false, false, false, true);
wizardPage.setAddComments(true, true);
wizardPage.enableCommentControl(true);

FussyProgressMonitor testMonitor= new FussyProgressMonitor();
wizardPage.createType(testMonitor);
testMonitor.assertUsedUp();

String actual= wizardPage.getCreatedType().getCompilationUnit().getSource();

String expected= """
/**
* File
*/
package test1;
/**
* Type
*/
public class E {
/* class body */
}
""";

StringAsserts.assertEqualStringIgnoreDelim(actual, expected);
}

@Test
public void testCreateMultipleClassesDuplicateError1() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);

NewClassWizardPage wizardPage = new NewClassWizardPage();
wizardPage.setPackageFragmentRoot(fSourceFolder, true);
wizardPage.setPackageFragment(pack1, true);
wizardPage.setEnclosingTypeSelection(false, true);
wizardPage.setTypeName("X", true); // Will be changed below
wizardPage.setSuperClass("", true);
wizardPage.setSuperInterfaces(new ArrayList<>(), true);
wizardPage.setMethodStubSelection(false, false, false, true);
wizardPage.setAddComments(true, true);
wizardPage.enableCommentControl(true);

// In this error examples the first (single letter) class name is always the duplicate
Stream.of("A;A", "B;C;B", "D;D<T>", "E;E< T >", "F;F<T, U>", "G; T1; T2; T3; G")
.forEach(names -> {
wizardPage.setTypeName(names, true);
IStatus status = wizardPage.getTypeNameStatus();
assertNotNull(status);
assertTrue(status.getSeverity() == IStatus.ERROR);
String duplicate = names.substring(0, 1); // See comment above
String expected = Messages.format(NewWizardMessages.NewTypeWizardPage_error_DuplicateName, duplicate);
assertEquals(expected, status.getMessage());
});
}

private static ITypeBinding getTypeBinding(ICompilationUnit cu) {
CompilationUnit compUnit= ASTResolving.createQuickFixAST(cu, null);
ITypeBinding tBinding= null;
Expand Down
Loading

0 comments on commit 1c59008

Please sign in to comment.