Skip to main content
summaryrefslogtreecommitdiffstats
blob: 81afd7dfd035c78d0e1fc7b8a003be9edd691eb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (c) 2005, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.tests.internal.projects;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
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.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.utility.internal.ArrayTools;

/**
 * This builds and holds a "Java" project.
 * Support for adding packages and types.
 * 
 * "Java" projects aren't required to be "faceted" projects, but for JPA
 * testing they are.
 */
@SuppressWarnings("nls")
public class TestJavaProject extends TestFacetedProject {
	private final IJavaProject javaProject;
	private final IPackageFragmentRoot sourceFolder;


	// ********** builders *****************************
	
	public static TestJavaProject buildJavaProject(String baseProjectName, boolean autoBuild)
			throws CoreException {
		return new TestJavaProject(baseProjectName, autoBuild);
	}
	
	
	// ********** constructors/initialization **********

	public TestJavaProject(String projectName) throws CoreException {
		this(projectName, false);
	}

	public TestJavaProject(String projectName, boolean autoBuild) throws CoreException {
		super(projectName, autoBuild);
		this.installFacet("jst.java", "5.0");
		this.javaProject = JavaCore.create(this.getProject());
		this.sourceFolder = this.javaProject.getPackageFragmentRoot(this.getProject().getFolder("src"));
	}

	public void addJar(String jarPath) throws JavaModelException {
		this.addClasspathEntry(JavaCore.newLibraryEntry(new Path(jarPath), null, null));
	}

	private void addClasspathEntry(IClasspathEntry entry) throws JavaModelException {
		this.javaProject.setRawClasspath(ArrayTools.add(this.javaProject.getRawClasspath(), entry), null);
	}
	

	// ********** public methods **********

	public IJavaProject getJavaProject() {
		return this.javaProject;
	}

	public IPackageFragment createPackage(String packageName) throws CoreException {
		return this.sourceFolder.createPackageFragment(packageName, false, null);	// false = "no force"
	}

	/**
	 * The source should NOT contain a package declaration;
	 * it will be added here.
	 */
	public ICompilationUnit createCompilationUnit(String packageName, String compilationUnitName, String source) throws CoreException {
		return this.createCompilationUnit(this.createPackage(packageName), compilationUnitName, new SimpleSourceWriter(source));
	}

	/**
	 * The source should NOT contain a package declaration;
	 * it will be added here.
	 */
	public ICompilationUnit createCompilationUnit(String packageName, String compilationUnitName, SourceWriter sourceWriter) throws CoreException {
		return this.createCompilationUnit(this.createPackage(packageName), compilationUnitName, sourceWriter);
	}

	/**
	 * The source should NOT contain a package declaration;
	 * it will be added here.
	 */
	public ICompilationUnit createCompilationUnit(IPackageFragment packageFragment, String compilationUnitName, SourceWriter sourceWriter) throws CoreException {
		StringBuilder sb = new StringBuilder(2000);
		sourceWriter.appendSourceTo(sb);
		String source = sb.toString();
		return packageFragment.createCompilationUnit(compilationUnitName, source, false, null);	// false = "no force"
	}


	// ********** member classes **********

	public interface SourceWriter {
		void appendSourceTo(StringBuilder sb);
	}

	public class SimpleSourceWriter implements SourceWriter {
		private final String source;
		public SimpleSourceWriter(String source) {
			super();
			this.source = source;
		}
		public void appendSourceTo(StringBuilder sb) {
			sb.append(source);
		}
	}

}

Back to the top