- Revision
- 668
- Author
- felix
- Date
- 2007-01-11 12:02:33 -0600 (Thu, 11 Jan 2007)
Log Message
[fl] added simplistic PDE junit tests for the eclipse plugin
Modified Paths
Added Paths
- trunk/plugins/eclipse/org.jbehave/
- trunk/plugins/eclipse/org.jbehave/.classpath
- trunk/plugins/eclipse/org.jbehave/.project
- trunk/plugins/eclipse/org.jbehave/META-INF/
- trunk/plugins/eclipse/org.jbehave/META-INF/MANIFEST.MF
- trunk/plugins/eclipse/org.jbehave/bin/
- trunk/plugins/eclipse/org.jbehave/bin/org/
- trunk/plugins/eclipse/org.jbehave/bin/org/jbehave/
- trunk/plugins/eclipse/org.jbehave/bin/org/jbehave/Activator.class
- trunk/plugins/eclipse/org.jbehave/build.properties
- trunk/plugins/eclipse/org.jbehave/lib/
- trunk/plugins/eclipse/org.jbehave/lib/jbehave.jar
- trunk/plugins/eclipse/org.jbehave/src/
- trunk/plugins/eclipse/org.jbehave/src/org/
- trunk/plugins/eclipse/org.jbehave/src/org/jbehave/
- trunk/plugins/eclipse/org.jbehave/src/org/jbehave/Activator.java
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.classpath
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.project
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/META-INF/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/META-INF/MANIFEST.MF
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/build.properties
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/Activator.java
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/BehaviourRunnerTest.java
- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/TestProject.java
Diff
Added: trunk/plugins/eclipse/org.jbehave/.classpath (0 => 668)
--- trunk/plugins/eclipse/org.jbehave/.classpath (rev 0) +++ trunk/plugins/eclipse/org.jbehave/.classpath 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="output" path="bin"/> +</classpath>
Added: trunk/plugins/eclipse/org.jbehave/.project (0 => 668)
--- trunk/plugins/eclipse/org.jbehave/.project (rev 0) +++ trunk/plugins/eclipse/org.jbehave/.project 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.jbehave</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription>
Added: trunk/plugins/eclipse/org.jbehave/META-INF/MANIFEST.MF (0 => 668)
--- trunk/plugins/eclipse/org.jbehave/META-INF/MANIFEST.MF (rev 0) +++ trunk/plugins/eclipse/org.jbehave/META-INF/MANIFEST.MF 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,11 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Jbehave Plug-in +Bundle-SymbolicName: org.jbehave +Bundle-Version: 1.0.0 +Bundle-Activator: org.jbehave.Activator +Bundle-Localization: plugin +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime +Eclipse-LazyStart: true +Export-Package: org.jbehave
Added: trunk/plugins/eclipse/org.jbehave/bin/org/jbehave/Activator.class
(Binary files differ)
Added: trunk/plugins/eclipse/org.jbehave/build.properties (0 => 668)
--- trunk/plugins/eclipse/org.jbehave/build.properties (rev 0) +++ trunk/plugins/eclipse/org.jbehave/build.properties 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . +jars.extra.classpath = lib/jbehave.jar
Added: trunk/plugins/eclipse/org.jbehave/lib/jbehave.jar
(Binary files differ)
Added: trunk/plugins/eclipse/org.jbehave/src/org/jbehave/Activator.java (0 => 668)
--- trunk/plugins/eclipse/org.jbehave/src/org/jbehave/Activator.java (rev 0) +++ trunk/plugins/eclipse/org.jbehave/src/org/jbehave/Activator.java 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,50 @@ +package org.jbehave; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.jbehave"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +}
Modified: trunk/plugins/eclipse/org.jbehave.plugin.eclipse/META-INF/MANIFEST.MF (667 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse/META-INF/MANIFEST.MF 2007-01-10 15:06:40 UTC (rev 667) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse/META-INF/MANIFEST.MF 2007-01-11 18:02:33 UTC (rev 668) @@ -25,3 +25,4 @@ Eclipse-LazyStart: true Bundle-Vendor: JBehave Bundle-ClassPath: eclipse-plugin.jar +Export-Package: org.jbehave.plugin.eclipse
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.classpath (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.classpath (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.classpath 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="output" path="bin"/> +</classpath>
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.project (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.project (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/.project 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.jbehave.plugin.eclipse.test</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription>
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/META-INF/MANIFEST.MF (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/META-INF/MANIFEST.MF (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/META-INF/MANIFEST.MF 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,21 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Test Plug-in +Bundle-SymbolicName: org.jbehave.plugin.eclipse.test +Bundle-Version: 1.0.0 +Bundle-Activator: org.jbehave.plugin.eclipse.test.Activator +Bundle-Vendor: jbehave.org +Bundle-Localization: plugin +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.eclipse.core.resources, + org.eclipse.jdt.core, + org.eclipse.jdt.launching, + org.junit, + org.jbehave.plugin.eclipse, + org.eclipse.jdt.debug, + org.eclipse.debug.core, + org.eclipse.debug.ui, + org.eclipse.ui.console, + org.eclipse.jface.text +Eclipse-LazyStart: true
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/build.properties (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/build.properties (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/build.properties 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/Activator.java (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/Activator.java (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/Activator.java 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,50 @@ +package org.jbehave.plugin.eclipse.test; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.jbehave.plugin.eclipse.test"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +}
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/BehaviourRunnerTest.java (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/BehaviourRunnerTest.java (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/BehaviourRunnerTest.java 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,179 @@ +package org.jbehave.plugin.eclipse.test; + +import java.io.IOException; +import java.net.MalformedURLException; + +import junit.framework.TestCase; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRunnable; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationType; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.internal.ui.DebugUIPlugin; +import org.eclipse.jdt.core.IJavaModelMarker; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IType; +import org.eclipse.jface.text.IDocument; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.console.ConsolePlugin; +import org.eclipse.ui.console.IConsole; +import org.eclipse.ui.console.IConsoleListener; +import org.eclipse.ui.console.IConsoleManager; +import org.eclipse.ui.console.TextConsole; +import org.jbehave.plugin.eclipse.ConfigurationState; +import org.jbehave.plugin.eclipse.JBehaveLaunchConfiguration; + + + +public class BehaviourRunnerTest extends TestCase { + private TestProject testProject; + + protected void setUp() throws Exception { + super.setUp(); + testProject=new TestProject(); + + } + + public void testBuggyClassGetsProblemMarker() throws CoreException, OperationCanceledException, InterruptedException{ + IWorkspaceRunnable runnable=new IWorkspaceRunnable(){ + public void run(IProgressMonitor monitor) throws CoreException { + IPackageFragment pack = testProject.createPackage("paket1") ; + IType type= testProject.createType(pack, "AClass.java", "public class AClass {public voi m(){}}"); + } + }; + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.run(runnable,null); + Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + IMarker[] markers = workspace.getRoot().findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE); + assertEquals(1, markers.length); + } + + public void testCorrectClassHasNoProblemMarkers() throws CoreException, OperationCanceledException, InterruptedException{ + IWorkspaceRunnable runnable=new IWorkspaceRunnable(){ + public void run(IProgressMonitor monitor) throws CoreException { + IPackageFragment pack = testProject.createPackage("paket1") ; + IType type= testProject.createType(pack, "AClass.java", "public class AClass {public void m(){}}"); + } + }; + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.run(runnable,null); + Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + + IMarker[] markers = workspace.getRoot().findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE); + assertEquals(0, markers.length); + } + + + public void testUsingJBehaveJar() throws CoreException, OperationCanceledException, InterruptedException, MalformedURLException, IOException{ + testProject.addJar("org.jbehave", "lib/jbehave.jar"); + IWorkspaceRunnable runnable=new IWorkspaceRunnable(){ + public void run(IProgressMonitor monitor) throws CoreException { + + IPackageFragment pack = testProject.createPackage("paket1") ; + IType type= testProject.createType(pack, "AClass.java", "public class AClass {public void m(){}}"); + IType testClass = testProject.createType(pack, "AClassBehaviour.java", "public class AClassBehaviour extends org.jbehave.core.mock.UsingMatchers{public void shouldAddUp(){ensureThat(2,eq(1+1));}}"); + + } + }; + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.run(runnable,null); + Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + + IMarker[] markers = workspace.getRoot().findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE); + assertEquals(0, markers.length); + } + + public void testLaunchingTestCase() throws CoreException, OperationCanceledException, InterruptedException, MalformedURLException, IOException{ + testProject.addJar("org.jbehave", "lib/jbehave.jar"); + IWorkspaceRunnable runnable=new IWorkspaceRunnable(){ + public void run(IProgressMonitor monitor) throws CoreException { + IPackageFragment pack = testProject.createPackage("paket1") ; + IType type= testProject.createType(pack, "AClass.java", "public class AClass {public void m(){}}"); + IType testClass = testProject.createType(pack, "AClassBehaviour.java", "public class AClassBehaviour extends org.jbehave.core.mock.UsingMatchers{public void shouldAddUp(){ensureThat(2,eq(1+1));}}"); + } + }; + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.run(runnable,null); + Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + + IMarker[] markers = workspace.getRoot().findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE); + assertEquals(0, markers.length); + ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager(); + ILaunchConfigurationType configType = lm.getLaunchConfigurationType(JBehaveLaunchConfiguration.ID_JBEHAVE_APPLICATION); + + ConfigurationState state=new ConfigurationState(); + state.setProjectName(testProject.getProject().getName()); + state.setBehaviorClass("paket1.AClassBehaviour"); + state.setBehaviorMethod("shouldAddUp"); + ILaunchConfigurationWorkingCopy wc = configType.newInstance( + null, lm.generateUniqueLaunchConfigurationNameFrom(state.getName())); + + state.setAttributes(wc); + ILaunchConfiguration config = wc.doSave(); + + IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); + final TextConsole[] console=new TextConsole[1]; + manager.addConsoleListener(new IConsoleListener(){ + public void consolesAdded(IConsole[] consoles) { + console[0]=((TextConsole)consoles[0]); + console[0].activate(); + } + public void consolesRemoved(IConsole[] consoles) { + } + + }); + + DebugUIPlugin.launchInForeground(config, ILaunchManager.RUN_MODE); + waitForJobs(); + delay(1000); + + IDocument document = console[0].getDocument(); + String output=document.get(); + + assertTrue(output.startsWith(".\r\nTime:")); + + } + + private void delay(long waitTimeInMillis){ + Display display= Display.getCurrent(); + if (display!=null){ + long endTimeMillis=System.currentTimeMillis()+waitTimeInMillis; + while (System.currentTimeMillis()<endTimeMillis){ + if (!display.readAndDispatch()){ + display.sleep(); + } + } + display.update(); + } else { + try { + Thread.sleep(waitTimeInMillis); + } catch (InterruptedException e) {} + } + + } + + public void waitForJobs(){ + while(Platform.getJobManager().currentJob() != null) + delay(1000); + } + + protected void tearDown() throws Exception { + super.tearDown(); + testProject.dispose(); + } + + + + + +}
Added: trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/TestProject.java (0 => 668)
--- trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/TestProject.java (rev 0) +++ trunk/plugins/eclipse/org.jbehave.plugin.eclipse.test/src/org/jbehave/plugin/eclipse/test/TestProject.java 2007-01-11 18:02:33 UTC (rev 668) @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Erich Gamma ([EMAIL PROTECTED]) and + * Kent Beck ([EMAIL PROTECTED]) + *******************************************************************************/ +package org.jbehave.plugin.eclipse.test; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IPluginDescriptor; +import org.eclipse.core.runtime.IPluginRegistry; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.search.IJavaSearchConstants; +import org.eclipse.jdt.core.search.ITypeNameRequestor; +import org.eclipse.jdt.core.search.SearchEngine; +import org.eclipse.jdt.launching.JavaRuntime; + +public class TestProject { + + // this is a modified version of the TestProject class + // written by Erich Gamma and Kent Beck and described in + // their book "Contributing to Eclipse: Principles, Patterns, and Plug-Ins" + private IProject project; + private IJavaProject javaProject; + private IPackageFragmentRoot sourceFolder; + + public TestProject() throws CoreException { + IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); + project= root.getProject("TestProject"); + project.create(null); + project.open(null); + javaProject= JavaCore.create(project); + + IFolder binFolder= createBinFolder(); + + setJavaNature(); + javaProject.setRawClasspath(new IClasspathEntry[0], null); + + createOutputFolder(binFolder); + addSystemLibraries(); + } + + public IProject getProject() { + return project; + } + + public IJavaProject getJavaProject() { + return javaProject; + } + + public void addJar(String plugin, String jar) throws MalformedURLException, IOException, JavaModelException { + Path result= findFileInPlugin(plugin, jar); + IClasspathEntry[] oldEntries= javaProject.getRawClasspath(); + IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length + 1]; + System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length); + newEntries[oldEntries.length]= JavaCore.newLibraryEntry(result, null, null); + javaProject.setRawClasspath(newEntries, null); + } + + public IPackageFragment createPackage(String name) throws CoreException { + if (sourceFolder == null) + sourceFolder= createSourceFolder(); + return sourceFolder.createPackageFragment(name, false, null); + } + + public IType createType(IPackageFragment pack, String cuName, String source) throws JavaModelException { + StringBuffer buf= new StringBuffer(); + buf.append("package " + pack.getElementName() + ";\n"); + buf.append("\n"); + buf.append(source); + ICompilationUnit cu= pack.createCompilationUnit(cuName, buf.toString(), false, null); + return cu.getTypes()[0]; + } + + /** + * Wait for autobuild notification to occur, that is + * for the autbuild to finish. + */ + public void waitForAutoBuild() { + + boolean wasInterrupted = false; + do { + try { + Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + wasInterrupted = false; + } catch (OperationCanceledException e) { + throw(e); + } catch (InterruptedException e) { + wasInterrupted = true; + } + } while (wasInterrupted); + + } + + public void dispose() throws CoreException { + waitForIndexer(); + project.delete(true, true, null); + } + + private IFolder createBinFolder() throws CoreException { + IFolder binFolder= project.getFolder("bin"); + binFolder.create(false, true, null); + return binFolder; + } + + IFolder createXmlFolder() throws CoreException { + IFolder xmlFolder= project.getFolder("xml"); + xmlFolder.create(false, true, null); + return xmlFolder; + } + + + private void setJavaNature() throws CoreException { + IProjectDescription description= project.getDescription(); + description.setNatureIds(new String[] { JavaCore.NATURE_ID }); + project.setDescription(description, null); + } + + private void createOutputFolder(IFolder binFolder) throws JavaModelException { + IPath outputLocation= binFolder.getFullPath(); + javaProject.setOutputLocation(outputLocation, null); + } + + private IPackageFragmentRoot createSourceFolder() throws CoreException { + IFolder folder= project.getFolder("src"); + folder.create(false, true, null); + IPackageFragmentRoot root= javaProject.getPackageFragmentRoot(folder); + + IClasspathEntry[] oldEntries= javaProject.getRawClasspath(); + IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length + 1]; + System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length); + newEntries[oldEntries.length]= JavaCore.newSourceEntry(root.getPath()); + javaProject.setRawClasspath(newEntries, null); + return root; + } + + private void addSystemLibraries() throws JavaModelException { + IClasspathEntry[] oldEntries= javaProject.getRawClasspath(); + IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length + 1]; + System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length); + newEntries[oldEntries.length]= JavaRuntime.getDefaultJREContainerEntry(); + javaProject.setRawClasspath(newEntries, null); + } + + private Path findFileInPlugin(String plugin, String file) throws MalformedURLException, IOException { + IPluginRegistry registry= Platform.getPluginRegistry(); + IPluginDescriptor descriptor= registry.getPluginDescriptor(plugin); + URL pluginURL= descriptor.getInstallURL(); + URL jarURL= new URL(pluginURL, file); + URL localJarURL= Platform.asLocalURL(jarURL); + return new Path(localJarURL.getPath()); + } + + private void waitForIndexer() throws JavaModelException { + new SearchEngine() + .searchAllTypeNames( + ResourcesPlugin.getWorkspace(), + null, + null, + IJavaSearchConstants.EXACT_MATCH, + IJavaSearchConstants.CASE_SENSITIVE, + IJavaSearchConstants.CLASS, + SearchEngine.createJavaSearchScope(new IJavaElement[0]), + new ITypeNameRequestor() { + public void acceptClass(char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { + } + public void acceptInterface(char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { + } + }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null); + } +}
To unsubscribe from this list please visit:
