Skip to content

Commit

Permalink
#41 - Use correct Java project for class under test
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Dec 24, 2021
1 parent c247d10 commit 2ac1539
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.moreunit.log.LogHandler;
Expand Down Expand Up @@ -35,7 +36,8 @@ public CorrespondingTypeSearcher(ICompilationUnit compilationUnit, Preferences p
{
this.preferences = preferences.getProjectView(compilationUnit.getJavaProject());
nameEvaluation = this.preferences.getTestClassNamePattern().evaluate(compilationUnit.findPrimaryType());
searchScope = SearchScopeSingelton.getInstance().getSearchScope(PluginTools.getSourceFolder(compilationUnit));
IPackageFragmentRoot testSourceFolder = preferences.getTestSourceFolder(compilationUnit.getJavaProject(), PluginTools.getSourceFolder(compilationUnit));
searchScope = SearchScopeSingelton.getInstance().getSearchScope(testSourceFolder);
}

public Collection<IType> getMatches(boolean alsoIncludeLikelyMatches)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ public class MoreUnitWizardPageOne extends NewTypeWizardPage
private Button testNgToggle;

private final ProjectPreferences preferences;
private IJavaProject javaProjectUnderTest;
private final LanguageType langType;

private TmpMemento tmpMemento;

public MoreUnitWizardPageOne(NewTestCaseWizardPageTwo page2, ProjectPreferences preferences, LanguageType langType)
public MoreUnitWizardPageOne(NewTestCaseWizardPageTwo page2, IJavaProject javaProjectUnderTest, ProjectPreferences preferences, LanguageType langType)
{
super(true, PAGE_NAME);
this.javaProjectUnderTest = javaProjectUnderTest;
this.preferences = preferences;

fPage2 = page2;
Expand Down Expand Up @@ -736,7 +738,7 @@ private IType chooseClassToTestType()
if(root == null)
return null;

IJavaElement[] elements = new IJavaElement[] { root.getJavaProject() };
IJavaElement[] elements = new IJavaElement[] { javaProjectUnderTest };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);

try
Expand Down Expand Up @@ -806,7 +808,7 @@ protected IStatus classUnderTestChanged()
IPackageFragment pack = getPackageFragment(); // can be null
try
{
IType type = resolveClassNameToType(root.getJavaProject(), pack, classToTestName);
IType type = resolveClassNameToType(javaProjectUnderTest, pack, classToTestName);
if(type == null)
{
status.setError(WizardMessages.NewTestCaseWizardPageOne_error_class_to_test_not_exist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -30,12 +31,14 @@ public class NewTestCaseWizard extends NewClassyWizard
private NewTestCaseWizardPageTwo pageTwo;
private NewTestCaseWizardContext context;
private NewTestCaseWizardComposer wizardComposer;
private IJavaProject javaProjectUnderTest;

public NewTestCaseWizard(final IType element)
{
super(element);

this.preferences = Preferences.forProject(element.getJavaProject());
this.javaProjectUnderTest = element.getJavaProject();
this.preferences = Preferences.forProject(javaProjectUnderTest);
this.participatorManager = new NewTestCaseWizardParticipatorManager();

mainSrcFolder = (IPackageFragmentRoot) element.getPackageFragment().getParent();
Expand All @@ -48,7 +51,7 @@ public NewTestCaseWizard(final IType element)
public void addPages()
{
this.pageTwo = new NewTestCaseWizardPageTwo();
this.pageOne = new MoreUnitWizardPageOne(this.pageTwo, this.preferences, LanguageType.forPath(getType().getPath()));
this.pageOne = new MoreUnitWizardPageOne(this.pageTwo, javaProjectUnderTest, this.preferences, LanguageType.forPath(getType().getPath()));
this.pageOne.setWizard(this);
this.pageTwo.setWizard(this);
this.pageOne.init(new StructuredSelection(getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.moreunit.JavaProjectSWTBotTestHelper;
import org.moreunit.test.context.Project;
import org.moreunit.test.context.Properties;
import org.moreunit.test.context.TestProject;
import org.moreunit.test.context.TestType;

@RunWith(SWTBotJunit4ClassRunner.class)
Expand Down Expand Up @@ -96,4 +97,58 @@ public String getFailureMessage() {
assertThat(methods).hasSize(2).onProperty("elementName").contains("testGetNumber1Suffix");
}

@Project(mainSrc = "MethodCreation_class_with_method.txt",
mainSrcFolder = "src",
testProject = @TestProject(src = "MethodCreation_test_without_method.txt"),
properties = @Properties(
testType = TestType.JUNIT4,
testClassNameTemplate = "${srcFile}Test",
testMethodPrefix = true))
@Test
public void should_create_testmethod_when_shortcut_is_pressed_in_method_with_test_folder_in_another_project() throws JavaModelException
{
openResource("TheWorld.java");
SWTBotEclipseEditor cutEditor = bot.activeEditor().toTextEditor();
// move cursor to method
int lineNumberOfMethod = 6;
cutEditor.navigateTo(lineNumberOfMethod, 9);
bot.waitUntil(new ConditionCursorLine(cutEditor, lineNumberOfMethod));

getShortcutStrategy().pressGenerateShortcut();

// adding the method to the testcase takes a short moment
bot.waitUntil(new DefaultCondition()
{

@Override
public boolean test() throws Exception
{
IMethod[] methods = context.getCompilationUnit("testing.TheWorldTest").findPrimaryType().getMethods();
return methods.length == 1;
}

@Override
public String getFailureMessage()
{
try
{
IMethod[] methods = context.getCompilationUnit("testing.TheWorldTest").findPrimaryType().getMethods();
StringBuilder errorMessage = new StringBuilder("Element names of methods found: ");
for (IMethod method : methods)
{
errorMessage.append(method.getElementName());
}
return errorMessage.toString();
}
catch (JavaModelException e)
{
return "Cannot retrieve methods " + e.getMessage();
}
}
}, 20000);

IMethod[] methods = context.getCompilationUnit("testing.TheWorldTest").findPrimaryType().getMethods();
assertThat(methods).onProperty("elementName").containsOnly("testGetNumber1");
}

}

0 comments on commit 2ac1539

Please sign in to comment.