diff --git a/org.moreunit.plugin/src/org/moreunit/matching/CorrespondingTypeSearcher.java b/org.moreunit.plugin/src/org/moreunit/matching/CorrespondingTypeSearcher.java index b7495f2f..91e5d35a 100644 --- a/org.moreunit.plugin/src/org/moreunit/matching/CorrespondingTypeSearcher.java +++ b/org.moreunit.plugin/src/org/moreunit/matching/CorrespondingTypeSearcher.java @@ -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; @@ -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 getMatches(boolean alsoIncludeLikelyMatches) diff --git a/org.moreunit.plugin/src/org/moreunit/wizards/MoreUnitWizardPageOne.java b/org.moreunit.plugin/src/org/moreunit/wizards/MoreUnitWizardPageOne.java index f5cc57e0..71275884 100644 --- a/org.moreunit.plugin/src/org/moreunit/wizards/MoreUnitWizardPageOne.java +++ b/org.moreunit.plugin/src/org/moreunit/wizards/MoreUnitWizardPageOne.java @@ -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; @@ -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 @@ -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); diff --git a/org.moreunit.plugin/src/org/moreunit/wizards/NewTestCaseWizard.java b/org.moreunit.plugin/src/org/moreunit/wizards/NewTestCaseWizard.java index 674afc94..f95fa0e4 100644 --- a/org.moreunit.plugin/src/org/moreunit/wizards/NewTestCaseWizard.java +++ b/org.moreunit.plugin/src/org/moreunit/wizards/NewTestCaseWizard.java @@ -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; @@ -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(); @@ -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())); diff --git a/org.moreunit.swtbot.test/src/org/moreunit/create/MethodCreationTest.java b/org.moreunit.swtbot.test/src/org/moreunit/create/MethodCreationTest.java index ce347639..4d00a535 100644 --- a/org.moreunit.swtbot.test/src/org/moreunit/create/MethodCreationTest.java +++ b/org.moreunit.swtbot.test/src/org/moreunit/create/MethodCreationTest.java @@ -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) @@ -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"); + } + }