Skip to main content
summaryrefslogtreecommitdiffstats
blob: ec8d1f2bb47d770cb6fdc330ca6c3979b0d5847f (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.resource.java;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jpt.core.internal.IContextModel;
import org.eclipse.jpt.core.internal.IJpaProject;
import org.eclipse.jpt.core.internal.JpaProject;
import org.eclipse.jpt.core.internal.JptCorePlugin;
import org.eclipse.jpt.core.internal.SimpleJpaProjectConfig;
import org.eclipse.jpt.core.internal.jdtutility.NullAnnotationEditFormatter;
import org.eclipse.jpt.core.internal.resource.java.JavaPersistentTypeResource;
import org.eclipse.jpt.core.internal.resource.java.JavaResourceModel;
import org.eclipse.jpt.core.tests.internal.jdtutility.AnnotationTestCase;
import org.eclipse.jpt.utility.internal.StringTools;

public class JavaResourceModelTestCase extends AnnotationTestCase
{
	private JavaElementChangeListener javaElementChangeListener;
	protected JavaResourceModel javaResourceModel;
	

	public JavaResourceModelTestCase(String name) {
		super(name);
	}
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.javaElementChangeListener = new JavaElementChangeListener();
		JavaCore.addElementChangedListener(this.javaElementChangeListener);
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		JavaCore.removeElementChangedListener(this.javaElementChangeListener);
		this.javaElementChangeListener = null;
	}
	
	/**
	 * Forward the Java element change event back to the JPA model manager.
	 */
	private class JavaElementChangeListener implements IElementChangedListener {
		JavaElementChangeListener() {
			super();
		}
		public void elementChanged(ElementChangedEvent event) {
			if (JavaResourceModelTestCase.this.javaResourceModel != null) {
				JavaResourceModelTestCase.this.javaResourceModel.javaElementChanged(event);
			}
		}
		@Override
		public String toString() {
			return StringTools.buildToStringFor(this);
		}
	}

	protected IType createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
		return this.javaProject.createType("javax.persistence", annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
	}
	
	protected IType createEnumAndMembers(String enumName, String enumBody) throws Exception {
		return this.javaProject.createType("javax.persistence", enumName + ".java", "public enum " + enumName + " { " + enumBody + " }");
	}

	//build up a dummy JpaProject that does not have JpaFiles in it and does not update from java changes
	protected IJpaProject buildJpaProject() throws CoreException {
		return new TestJpaProject(this.buildJpaProjectConfig(this.javaProject.getProject()));
	}

	protected class TestJpaProject extends JpaProject {
		protected TestJpaProject(IJpaProject.Config config) throws CoreException {
			super(config);
		}

		@Override
		protected IResourceProxyVisitor buildInitialResourceProxyVisitor() {
			return new IResourceProxyVisitor() {
				public boolean visit(IResourceProxy proxy) throws CoreException {
					return false;  // ignore all the files in the Eclipse project
				}
			};
		}

		@Override
		protected IContextModel buildContextModel() {
			return null;  // no context model
		}

		@Override
		public void update() {
			// ignore all updates, since there is no context model
		}

	}

	private IJpaProject.Config buildJpaProjectConfig(IProject project) {
		SimpleJpaProjectConfig config = new SimpleJpaProjectConfig();
		config.setProject(project);
		config.setJpaPlatform(JptCorePlugin.jpaPlatform(project));
		config.setConnectionProfileName(JptCorePlugin.connectionProfileName(project));
		config.setDiscoverAnnotatedClasses(JptCorePlugin.discoverAnnotatedClasses(project));
		return config;
	}

	protected JavaPersistentTypeResource buildJavaTypeResource(IType testType) 
		throws CoreException {
		this.javaResourceModel = buildJavaResourceModel(testType);
		this.javaResourceModel.resolveTypes();
		return this.javaResourceModel.resource().getPersistentType();
	}	
	
	protected JavaResourceModel buildJavaResourceModel(IType testType) throws CoreException {
		if (this.javaResourceModel != null) {
			throw new IllegalStateException();
		}
		IFile file = (IFile) testType.getResource();
		IJpaProject jpaProject = buildJpaProject();
		return new JavaResourceModel(
			file, 
			jpaProject.jpaPlatform().annotationProvider(),
			MODIFY_SHARED_DOCUMENT_COMMAND_EXECUTOR_PROVIDER,
			NullAnnotationEditFormatter.instance());
	}

}

Back to the top