package junit.extensions.eclipse.quick.internal.action; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import junit.extensions.eclipse.quick.JavaElements; import junit.extensions.eclipse.quick.JavaTypes; import junit.extensions.eclipse.quick.NamingRules; import junit.extensions.eclipse.quick.TestingPair; import junit.extensions.eclipse.quick.internal.ExtensionSupport; import junit.extensions.eclipse.quick.internal.HelpSupport; import junit.extensions.eclipse.quick.internal.Messages; import junit.extensions.eclipse.quick.internal.PopupTableSelector; import junit.extensions.eclipse.quick.internal.QuickJUnitPlugin; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.junit.wizards.NewTestCaseWizardPageOne; import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.wizard.IWizard; import org.eclipse.jface.wizard.IWizardPage; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchWizard; import org.eclipse.ui.IWorkingSet; import org.eclipse.ui.PlatformUI; public class OpenTestingPairAction extends QuickJUnitAction { public static final String NEW_WIZARD_SHORTCUT = PlatformUI.PLUGIN_ID + ".new_wizard_shortcut_context"; //$NON-NLS-1$ private IType getTypeOfJavaEditor() throws JavaModelException { IJavaElement element = getElementOfJavaEditor(); if (element == null) return null; IType type = (IType) element.getAncestor(IJavaElement.TYPE); if (type != null) return type; ICompilationUnit unit = getCompilationUnitOfJavaEditor(); if (unit == null) return null; return unit.findPrimaryType(); } private IType getTypeOfJavaElement() { return JavaElements.getPrimaryTypeOf(javaElement); } private IType getTargetType() throws JavaModelException { IType targetType = getTypeOfJavaEditor(); if (targetType != null) return targetType; return getTypeOfJavaElement(); } private static class TypeLabelProvider extends LabelProvider { public String getText(Object element) { return ((IType) element).getFullyQualifiedName(); } } private IType selectType(IAction action, List types) { PopupTableSelector selector = new PopupTableSelector(getShell(), types); selector.setTitle(action.getText()); selector.setCommandForward(action.getActionDefinitionId()); selector.setLabelProvider(new TypeLabelProvider()); return (IType) selector.select(); } private void openPairWizard(IType targetType, String[] pairNames) throws CoreException, JavaModelException { IWorkbench workbench = PlatformUI.getWorkbench(); IWizard wizard = createNewPairWizard(targetType, workbench); Shell parent = workbench.getActiveWorkbenchWindow().getShell(); WizardDialog dialog = new WizardDialog(parent, wizard); dialog.create(); NewTypeWizardPage page = getNewTypeWizardPage(wizard); if(page == null) return; setUpSourceFolderWizard(page,targetType); setUpNewClassCreationWizard(page,pairNames); if (page instanceof NewTestCaseWizardPageOne) { NewTestCaseWizardPageOne testCasePage = (NewTestCaseWizardPageOne) page; boolean isJUnit4 = testCasePage.isJUnit4(); if(isJUnit4){ testCasePage.setSuperClass("java.lang.Object", true); } } HelpSupport.setHelp(dialog.getShell(), NEW_WIZARD_SHORTCUT); dialog.open(); } private NewTypeWizardPage getNewTypeWizardPage(IWizard wizard) { IWizardPage page = wizard.getPages()[0]; if (page instanceof NewTypeWizardPage) { return (NewTypeWizardPage) page; }else{ QuickJUnitPlugin.getDefault().handleSystemError(new ClassCastException(), this); return null; } } private void setUpSourceFolderWizard(NewTypeWizardPage page, IType targetType) { if(isJavaTest(targetType)) return; IJavaProject javaProject = targetType.getJavaProject(); IPackageFragmentRoot[] roots = getPackageFragmentRoots(javaProject); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; String str = root.getPath().makeRelative().toString(); if(str.indexOf("test") != -1){ page.setPackageFragmentRoot(root,true); return; } } } private boolean isJavaTest(IType targetType) { boolean isTest = false; try { isTest = JavaTypes.isTest(targetType); } catch (Exception e){ QuickJUnitPlugin.getDefault().handleSystemError(e, this); } return isTest; } private IPackageFragmentRoot[] getPackageFragmentRoots(IJavaProject javaProject) { IPackageFragmentRoot[] roots = new IPackageFragmentRoot[0]; try { roots = javaProject.getPackageFragmentRoots(); } catch (Exception e){ QuickJUnitPlugin.getDefault().handleSystemError(e, this); } return roots; } private String extractTypeName(String fullName) { int index = fullName.lastIndexOf('.'); if (index == -1) return fullName; return fullName.substring(index + 1); } private void setUpNewClassCreationWizard(NewTypeWizardPage page, String[] pairNames) { if (pairNames.length == 0) return; String typeName = extractTypeName(pairNames[0]); page.setTypeName(typeName, true); } private IWizard createNewPairWizard(IType targetType, IWorkbench workbench) throws CoreException, JavaModelException { if (JavaTypes.isTest(targetType)) { IWorkbenchWizard wizard = ExtensionSupport.createNewClassCreationWizard(); IStructuredSelection selection = new StructuredSelection(targetType.getPackageFragment()); wizard.init(workbench, selection); return wizard; } else { IWorkbenchWizard wizard = ExtensionSupport.createNewTestCaseCreationWizard(); IStructuredSelection selection = new StructuredSelection(targetType); wizard.init(workbench, selection); return wizard; } } public void run(IAction action) { try { IType targetType = getTargetType(); if (targetType == null) return; String[] pairNames = createTestPair().getPairClassNames(targetType.getFullyQualifiedName()); List testPairTypes = findPairTypes(pairNames); if (testPairTypes.isEmpty()) { runWhenTestingPairNotExist(action, targetType, pairNames); } else { openTestingPair(action, testPairTypes); } } catch (JavaModelException e) { QuickJUnitPlugin.getDefault().handleSystemError(e, this); } catch (CoreException e) { QuickJUnitPlugin.getDefault().handleSystemError(e, this); } } private List findPairTypes(String[] pairNames) throws JavaModelException { Set foundTypes = findCurrentProject(pairNames); if(foundTypes != null && foundTypes.isEmpty() == false){ return new ArrayList(foundTypes); } return new ArrayList(findWorkspace(pairNames)); } private Set findWorkspace(String[] pairNames) throws JavaModelException { Set result = new LinkedHashSet(); IJavaProject[] projects = getJavaProjects(); for (int i = 0; i < projects.length; ++i) { IJavaProject project = projects[i]; if(project.equals(getCurrentProject())){ continue; } Set foundTypes = findType(pairNames, project); result.addAll(foundTypes); } return result; } private Set findCurrentProject(String[] pairNames) throws JavaModelException { IJavaProject project = getCurrentProject(); Set foundTypes = findType(pairNames, project); return foundTypes; } private IJavaProject getCurrentProject() throws JavaModelException { return getCompilationUnitOfJavaEditor().getJavaProject(); } private Set findType(String[] pairNames, IJavaProject project) throws JavaModelException { Set result = new LinkedHashSet(); for (int j = 0; j < pairNames.length; ++j) { IType pairType= project.findType(pairNames[j]); if (pairType == null || pairType instanceof IClassFile) continue; result.add(pairType); } return result; } protected IJavaProject[] getJavaProjects() throws JavaModelException { IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets(); if(workingSets == null) return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); List result = new ArrayList(); for (int i = 0; i < workingSets.length; i++) { IWorkingSet workingSet = workingSets[i]; if(workingSet.isVisible() == false) continue; IAdaptable[] adaptables = workingSet.getElements(); for (int j = 0; j < adaptables.length; j++) { IAdaptable adaptable = adaptables[j]; IJavaProject javaProject = (IJavaProject) adaptable.getAdapter(IJavaProject.class); if(javaProject != null) result.add(javaProject); } } return (IJavaProject[]) result.toArray(new IJavaProject[]{}); } private void runWhenTestingPairNotExist(IAction action, IType targetType, String[] pairNames) throws CoreException, JavaModelException { if (pairNames.length == 0) { openInformation(action, Messages.getString( "OpenTestingPairAction.testPairNotFound", targetType.getFullyQualifiedName())); //$NON-NLS-1$ return; } if (MessageDialog.openQuestion(getShell(), action.getText(), Messages .getString("OpenTestingPairAction.testPairNotFoundAndConfirm"))) { //$NON-NLS-1$ openPairWizard(targetType, pairNames); } } private void openTestingPair(IAction action, List testPairTypes) throws CoreException { IType selectedType = null; if (testPairTypes.size() >= 2) { selectedType = selectType(action, testPairTypes); } else { selectedType = (IType) testPairTypes.get(0); } if (selectedType == null) return; JavaUI.openInEditor(selectedType,true,false); } private TestingPair createTestPair() { TestingPair testPair = new TestingPair(); IPreferenceStore store = QuickJUnitPlugin.getDefault().getPreferenceStore(); String[] rules = new NamingRules(store).getEnableValues(); for (int i = 0; i < rules.length; ++i) { testPair.addNamingRule(rules[i]); } return testPair; } }