Vladimir Ivanov wrote:
> Do not you think these should be different Eclipse scenarios?:
> 1) simple scenario that run Eclipse, compile small application and runs it
> 2) simple debug scenario
> 3) scenario that runs ant-builder
> 4-N) other scenarios that emulate user work

Doing multiple scenarios indeed takes us closer to a "functional testing"
approach. This is a valid approach, but is indeed something different
from regular development.

I think that just one scenario will be sufficient for integration tests.

> Am I correct that you are going to use record and play tools?
> Or, these will be test(s) written using Eclipse API?

As far as I heard, using "record and play" tools proves unrobust,
as the test depends on the screen resolution, CPU load etc.
Eclipse API based approach seems to be more appropriate.

Below is the text of my experiment of modeling Java development in Eclipse.
The test below does the following
1. creates new project
2. inserts class Hi into the project
3. build the project
4. runs the project and checks the output stream of the process

The thing that I haven't managed to achieve is running from the command line.
-------------------------
import java.util.HashMap;

import junit.framework.TestCase;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMRunnerConfiguration;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.console.ConsolePlugin;

public class ClassCreateAndRunTest extends TestCase {

        protected void setUp() throws Exception {
        }

        public void testHi() throws CoreException {
                TestProject testProject = new TestProject();
                IPackageFragment pack = testProject.createPackage("hi");
                IType type = testProject.createType(pack, "Hi.java",
                                "public class Hi {"
                                                + "     public static void 
main(String[] args) {"
                                                + "             
System.out.println(\"Hi!\");" + "       }" + "}");
                testProject.getProject().build(
                                IncrementalProjectBuilder.INCREMENTAL_BUILD, 
null);
                IVMInstall vmInstall = JavaRuntime.getVMInstall(testProject
                                .getJavaProject());
                if (vmInstall == null)
                        vmInstall = JavaRuntime.getDefaultVMInstall();
                if (vmInstall != null) {
                        IVMRunner vmRunner = 
vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
                        if (vmRunner != null) {
                                String[] classPath = null;
                                try {
                                        classPath = JavaRuntime
                                                        
.computeDefaultRuntimeClassPath(testProject
                                                                        
.getJavaProject());
                                } catch (CoreException e) {
                                }
                                if (classPath != null) {
                                        VMRunnerConfiguration vmConfig = new 
VMRunnerConfiguration(
                                                        "hi.Hi", classPath);
                                        vmConfig.setWorkingDirectory("c:\\");
                                        ILaunch launch = new Launch(null, 
ILaunchManager.RUN_MODE,
                                                        null);
                                        vmRunner.run(vmConfig, launch, null);
                                        IProcess[] processes = 
launch.getProcesses();
                                        assertEquals(1, processes.length);
                                        int timeout = 3000;
                                        while (timeout > 0) {
                                                try {
                                                        assertEquals("Non-0 
status code", 0, processes[0]
                                                                        
.getExitValue());
                                                        break;
                                                } catch (DebugException e) {
                                                        timeout -= 100;
                                                        try {
                                                                
Thread.sleep(100);
                                                        } catch 
(InterruptedException ee) {
                                                        }
                                                }
                                        }
                                        assertTrue("timed out", timeout > 0);
                                        String out = 
processes[0].getStreamsProxy()
                                                        
.getOutputStreamMonitor().getContents();
                                        assertTrue(out.contains("Hi!"));
                                        try {
                                                Thread.sleep(500);
                                        } catch (InterruptedException e) {
                                        }
                                }
                        }
                }
        }

}

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.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
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.launching.JavaRuntime;

class TestProject {

        private IProject project;
        private IJavaProject javaProject;
        private IPackageFragmentRoot sourceFolder;

        public TestProject() throws CoreException {
                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                project = root.getProject("Eclipse scenario");
                project.create(null);
                project.open(null);

                javaProject = JavaCore.create(project);
                IFolder binFolder = createBinFolder();
                setJavaNature();
                javaProject.setRawClasspath(new IClasspathEntry[0], null);
                createOutputFolder(binFolder);
                addSystemLibraries();
        }

        private IFolder createBinFolder() throws CoreException {
                IFolder binFolder = project.getFolder("bin");
                binFolder.create(false, true, null);
                return binFolder;
        }

        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);
        }

        public IPackageFragment createPackage(String name) throws CoreException 
{
                if (sourceFolder == null)
                        sourceFolder = createSourceFolder();
                return sourceFolder.createPackageFragment(name, false, 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);
        }

        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];
        }

        public IProject getProject() {
                return project;
        }

        public IJavaProject getJavaProject() {
                return javaProject;
        }
}

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to