Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 90628
Collapse All | Expand All

(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/JUnitFolderProxyNode.java (-5 lines)
Lines 12-21 Link Here
12
 **********************************************************************/ 
12
 **********************************************************************/ 
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
14
15
import java.util.List;
16
17
import org.eclipse.core.resources.IContainer;
15
import org.eclipse.core.resources.IContainer;
18
import org.eclipse.hyades.test.tools.ui.java.internal.util.TestJavaUtil;
19
import org.eclipse.hyades.test.ui.navigator.IFileProxyManager;
16
import org.eclipse.hyades.test.ui.navigator.IFileProxyManager;
20
import org.eclipse.hyades.test.ui.navigator.IResourceChangeListenerProxyNode;
17
import org.eclipse.hyades.test.ui.navigator.IResourceChangeListenerProxyNode;
21
import org.eclipse.hyades.test.ui.navigator.ITestFolderContentValidator;
18
import org.eclipse.hyades.test.ui.navigator.ITestFolderContentValidator;
Lines 38-44 Link Here
38
    /** the type of test that this provider proxy node covers */
35
    /** the type of test that this provider proxy node covers */
39
    private String type;
36
    private String type;
40
    private String name;
37
    private String name;
41
    private List sourceFolders;
42
    private ITestFolderContentValidator validator;
38
    private ITestFolderContentValidator validator;
43
    private IFileProxyManager fileProxyManager;
39
    private IFileProxyManager fileProxyManager;
44
    
40
    
Lines 59-65 Link Here
59
    }
55
    }
60
    protected JUnitFolderProxyNode(IContainer container, ITestFolderContentValidator validator, String type, IFileProxyManager fileProxyManager, Object parent) {
56
    protected JUnitFolderProxyNode(IContainer container, ITestFolderContentValidator validator, String type, IFileProxyManager fileProxyManager, Object parent) {
61
        super(container, validator, fileProxyManager, false, parent);
57
        super(container, validator, fileProxyManager, false, parent);
62
        sourceFolders = TestJavaUtil.getSourceFolders(JavaCore.create(container.getProject()));
63
        this.validator = validator;
58
        this.validator = validator;
64
        this.fileProxyManager = fileProxyManager;
59
        this.fileProxyManager = fileProxyManager;
65
    }
60
    }
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/PackageProxyNode.java (-1 / +4 lines)
Lines 139-144 Link Here
139
                return null;
139
                return null;
140
            }
140
            }
141
        }
141
        }
142
        if (adapter.isAssignableFrom(IPackageFragment.class)) {
143
        	return fragment;
144
        }
142
        return null;
145
        return null;
143
    }
146
    }
144
147
Lines 329-335 Link Here
329
                } else {
332
                } else {
330
                    //- this should never arrive, because the proxy of a folder
333
                    //- this should never arrive, because the proxy of a folder
331
                    //- should be a IResourceChangeListenerProxyNode
334
                    //- should be a IResourceChangeListenerProxyNode
332
                    UiPlugin.logError("The proxy node derived from a folder should be a IResourceChangeListenerProxyNode"); // $NON-NLS-1$
335
                    UiPlugin.logError("The proxy node derived from a folder should be a IResourceChangeListenerProxyNode"); // $NON-NLS-1$ //$NON-NLS-1$
333
                }
336
                }
334
            }
337
            }
335
        }
338
        }
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/JUnitTypeProvider.java (-4 / +8 lines)
Lines 36-43 Link Here
36
public class JUnitTypeProvider implements ITypeProvider, IResourceChangeListener, IDisposable {
36
public class JUnitTypeProvider implements ITypeProvider, IResourceChangeListener, IDisposable {
37
    
37
    
38
    /** The file proxy manager used to cache file proxies */
38
    /** The file proxy manager used to cache file proxies */
39
    private IFileProxyManager fileProxyManager;
39
    protected IFileProxyManager fileProxyManager;
40
	private IProxyNodeListener refresher;
40
	protected IProxyNodeListener refresher;
41
41
42
	private Map projectToProxyMap = new HashMap();
42
	private Map projectToProxyMap = new HashMap();
43
	
43
	
Lines 53-61 Link Here
53
            return null;
53
            return null;
54
        }
54
        }
55
        if(projectToProxyMap.containsKey(project)) {
55
        if(projectToProxyMap.containsKey(project)) {
56
            return (JUnitTypeProviderProxyNode) projectToProxyMap.get(project);
56
            return (ITypeProviderProxyNode) projectToProxyMap.get(project);
57
        } else {
57
        } else {
58
            JUnitTypeProviderProxyNode node = JUnitTypeProviderProxyNode.create(project, type, fileProxyManager, project);
58
        	ITypeProviderProxyNode node = createTypeProviderProxyNode(project, type);
59
            if (node != null) {
59
            if (node != null) {
60
                projectToProxyMap.put(project, node);
60
                projectToProxyMap.put(project, node);
61
            }
61
            }
Lines 69-74 Link Here
69
//        return node;
69
//        return node;
70
    }
70
    }
71
    
71
    
72
    protected ITypeProviderProxyNode createTypeProviderProxyNode(IProject project, String type) {
73
    	return JUnitTypeProviderProxyNode.create(project, type, fileProxyManager, project);
74
    }
75
    
72
    /* (non-Javadoc)
76
    /* (non-Javadoc)
73
     * @see org.eclipse.hyades.test.ui.navigator.ITypeProvider#init(org.eclipse.hyades.test.ui.navigator.ITypeProviderContext)
77
     * @see org.eclipse.hyades.test.ui.navigator.ITypeProvider#init(org.eclipse.hyades.test.ui.navigator.ITypeProviderContext)
74
     */
78
     */
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/JUnitTypeProviderProxyNode.java (-3 / +24 lines)
Lines 113-121 Link Here
113
        IPackageFragmentRoot root = getProjectAsSourceFolder(project);
113
        IPackageFragmentRoot root = getProjectAsSourceFolder(project);
114
        if(root == null) {
114
        if(root == null) {
115
            //- the current project is not a source folder
115
            //- the current project is not a source folder
116
            proxy = JUnitFolderProxyNode.create(project, validator, type, fileProxyManager, parent);
116
            proxy = createNonSourceFolderProxyNode(project, type, fileProxyManager, parent);
117
        } else {
117
        } else {
118
            proxy = SourceFolderProxyNode.create(root, validator, fileProxyManager, parent);
118
            proxy = createSourceFolderProxyNode(fileProxyManager, parent, root);
119
        }
119
        }
120
        AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestSuiteMappingRegistry();
120
        AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestSuiteMappingRegistry();
121
        IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
121
        IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
Lines 130-137 Link Here
130
		this.fileProxyManager = fileProxyManager;
130
		this.fileProxyManager = fileProxyManager;
131
		this.parent = parent;
131
		this.parent = parent;
132
    }
132
    }
133
134
	/**
135
	 * @param fileProxyManager
136
	 * @param parent
137
	 * @param root
138
	 * @return
139
	 */
140
	protected IProxyNode createSourceFolderProxyNode(IFileProxyManager fileProxyManager, Object parent, IPackageFragmentRoot root) {
141
		return SourceFolderProxyNode.create(root, validator, fileProxyManager, parent);
142
	}
143
144
	/**
145
	 * @param project
146
	 * @param type
147
	 * @param fileProxyManager
148
	 * @param parent
149
	 * @return
150
	 */
151
	protected IProxyNode createNonSourceFolderProxyNode(IProject project, String type, IFileProxyManager fileProxyManager, Object parent) {
152
		return JUnitFolderProxyNode.create(project, validator, type, fileProxyManager, parent);
153
	}
133
    
154
    
134
    private IProxyNode getDelegate() {
155
    protected IProxyNode getDelegate() {
135
        return proxy;
156
        return proxy;
136
    }
157
    }
137
    
158
    
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/wizard/JUnitImportWizard.java (-4 / +6 lines)
Lines 20-26 Link Here
20
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.core.runtime.SubProgressMonitor;
22
import org.eclipse.core.runtime.SubProgressMonitor;
23
import org.eclipse.hyades.test.tools.core.common.TestCommon;
24
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
23
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
25
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
24
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
26
import org.eclipse.hyades.test.tools.ui.java.TestJavaImages;
25
import org.eclipse.hyades.test.tools.ui.java.TestJavaImages;
Lines 57-63 Link Here
57
     * @see Wizard#performFinish
56
     * @see Wizard#performFinish
58
     */
57
     */
59
    public boolean performFinish() {
58
    public boolean performFinish() {
59
    	page1.saveDialogSettings();
60
        final List junitScripts = page1.getSelectedElements();
60
        final List junitScripts = page1.getSelectedElements();
61
        final String type = page1.getSelectedType();
61
        IRunnableWithProgress operation = new WorkspaceModifyOperation() {
62
        IRunnableWithProgress operation = new WorkspaceModifyOperation() {
62
            public void execute(IProgressMonitor monitor) throws InvocationTargetException {
63
            public void execute(IProgressMonitor monitor) throws InvocationTargetException {
63
                try {
64
                try {
Lines 67-73 Link Here
67
                        //- by construction all elements checked should be of type ICompilationUnit
68
                        //- by construction all elements checked should be of type ICompilationUnit
68
                        if (object instanceof ICompilationUnit) {
69
                        if (object instanceof ICompilationUnit) {
69
                            ICompilationUnit cu = (ICompilationUnit) object;
70
                            ICompilationUnit cu = (ICompilationUnit) object;
70
                            generateTest(cu, new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
71
                            generateTest(type, cu, new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
71
                        } else {
72
                        } else {
72
                        	ToolsUiPlugin.logError("Returned element of JUnit scripts selection page should be a Compilation Unit"); //$NON-NLS-1$
73
                        	ToolsUiPlugin.logError("Returned element of JUnit scripts selection page should be a Compilation Unit"); //$NON-NLS-1$
73
                        }
74
                        }
Lines 93-102 Link Here
93
     * @param project
94
     * @param project
94
     * @param overwrite
95
     * @param overwrite
95
     */
96
     */
96
    protected void generateTest(ICompilationUnit cu, SubProgressMonitor monitor) {
97
    protected void generateTest(String type, ICompilationUnit cu, SubProgressMonitor monitor) {
97
        monitor.beginTask(MessageFormat.format(ToolsUiPlugin.getString("GENERATING_LABEL"), new Object[]{ cu.getElementName()}), 1); //$NON-NLS-1$
98
        monitor.beginTask(MessageFormat.format(ToolsUiPlugin.getString("GENERATING_LABEL"), new Object[]{ cu.getElementName()}), 1); //$NON-NLS-1$
98
        try {
99
        try {
99
        	JUnitTestSuiteFacade.createTestSuite(cu, TestCommon.JUNIT_TEST_SUITE_TYPE, /*force*/false);
100
        	JUnitTestSuiteFacade.createTestSuite(cu, type, /*force*/false);
100
        } catch (CoreException e) {
101
        } catch (CoreException e) {
101
			UiPlugin.logError(e);
102
			UiPlugin.logError(e);
102
		} finally {
103
		} finally {
Lines 115-120 Link Here
115
116
116
    public void addPages() {
117
    public void addPages() {
117
        page1 = createJUnitScriptsSelectionPage();
118
        page1 = createJUnitScriptsSelectionPage();
119
        page1.init(selection);
118
        addPage(page1);
120
        addPage(page1);
119
    }
121
    }
120
    
122
    
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/wizard/JUnitScriptsSelectionPage.java (-7 / +171 lines)
Lines 11-39 Link Here
11
 *******************************************************************************/
11
 *******************************************************************************/
12
package org.eclipse.hyades.test.tools.ui.java.internal.junit.wizard;
12
package org.eclipse.hyades.test.tools.ui.java.internal.junit.wizard;
13
13
14
import java.util.ArrayList;
15
import java.util.Iterator;
14
import java.util.List;
16
import java.util.List;
15
17
16
import org.eclipse.core.resources.ResourcesPlugin;
18
import org.eclipse.core.resources.ResourcesPlugin;
19
import org.eclipse.core.runtime.IAdaptable;
20
import org.eclipse.core.runtime.Platform;
21
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitExtensionsRegistry;
22
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitExtensionsRegistry.JUnitTestSuiteFactoryDescriptor;
23
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteFactory;
17
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
24
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
25
import org.eclipse.hyades.test.ui.TestUIExtension;
26
import org.eclipse.hyades.ui.HyadesUIPlugin;
27
import org.eclipse.hyades.ui.extension.IAssociationDescriptor;
28
import org.eclipse.hyades.ui.extension.IAssociationMapping;
29
import org.eclipse.hyades.ui.internal.extension.AssociationMappingRegistry;
18
import org.eclipse.hyades.ui.util.TreeSelector;
30
import org.eclipse.hyades.ui.util.TreeSelector;
19
import org.eclipse.jdt.core.ICompilationUnit;
31
import org.eclipse.jdt.core.ICompilationUnit;
20
import org.eclipse.jdt.core.IJavaModel;
32
import org.eclipse.jdt.core.IJavaModel;
21
import org.eclipse.jdt.core.JavaCore;
33
import org.eclipse.jdt.core.JavaCore;
22
import org.eclipse.jdt.ui.JavaElementLabelProvider;
34
import org.eclipse.jdt.ui.JavaElementLabelProvider;
35
import org.eclipse.jface.dialogs.IDialogSettings;
23
import org.eclipse.jface.viewers.CheckStateChangedEvent;
36
import org.eclipse.jface.viewers.CheckStateChangedEvent;
37
import org.eclipse.jface.viewers.ComboViewer;
24
import org.eclipse.jface.viewers.ICheckStateListener;
38
import org.eclipse.jface.viewers.ICheckStateListener;
39
import org.eclipse.jface.viewers.ISelection;
40
import org.eclipse.jface.viewers.ISelectionChangedListener;
41
import org.eclipse.jface.viewers.IStructuredContentProvider;
42
import org.eclipse.jface.viewers.IStructuredSelection;
43
import org.eclipse.jface.viewers.LabelProvider;
44
import org.eclipse.jface.viewers.SelectionChangedEvent;
45
import org.eclipse.jface.viewers.StructuredSelection;
46
import org.eclipse.jface.viewers.Viewer;
25
import org.eclipse.jface.wizard.WizardPage;
47
import org.eclipse.jface.wizard.WizardPage;
26
import org.eclipse.swt.SWT;
48
import org.eclipse.swt.SWT;
49
import org.eclipse.swt.graphics.Image;
27
import org.eclipse.swt.layout.GridData;
50
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
51
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Composite;
52
import org.eclipse.swt.widgets.Composite;
53
import org.eclipse.swt.widgets.Label;
30
54
31
/**
55
/**
32
 * @author jgout
56
 * @author jgout
33
 */
57
 */
34
public class JUnitScriptsSelectionPage extends WizardPage implements ICheckStateListener {
58
public class JUnitScriptsSelectionPage extends WizardPage implements ICheckStateListener {
35
59
36
    private TreeSelector selector;
60
    private static final String DS_TYPE = "type"; //$NON-NLS-1$
61
    
62
	private TreeSelector selector;
63
    private ComboViewer testFactoryList;
64
    private JUnitWorkspaceContentProvider contentProvider;
65
    protected List initialSelection;
37
	
66
	
38
    public JUnitScriptsSelectionPage() {
67
    public JUnitScriptsSelectionPage() {
39
    	super("JUnitScriptSelectionPage"); //$NON-NLS-1$
68
    	super("JUnitScriptSelectionPage"); //$NON-NLS-1$
Lines 45-68 Link Here
45
	public void createControl(Composite parent) {
74
	public void createControl(Composite parent) {
46
        initializeDialogUnits(parent);
75
        initializeDialogUnits(parent);
47
        Composite composite = new Composite(parent, SWT.NULL);
76
        Composite composite = new Composite(parent, SWT.NULL);
48
        GridLayout gl = new GridLayout();
77
        GridLayout gl = new GridLayout(2, false);
49
        gl.numColumns = 1;
50
        composite.setLayout(gl);
78
        composite.setLayout(gl);
51
        composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
79
        gl.verticalSpacing = 20;
80
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
52
        composite.setFont(parent.getFont());
81
        composite.setFont(parent.getFont());
82
        
83
        Label label = new Label(composite, SWT.NONE);
84
        label.setText("Destination type:");
85
        label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
86
        testFactoryList = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
87
        testFactoryList.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
88
        testFactoryList.setContentProvider(new IStructuredContentProvider() {
89
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
90
				// n/a
91
			}
92
			public void dispose() {
93
				// NOP
94
			}
95
			public Object[] getElements(Object inputElement) {
96
				return JUnitExtensionsRegistry.getInstance().getAvailableFactories(/*updateSupport*/false);
97
			}
98
		});
99
        testFactoryList.setLabelProvider(new LabelProvider() {
100
			public String getText(Object element) {
101
				String type = ((JUnitExtensionsRegistry.JUnitTestSuiteFactoryDescriptor)element).getType();
102
				AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestSuiteMappingRegistry();
103
				IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
104
				IAssociationDescriptor descriptor = associationMapping.getDefaultAssociationDescriptor(type);
105
				return descriptor.getName();
106
			}
107
			public Image getImage(Object element) {
108
				String type = ((JUnitExtensionsRegistry.JUnitTestSuiteFactoryDescriptor)element).getType();
109
				AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestSuiteMappingRegistry();
110
				IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
111
				IAssociationDescriptor descriptor = associationMapping.getDefaultAssociationDescriptor(type);
112
				return descriptor.getImage();
113
			}
114
		});
115
        testFactoryList.setInput(ResourcesPlugin.getWorkspace().getRoot());
116
        testFactoryList.addSelectionChangedListener(new ISelectionChangedListener() {
117
			public void selectionChanged(SelectionChangedEvent event) {
118
				IJUnitTestSuiteFactory factory = getTestSuiteFactory();
119
				if (factory != null) {
120
					contentProvider.setJUnitTestSuiteFactory(factory);
121
					List checkedElements;
122
					if (initialSelection != null) {
123
						checkedElements = initialSelection;
124
						initialSelection = null;
125
					} else {
126
						checkedElements = selector.getSelectedElements();
127
					}
128
					IJavaModel javaWorkspace = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
129
					selector.setInput(null);
130
					selector.setInput(javaWorkspace);
131
					selector.setInitialCheckedElements(checkedElements);
132
					setPageComplete(true);
133
					setErrorMessage(null);
134
				} else {
135
					selector.setInput(null);
136
					setPageComplete(false);
137
					setErrorMessage("The selected generator has encountered an internal error");
138
				}
139
			}
140
		});
141
        
53
        //- build the selection widget
142
        //- build the selection widget
54
        IJavaModel javaWorkspace = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
143
        IJavaModel javaWorkspace = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
55
        JavaElementLabelProvider javaLabelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
144
        JavaElementLabelProvider javaLabelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
56
        JUnitWorkspaceContentProvider contentProvider = createContentProvider();
145
        contentProvider = new JUnitWorkspaceContentProvider();
57
        selector = new TreeSelector(composite, javaWorkspace, contentProvider, javaLabelProvider, SWT.BORDER);
146
        selector = new TreeSelector(composite, javaWorkspace, contentProvider, javaLabelProvider, SWT.BORDER);
58
        contentProvider.setRunnableContext(this.getContainer());
147
        contentProvider.setRunnableContext(this.getContainer());
59
        selector.getTreeViewer().addCheckStateListener(this);
148
        selector.getTreeViewer().addCheckStateListener(this);
149
        selector.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
150
        
151
        readDialogSettings();
152
        
60
        setPageComplete(false);
153
        setPageComplete(false);
61
        setControl(composite);
154
        setControl(composite);
62
	}
155
	}
63
	
156
	
64
	protected JUnitWorkspaceContentProvider createContentProvider() {
157
	protected void init(IStructuredSelection selection) {
65
		return new JUnitWorkspaceContentProvider();
158
		Iterator it = selection.iterator();
159
		initialSelection = new ArrayList(selection.size());
160
		while (it.hasNext()) {
161
			Object o = it.next();
162
			if (o instanceof IAdaptable) {
163
				ICompilationUnit cu = (ICompilationUnit) ((IAdaptable)o).getAdapter(ICompilationUnit.class);
164
				if (cu != null) {
165
					initialSelection.add(cu);
166
				}
167
			}
168
		}
169
	}
170
	
171
	protected IJUnitTestSuiteFactory getTestSuiteFactory() {
172
		ISelection selection = testFactoryList.getSelection();
173
		if (selection instanceof IStructuredSelection) {
174
			JUnitExtensionsRegistry.JUnitTestSuiteFactoryDescriptor descr =
175
				(JUnitExtensionsRegistry.JUnitTestSuiteFactoryDescriptor) ((IStructuredSelection)selection).getFirstElement();
176
			if (descr != null) {
177
				return descr.getFactory();
178
			}
179
		}
180
		return null;
181
	}
182
	
183
	public String getSelectedType() {
184
		ISelection selection = testFactoryList.getSelection();
185
		if (selection instanceof IStructuredSelection) {
186
			JUnitTestSuiteFactoryDescriptor descr =
187
				(JUnitTestSuiteFactoryDescriptor) ((IStructuredSelection)selection).getFirstElement();
188
			if (descr != null) {
189
				return descr.getType();
190
			}
191
		}
192
		return null;
193
	}
194
	
195
	public void setSelectedType(String type) {
196
		JUnitTestSuiteFactoryDescriptor descr = JUnitExtensionsRegistry.getInstance().getDescriptor(type);
197
		if (descr != null) {
198
			testFactoryList.setSelection(new StructuredSelection(descr));
199
		} else {
200
			testFactoryList.setSelection(StructuredSelection.EMPTY);
201
		}
66
	}
202
	}
67
	
203
	
68
    public List getSelectedElements() {
204
    public List getSelectedElements() {
Lines 72-75 Link Here
72
	public void checkStateChanged(CheckStateChangedEvent event) {
208
	public void checkStateChanged(CheckStateChangedEvent event) {
73
		setPageComplete(selector.getSelectedElements().size() != 0);
209
		setPageComplete(selector.getSelectedElements().size() != 0);
74
	}
210
	}
211
	
212
	protected IDialogSettings getDialogSettings() {
213
		IDialogSettings pluginSettings = ToolsUiPlugin.getDefault().getDialogSettings();
214
		IDialogSettings settings = pluginSettings.getSection(JUnitScriptsSelectionPage.class.getName());
215
		if (settings == null) {
216
			settings = pluginSettings.addNewSection(JUnitScriptsSelectionPage.class.getName());
217
		}
218
		return settings;
219
	}
220
	
221
	protected void readDialogSettings() {
222
		IDialogSettings settings = getDialogSettings();
223
		String type = settings.get(DS_TYPE);
224
		if (type != null) {
225
			setSelectedType(type);
226
		}
227
		if (getSelectedType() == null) {
228
			// Default to the first item in list
229
			Object o = testFactoryList.getElementAt(0);
230
	        if (o != null) testFactoryList.setSelection(new StructuredSelection(o));
231
		}
232
	}
233
	
234
	/*package*/ void saveDialogSettings() {
235
		IDialogSettings settings = getDialogSettings();
236
		settings.put(DS_TYPE, getSelectedType());
237
	}
238
75
}
239
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/wizard/JUnitWorkspaceContentProvider.java (-14 / +39 lines)
Lines 17-26 Link Here
17
import java.util.List;
17
import java.util.List;
18
import java.util.Map;
18
import java.util.Map;
19
19
20
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IProject;
22
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IProgressMonitor;
23
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.NullProgressMonitor;
24
import org.eclipse.core.runtime.NullProgressMonitor;
22
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
25
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
23
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
26
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
27
import org.eclipse.hyades.test.tools.core.internal.plugin.util.PDEProjectUtil;
28
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteFactory;
29
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
24
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
30
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
25
import org.eclipse.jdt.core.ICompilationUnit;
31
import org.eclipse.jdt.core.ICompilationUnit;
26
import org.eclipse.jdt.core.IJavaElement;
32
import org.eclipse.jdt.core.IJavaElement;
Lines 41-49 Link Here
41
	
47
	
42
	private Map children = new HashMap();
48
	private Map children = new HashMap();
43
	private IRunnableContext context;
49
	private IRunnableContext context;
50
	private IJUnitTestSuiteFactory factory;
51
	
52
	public void setJUnitTestSuiteFactory(IJUnitTestSuiteFactory factory) {
53
		if (factory != this.factory) {
54
			this.factory = factory;
55
			children.clear();
56
		}
57
	}
44
	
58
	
45
	private Object[] initChildren(Object element) {
59
	private Object[] initChildren(Object element) {
46
		
47
		//- java project
60
		//- java project
48
		if (element instanceof IJavaProject) {
61
		if (element instanceof IJavaProject) {
49
			IJavaProject jProject = (IJavaProject) element;
62
			IJavaProject jProject = (IJavaProject) element;
Lines 102-108 Link Here
102
			try {
115
			try {
103
				IJavaElement [] compilationUnits = pack.getChildren();
116
				IJavaElement [] compilationUnits = pack.getChildren();
104
				for(int c = 0; c < compilationUnits.length; c++) {
117
				for(int c = 0; c < compilationUnits.length; c++) {
105
					if(isJUnitScript(compilationUnits[c])) {
118
					if(isDisplayed(compilationUnits[c])) {
106
						//- Actually, the current compilation unit is a JUnit script, we need to keep it
119
						//- Actually, the current compilation unit is a JUnit script, we need to keep it
107
						ret.add(compilationUnits[c]);
120
						ret.add(compilationUnits[c]);
108
					}
121
					}
Lines 120-138 Link Here
120
	 * @param element 
133
	 * @param element 
121
	 * @return
134
	 * @return
122
	 */
135
	 */
123
	private boolean isJUnitScript(IJavaElement element) {
136
	private boolean isDisplayed(IJavaElement element) {
124
		if (element instanceof ICompilationUnit) {
137
		if (element instanceof ICompilationUnit) {
125
			ICompilationUnit cu = (ICompilationUnit) element;
138
			ICompilationUnit cu = (ICompilationUnit)element;
126
			IType mainType = cu.findPrimaryType();
139
			if (factory.isSupported(cu)) {
127
			try {
140
				try {
128
				if (mainType != null) {
141
					return JUnitModelUpdater.findTestSuiteFile((IFile)cu.getUnderlyingResource()) == null;
129
					if (SourcePage.isTestImplementor(mainType)) {
142
				} catch (JavaModelException e) {
130
						// Has a TestSuite already been defined for this type?
143
					return false;
131
						return JUnitModelUpdater.findTestSuite(mainType, new ResourceSetImpl()) == null;
144
				} catch (CoreException e) {
132
					}
145
					return false;
133
				}
146
				}
134
			} catch (JavaModelException e) {
135
				ToolsUiPlugin.logError(e);
136
			}
147
			}
137
		}
148
		}
138
		return false;
149
		return false;
Lines 140-146 Link Here
140
151
141
	public Object[] getChildren(Object element) {
152
	public Object[] getChildren(Object element) {
142
		if (element instanceof IJavaModel) {
153
		if (element instanceof IJavaModel) {
143
			return super.getChildren(element);
154
			Object[] projects = super.getChildren(element);
155
			return filterProjects(projects);
144
		}
156
		}
145
		if (!children.containsKey(element)) {
157
		if (!children.containsKey(element)) {
146
			Object[] ch = initChildren(element);
158
			Object[] ch = initChildren(element);
Lines 151-156 Link Here
151
		}
163
		}
152
	}
164
	}
153
	
165
	
166
	private Object[] filterProjects(Object[] projects) {
167
		if (factory == null) {
168
			return NO_CHILDREN;
169
		}
170
		ArrayList result = new ArrayList(projects.length);
171
		for (int i = 0; i < projects.length; i++) {
172
			if (factory.isSupported((IJavaProject)projects[i])) {
173
				result.add(projects[i]);
174
			}
175
		}
176
		return result.toArray();
177
	}
178
	
154
	public boolean hasChildren(Object element) {
179
	public boolean hasChildren(Object element) {
155
		if (element instanceof IJavaProject) {
180
		if (element instanceof IJavaProject) {
156
			// Always assume true. Digging into the project would be too
181
			// Always assume true. Digging into the project would be too
(-)plugin.xml (-13 / +35 lines)
Lines 440-445 Link Here
440
               menubarPath="org.eclipse.ui.OpenWithSubMenu"
440
               menubarPath="org.eclipse.ui.OpenWithSubMenu"
441
               id="org.eclipse.hyades.test.tools.ui.JUnitTestSuiteOpenCode"/>
441
               id="org.eclipse.hyades.test.tools.ui.JUnitTestSuiteOpenCode"/>
442
      </objectContribution>
442
      </objectContribution>
443
      <objectContribution
444
            adaptable="false"
445
            id="org.eclipse.hyades.test.tools.ui.RegularJUnitTestSuiteContribution"
446
            objectClass="org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator.RegularJUnitTestSuiteProxyNode">
447
         <action
448
               class="org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator.actions.ImportRegularJUnitTestSuiteAction"
449
               enablesFor="*"
450
               id="org.eclipse.hyades.test.tools.ui.RegularJUnitTestSuiteImport"
451
               label="Create Model"
452
               menubarPath="org.eclipse.ui.OpenWithSubMenu"/>
453
      </objectContribution>
443
   </extension>   
454
   </extension>   
444
   <extension
455
   <extension
445
         point="org.eclipse.ui.popupMenus">
456
         point="org.eclipse.ui.popupMenus">
Lines 514-532 Link Here
514
      </wizard>
525
      </wizard>
515
   </extension>
526
   </extension>
516
   <extension
527
   <extension
517
         point="org.eclipse.ui.importWizards">
518
      <wizard
519
            category="org.eclipse.tptp.platform.category.test.import"
520
            class="org.eclipse.hyades.test.tools.ui.plugin.internal.junit.wizard.PluginJUnitImportWizard"
521
            icon="icons/full/etool16/newpluginjsuite_wiz.gif"
522
            id="org.eclipse.hyades.test.tools.ui.plugin.import.junit"
523
            name="%PluginJUnitImportWizard.name">
524
         <description>
525
            %PluginJUnitImportWizard.description
526
         </description>
527
      </wizard>
528
   </extension>
529
   <extension
530
         point="org.eclipse.hyades.ui.editorExtensions">
528
         point="org.eclipse.hyades.ui.editorExtensions">
531
      <editorExtension
529
      <editorExtension
532
            class="org.eclipse.hyades.test.tools.ui.java.internal.junit.editor.JUnitEditorExtension"
530
            class="org.eclipse.hyades.test.tools.ui.java.internal.junit.editor.JUnitEditorExtension"
Lines 580-585 Link Here
580
            recorderMessageHandlers="org.eclipse.hyades.execution.recorder.http.local.HttpRecorderDataProcessor"
578
            recorderMessageHandlers="org.eclipse.hyades.execution.recorder.http.local.HttpRecorderDataProcessor"
581
            wizardPageProvider="org.eclipse.tptp.test.tools.internal.recorder.http.providers.HttpRecorderWizardProvider"/>
579
            wizardPageProvider="org.eclipse.tptp.test.tools.internal.recorder.http.providers.HttpRecorderWizardProvider"/>
582
   </extension>
580
   </extension>
581
   <extension
582
         point="org.eclipse.hyades.ui.typeDescriptions">
583
      <typeDescription
584
            name="Other JUnit tests"
585
            icon="icons/full/obj16/java_jsuite.gif"
586
            type="org.eclipse.hyades.test.other.junit.testSuite"
587
            description="JUnit tests not imported to TPTP"
588
            extension="testsuite">
589
      </typeDescription>
590
      <typeDescription
591
            name="JUnit test method"
592
            icon="icons/full/obj16/java_test.gif"
593
            type="org.eclipse.hyades.test.other.junit.testCase"
594
            description="JUnit test method not imported to TPTP"
595
            extension="testcase">
596
      </typeDescription>
597
   </extension>
598
      <extension
599
            point="org.eclipse.hyades.test.ui.testNavigatorTypeProvider">
600
         <provider
601
               class="org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator.RegularJUnitProvider"
602
               label="Other JUnit tests"
603
               type="org.eclipse.hyades.test.other.junit.testSuite"/>
604
      </extension>
583
      <extension
605
      <extension
584
            point="org.eclipse.ui.preferencePages">
606
            point="org.eclipse.ui.preferencePages">
585
         <page
607
         <page
(-)plugin.properties (-3 lines)
Lines 475-480 Link Here
475
WIZ_NEW_WORKBENCH_LOCATION_DSC        = Creates a new location for an Eclipse workbench. A workbench location is used to specify where a TPTP JUnit Plug-in test will run.
475
WIZ_NEW_WORKBENCH_LOCATION_DSC        = Creates a new location for an Eclipse workbench. A workbench location is used to specify where a TPTP JUnit Plug-in test will run.
476
WORKBENCH_PROPERTY_GROUP_TTL          = Eclipse Workbench Properties
476
WORKBENCH_PROPERTY_GROUP_TTL          = Eclipse Workbench Properties
477
WORKBENCH_PROPERTY_GROUP_DSC          = This location defines the following workbench properties:
477
WORKBENCH_PROPERTY_GROUP_DSC          = This location defines the following workbench properties:
478
PluginJUnitImportWizard.name		  = JUnit plug-in tests to TPTP
479
PluginJUnitImportWizard.description   = Import JUnit Tests in the workspace as TPTP plug-in tests.
480
PluginJUnitImportWizard.title         = Import JUnit plug-in tests to TPTP
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/editor/SourceInfoViewer.java (-1 / +3 lines)
Lines 36-41 Link Here
36
import org.eclipse.hyades.test.core.util.EMFUtil;
36
import org.eclipse.hyades.test.core.util.EMFUtil;
37
import org.eclipse.hyades.test.tools.core.CorePlugin;
37
import org.eclipse.hyades.test.tools.core.CorePlugin;
38
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
38
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
39
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
39
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
40
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
40
import org.eclipse.hyades.test.tools.ui.java.internal.util.TestJavaUtil;
41
import org.eclipse.hyades.test.tools.ui.java.internal.util.TestJavaUtil;
41
import org.eclipse.hyades.test.ui.editor.form.util.EditorForm;
42
import org.eclipse.hyades.test.ui.editor.form.util.EditorForm;
Lines 466-472 Link Here
466
				markDirty();
467
				markDirty();
467
			} else if (e.widget == updateFromCodeButton) {
468
			} else if (e.widget == updateFromCodeButton) {
468
				try {
469
				try {
469
					if (JUnitModelUpdater.updateTestSuiteFromCode((ITestSuite)getImplementor().getOwner())) {
470
					ITestSuite testSuite = (ITestSuite)getImplementor().getOwner();
471
					if (JUnitModelUpdater.updateTestSuiteFromCode(testSuite, JUnitTestSuiteFacade.getJUnitSourceFile(testSuite))) {
470
						markDirty();
472
						markDirty();
471
					}
473
					}
472
				} catch (CoreException e1) {
474
				} catch (CoreException e1) {
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitPackageProxyNode.java (+69 lines)
Added Link Here
1
/********************************************************************** 
2
 * Copyright (c) 2005 IBM Corporation and others. 
3
 * All rights reserved.   This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0 
5
 * which accompanies this distribution, and is available at 
6
 * http://www.eclipse.org/legal/epl-v10.html 
7
 * $Id: PackageProxyNode.java,v 1.3 2005/06/09 09:33:48 jgout Exp $ 
8
 * 
9
 * Contributors: 
10
 * IBM - Initial API and implementation 
11
 **********************************************************************/ 
12
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
13
14
import org.eclipse.jdt.core.ICompilationUnit;
15
import org.eclipse.jdt.core.IJavaElement;
16
import org.eclipse.jdt.core.IPackageFragment;
17
import org.eclipse.jdt.ui.ISharedImages;
18
import org.eclipse.jdt.ui.JavaUI;
19
import org.eclipse.swt.graphics.Image;
20
21
/**
22
 * Proxy node for a package. This proxy contains all JUnit test found in the corresponding java package.
23
 * @author jcanches
24
 * @since 4.3
25
 */
26
public class RegularJUnitPackageProxyNode extends JavaParentElementProxyNode {
27
    
28
    public static RegularJUnitPackageProxyNode create(IPackageFragment fragment, Object parent) {
29
        RegularJUnitPackageProxyNode proxy = new RegularJUnitPackageProxyNode(fragment, parent);
30
        if(proxy.getChildren().length > 0) {
31
            return proxy;
32
        }
33
        return null;
34
    }
35
    
36
    private RegularJUnitPackageProxyNode(IPackageFragment fragment, Object parent) {
37
    	super(fragment, parent);
38
    }
39
    
40
    public IPackageFragment getPackageFragment() {
41
    	return (IPackageFragment)getJavaElement();
42
    }
43
    
44
    /* (non-Javadoc)
45
     * @see org.eclipse.hyades.test.ui.navigator.IProxyNode#getText()
46
     */
47
    public String getText() {
48
    	String name = getJavaElement().getElementName();
49
    	if ("".equals(name)) { //$NON-NLS-1$
50
    		name = "(default package)";
51
    	}
52
        return name;
53
    }
54
55
    /* (non-Javadoc)
56
     * @see org.eclipse.hyades.test.ui.navigator.IProxyNode#getImage()
57
     */
58
    public Image getImage() {
59
        return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKAGE);
60
    }
61
62
	protected JavaElementProxyNode createChildProxy(IJavaElement childElement) {
63
		if (childElement instanceof ICompilationUnit) {
64
			return RegularJUnitTestSuiteProxyNode.create((ICompilationUnit)childElement, this);
65
		}
66
		return null;
67
	}
68
69
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitTestSuiteProxyNode.java (+138 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import java.util.ArrayList;
16
import java.util.Iterator;
17
import java.util.List;
18
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
21
import org.eclipse.hyades.models.common.facades.behavioral.ITestCase;
22
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
23
import org.eclipse.hyades.models.common.testprofile.TPFTestSuite;
24
import org.eclipse.hyades.test.tools.core.common.TestCommon;
25
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
26
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
27
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
28
import org.eclipse.hyades.test.tools.ui.java.internal.junit.wizard.SourcePage;
29
import org.eclipse.hyades.test.ui.TestUIImages;
30
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
31
import org.eclipse.hyades.test.ui.navigator.ITestSuiteProxyNode;
32
import org.eclipse.jdt.core.ICompilationUnit;
33
import org.eclipse.jdt.core.IJavaElementDelta;
34
import org.eclipse.jdt.core.IType;
35
import org.eclipse.jdt.core.JavaModelException;
36
import org.eclipse.swt.graphics.Image;
37
import org.eclipse.ui.IMemento;
38
39
public class RegularJUnitTestSuiteProxyNode extends JavaElementProxyNode implements ITestSuiteProxyNode/*, IPersistableProxyNode*/ {
40
41
	private IProxyNode[] children;
42
43
	public static RegularJUnitTestSuiteProxyNode create(ICompilationUnit cu, Object parent) {
44
		if (isJUnitScript(cu)) {
45
			return new RegularJUnitTestSuiteProxyNode(cu, parent);
46
		}
47
		return null;
48
	}
49
	
50
	/** Checks whether the given java element is a JUnit test script
51
	 * @param element 
52
	 * @return
53
	 */
54
	private static boolean isJUnitScript(ICompilationUnit element) {
55
		ICompilationUnit cu = (ICompilationUnit) element;
56
		IType mainType = cu.findPrimaryType();
57
		try {
58
			if (mainType != null) {
59
				if (SourcePage.isTestImplementor(mainType)) {
60
					// Has a TestSuite already been defined for this type?
61
					return JUnitModelUpdater.findTestSuite(mainType, new ResourceSetImpl()) == null;
62
				}
63
			}
64
		} catch (JavaModelException e) {
65
			ToolsUiPlugin.logError(e);
66
		}
67
		return false;
68
	}
69
70
	private RegularJUnitTestSuiteProxyNode(ICompilationUnit cu, Object parent) {
71
		super(cu, parent);
72
		computeChildren();
73
	}
74
	
75
	private void computeChildren() {
76
		try {
77
			ITestSuite testSuite = JUnitTestSuiteFacade.createTestSuite(getCompilationUnit(), TestCommon.JUNIT_TEST_SUITE_TYPE);
78
			List _children = new ArrayList(testSuite.getITestCases().size());
79
			Iterator it = testSuite.getITestCases().iterator();
80
			while (it.hasNext()) {
81
				ITestCase tc = (ITestCase) it.next();
82
				IProxyNode proxy = new RegularJUnitTestCaseProxyNode(tc, this);
83
				_children.add(proxy);
84
			}
85
			this.children = (IProxyNode[])_children.toArray(new IProxyNode[_children.size()]);
86
		} catch (CoreException e) {
87
			this.children = new IProxyNode[0];
88
		}
89
	}
90
	
91
	public ICompilationUnit getCompilationUnit() {
92
		return (ICompilationUnit)getJavaElement();
93
	}
94
	
95
	public TPFTestSuite getTestSuite() {
96
		try {
97
			return (TPFTestSuite) JUnitTestSuiteFacade.createTestSuite(getCompilationUnit(), TestCommon.JUNIT_TEST_SUITE_TYPE);
98
		} catch (CoreException e) {
99
			ToolsUiPlugin.logError(e);
100
			return null;
101
		}
102
	}
103
104
	public String getType() {
105
		return TestCommon.OTHER_JUNIT_TEST_SUITE_TYPE;
106
	}
107
108
	public IProxyNode[] getChildren() {
109
		return children;
110
	}
111
112
	public Image getImage() {
113
		// TODO Auto-generated method stub
114
		return TestUIImages.INSTANCE.getImage(TestUIImages.IMG_TEST_SUITE);
115
	}
116
117
	public String getFactoryID() {
118
		// TODO Auto-generated method stub
119
		return null;
120
	}
121
122
	public boolean saveState(IMemento memento) {
123
		// TODO Auto-generated method stub
124
		return false;
125
	}
126
127
	public IProxyNode elementChanged(IJavaElementDelta delta) {
128
		if (delta.getKind() == IJavaElementDelta.CHANGED) {
129
			int flags = delta.getFlags();
130
			if ((flags & IJavaElementDelta.F_PRIMARY_RESOURCE) != 0) {
131
				computeChildren();
132
				return this;
133
			}
134
		}
135
		return null;
136
	}
137
138
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/JavaParentElementProxyNode.java (+165 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import java.util.ArrayList;
16
import java.util.Collections;
17
import java.util.List;
18
19
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
20
import org.eclipse.jdt.core.IJavaElement;
21
import org.eclipse.jdt.core.IJavaElementDelta;
22
import org.eclipse.jdt.core.IParent;
23
import org.eclipse.jdt.core.JavaModelException;
24
25
/**
26
 * Proxy node representing a java element that contains java elements.
27
 * @author jcanches
28
 * @since 4.3
29
 */
30
public abstract class JavaParentElementProxyNode extends JavaElementProxyNode {
31
32
	protected JavaElementProxyNode[] children;
33
	
34
	protected JavaParentElementProxyNode(IJavaElement element, Object parent) {
35
		super(element, parent);
36
		setChildren(computeChildren());
37
	}
38
39
	protected abstract JavaElementProxyNode createChildProxy(IJavaElement childElement);
40
    
41
	public IProxyNode[] getChildren() {
42
		return children;
43
	}
44
	
45
	protected final synchronized void setChildren(List _children) {
46
		children = (JavaElementProxyNode[])_children.toArray(new JavaElementProxyNode[_children.size()]);
47
	}
48
49
	protected List computeChildren() {
50
		if (getJavaElement() instanceof IParent) {
51
			try {
52
				IJavaElement[] elements = ((IParent)getJavaElement()).getChildren();
53
				List _children = new ArrayList(elements.length); 
54
				for (int i = 0; i < elements.length; i++) {
55
					JavaElementProxyNode proxy = createChildProxy(elements[i]);
56
					if (proxy != null) {
57
						_children.add(proxy);
58
					}
59
				}
60
				return _children;
61
			} catch (JavaModelException e) {
62
				return Collections.EMPTY_LIST;
63
			}
64
		}
65
		return Collections.EMPTY_LIST;
66
	}
67
68
	public IProxyNode elementChanged(IJavaElementDelta delta) {
69
		IProxyNode lowestChanged = null;
70
		IJavaElementDelta[] deltas = delta.getAffectedChildren();
71
		for (int i = 0; i < deltas.length; i++) {
72
			IJavaElementDelta childDelta = deltas[i];
73
			IJavaElement childElement = childDelta.getElement();
74
			IProxyNode lowestChild = null;
75
			switch(childDelta.getKind()) {
76
				case IJavaElementDelta.ADDED: {
77
					JavaElementProxyNode proxy = createChildProxy(childElement);
78
					if (proxy != null) {
79
						addChildProxy(proxy);
80
						lowestChild = this;
81
					}
82
					break;
83
				} case IJavaElementDelta.REMOVED: {
84
					JavaElementProxyNode proxy = getChildProxy(childElement);
85
					if (proxy != null) {
86
						removeChildProxy(proxy);
87
						lowestChild = this;
88
					}
89
					break;
90
				} case IJavaElementDelta.CHANGED: {
91
					JavaElementProxyNode proxy = getChildProxy(childElement);
92
					if (proxy != null) {
93
						lowestChild = proxy.elementChanged(childDelta);
94
						if (proxy.getChildren().length == 0) {
95
							removeChildProxy(proxy);
96
							lowestChild = this;
97
						}
98
					} else {
99
						proxy = createChildProxy(childElement);
100
						if (proxy != null) {
101
							addChildProxy(proxy);
102
							lowestChild = this;
103
						}
104
					}
105
					break;
106
				}
107
			}
108
			if (lowestChild != null) {
109
				if (lowestChanged == null) {
110
					lowestChanged = lowestChild;
111
				} else {
112
					lowestChanged = this;
113
				}
114
			}
115
		}
116
		return lowestChanged;
117
	}
118
119
    protected final JavaElementProxyNode getChildProxy(IJavaElement element) {
120
        for (int i = 0; i < children.length; i++) {
121
            if (element.equals(children[i].getJavaElement())) {
122
                return children[i];
123
            }
124
        }
125
        return null;
126
    }
127
128
    protected final void replaceChildProxy(JavaElementProxyNode oldProxy, JavaElementProxyNode newProxy) {
129
        for (int i = 0; i < children.length; i++) {
130
            if(children[i] == oldProxy) {
131
                children[i] = newProxy;
132
                break;
133
            }
134
        }
135
    }
136
 
137
	protected final boolean addChildProxy(JavaElementProxyNode proxy) {
138
        if(proxy == null) return false;
139
        JavaElementProxyNode[] newChildren = new JavaElementProxyNode[children.length + 1];
140
        for (int i = 0; i < children.length; i++) {
141
            //- if the proxy to add is already a child do not add it
142
            if(children[i].equals(proxy)) {
143
                return false;
144
            }
145
            newChildren[i] = children[i];
146
        }
147
        newChildren[children.length] = proxy;
148
        //- change the children of the current proxy node
149
        children = newChildren;
150
        return true;
151
	}
152
153
    protected final void removeChildProxy(JavaElementProxyNode proxy) {
154
    	JavaElementProxyNode[] newChildren = new JavaElementProxyNode[children.length - 1];
155
        int j = 0;
156
        for (int i = 0; i < children.length; i++) {
157
            if(children[i] != proxy) {
158
                newChildren[j++] = children[i];
159
            }
160
        }
161
        //- change the children of the current proxy node
162
        children = newChildren;
163
    }
164
165
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitTestCaseProxyNode.java (+87 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import org.eclipse.core.resources.IResource;
16
import org.eclipse.hyades.models.common.facades.behavioral.ITestCase;
17
import org.eclipse.hyades.models.common.testprofile.TPFTestCase;
18
import org.eclipse.hyades.test.tools.core.common.TestCommon;
19
import org.eclipse.hyades.test.ui.TestUIExtension;
20
import org.eclipse.hyades.test.ui.TestUIImages;
21
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
22
import org.eclipse.hyades.test.ui.navigator.ITestCaseProxyNode;
23
import org.eclipse.hyades.ui.HyadesUIPlugin;
24
import org.eclipse.hyades.ui.extension.IAssociationDescriptor;
25
import org.eclipse.hyades.ui.extension.IAssociationMapping;
26
import org.eclipse.hyades.ui.internal.extension.AssociationMappingRegistry;
27
import org.eclipse.swt.graphics.Image;
28
29
public class RegularJUnitTestCaseProxyNode implements ITestCaseProxyNode {
30
31
	private RegularJUnitTestSuiteProxyNode parent;
32
	private String name;
33
	private String id;
34
	
35
	public RegularJUnitTestCaseProxyNode(ITestCase testCase, RegularJUnitTestSuiteProxyNode parent) {
36
		this.parent = parent;
37
		this.name = testCase.getName();
38
		this.id = testCase.getId();
39
	}
40
	
41
	public TPFTestCase getTestCase() {
42
		// TODO Auto-generated method stub
43
		return null;
44
	}
45
46
	public String getType() {
47
		return TestCommon.OTHER_JUNIT_TEST_CASE_TYPE;
48
	}
49
50
	public IProxyNode[] getChildren() {
51
		return new IProxyNode[0];
52
	}
53
54
	public Image getImage() {
55
		AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestCaseMappingRegistry();
56
		IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
57
		IAssociationDescriptor descriptor = associationMapping.getDefaultAssociationDescriptor(getType());	
58
		if(descriptor != null) {
59
			return descriptor.getImage();
60
		} else {
61
			//- the type of this element wasn't registered by any extension (hyades.ui.typeDescriptions)
62
			return TestUIImages.INSTANCE.getImage(TestUIImages.IMG_TEST_CASE);
63
		}
64
	}
65
66
	public Object getParent() {
67
		return parent;
68
	}
69
70
	public String getText() {
71
		return name;
72
	}
73
74
	public Object getAdapter(Class adapter) {
75
		// TODO Auto-generated method stub
76
		return null;
77
	}
78
79
	public String getIdentifier() {
80
		return id;
81
	}
82
83
	public IResource getUnderlyingResource() {
84
		return parent.getUnderlyingResource();
85
	}
86
87
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitProviderProxyNode.java (+161 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import org.eclipse.hyades.test.ui.TestUIExtension;
16
import org.eclipse.hyades.test.ui.UiPlugin;
17
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigator;
18
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
19
import org.eclipse.hyades.test.ui.navigator.ITypeProviderProxyNode;
20
import org.eclipse.hyades.ui.HyadesUIPlugin;
21
import org.eclipse.hyades.ui.extension.IAssociationDescriptor;
22
import org.eclipse.hyades.ui.extension.IAssociationMapping;
23
import org.eclipse.hyades.ui.internal.extension.AssociationMappingRegistry;
24
import org.eclipse.jdt.core.IJavaElement;
25
import org.eclipse.jdt.core.IJavaElementDelta;
26
import org.eclipse.jdt.core.IJavaProject;
27
import org.eclipse.jdt.core.IPackageFragmentRoot;
28
import org.eclipse.swt.graphics.Image;
29
30
/**
31
 * Top-level proxy node under a project, for representing "other junit tests".
32
 * This proxy is associated to a Java project, and shows source folders as childs, or
33
 * directly packages if the java project is the source folder itself. 
34
 * @author jcanches
35
 * @since 4.3
36
 */
37
public class RegularJUnitProviderProxyNode extends JavaParentElementProxyNode implements ITypeProviderProxyNode {
38
39
	private String name;
40
	private String imageKey;
41
	
42
	public static RegularJUnitProviderProxyNode create(IJavaProject project, String type, Object parent) {
43
		RegularJUnitProviderProxyNode proxy = new RegularJUnitProviderProxyNode(project, type, parent);
44
        if(proxy.getChildren().length > 0) {
45
            return proxy;
46
        }
47
        return null;
48
	}
49
	
50
	protected RegularJUnitProviderProxyNode(IJavaProject project, String type, Object parent) {
51
		super(project, parent);
52
        AssociationMappingRegistry registry = (AssociationMappingRegistry)TestUIExtension.getTestSuiteMappingRegistry();
53
        IAssociationMapping associationMapping = registry.getAssociationMapping(HyadesUIPlugin.EP_TYPE_DESCRIPTIONS);
54
        IAssociationDescriptor descriptor = associationMapping.getDefaultAssociationDescriptor(type);
55
        if (descriptor != null) {
56
            this.name = descriptor.getName();
57
        } else {
58
            this.name = "[Invalid Descriptor]"; //$NON-NLS-1$
59
        }
60
        this.imageKey = TestNavigator.getTypeProviderManager().getImageKeyFor(type);
61
	}
62
	
63
	protected JavaElementProxyNode createChildProxy(IJavaElement childElement) {
64
		if (childElement instanceof IPackageFragmentRoot) {
65
			return RegularJUnitSourceFolderProxyNode.create((IPackageFragmentRoot)childElement, this);
66
		}
67
		return null;
68
	}
69
	
70
	private boolean isSourceFolder() {
71
		if (children.length == 1) {
72
			return ((RegularJUnitSourceFolderProxyNode)children[0]).isProjectRoot();
73
		}
74
		return false;
75
	}
76
	
77
	public IJavaProject getJavaProject() {
78
		return (IJavaProject)getJavaElement();
79
	}
80
	
81
	public Image getImage() {
82
		return UiPlugin.getInstance().getImageRegistry().get(this.imageKey);
83
	}
84
85
	public String getText() {
86
        return name;
87
	}
88
89
	public IProxyNode elementChanged(IJavaElementDelta delta) {
90
		if (delta.getKind() == IJavaElementDelta.CHANGED) {
91
			IJavaElementDelta[] deltas = delta.getAffectedChildren();
92
			IProxyNode lowestChanged = null;
93
			for (int i = 0; i < deltas.length; i++) {
94
				IProxyNode lowestChild = null;
95
				IJavaElementDelta childDelta = deltas[i];
96
				IJavaElement elem = childDelta.getElement();
97
				if (elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
98
					if (childDelta.getKind() == IJavaElementDelta.ADDED
99
						|| (childDelta.getKind() == IJavaElementDelta.CHANGED
100
							&& (childDelta.getFlags() & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0)) {
101
						lowestChild = packageFragmentRootAdded((IPackageFragmentRoot)elem);
102
					} else if (childDelta.getKind() == IJavaElementDelta.REMOVED
103
							   || (childDelta.getKind() == IJavaElementDelta.CHANGED
104
								   && (childDelta.getFlags() & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0)) {
105
						lowestChild = packageFragmentRootRemoved((IPackageFragmentRoot)elem);
106
					} else if (childDelta.getKind() == IJavaElementDelta.CHANGED
107
							   && (childDelta.getFlags() & IJavaElementDelta.F_CHILDREN) > 0) {
108
						lowestChild = packageFragmentRootChanged((IPackageFragmentRoot)elem, childDelta);
109
					}
110
				}
111
				if (lowestChild != null) {
112
	                if(lowestChanged == null) {
113
	                    lowestChanged = lowestChild;
114
	                } else {
115
	                    lowestChanged = this;
116
	                }
117
				}
118
			}
119
			return lowestChanged;
120
		}
121
		return null;
122
	}
123
124
	private IProxyNode packageFragmentRootAdded(IPackageFragmentRoot root) {
125
		RegularJUnitSourceFolderProxyNode proxy = RegularJUnitSourceFolderProxyNode.create(root, this);
126
		if (proxy != null) {
127
			addChildProxy(proxy);
128
			return this;
129
		}
130
		return null;
131
	}
132
133
	private IProxyNode packageFragmentRootRemoved(IPackageFragmentRoot root) {
134
		JavaElementProxyNode proxy = getChildProxy(root);
135
		if (proxy != null) {
136
			removeChildProxy(proxy);
137
			return this;
138
		}
139
		return null;
140
	}
141
	
142
	private IProxyNode packageFragmentRootChanged(IPackageFragmentRoot root, IJavaElementDelta delta) {
143
		JavaElementProxyNode proxy = getChildProxy(root);
144
		if (proxy != null) {
145
			IProxyNode lowestChanged  = proxy.elementChanged(delta);
146
			if (proxy.getChildren().length == 0) {
147
				removeChildProxy(proxy);
148
				return this;
149
			}
150
			return lowestChanged;
151
		} else {
152
			proxy = RegularJUnitSourceFolderProxyNode.create(root, this);
153
			if (proxy != null) {
154
				addChildProxy(proxy);
155
				return this;
156
			}
157
		}
158
		return null;
159
	}
160
161
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitProvider.java (+104 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import java.util.HashMap;
16
import java.util.Map;
17
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.hyades.test.ui.navigator.IFileProxyManager;
20
import org.eclipse.hyades.test.ui.navigator.IProxyNodeListener;
21
import org.eclipse.hyades.test.ui.navigator.ITypeProvider;
22
import org.eclipse.hyades.test.ui.navigator.ITypeProviderContext;
23
import org.eclipse.hyades.test.ui.navigator.ITypeProviderProxyNode;
24
import org.eclipse.hyades.ui.util.IDisposable;
25
import org.eclipse.jdt.core.ElementChangedEvent;
26
import org.eclipse.jdt.core.IElementChangedListener;
27
import org.eclipse.jdt.core.IJavaElementDelta;
28
import org.eclipse.jdt.core.IJavaProject;
29
import org.eclipse.jdt.core.JavaCore;
30
31
/**
32
 * A provider showing Java projects content, with non-JUnit classes filtered out.
33
 * @author jcanches
34
 * @since 4.3
35
 */
36
public class RegularJUnitProvider implements ITypeProvider, IElementChangedListener, IDisposable {
37
38
    /** The file proxy manager used to cache file proxies */
39
    protected IFileProxyManager fileProxyManager;
40
	protected IProxyNodeListener refresher;
41
42
	private Map projectToProxyMap = new HashMap();
43
	
44
	public RegularJUnitProvider() {
45
        JavaCore.addElementChangedListener(this, ElementChangedEvent.POST_CHANGE);
46
	}
47
	
48
    /* (non-Javadoc)
49
     * @see org.eclipse.hyades.test.ui.navigator.ITypeProvider#get(org.eclipse.core.resources.IProject, java.lang.String)
50
     */
51
    public ITypeProviderProxyNode get(IProject project, String type) {
52
    	IJavaProject jproject = JavaCore.create(project);
53
        if(!jproject.exists()) return null;
54
        RegularJUnitProviderProxyNode proxy = (RegularJUnitProviderProxyNode) projectToProxyMap.get(jproject);
55
        if(proxy == null) {
56
        	proxy = RegularJUnitProviderProxyNode.create(jproject, type, this);
57
        	if (proxy != null) {
58
        		projectToProxyMap.put(jproject, proxy);
59
        	}
60
        }
61
        return proxy;
62
    }
63
    
64
    /* (non-Javadoc)
65
     * @see org.eclipse.hyades.test.ui.navigator.ITypeProvider#init(org.eclipse.hyades.test.ui.navigator.ITypeProviderContext)
66
     */
67
    public void init(ITypeProviderContext context) {
68
        fileProxyManager = context.getFileProxyManager();
69
		refresher = context.getProxyNodeListener();
70
    }
71
72
    public void dispose() {
73
        JavaCore.removeElementChangedListener(this);
74
    }
75
76
	public void elementChanged(ElementChangedEvent event) {
77
		IJavaElementDelta[] children = event.getDelta().getAffectedChildren();
78
		for(int i = 0; i < children.length; i++) {
79
			IJavaProject jproject = (IJavaProject) children[i].getElement();
80
			RegularJUnitProviderProxyNode node = (RegularJUnitProviderProxyNode) projectToProxyMap.get(jproject);
81
			if (node != null) {
82
				Object lowestChange = node.elementChanged(children[i]);
83
				if(node.getChildren().length == 0) {
84
                    lowestChange = jproject.getProject();
85
                    projectToProxyMap.remove(jproject);
86
                }
87
				if (lowestChange != null) {
88
					refresher.nodeChanged(lowestChange);
89
				}
90
			}
91
		}
92
	}
93
	
94
	public void notifyChanged(RegularJUnitProviderProxyNode proxy) {
95
		if (proxy.getChildren().length == 0) {
96
			IJavaProject project = proxy.getJavaProject();
97
			projectToProxyMap.remove(project);
98
			refresher.nodeChanged(project);
99
		} else {
100
			refresher.nodeChanged(proxy);
101
		}
102
	}
103
104
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/JavaElementProxyNode.java (+82 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import org.eclipse.core.resources.IResource;
16
import org.eclipse.hyades.test.tools.ui.ToolsUiPlugin;
17
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
18
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.JavaModelException;
20
21
/**
22
 * Proxy node representing a java element.
23
 * @author jcanches
24
 * @since 4.3
25
 */
26
public abstract class JavaElementProxyNode implements IJavaElementChangedListenerProxyNode {
27
28
	protected static final JavaElementProxyNode[] NO_CHILDREN = new JavaElementProxyNode[0];
29
30
	private Object parent;
31
	private IJavaElement element;
32
	
33
	protected JavaElementProxyNode(IJavaElement element, Object parent) {
34
		this.parent = parent;
35
		this.element = element;
36
	}
37
	
38
	public IProxyNode[] getChildren() {
39
		return NO_CHILDREN;
40
	}
41
42
	public Object getParent() {
43
		return parent;
44
	}
45
46
	public String getText() {
47
		return element.getElementName();
48
	}
49
50
	public Object getAdapter(Class adapter) {
51
		if (adapter.isAssignableFrom(element.getClass())) {
52
			return element;
53
		}
54
		try {
55
			IResource res = element.getCorrespondingResource();
56
			if (adapter.isAssignableFrom(res.getClass())) {
57
				return res;
58
			}
59
		} catch (JavaModelException e) {
60
			return null;
61
		}
62
		return null;
63
	}
64
65
	public String getIdentifier() {
66
		return element.getHandleIdentifier();
67
	}
68
69
	public IResource getUnderlyingResource() {
70
		try {
71
			return element.getUnderlyingResource();
72
		} catch (JavaModelException e) {
73
			ToolsUiPlugin.logError(e);
74
			return null;
75
		}
76
	}
77
	
78
	public IJavaElement getJavaElement() {
79
		return element;
80
	}
81
	
82
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/IJavaElementChangedListenerProxyNode.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import org.eclipse.hyades.test.ui.navigator.IProxyNode;
16
import org.eclipse.jdt.core.IJavaElementDelta;
17
18
/**
19
 * A proxy node that is able to modify itself in reaction to changes in the java model.
20
 * @author jcanches
21
 * @since 4.3
22
 */
23
public interface IJavaElementChangedListenerProxyNode extends IProxyNode {
24
25
	/**
26
	 * Processes the delta, rooted at the java element that this proxy is associated to.
27
	 * @param delta
28
	 * @return The lowest proxy, in the subtree of the proxy, that needs to be refreshed
29
	 * by a viewer. This may be this proxy itself.
30
	 */
31
	IProxyNode elementChanged(IJavaElementDelta delta);
32
	
33
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/actions/ImportRegularJUnitTestSuiteAction.java (+42 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator.actions;
14
15
import org.eclipse.hyades.test.tools.ui.java.internal.junit.wizard.JUnitImportWizard;
16
import org.eclipse.jface.action.IAction;
17
import org.eclipse.jface.viewers.ISelection;
18
import org.eclipse.jface.viewers.IStructuredSelection;
19
import org.eclipse.jface.wizard.WizardDialog;
20
import org.eclipse.ui.IActionDelegate;
21
import org.eclipse.ui.IWorkbenchWindow;
22
import org.eclipse.ui.PlatformUI;
23
24
public class ImportRegularJUnitTestSuiteAction implements IActionDelegate {
25
26
	private IStructuredSelection selection;
27
	
28
	public void run(IAction action) {
29
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
30
		JUnitImportWizard wizard = new JUnitImportWizard();
31
		wizard.init(window.getWorkbench(), selection);
32
		WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
33
		dialog.open();
34
	}
35
36
	public void selectionChanged(IAction action, ISelection selection) {
37
		if (selection instanceof IStructuredSelection) {
38
            this.selection = (IStructuredSelection) selection;
39
        }
40
	}
41
42
}
(-)src/org/eclipse/hyades/test/tools/ui/java/internal/junit/navigator/RegularJUnitSourceFolderProxyNode.java (+67 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.ui.java.internal.junit.navigator;
14
15
import org.eclipse.jdt.core.IJavaElement;
16
import org.eclipse.jdt.core.IPackageFragment;
17
import org.eclipse.jdt.core.IPackageFragmentRoot;
18
import org.eclipse.jdt.ui.ISharedImages;
19
import org.eclipse.jdt.ui.JavaUI;
20
import org.eclipse.swt.graphics.Image;
21
22
/**
23
 * Proxy node for a source folder. This proxy represents a package fragment root,
24
 * and has package proxy as children.
25
 * @author jcanches
26
 * @since 4.3
27
 */
28
public class RegularJUnitSourceFolderProxyNode extends JavaParentElementProxyNode {
29
30
	private boolean isProjectRoot;
31
	
32
	public static RegularJUnitSourceFolderProxyNode create(IPackageFragmentRoot root, Object parent) {
33
		RegularJUnitSourceFolderProxyNode proxy = new RegularJUnitSourceFolderProxyNode(root, parent);
34
		if (proxy.getChildren().length == 0) {
35
			return null;
36
		}
37
		return proxy;
38
	}
39
	
40
	private RegularJUnitSourceFolderProxyNode(IPackageFragmentRoot root, Object parent) {
41
		super(root, parent);
42
		isProjectRoot = root.getPath().equals(root.getJavaProject().getPath());
43
	}
44
45
    public Image getImage() {
46
		return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
47
	}
48
49
	protected JavaElementProxyNode createChildProxy(IJavaElement childElement) {
50
		if (childElement instanceof IPackageFragment) {
51
			return RegularJUnitPackageProxyNode.create((IPackageFragment)childElement, this);
52
		}
53
		return null;
54
	}
55
	
56
	public boolean isProjectRoot() {
57
		return isProjectRoot;
58
	}
59
60
	public String getText() {
61
		if (isProjectRoot) {
62
			return "[project root]";
63
		}
64
		return super.getText();
65
	}
66
	
67
}
(-)src/org/eclipse/hyades/test/tools/core/common/TestCommon.java (+3 lines)
Lines 54-57 Link Here
54
54
55
	public final static String PLUGIN_JUNIT_TEST_SUITE_TYPE = "org.eclipse.hyades.test.plugin.junit.testSuite"; //$NON-NLS-1$
55
	public final static String PLUGIN_JUNIT_TEST_SUITE_TYPE = "org.eclipse.hyades.test.plugin.junit.testSuite"; //$NON-NLS-1$
56
56
57
	public final static String OTHER_JUNIT_TEST_SUITE_TYPE = "org.eclipse.hyades.test.other.junit.testSuite"; //$NON-NLS-1$
58
	public final static String OTHER_JUNIT_TEST_CASE_TYPE = "org.eclipse.hyades.test.other.junit.testCase"; //$NON-NLS-1$
59
57
}
60
}
(-)src/org/eclipse/hyades/test/tools/core/internal/java/codegen/GenTestSuite.java (-1 / +1 lines)
Lines 4-10 Link Here
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: GenTestSuite.java,v 1.11 2006/07/29 01:57:42 sschneid Exp $
7
 * $Id$
8
 * 
8
 * 
9
 * Contributors: 
9
 * Contributors: 
10
 * IBM Corporation - initial API and implementation
10
 * IBM Corporation - initial API and implementation
(-)src/org/eclipse/hyades/test/tools/core/internal/java/codegen/GenTestSuiteConstructor.java (-1 / +1 lines)
Lines 4-10 Link Here
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: GenTestSuiteConstructor.java,v 1.2 2006/07/29 01:57:42 sschneid Exp $
7
 * $Id$
8
 * 
8
 * 
9
 * Contributors: 
9
 * Contributors: 
10
 * IBM Corporation - initial API and implementation
10
 * IBM Corporation - initial API and implementation
(-)src/org/eclipse/hyades/test/tools/core/internal/java/codegen/GenSuiteMethod.java (-1 / +1 lines)
Lines 4-10 Link Here
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: GenSuiteMethod.java,v 1.10 2006/07/29 21:36:45 sschneid Exp $
7
 * $Id$
8
 * 
8
 * 
9
 * Contributors: 
9
 * Contributors: 
10
 * IBM Corporation - initial API and implementation
10
 * IBM Corporation - initial API and implementation
(-)src/org/eclipse/hyades/test/tools/core/internal/java/codegen/GenTestMethod.java (-1 / +1 lines)
Lines 4-10 Link Here
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: GenTestMethod.java,v 1.9 2006/07/29 21:36:45 sschneid Exp $
7
 * $Id$
8
 * 
8
 * 
9
 * Contributors: 
9
 * Contributors: 
10
 * IBM Corporation - initial API and implementation
10
 * IBM Corporation - initial API and implementation
(-)src/org/eclipse/hyades/test/tools/core/java/JUnitTestSuiteFacade.java (-72 / +55 lines)
Lines 24-54 Link Here
24
import org.eclipse.emf.ecore.resource.Resource;
24
import org.eclipse.emf.ecore.resource.Resource;
25
import org.eclipse.emf.ecore.resource.ResourceSet;
25
import org.eclipse.emf.ecore.resource.ResourceSet;
26
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
26
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
27
import org.eclipse.hyades.models.common.facades.behavioral.ITestCase;
28
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
27
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
29
import org.eclipse.hyades.models.common.facades.behavioral.impl.HyadesFactory;
30
import org.eclipse.hyades.models.common.util.ICommonConstants;
28
import org.eclipse.hyades.models.common.util.ICommonConstants;
31
import org.eclipse.hyades.test.core.util.EMFUtil;
29
import org.eclipse.hyades.test.core.util.EMFUtil;
32
import org.eclipse.hyades.test.tools.core.CorePlugin;
30
import org.eclipse.hyades.test.tools.core.CorePlugin;
33
import org.eclipse.hyades.test.tools.core.common.TestCommon;
31
import org.eclipse.hyades.test.tools.core.common.TestCommon;
34
import org.eclipse.hyades.test.tools.core.common.util.TestCommonUtil;
32
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitExtensionsRegistry;
35
import org.eclipse.hyades.test.tools.core.internal.common.codegen.Helper;
36
import org.eclipse.hyades.test.tools.core.internal.java.codegen.JUnitGenerator;
37
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
33
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater;
38
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitProjectBuilder;
34
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitProjectBuilder;
35
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitTypeFactory;
39
import org.eclipse.jdt.core.ICompilationUnit;
36
import org.eclipse.jdt.core.ICompilationUnit;
40
import org.eclipse.jdt.core.IJavaElement;
37
import org.eclipse.jdt.core.IJavaElement;
41
import org.eclipse.jdt.core.IMethod;
42
import org.eclipse.jdt.core.IPackageFragmentRoot;
38
import org.eclipse.jdt.core.IPackageFragmentRoot;
43
import org.eclipse.jdt.core.IType;
39
import org.eclipse.jdt.core.IType;
44
import org.eclipse.jdt.core.JavaCore;
40
import org.eclipse.jdt.core.JavaCore;
45
import org.eclipse.jdt.core.JavaModelException;
41
import org.eclipse.jdt.core.JavaModelException;
42
import org.eclipse.osgi.util.NLS;
46
43
47
/**
44
/**
48
 * <p>Provides high-level methods for navigating between JUnit source files (as JDT model
45
 * <p>Provides high-level methods for navigating between JUnit source files (as JDT model
49
 * elements) and TPTP concepts (as TPTP Test Model elements).</p>
46
 * elements) and TPTP concepts (as TPTP Test Model elements).</p>
50
 * <p><b>This API is provisional and is subject to changes in future releases.</b></p>
47
 * <p><b>This API is provisional and is subject to changes in future releases.</b></p>
51
 * @author jcanches
48
 * @author jcanches
49
 * @since 4.1
52
 */
50
 */
53
public class JUnitTestSuiteFacade {
51
public class JUnitTestSuiteFacade {
54
52
Lines 82-157 Link Here
82
    public static ITestSuite createTestSuite(ICompilationUnit junitTestCompilationUnit, String type, boolean force) throws CoreException {
80
    public static ITestSuite createTestSuite(ICompilationUnit junitTestCompilationUnit, String type, boolean force) throws CoreException {
83
   		String name = junitTestCompilationUnit.getElementName();
81
   		String name = junitTestCompilationUnit.getElementName();
84
		name = name.substring(0, name.indexOf('.'));
82
		name = name.substring(0, name.indexOf('.'));
85
		try {
83
		IFile javaFile = (IFile)junitTestCompilationUnit.getCorrespondingResource();
86
			IFile javaFile = (IFile)junitTestCompilationUnit.getCorrespondingResource();
84
		IFile testSuiteFile = javaFile
87
			IFile testSuiteFile = javaFile
85
				.getParent()
88
					.getParent()
86
				.getFile(new Path(name + "." + ICommonConstants.TEST_SUITE_FILE_EXTENSION)); //$NON-NLS-1$
89
					.getFile(new Path(name + "." + ICommonConstants.TEST_SUITE_FILE_EXTENSION)); //$NON-NLS-1$
87
90
88
		ITestSuite testSuite = createTestSuite(junitTestCompilationUnit, type);
91
			if (testSuiteFile.exists()) {
89
		testSuite.setName(name);
92
				if (force) {
90
		saveTestSuite(testSuite, testSuiteFile, force);
93
					testSuiteFile.delete(/*force*/true, /*keepHistory*/true, null);
91
		return testSuite;
94
				} else {
92
    }
95
					throw new CoreException(new Status(
93
    
96
							IStatus.ERROR,
94
    public static ITestSuite createTestSuite(ICompilationUnit cu, String type) throws CoreException {
97
							CorePlugin.PLUGIN_ID,
95
    	IJUnitTestSuiteFactory factory = JUnitExtensionsRegistry.getInstance().getFactory(type);
98
							1,
96
    	if (factory == null) {
99
							"Resource already exists: " + testSuiteFile.getFullPath(), //$NON-NLS-1$
97
    		throw new CoreException(makeErrorStatus(NLS.bind("No JUnit test suite factory registered for the type {0}", type), null)); //$NON-NLS-1$
100
							null));
98
    	}
101
				}
99
		ITestSuite testSuite = factory.generate(cu);
100
		// Store in the Test Suite the reference to the JUnit code:
101
		testSuite.getImplementor().setLocation(getPackageFragmentRoot(cu).getCorrespondingResource().getFullPath().toString());
102
		testSuite.getImplementor().setResource(cu.findPrimaryType().getFullyQualifiedName());
103
		return testSuite;
104
    }
105
    
106
    public static void saveTestSuite(ITestSuite testSuite, IFile file, boolean force) throws CoreException {
107
		if (file.exists()) {
108
			if (force) {
109
				file.delete(/*force*/true, /*keepHistory*/true, null);
110
			} else {
111
				throw new CoreException(makeErrorStatus(NLS.bind("Resource {0} already exists.", file.getFullPath()), null)); //$NON-NLS-1$
102
			}
112
			}
103
			ResourceSet resourceSet = new ResourceSetImpl();
113
		}
104
			URI uri = URI.createPlatformResourceURI(testSuiteFile.getFullPath().toString());
114
		ResourceSet resourceSet = new ResourceSetImpl();
105
			Resource resource = resourceSet.createResource(uri);
115
		URI uri = URI.createPlatformResourceURI(file.getFullPath().toString());
106
			ITestSuite testSuite = HyadesFactory.INSTANCE.createTestSuite(resource);
116
		Resource resource = resourceSet.createResource(uri);
107
			testSuite.setType(type);
117
		IJUnitTestSuiteFactory factory = new JUnitTypeFactory();
108
			testSuite.setName(name);
118
		resource.getContents().add(testSuite);
109
119
110
			HyadesFactory.INSTANCE.createImplementor(testSuite, /*isExternalImplementor*/true);
120
		try {
111
			// This TestSuite has a code-implemented behavior:
112
			JUnitModelUpdater.setImplementorBlock(testSuite.getImplementor(), null);
113
			// Store in the Test Suite the reference to the JUnit code:
114
			testSuite.getImplementor().setLocation(getPackageFragmentRoot(junitTestCompilationUnit).getCorrespondingResource().getFullPath().toString());
115
			testSuite.getImplementor().setResource(junitTestCompilationUnit.findPrimaryType().getFullyQualifiedName());
116
			
117
			populateTestCases(testSuite, junitTestCompilationUnit);
118
			
119
			EMFUtil.save(resource);
121
			EMFUtil.save(resource);
120
			JUnitProjectBuilder.installBuilder(testSuiteFile.getProject());
121
			JUnitProjectBuilder.installBuilder(javaFile.getProject());
122
			JUnitModelUpdater.associateTestSuiteToJUnitSourceFile(javaFile, testSuiteFile);
123
			return testSuite;
124
		} catch (Exception e) {
122
		} catch (Exception e) {
125
			CorePlugin.logError(e);
123
			throw new CoreException(makeErrorStatus("Problem in saving resource " + file.getFullPath(), e)); //$NON-NLS-1$
126
			return null;
127
		}
124
		}
128
    }
125
		JUnitProjectBuilder.installBuilder(file.getProject());
129
126
		IFile javaFile = getJUnitSourceFile(testSuite);
130
	/**
127
		JUnitProjectBuilder.installBuilder(javaFile.getProject());
131
	 * Populates an empty JUnit ITestSuite with one ITestCase for each test method found
128
		JUnitModelUpdater.associateTestSuiteToJUnitSourceFile(javaFile, file);
132
	 * in compilation unit cu.
133
	 * For the more generic case where the ITestSuite already owns ITestCase children which
134
	 * need to updated according to the new state of the source code, see 
135
	 * ModelUpdater.updateTestSuiteFromCode(ITestSuite).
136
	 * @param testSuite
137
	 * @param cu
138
	 * @throws JavaModelException
139
	 * @see org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater#updateTestSuiteFromCode(ITestSuite)
140
	 */
141
    private static void populateTestCases(ITestSuite testSuite, ICompilationUnit cu) throws JavaModelException {
142
		boolean isExternalBehavior = testSuite.getImplementor().isExternalImplementor();
143
    	IType mainType = cu.findPrimaryType();
144
    	if (mainType != null) {
145
    		IMethod[] methods = mainType.getMethods();
146
    		//List testCases = testSuite.getITestCases();
147
    		for (int i = 0; i < methods.length; i++) {
148
    			if (JUnitGenerator.isTestMethod(methods[i])) {
149
					String methodName = methods[i].getElementName();
150
					ITestCase tc = TestCommonUtil.createTestCase(testSuite, TestCommon.JUNIT_TEST_CASE_TYPE, /*externalBehavior*/true, methodName);
151
					Helper.setTestMethodName(tc, methodName);
152
    			}
153
    		}
154
    	}
155
    }
129
    }
156
130
157
	/**
131
	/**
Lines 209-212 Link Here
209
		return null;
183
		return null;
210
	}
184
	}
211
185
186
	private static IStatus makeErrorStatus(String message, Throwable exception) {
187
		return new Status(IStatus.ERROR,
188
				CorePlugin.PLUGIN_ID,
189
				1,
190
				message,
191
				exception);
192
	}
193
	
194
212
}
195
}
(-)src/org/eclipse/hyades/test/tools/core/internal/manual/codegen/GenTestSuite.java (-1 / +1 lines)
Lines 4-10 Link Here
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: GenTestSuite.java,v 1.11 2006/07/29 01:57:42 sschneid Exp $
7
 * $Id$
8
 * 
8
 * 
9
 * Contributors: 
9
 * Contributors: 
10
 * IBM - Initial API and implementation
10
 * IBM - Initial API and implementation
(-)src/org/eclipse/hyades/test/tools/core/internal/java/modelsync/JUnitModelUpdater.java (-126 / +12 lines)
Lines 12-20 Link Here
12
12
13
package org.eclipse.hyades.test.tools.core.internal.java.modelsync;
13
package org.eclipse.hyades.test.tools.core.internal.java.modelsync;
14
14
15
import java.util.ArrayList;
16
import java.util.Iterator;
15
import java.util.Iterator;
17
import java.util.List;
18
16
19
import org.eclipse.core.resources.IContainer;
17
import org.eclipse.core.resources.IContainer;
20
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.resources.IFile;
Lines 24-39 Link Here
24
import org.eclipse.core.resources.ResourcesPlugin;
22
import org.eclipse.core.resources.ResourcesPlugin;
25
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.CoreException;
26
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.IProgressMonitor;
27
import org.eclipse.core.runtime.IStatus;
28
import org.eclipse.core.runtime.Path;
25
import org.eclipse.core.runtime.Path;
29
import org.eclipse.core.runtime.Status;
30
import org.eclipse.emf.ecore.EObject;
26
import org.eclipse.emf.ecore.EObject;
31
import org.eclipse.emf.ecore.resource.ResourceSet;
27
import org.eclipse.emf.ecore.resource.ResourceSet;
32
import org.eclipse.hyades.models.common.configuration.CFGClass;
28
import org.eclipse.hyades.models.common.configuration.CFGClass;
33
import org.eclipse.hyades.models.common.configuration.CFGInstance;
29
import org.eclipse.hyades.models.common.configuration.CFGInstance;
34
import org.eclipse.hyades.models.common.facades.behavioral.IBlock;
30
import org.eclipse.hyades.models.common.facades.behavioral.IBlock;
35
import org.eclipse.hyades.models.common.facades.behavioral.IImplementor;
31
import org.eclipse.hyades.models.common.facades.behavioral.IImplementor;
36
import org.eclipse.hyades.models.common.facades.behavioral.ITestCase;
37
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
32
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
38
import org.eclipse.hyades.models.common.fragments.BVRInteraction;
33
import org.eclipse.hyades.models.common.fragments.BVRInteraction;
39
import org.eclipse.hyades.models.common.interactions.BVRLifeline;
34
import org.eclipse.hyades.models.common.interactions.BVRLifeline;
Lines 43-60 Link Here
43
import org.eclipse.hyades.models.common.util.ICommonConstants;
38
import org.eclipse.hyades.models.common.util.ICommonConstants;
44
import org.eclipse.hyades.test.core.util.EMFUtil;
39
import org.eclipse.hyades.test.core.util.EMFUtil;
45
import org.eclipse.hyades.test.tools.core.CorePlugin;
40
import org.eclipse.hyades.test.tools.core.CorePlugin;
46
import org.eclipse.hyades.test.tools.core.common.TestCommon;
41
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteFactory;
47
import org.eclipse.hyades.test.tools.core.common.util.TestCommonUtil;
42
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteUpdateFactory;
48
import org.eclipse.hyades.test.tools.core.internal.common.codegen.ASTHelper;
49
import org.eclipse.hyades.test.tools.core.internal.common.codegen.Helper;
50
import org.eclipse.hyades.test.tools.core.internal.java.codegen.JUnitGenerator;
51
import org.eclipse.hyades.test.tools.core.java.JUnitTestSuiteFacade;
52
import org.eclipse.jdt.core.ICompilationUnit;
43
import org.eclipse.jdt.core.ICompilationUnit;
53
import org.eclipse.jdt.core.IMethod;
54
import org.eclipse.jdt.core.IType;
44
import org.eclipse.jdt.core.IType;
55
import org.eclipse.jdt.core.JavaCore;
45
import org.eclipse.jdt.core.JavaCore;
56
import org.eclipse.jdt.core.JavaModelException;
57
import org.eclipse.jdt.core.dom.TagElement;
58
46
59
/**
47
/**
60
 * This class provides methods for creating/updating a JUnit TestSuite model
48
 * This class provides methods for creating/updating a JUnit TestSuite model
Lines 69-196 Link Here
69
	
57
	
70
	/**
58
	/**
71
	 * Update the Test Suite model in order to take the last changes from the code
59
	 * Update the Test Suite model in order to take the last changes from the code
72
	 * into account. Invoking this method updates the list of TestCases belonging to
60
	 * into account. This is done by invoking the registered update factory for the type
73
	 * the specified TestSuite.<br>
61
	 * of the test suite. If no update factory is registered, this method does nothing and return
74
	 * This method does the following:
62
	 * <code>false</code>.
75
	 * <ul>
76
	 *   <li>For each test method (ie "void testXxxx()") in the code, ensures that there is a TestCase
77
	 *   associated to it. If not, creates a TestCase with the same name as the test method.</li>
78
	 *   <li>For each TestCase, ensures that the method it is associated with still exists.
79
	 *   If not, deletes the TestCase.</li>
80
	 * </ul>
81
	 * @param testSuite
63
	 * @param testSuite
64
	 * @param javaFile 
82
	 * @return <code>true</code> if any update was applied to the specified test suite,
65
	 * @return <code>true</code> if any update was applied to the specified test suite,
83
	 * or <code>false</code> if the test suite was already up-to-date.
66
	 * or <code>false</code> if the test suite was already up-to-date.
84
	 * @throws CoreException
67
	 * @throws CoreException
85
	 */
68
	 */
86
	public static boolean updateTestSuiteFromCode(ITestSuite testSuite) throws CoreException {
69
	public static boolean updateTestSuiteFromCode(ITestSuite testSuite, IFile junitCode) throws CoreException {
87
		boolean changed = false;
70
		boolean changed = false;
88
		IFile junitCode = JUnitTestSuiteFacade.getJUnitSourceFile(testSuite);
71
		IJUnitTestSuiteFactory factory = JUnitExtensionsRegistry.getInstance().getFactory(testSuite.getType());
89
		if (junitCode != null) {
72
		if (factory instanceof IJUnitTestSuiteUpdateFactory) {
73
			IJUnitTestSuiteUpdateFactory ufactory = (IJUnitTestSuiteUpdateFactory) factory;
90
			ICompilationUnit cu = JavaCore.createCompilationUnitFrom(junitCode);
74
			ICompilationUnit cu = JavaCore.createCompilationUnitFrom(junitCode);
91
			if (!cu.isStructureKnown()) {
75
			return ufactory.update(testSuite, cu);
92
				throw new CoreException(makeErrorStatus("JUnit Compilation Unit structure is unknown", null)); //$NON-NLS-1$
93
			}
94
			IType primaryType = cu.findPrimaryType();
95
			if (cu == null) {
96
				throw new CoreException(makeErrorStatus("JUnit Compilation Unit has no primary type", null)); //$NON-NLS-1$
97
			}
98
			boolean isExternalBehavior = testSuite.getImplementor().isExternalImplementor();
99
			// Search for new test methods (=do not have a matching test case)
100
			IMethod[] methods = primaryType.getMethods();
101
			List usedTestCases = new ArrayList(methods.length);
102
			for (int i = 0; i < methods.length; i++) {
103
    			if (JUnitGenerator.isTestMethod(methods[i])) {
104
					ITestCase testCase = findTestCase(testSuite, methods[i]);
105
					if (testCase == null) {
106
						String methodName = methods[i].getElementName();
107
						testCase = TestCommonUtil.createTestCase(testSuite, TestCommon.JUNIT_TEST_CASE_TYPE, isExternalBehavior, methodName);
108
						testCase.getImplementor().setResource(methodName);
109
						changed = true;
110
					}
111
					usedTestCases.add(testCase);
112
					if (updateTestCaseDescription(methods[i], testCase)) {
113
						changed = true;
114
					}
115
    			}
116
			}
117
			// Search for Test Cases that do not have a matching test method
118
			List unusedTestCases = new ArrayList();
119
			Iterator it = testSuite.getITestCases().iterator();
120
			while (it.hasNext()) {
121
				ITestCase testCase = (ITestCase) it.next();
122
				if (!usedTestCases.contains(testCase)) {
123
					String methodName = Helper.getTestMethodName(testCase);
124
					if (methodName != null) {
125
						IMethod method = primaryType.getMethod(methodName, new String[0]);
126
						if (!method.exists()) {
127
							unusedTestCases.add(testCase);
128
						}
129
					}
130
				}
131
			}
132
			// Remove test cases that are not associated to a test method any more
133
			it = unusedTestCases.iterator();
134
			while (it.hasNext()) {
135
				EObject testCase = (EObject)it.next();
136
				EMFUtil.remove(testCase);
137
				changed = true;
138
			}
139
		}
140
		return changed;
141
	}
142
	
143
	private static boolean updateTestCaseDescription(IMethod method, ITestCase testCase) throws JavaModelException {
144
		TagElement sourceTag = ASTHelper.getJavadocDescription(method);
145
		if (sourceTag == null) {
146
			if (testCase.getDescription() != null && testCase.getDescription().length() > 0) {
147
				testCase.setDescription(null);
148
				return true;
149
			}
150
			return false;
151
		}
152
		
153
		String sourceDescription = ASTHelper.extractDescription(sourceTag);
154
		if (sourceDescription.startsWith(testCase.getName())) {
155
			sourceDescription = sourceDescription.substring(testCase.getName().length());
156
			if (sourceDescription.startsWith("\n")) sourceDescription = sourceDescription.substring("\n".length());  //$NON-NLS-1$//$NON-NLS-2$
157
		}
158
		
159
		String tcDescription = testCase.getDescription();
160
		if (tcDescription == null) tcDescription = ""; //$NON-NLS-1$
161
		if (!Helper.compareJavaComments(sourceDescription, tcDescription)) {
162
			testCase.setDescription(sourceDescription.trim());
163
			return true;
164
		}
76
		}
165
		return false;
77
		return false;
166
	}
78
	}
167
	
79
	
168
	private static ITestCase findTestCase(ITestSuite testSuite, IMethod testMethod) {
169
		Iterator it = testSuite.getITestCases().iterator();
170
		Helper helper = new Helper();
171
		try {
172
			String testMethodName = testMethod.getElementName();
173
			while (it.hasNext()) {
174
				ITestCase testCase = (ITestCase) it.next();
175
				String methodName = Helper.getTestMethodName(testCase);
176
				if (testMethodName.equals(methodName)) {
177
					return testCase;
178
				}
179
			}
180
		} finally {
181
			helper.dispose();
182
		}
183
		return null;
184
	}
185
	
186
	private static IStatus makeErrorStatus(String message, Throwable exception) {
187
		return new Status(IStatus.ERROR,
188
				CorePlugin.PLUGIN_ID,
189
				1,
190
				message,
191
				exception);
192
	}
193
194
	/**
80
	/**
195
	 * This method is temporarily public. Clients should not use it.
81
	 * This method is temporarily public. Clients should not use it.
196
	 * @param implementor
82
	 * @param implementor
Lines 270-276 Link Here
270
		}
156
		}
271
	}
157
	}
272
	
158
	
273
	private static IFile findTestSuiteFile(IFile javaFile) throws CoreException {
159
	public static IFile findTestSuiteFile(IFile javaFile) throws CoreException {
274
		// First method (typical): use the marker to find the test suite definition
160
		// First method (typical): use the marker to find the test suite definition
275
		IFile file = getRegisteredTestSuiteFile(javaFile);
161
		IFile file = getRegisteredTestSuiteFile(javaFile);
276
		if (file != null) {
162
		if (file != null) {
(-)src/org/eclipse/hyades/test/tools/core/internal/java/modelsync/JUnitProjectBuilder.java (-1 / +1 lines)
Lines 124-130 Link Here
124
		try {
124
		try {
125
			TPFTestSuite testSuite = (TPFTestSuite)JUnitTestSuiteFacade.findTestSuite(javaFile, new ResourceSetImpl());
125
			TPFTestSuite testSuite = (TPFTestSuite)JUnitTestSuiteFacade.findTestSuite(javaFile, new ResourceSetImpl());
126
			if (testSuite != null) {
126
			if (testSuite != null) {
127
				if (JUnitModelUpdater.updateTestSuiteFromCode(testSuite)) {
127
				if (JUnitModelUpdater.updateTestSuiteFromCode(testSuite, javaFile)) {
128
					SaveManager.saveResource(testSuite);
128
					SaveManager.saveResource(testSuite);
129
					IFile file = EMFUtil.getWorkspaceFile(testSuite);
129
					IFile file = EMFUtil.getWorkspaceFile(testSuite);
130
					file.refreshLocal(0, null);
130
					file.refreshLocal(0, null);
(-)plugin.xml (+12 lines)
Lines 3-8 Link Here
3
<plugin>
3
<plugin>
4
4
5
    	<extension-point id="nodeHandlers" name="org.eclipse.hyades.test.tools.core.nodeHandlers" schema="schema/nodeHandlers.exsd"/>
5
    	<extension-point id="nodeHandlers" name="org.eclipse.hyades.test.tools.core.nodeHandlers" schema="schema/nodeHandlers.exsd"/>
6
     <extension-point id="junitFactory" name="JUnit Test Model Factory" schema="schema/junitFactory.exsd"/>
6
	
7
	
7
   <extension
8
   <extension
8
         point="org.eclipse.hyades.test.core.launchconfigLaunchableType">
9
         point="org.eclipse.hyades.test.core.launchconfigLaunchableType">
Lines 285-290 Link Here
285
		<service class="org.eclipse.hyades.test.tools.core.services.AgentControllerVerificationService"/>
286
		<service class="org.eclipse.hyades.test.tools.core.services.AgentControllerVerificationService"/>
286
	</extension>
287
	</extension>
287
 <extension
288
 <extension
289
       point="org.eclipse.hyades.test.tools.core.junitFactory">
290
    <factory
291
          class="org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitTypeFactory"
292
          type="org.eclipse.hyades.test.java.junit.testSuite"
293
          updateSupport="true"/>
294
    <factory
295
          class="org.eclipse.hyades.test.tools.core.internal.plugin.modelsync.JUnitPluginTypeFactory"
296
          type="org.eclipse.hyades.test.plugin.junit.testSuite"
297
          updateSupport="true"/>
298
 </extension>
299
 <extension
288
       id="junit"
300
       id="junit"
289
       name="TPTP JUnit source metadata"
301
       name="TPTP JUnit source metadata"
290
       point="org.eclipse.core.resources.markers">
302
       point="org.eclipse.core.resources.markers">
(-)schema/junitFactory.exsd (+119 lines)
Added Link Here
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!-- Schema file written by PDE -->
3
<schema targetNamespace="org.eclipse.hyades.test.tools.core">
4
<annotation>
5
      <appInfo>
6
         <meta.schema plugin="org.eclipse.hyades.test.tools.core" id="junitFactory" name="JUnit Test Model Factory"/>
7
      </appInfo>
8
      <documentation>
9
         This extension point registers a JUnit test model factory. A JUnit test model factory generates a TPTP test model from a java source file. Such a factory has also the optional capability of updating a test model to reflect changes occurred in the java source file it is associated to.
10
      </documentation>
11
   </annotation>
12
13
   <element name="extension">
14
      <complexType>
15
         <sequence minOccurs="0" maxOccurs="unbounded">
16
            <element ref="factory"/>
17
         </sequence>
18
         <attribute name="point" type="string" use="required">
19
            <annotation>
20
               <documentation>
21
                  
22
               </documentation>
23
            </annotation>
24
         </attribute>
25
         <attribute name="id" type="string">
26
            <annotation>
27
               <documentation>
28
                  
29
               </documentation>
30
            </annotation>
31
         </attribute>
32
         <attribute name="name" type="string">
33
            <annotation>
34
               <documentation>
35
                  
36
               </documentation>
37
               <appInfo>
38
                  <meta.attribute translatable="true"/>
39
               </appInfo>
40
            </annotation>
41
         </attribute>
42
      </complexType>
43
   </element>
44
45
   <element name="factory">
46
      <complexType>
47
         <attribute name="class" type="string" use="required">
48
            <annotation>
49
               <documentation>
50
                  The class implementing the factory.
51
               </documentation>
52
               <appInfo>
53
                  <meta.attribute kind="java" basedOn="org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteFactory"/>
54
               </appInfo>
55
            </annotation>
56
         </attribute>
57
         <attribute name="updateSupport" type="boolean" use="required">
58
            <annotation>
59
               <documentation>
60
                  Whether the class supports test suite model update. If true, class must implement org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteUpdateFactory.
61
               </documentation>
62
            </annotation>
63
         </attribute>
64
         <attribute name="type" type="string" use="required">
65
            <annotation>
66
               <documentation>
67
                  The type of test suite that this factory generates.
68
               </documentation>
69
            </annotation>
70
         </attribute>
71
      </complexType>
72
   </element>
73
74
   <annotation>
75
      <appInfo>
76
         <meta.section type="since"/>
77
      </appInfo>
78
      <documentation>
79
         [Enter the first release in which this extension point appears.]
80
      </documentation>
81
   </annotation>
82
83
   <annotation>
84
      <appInfo>
85
         <meta.section type="examples"/>
86
      </appInfo>
87
      <documentation>
88
         [Enter extension point usage example here.]
89
      </documentation>
90
   </annotation>
91
92
   <annotation>
93
      <appInfo>
94
         <meta.section type="apiInfo"/>
95
      </appInfo>
96
      <documentation>
97
         [Enter API information here.]
98
      </documentation>
99
   </annotation>
100
101
   <annotation>
102
      <appInfo>
103
         <meta.section type="implementation"/>
104
      </appInfo>
105
      <documentation>
106
         [Enter information about supplied implementation of this extension point.]
107
      </documentation>
108
   </annotation>
109
110
   <annotation>
111
      <appInfo>
112
         <meta.section type="copyright"/>
113
      </appInfo>
114
      <documentation>
115
         
116
      </documentation>
117
   </annotation>
118
119
</schema>
(-)src/org/eclipse/hyades/test/tools/core/internal/java/modelsync/JUnitExtensionsRegistry.java (+139 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.core.internal.java.modelsync;
14
15
import java.util.ArrayList;
16
import java.util.Collection;
17
import java.util.HashMap;
18
import java.util.Iterator;
19
import java.util.Map;
20
21
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.core.runtime.IConfigurationElement;
23
import org.eclipse.core.runtime.IExtension;
24
import org.eclipse.core.runtime.IExtensionPoint;
25
import org.eclipse.core.runtime.Platform;
26
import org.eclipse.hyades.test.tools.core.CorePlugin;
27
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteFactory;
28
import org.eclipse.jface.resource.ImageDescriptor;
29
30
public class JUnitExtensionsRegistry {
31
32
	private static JUnitExtensionsRegistry instance;
33
	private final static String EXT_POINT_ID = CorePlugin.getID() + ".junitFactory"; //$NON-NLS-1$
34
	
35
	public static JUnitExtensionsRegistry getInstance() {
36
		if (instance == null) {
37
			instance = new JUnitExtensionsRegistry();
38
		}
39
		return instance;
40
	}
41
	
42
	private Map factoryDescriptors;
43
	
44
	private JUnitExtensionsRegistry() {
45
		registerFactories();
46
	}
47
	
48
	public class JUnitTestSuiteFactoryDescriptor {
49
		private IConfigurationElement element;
50
		private IJUnitTestSuiteFactory factory;
51
		public JUnitTestSuiteFactoryDescriptor(IConfigurationElement element) {
52
			this.element = element;
53
		}
54
		public String getType() {
55
			return element.getAttribute("type"); //$NON-NLS-1$
56
		}
57
		public boolean isUpdateCapable() {
58
			return Boolean.valueOf(element.getAttribute("updateSupport")).booleanValue(); //$NON-NLS-1$
59
		}
60
		public IJUnitTestSuiteFactory getFactory() {
61
			if (factory == null) {
62
				try {
63
					factory =(IJUnitTestSuiteFactory)element.createExecutableExtension("class"); //$NON-NLS-1$
64
				} catch (CoreException e) {
65
					CorePlugin.logError(e);
66
					invalidFactory(this);
67
				}
68
			}
69
			return factory;
70
		}
71
		public boolean equals(Object obj) {
72
			if (!(obj instanceof JUnitTestSuiteFactoryDescriptor)) return false;
73
			if (getType() == null) return false; // This is an error case
74
			JUnitTestSuiteFactoryDescriptor other = (JUnitTestSuiteFactoryDescriptor) obj;
75
			return getType().equals(other.getType());
76
		}
77
		public int hashCode() {
78
			String type = getType();
79
			if (type == null) return super.hashCode();
80
			else return type.hashCode();
81
		}
82
		
83
	}
84
	
85
	protected void invalidFactory(JUnitTestSuiteFactoryDescriptor descr) {
86
		factoryDescriptors.remove(descr.getType());
87
	}
88
	
89
	public IJUnitTestSuiteFactory getFactory(String type) {
90
		JUnitTestSuiteFactoryDescriptor descr = getDescriptor(type);
91
		if (descr != null) {
92
			return descr.getFactory();
93
		}
94
		return null;
95
	}
96
	
97
	public JUnitTestSuiteFactoryDescriptor getDescriptor(String type) {
98
		return (JUnitTestSuiteFactoryDescriptor) factoryDescriptors.get(type);
99
	}
100
	
101
	public JUnitTestSuiteFactoryDescriptor[] getAvailableFactories(boolean updateSupport) {
102
		Collection list;
103
		if (updateSupport) {
104
			list = getUpdateCapableFactories();
105
		} else {
106
			list = factoryDescriptors.values();
107
		}
108
		JUnitTestSuiteFactoryDescriptor[] ret = new JUnitTestSuiteFactoryDescriptor[list.size()];
109
		return (JUnitTestSuiteFactoryDescriptor[]) factoryDescriptors.values().toArray(ret);
110
	}
111
	
112
	private Collection getUpdateCapableFactories() {
113
		Collection values = factoryDescriptors.values();
114
		ArrayList ret = new ArrayList(values.size());
115
		Iterator it = values.iterator();
116
		while (it.hasNext()) {
117
			JUnitTestSuiteFactoryDescriptor descr = (JUnitTestSuiteFactoryDescriptor) it.next();
118
			if (descr.isUpdateCapable()) {
119
				ret.add(descr);
120
			}
121
		}
122
		return ret;
123
	}
124
125
	private void registerFactories() {
126
		factoryDescriptors = new HashMap();
127
		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(EXT_POINT_ID);
128
		IExtension[] extensions = point.getExtensions();
129
		for (int i = 0; i < extensions.length; i++) {
130
			IExtension extension = extensions[i];
131
			IConfigurationElement[] elements = extension.getConfigurationElements();
132
			for (int j = 0; j < elements.length; j++) {
133
				IConfigurationElement element = elements[j];
134
				JUnitTestSuiteFactoryDescriptor descr = new JUnitTestSuiteFactoryDescriptor(element);
135
				factoryDescriptors.put(descr.getType(), descr);
136
			}
137
		}
138
	}
139
}
(-)src/org/eclipse/hyades/test/tools/core/java/IJUnitTestSuiteUpdateFactory.java (+40 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.core.java;
14
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
17
import org.eclipse.jdt.core.ICompilationUnit;
18
19
/**
20
 * A factory capable of generating and updating a test suite model from a compilation
21
 * unit.
22
 * @author jcanches
23
 * @since 4.3
24
 */
25
public interface IJUnitTestSuiteUpdateFactory extends IJUnitTestSuiteFactory {
26
27
	/**
28
	 * Updates a test suite model to take the changes occurred in a compilation
29
	 * unit since the last time the test suite model was generated or updated.
30
	 * @param testSuite A test suite that has been generated or updated by this
31
	 * factory in a point in the past.
32
	 * @param cu A compilation that may have arbitrary changes since the last update
33
	 * or generation.
34
	 * @return Whether the testSuite was changed by the update. Returning <code>false</code>
35
	 * means that the changes in the compilation unit did not have any effect on the test
36
	 * suite model.
37
	 */
38
	boolean update(ITestSuite testSuite, ICompilationUnit cu) throws CoreException;
39
40
}
(-)src/org/eclipse/hyades/test/tools/core/internal/plugin/modelsync/JUnitPluginTypeFactory.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.core.internal.plugin.modelsync;
14
15
import org.eclipse.core.resources.IProject;
16
import org.eclipse.hyades.test.tools.core.common.TestCommon;
17
import org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitTypeFactory;
18
import org.eclipse.hyades.test.tools.core.internal.plugin.util.PDEProjectUtil;
19
import org.eclipse.jdt.core.ICompilationUnit;
20
import org.eclipse.jdt.core.IJavaProject;
21
import org.eclipse.jdt.core.JavaModelException;
22
23
/**
24
 * This test suite factory handles the generation and update of test suite models
25
 * of type "TPTP Plug-in JUnit". It is identical in all aspects to {@link JUnitTypeFactory},
26
 * except that generated test suites are of JUnit plug-in type.
27
 * @author jcanches
28
 * @since 4.3
29
 */
30
public class JUnitPluginTypeFactory extends JUnitTypeFactory {
31
32
	protected String getTestSuiteType() {
33
		return TestCommon.PLUGIN_JUNIT_TEST_SUITE_TYPE;
34
	}
35
36
	public boolean isSupported(ICompilationUnit cu) {
37
		try {
38
			IProject project = cu.getUnderlyingResource().getProject();
39
			if (!PDEProjectUtil.isStrictPluginProject(project)) {
40
				return false;
41
			}
42
		} catch (JavaModelException e) {
43
			return false;
44
		}
45
		return super.isSupported(cu);
46
	}
47
	
48
	public boolean isSupported(IJavaProject project) {
49
		IProject rproject = project.getProject();
50
		if (!PDEProjectUtil.isStrictPluginProject(rproject)) {
51
			return false;
52
		}
53
		return super.isSupported(project);
54
	}
55
	
56
}
(-)src/org/eclipse/hyades/test/tools/core/java/IJUnitTestSuiteFactory.java (+55 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.core.java;
14
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
17
import org.eclipse.jdt.core.ICompilationUnit;
18
import org.eclipse.jdt.core.IJavaProject;
19
20
/**
21
 * A factory capable of generating a test suite model from a JUnit source.
22
 * @author jcanches
23
 * @since 4.3
24
 */
25
public interface IJUnitTestSuiteFactory {
26
27
	/**
28
	 * Returns whether this factory supports test suite generation for a given compilation unit.
29
	 * @param cu A compilation unit
30
	 * @return Whether test suite generaton is supported for <code>cu</code>.
31
	 */
32
	boolean isSupported(ICompilationUnit cu);
33
	
34
	/**
35
	 * Generates a test suite for a given compilation unit.
36
	 * @param cu A compilation unit
37
	 * @return A test suite.
38
	 */
39
	ITestSuite generate(ICompilationUnit cu) throws CoreException;
40
41
	/**
42
	 * Determines whether the factory supports test suite generation for compilation
43
	 * units found in the given project. This method exists for improving performance, and
44
	 * as such should respond quicky. In no way it should dig into a complete project to check if
45
	 * there is a supported compilation unit. In case of doubt, it should return <code>true</code>.
46
	 * @param project A java project.
47
	 * @return <code>true</code> if this factory may support generation for compilation
48
	 * units in the project (even if it does not support generation for <strong>all</strong>
49
	 * the compilation units of the project), and <code>false</code> if it does not support
50
	 * generation for the project at all.
51
	 */
52
	public boolean isSupported(IJavaProject project);
53
	
54
55
}
(-)src/org/eclipse/hyades/test/tools/core/internal/java/modelsync/JUnitTypeFactory.java (+256 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * $Id: $
8
 * 
9
 * Contributors:
10
 *     IBM Corporation - initial API and implementation
11
 *******************************************************************************/
12
13
package org.eclipse.hyades.test.tools.core.internal.java.modelsync;
14
15
import java.util.ArrayList;
16
import java.util.Iterator;
17
import java.util.List;
18
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IStatus;
21
import org.eclipse.core.runtime.Status;
22
import org.eclipse.emf.ecore.EObject;
23
import org.eclipse.emf.ecore.resource.Resource;
24
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
25
import org.eclipse.hyades.models.common.facades.behavioral.ITestCase;
26
import org.eclipse.hyades.models.common.facades.behavioral.ITestSuite;
27
import org.eclipse.hyades.models.common.facades.behavioral.impl.HyadesFactory;
28
import org.eclipse.hyades.test.core.util.EMFUtil;
29
import org.eclipse.hyades.test.tools.core.CorePlugin;
30
import org.eclipse.hyades.test.tools.core.common.TestCommon;
31
import org.eclipse.hyades.test.tools.core.common.util.TestCommonUtil;
32
import org.eclipse.hyades.test.tools.core.internal.common.codegen.ASTHelper;
33
import org.eclipse.hyades.test.tools.core.internal.common.codegen.Helper;
34
import org.eclipse.hyades.test.tools.core.internal.java.codegen.JUnitGenerator;
35
import org.eclipse.hyades.test.tools.core.java.IJUnitTestSuiteUpdateFactory;
36
import org.eclipse.jdt.core.ICompilationUnit;
37
import org.eclipse.jdt.core.IJavaProject;
38
import org.eclipse.jdt.core.IMethod;
39
import org.eclipse.jdt.core.IType;
40
import org.eclipse.jdt.core.ITypeHierarchy;
41
import org.eclipse.jdt.core.JavaModelException;
42
import org.eclipse.jdt.core.dom.TagElement;
43
44
/**
45
 * This test suite factory handles the generation and update of test suite models
46
 * of type "TPTP JUnit".
47
 * @author jcanches
48
 * @since 4.3
49
 */
50
public class JUnitTypeFactory implements IJUnitTestSuiteUpdateFactory {
51
52
	/**
53
	 * Sub-classes should redefine this method to support an additional type.
54
	 * @return The test suite type that this factory can generate.
55
	 */
56
	protected String getTestSuiteType() {
57
		return TestCommon.JUNIT_TEST_SUITE_TYPE;
58
	}
59
60
    /**
61
     * This implementation accepts generation for the given compilation unit provided that
62
     * its main type implements a JUnit test interface.
63
     */
64
	public boolean isSupported(ICompilationUnit cu) {
65
		try {
66
			IType type = cu.findPrimaryType();
67
			if (type != null) {
68
				ITypeHierarchy typeHier= type.newSupertypeHierarchy(null);
69
				IType[] superInterfaces= typeHier.getAllInterfaces();
70
				for (int i= 0; i < superInterfaces.length; i++) {
71
					if (superInterfaces[i].getFullyQualifiedName().equals(Helper.JUNIT_TEST_CLASS_NAME))
72
						return true;
73
				}
74
			}
75
			return false;
76
		} catch (JavaModelException e) {
77
			return false;
78
		}
79
	}
80
81
	/**
82
	 * Generates a basic model, with no model behavior, and one test case per test method.
83
	 */
84
	public ITestSuite generate(ICompilationUnit cu) throws JavaModelException {
85
		Resource resource = new ResourceImpl();
86
		ITestSuite testSuite = HyadesFactory.INSTANCE.createTestSuite(resource);
87
		testSuite.setType(getTestSuiteType());
88
89
		HyadesFactory.INSTANCE.createImplementor(testSuite, /*isExternalImplementor*/true);
90
		// This TestSuite has a code-implemented behavior:
91
		JUnitModelUpdater.setImplementorBlock(testSuite.getImplementor(), null);
92
		
93
		populateTestCases(testSuite, cu);
94
		return testSuite;
95
	}
96
97
	/**
98
	 * Updates the Test Suite model in order to take the last changes from the code
99
	 * into account. Invoking this method updates the list of TestCases belonging to
100
	 * the specified TestSuite.<br>
101
	 * This method does the following:
102
	 * <ul>
103
	 *   <li>for each test method (ie "void testXxxx()") in the code, ensures that there is a TestCase
104
	 *   associated to it. If not, creates a TestCase with the same name as the test method.</li>
105
	 *   <li>for each TestCase, ensures that the method it is associated with still exists.
106
	 *   If not, deletes the TestCase.</li>
107
	 * </ul>
108
	 * @param testSuite
109
	 * @param cu
110
	 * @return <code>true</code> if any update was applied to the specified test suite,
111
	 * or <code>false</code> if the test suite was already up-to-date.
112
	 * @throws CoreException
113
	 */
114
	public boolean update(ITestSuite testSuite, ICompilationUnit cu) throws CoreException {
115
		boolean changed = false;
116
		if (!cu.isStructureKnown()) {
117
			throw new CoreException(makeErrorStatus("JUnit Compilation Unit structure is unknown", null)); //$NON-NLS-1$
118
		}
119
		IType primaryType = cu.findPrimaryType();
120
		if (cu == null) {
121
			throw new CoreException(makeErrorStatus("JUnit Compilation Unit has no primary type", null)); //$NON-NLS-1$
122
		}
123
		boolean isExternalBehavior = testSuite.getImplementor().isExternalImplementor();
124
		// Search for new test methods (=do not have a matching test case)
125
		IMethod[] methods = primaryType.getMethods();
126
		List usedTestCases = new ArrayList(methods.length);
127
		for (int i = 0; i < methods.length; i++) {
128
			if (JUnitGenerator.isTestMethod(methods[i])) {
129
				ITestCase testCase = findTestCase(testSuite, methods[i]);
130
				if (testCase == null) {
131
					String methodName = methods[i].getElementName();
132
					testCase = TestCommonUtil.createTestCase(testSuite, TestCommon.JUNIT_TEST_CASE_TYPE, isExternalBehavior, methodName);
133
					testCase.getImplementor().setResource(methodName);
134
					changed = true;
135
				}
136
				usedTestCases.add(testCase);
137
				if (updateTestCaseDescription(methods[i], testCase)) {
138
					changed = true;
139
				}
140
			}
141
		}
142
		// Search for Test Cases that do not have a matching test method
143
		List unusedTestCases = new ArrayList();
144
		Iterator it = testSuite.getITestCases().iterator();
145
		while (it.hasNext()) {
146
			ITestCase testCase = (ITestCase) it.next();
147
			if (!usedTestCases.contains(testCase)) {
148
				String methodName = Helper.getTestMethodName(testCase);
149
				if (methodName != null) {
150
					IMethod method = primaryType.getMethod(methodName, new String[0]);
151
					if (!method.exists()) {
152
						unusedTestCases.add(testCase);
153
					}
154
				}
155
			}
156
		}
157
		// Remove test cases that are not associated to a test method any more
158
		it = unusedTestCases.iterator();
159
		while (it.hasNext()) {
160
			EObject testCase = (EObject)it.next();
161
			EMFUtil.remove(testCase);
162
			changed = true;
163
		}
164
		return changed;
165
	}
166
	
167
	private static boolean updateTestCaseDescription(IMethod method, ITestCase testCase) throws JavaModelException {
168
		TagElement sourceTag = ASTHelper.getJavadocDescription(method);
169
		if (sourceTag == null) {
170
			if (testCase.getDescription() != null && testCase.getDescription().length() > 0) {
171
				testCase.setDescription(null);
172
				return true;
173
			}
174
			return false;
175
		}
176
		
177
		String sourceDescription = ASTHelper.extractDescription(sourceTag);
178
		if (sourceDescription.startsWith(testCase.getName())) {
179
			sourceDescription = sourceDescription.substring(testCase.getName().length());
180
			if (sourceDescription.startsWith("\n")) sourceDescription = sourceDescription.substring("\n".length());  //$NON-NLS-1$//$NON-NLS-2$
181
		}
182
		
183
		String tcDescription = testCase.getDescription();
184
		if (tcDescription == null) tcDescription = ""; //$NON-NLS-1$
185
		if (!Helper.compareJavaComments(sourceDescription, tcDescription)) {
186
			testCase.setDescription(sourceDescription.trim());
187
			return true;
188
		}
189
		return false;
190
	}
191
192
	/**
193
	 * Populates an empty JUnit ITestSuite with one ITestCase for each test method found
194
	 * in compilation unit cu.
195
	 * For the more generic case where the ITestSuite already owns ITestCase children which
196
	 * need to updated according to the new state of the source code, see 
197
	 * ModelUpdater.updateTestSuiteFromCode(ITestSuite).
198
	 * @param testSuite
199
	 * @param cu
200
	 * @throws JavaModelException
201
	 * @see org.eclipse.hyades.test.tools.core.internal.java.modelsync.JUnitModelUpdater#updateTestSuiteFromCode(ITestSuite)
202
	 */
203
    private static void populateTestCases(ITestSuite testSuite, ICompilationUnit cu) throws JavaModelException {
204
		boolean isExternalBehavior = testSuite.getImplementor().isExternalImplementor();
205
    	IType mainType = cu.findPrimaryType();
206
    	if (mainType != null) {
207
    		IMethod[] methods = mainType.getMethods();
208
    		//List testCases = testSuite.getITestCases();
209
    		for (int i = 0; i < methods.length; i++) {
210
    			if (JUnitGenerator.isTestMethod(methods[i])) {
211
					String methodName = methods[i].getElementName();
212
					ITestCase tc = TestCommonUtil.createTestCase(testSuite, TestCommon.JUNIT_TEST_CASE_TYPE, /*externalBehavior*/true, methodName);
213
					Helper.setTestMethodName(tc, methodName);
214
    			}
215
    		}
216
    	}
217
    }
218
219
    /**
220
     * Returns the test case matching a given test method, or <code>null</code> if there
221
     * is none.
222
     * @param testSuite
223
     * @param testMethod
224
     * @return
225
     */
226
	private static ITestCase findTestCase(ITestSuite testSuite, IMethod testMethod) {
227
		Iterator it = testSuite.getITestCases().iterator();
228
		Helper helper = new Helper();
229
		try {
230
			String testMethodName = testMethod.getElementName();
231
			while (it.hasNext()) {
232
				ITestCase testCase = (ITestCase) it.next();
233
				String methodName = Helper.getTestMethodName(testCase);
234
				if (testMethodName.equals(methodName)) {
235
					return testCase;
236
				}
237
			}
238
		} finally {
239
			helper.dispose();
240
		}
241
		return null;
242
	}
243
	
244
	private static IStatus makeErrorStatus(String message, Throwable exception) {
245
		return new Status(IStatus.ERROR,
246
				CorePlugin.PLUGIN_ID,
247
				1,
248
				message,
249
				exception);
250
	}
251
252
	public boolean isSupported(IJavaProject project) {
253
		return true;
254
	}
255
	
256
}

Return to bug 90628