kmoore | da90c11 | 2011-02-06 02:33:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2005, 2010 Oracle. All rights reserved. |
| 3 | * This program and the accompanying materials are made available under the |
| 4 | * terms of the Eclipse Public License v1.0, which accompanies this distribution |
| 5 | * and is available at http://www.eclipse.org/legal/epl-v10.html. |
| 6 | * |
| 7 | * Contributors: |
| 8 | * Oracle - initial API and implementation |
| 9 | ******************************************************************************/ |
| 10 | package org.eclipse.jpt.jpa.core.tests.internal.projects; |
| 11 | |
| 12 | import org.eclipse.core.runtime.CoreException; |
| 13 | import org.eclipse.jpt.common.core.tests.internal.projects.TestJavaProject; |
| 14 | import org.eclipse.jpt.common.utility.Command; |
| 15 | import org.eclipse.jpt.common.utility.internal.synchronizers.CallbackSynchronousSynchronizer; |
| 16 | import org.eclipse.jpt.common.utility.internal.synchronizers.SynchronousSynchronizer; |
| 17 | import org.eclipse.jpt.common.utility.synchronizers.CallbackSynchronizer; |
| 18 | import org.eclipse.jpt.common.utility.synchronizers.Synchronizer; |
| 19 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 20 | import org.eclipse.jpt.jpa.core.JpaFacet; |
| 21 | import org.eclipse.jpt.jpa.core.JpaProject; |
| 22 | import org.eclipse.jpt.jpa.core.JptJpaCorePlugin; |
| 23 | import org.eclipse.wst.common.componentcore.datamodel.properties.IFacetDataModelProperties; |
| 24 | import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| 25 | |
| 26 | /** |
| 27 | * This builds and holds a "JPA" project. |
| 28 | * Support for adding packages and types. |
| 29 | * |
| 30 | * The JPA project's settings (platform, database connection, etc.) can be |
| 31 | * controlled by building a data model and passing it into the constructor. |
| 32 | */ |
| 33 | @SuppressWarnings("nls") |
| 34 | public class TestJpaProject extends TestJavaProject { |
| 35 | final JpaProject jpaProject; |
| 36 | |
| 37 | public static final String JPA_JAR_NAME_SYSTEM_PROPERTY = "org.eclipse.jpt.jpa.jar"; |
| 38 | public static final String ECLIPSELINK_JAR_NAME_SYSTEM_PROPERTY = "org.eclipse.jpt.eclipselink.jar"; |
| 39 | |
| 40 | |
| 41 | // ********** builders ********** |
| 42 | |
| 43 | public static TestJpaProject buildJpaProject(String baseProjectName, boolean autoBuild, IDataModel jpaConfig) |
| 44 | throws CoreException { |
| 45 | return new TestJpaProject(baseProjectName, autoBuild, jpaConfig); |
| 46 | } |
| 47 | |
| 48 | public static TestJpaProject buildJpaProject(String baseProjectName, boolean autoBuild) |
| 49 | throws CoreException { |
| 50 | return buildJpaProject(baseProjectName, autoBuild, null); |
| 51 | } |
| 52 | |
| 53 | // ********** constructors/initialization ********** |
| 54 | |
| 55 | public TestJpaProject(String projectName) throws CoreException { |
| 56 | this(projectName, false); |
| 57 | } |
| 58 | |
| 59 | public TestJpaProject(String projectName, boolean autoBuild) throws CoreException { |
| 60 | this(projectName, autoBuild, null); |
| 61 | } |
| 62 | |
| 63 | public TestJpaProject(String projectName, boolean autoBuild, IDataModel jpaConfig) throws CoreException { |
| 64 | super(projectName, autoBuild); |
| 65 | String jpaFacetVersion = JpaFacet.VERSION_1_0.getVersionString(); |
| 66 | if (jpaConfig != null) { |
| 67 | jpaFacetVersion = jpaConfig.getStringProperty(IFacetDataModelProperties.FACET_VERSION_STR); |
| 68 | } |
| 69 | this.installFacet("jst.utility", "1.0"); |
| 70 | this.installFacet(JpaFacet.ID, jpaFacetVersion, jpaConfig); |
| 71 | this.addJar(jpaJarName()); |
| 72 | if (eclipseLinkJarName() != null) { |
| 73 | this.addJar(eclipseLinkJarName()); |
| 74 | } |
| 75 | this.jpaProject = JptJpaCorePlugin.getJpaProject(this.getProject()); |
| 76 | this.jpaProject.setDiscoversAnnotatedClasses(true); |
| 77 | this.jpaProject.setContextModelSynchronizer(this.buildSynchronousContextModelSynchronizer()); |
| 78 | this.jpaProject.setUpdateSynchronizer(this.buildSynchronousUpdateSynchronizer()); |
| 79 | } |
| 80 | |
| 81 | protected Synchronizer buildSynchronousContextModelSynchronizer() { |
| 82 | return new SynchronousSynchronizer(this.buildSynchronousContextModelSynchronizerCommand()); |
| 83 | } |
| 84 | |
| 85 | protected Command buildSynchronousContextModelSynchronizerCommand() { |
| 86 | return new SynchronousContextModelSynchronizerCommand(); |
| 87 | } |
| 88 | |
| 89 | protected class SynchronousContextModelSynchronizerCommand implements Command { |
| 90 | public void execute() { |
| 91 | TestJpaProject.this.jpaProject.synchronizeContextModel(new NullProgressMonitor()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected CallbackSynchronizer buildSynchronousUpdateSynchronizer() { |
| 96 | return new CallbackSynchronousSynchronizer(this.buildSynchronousUpdateSynchronizerCommand()); |
| 97 | } |
| 98 | |
| 99 | protected Command buildSynchronousUpdateSynchronizerCommand() { |
| 100 | return new SynchronousUpdateSynchronizerCommand(); |
| 101 | } |
| 102 | |
| 103 | protected class SynchronousUpdateSynchronizerCommand implements Command { |
| 104 | public void execute() { |
| 105 | TestJpaProject.this.jpaProject.update(new NullProgressMonitor()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public static String jpaJarName() { |
| 110 | return getSystemProperty(JPA_JAR_NAME_SYSTEM_PROPERTY); |
| 111 | } |
| 112 | |
| 113 | public static String eclipseLinkJarName() { |
| 114 | return getSystemProperty(ECLIPSELINK_JAR_NAME_SYSTEM_PROPERTY); |
| 115 | } |
| 116 | |
| 117 | private static String getSystemProperty(String propertyName) { |
| 118 | return System.getProperty(propertyName); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // ********** public methods ********** |
| 123 | |
| 124 | public JpaProject getJpaProject() { |
| 125 | return this.jpaProject; |
| 126 | } |
| 127 | |
| 128 | } |