1 8 package com.mountainminds.eclemma.internal.core.testutils; 9 10 import java.io.IOException ; 11 import java.io.InputStreamReader ; 12 import java.io.Reader ; 13 import java.net.URL ; 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 43 public abstract class JavaProjectTestBase extends TestCase { 44 45 public static final String 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 name) { 57 super(name); 58 } 59 60 protected void setUp() throws Exception { 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 []{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 getProjectName() { 75 return getClass().getName() + '.' + getName(); 76 } 77 78 protected IFolder setDefaultOutputLocation(String 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 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 name) throws CoreException{ 94 return fragmentRoot.createPackageFragment(name, false, null); 95 } 96 97 public ICompilationUnit createCompilationUnit(IPackageFragment fragment, String name, String content) throws JavaModelException{ 98 return fragment.createCompilationUnit(name, content, false, null); 99 } 100 101 public ICompilationUnit createCompilationUnit(IPackageFragmentRoot fragmentRoot, String testsrc, String path) throws CoreException, IOException { 102 IPath typepath = new Path(path); 103 String pkgname = typepath.removeLastSegments(1).toString().replace('/', '.'); 104 IPackageFragment fragment = createPackage(fragmentRoot, pkgname); 105 StringBuffer sb = new StringBuffer (); 106 URL url = Platform.find(Platform.getBundle("com.mountainminds.eclemma.core"), new Path(testsrc).append(typepath)); 107 Reader r = new InputStreamReader (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 { 123 project.delete(true, true, null); 124 } 125 126 } 127 | Popular Tags |