KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > testutils > JavaProjectTestBase


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: JavaProjectTestBase.java 13 2006-08-28 20:07:41Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.testutils;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.Reader JavaDoc;
13 import java.net.URL JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import org.eclipse.core.resources.IFolder;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IProjectDescription;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IPackageFragment;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.launching.JavaRuntime;
35
36 /**
37  * Base class for test cases working on Java projects providing infrastructure
38  * to setup Java projects.
39  *
40  * @author Marc R. Hoffmann
41  * @version $Revision: 13 $
42  */

43 public abstract class JavaProjectTestBase extends TestCase {
44
45   public static final String JavaDoc PROJECT_NAME = "UnitTestProject";
46   
47   protected IWorkspace workspace;
48   protected IProject project;
49   protected IJavaProject javaProject;
50   
51   
52   public JavaProjectTestBase() {
53     super();
54   }
55
56   public JavaProjectTestBase(String JavaDoc name) {
57     super(name);
58   }
59
60   protected void setUp() throws Exception JavaDoc {
61     workspace = ResourcesPlugin.getWorkspace();
62     IWorkspaceRoot root = workspace.getRoot();
63     project = root.getProject(PROJECT_NAME);
64     project.create(null);
65     project.open(null);
66     IProjectDescription description = project.getDescription();
67     description.setNatureIds(new String JavaDoc[]{JavaCore.NATURE_ID});
68     project.setDescription(description, null);
69     javaProject = JavaCore.create(project);
70     javaProject.setRawClasspath(new IClasspathEntry[0], null);
71     addClassPathEntry(JavaRuntime.getDefaultJREContainerEntry());
72   }
73   
74   protected String JavaDoc getProjectName() {
75     return getClass().getName() + '.' + getName();
76   }
77
78   protected IFolder setDefaultOutputLocation(String JavaDoc foldername) throws CoreException {
79     IFolder folder= project.getFolder(foldername);
80     folder.create(false, true, null);
81     javaProject.setOutputLocation(folder.getFullPath(), null);
82     return folder;
83   }
84   
85   protected IPackageFragmentRoot createSourceFolder(String JavaDoc foldername) throws CoreException {
86     IFolder folder = project.getFolder(foldername);
87     folder.create(false, true, null);
88     IPackageFragmentRoot packageRoot= javaProject.getPackageFragmentRoot(folder);
89     addClassPathEntry(JavaCore.newSourceEntry(packageRoot.getPath()));
90     return packageRoot;
91   }
92   
93   public IPackageFragment createPackage(IPackageFragmentRoot fragmentRoot, String JavaDoc name) throws CoreException{
94     return fragmentRoot.createPackageFragment(name, false, null);
95   }
96   
97   public ICompilationUnit createCompilationUnit(IPackageFragment fragment, String JavaDoc name, String JavaDoc content) throws JavaModelException{
98     return fragment.createCompilationUnit(name, content, false, null);
99   }
100   
101   public ICompilationUnit createCompilationUnit(IPackageFragmentRoot fragmentRoot, String JavaDoc testsrc, String JavaDoc path) throws CoreException, IOException JavaDoc {
102     IPath typepath = new Path(path);
103     String JavaDoc pkgname = typepath.removeLastSegments(1).toString().replace('/', '.');
104     IPackageFragment fragment = createPackage(fragmentRoot, pkgname);
105     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106     URL JavaDoc url = Platform.find(Platform.getBundle("com.mountainminds.eclemma.core"), new Path(testsrc).append(typepath));
107     Reader JavaDoc r = new InputStreamReader JavaDoc(url.openStream());
108     int c;
109     while ((c = r.read()) != -1) sb.append((char) c);
110     r.close();
111     return createCompilationUnit(fragment, typepath.lastSegment(), sb.toString());
112   }
113   
114   protected void addClassPathEntry(IClasspathEntry entry) throws CoreException{
115     IClasspathEntry[] oldEntries= javaProject.getRawClasspath();
116     IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length + 1];
117     System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
118     newEntries[oldEntries.length]= entry;
119     javaProject.setRawClasspath(newEntries, null);
120   }
121   
122   protected void tearDown() throws Exception JavaDoc {
123     project.delete(true, true, null);
124   }
125
126 }
127
Popular Tags