blob: 4daf73bc3e0a3b9b97859c1b2bdf83e45d15b9ed [file] [log] [blame]
/*******************************************************************************
* 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.IProject;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.core.JpaProject;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.ResourceModelListener;
import org.eclipse.jpt.core.context.JpaRootContextNode;
import org.eclipse.jpt.core.internal.GenericJpaProject;
import org.eclipse.jpt.core.internal.SimpleJpaProjectConfig;
import org.eclipse.jpt.core.internal.resource.java.JpaCompilationUnitImpl;
import org.eclipse.jpt.core.internal.utility.jdt.NullAnnotationEditFormatter;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.core.resource.java.JpaCompilationUnit;
import org.eclipse.jpt.core.tests.internal.utility.jdt.AnnotationTestCase;
import org.eclipse.jpt.utility.CommandExecutorProvider;
import org.eclipse.jpt.utility.internal.ClassTools;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.StringTools;
@SuppressWarnings("nls")
public class JavaResourceModelTestCase extends AnnotationTestCase
{
private JavaElementChangeListener javaElementChangeListener;
protected JpaCompilationUnit jpaCompilationUnit;
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) {
JavaResourceModelTestCase.this.javaElementChanged(event);
}
@Override
public String toString() {
return StringTools.buildToStringFor(this);
}
}
void javaElementChanged(ElementChangedEvent event) {
if (this.jpaCompilationUnit != null) {
this.jpaCompilationUnit.javaElementChanged(event);
}
}
protected ICompilationUnit createAnnotationAndMembers(String annotationName, String annotationBody) throws Exception {
return createAnnotationAndMembers("javax.persistence", annotationName, annotationBody);
}
protected ICompilationUnit createAnnotationAndMembers(String packageName, String annotationName, String annotationBody) throws Exception {
return this.javaProject.createCompilationUnit(packageName, annotationName + ".java", "public @interface " + annotationName + " { " + annotationBody + " }");
}
protected ICompilationUnit createEnumAndMembers(String enumName, String enumBody) throws Exception {
return createEnumAndMembers("javax.persistence", enumName, enumBody);
}
protected ICompilationUnit createEnumAndMembers(String packageName, String enumName, String enumBody) throws Exception {
return this.javaProject.createCompilationUnit(packageName, 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 JpaProject buildJpaProject() throws CoreException {
return new TestJpaProject(this.buildJpaProjectConfig(this.javaProject.getProject()));
}
protected class TestJpaProject extends GenericJpaProject {
protected TestJpaProject(JpaProject.Config config) throws CoreException {
super(config);
this.setUpdater(Updater.Null.instance());// ignore all updates, since there is no context model
this.addJar(org.eclipse.jpt.core.tests.internal.projects.TestJpaProject.jpaJarName());
}
protected void addJar(String jarPath) throws JavaModelException {
this.addClasspathEntry(JavaCore.newLibraryEntry(new Path(jarPath), null, null));
}
private void addClasspathEntry(IClasspathEntry entry) throws JavaModelException {
getJavaProject().setRawClasspath(CollectionTools.add(getJavaProject().getRawClasspath(), entry), null);
}
@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 JpaRootContextNode buildRootContextNode() {
return null; // no root context
}
}
protected JpaProject.Config buildJpaProjectConfig(IProject project) {
SimpleJpaProjectConfig config = new SimpleJpaProjectConfig();
config.setProject(project);
config.setJpaPlatform(JptCorePlugin.getJpaPlatform(project));
config.setConnectionProfileName(JptCorePlugin.getConnectionProfileName(project));
config.setDiscoverAnnotatedClasses(JptCorePlugin.discoverAnnotatedClasses(project));
return config;
}
protected JavaResourcePersistentType buildJavaTypeResource(ICompilationUnit cu) throws CoreException {
this.jpaCompilationUnit = this.buildJpaCompilationUnit(cu);
this.jpaCompilationUnit.resolveTypes();
return this.hackJavaResourcePersistentType();
}
protected JavaResourcePersistentType hackJavaResourcePersistentType() {
return (JavaResourcePersistentType) ClassTools.fieldValue(this.jpaCompilationUnit, "persistentType");
}
protected JpaCompilationUnit buildJpaCompilationUnit(ICompilationUnit cu) throws CoreException {
if (this.jpaCompilationUnit != null) {
throw new IllegalStateException();
}
JpaProject jpaProject = buildJpaProject();
return new JpaCompilationUnitImpl(
cu,
jpaProject.getJpaPlatform().getAnnotationProvider(),
CommandExecutorProvider.Default.instance(),
NullAnnotationEditFormatter.instance(),
this.buildResourceModelListener()
);
}
ResourceModelListener buildResourceModelListener() {
return new ResourceModelListener() {
public void resourceModelChanged() {
// ignore
}
};
}
}