| /******************************************************************************* |
| * Copyright (c) 2010-2013 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| * which accompanies this distribution. |
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| * and the Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * dclarke - EclipseLink 2.4 - MySports Demo Bug 344608 |
| ******************************************************************************/ |
| package eclipselink.example.mysports.admin.services.test; |
| |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.CONNECTION_POOL; |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.CONNECTION_POOL_INITIAL; |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.CONNECTION_POOL_MIN; |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.CONNECTION_POOL_READ; |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.NON_JTA_DATASOURCE; |
| import static org.eclipse.persistence.config.PersistenceUnitProperties.TRANSACTION_TYPE; |
| |
| import java.lang.reflect.Proxy; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import javax.persistence.EntityManagerFactory; |
| import javax.persistence.Persistence; |
| |
| import org.eclipse.persistence.jpa.JpaHelper; |
| import org.eclipse.persistence.sessions.server.Server; |
| import org.eclipse.persistence.tools.schemaframework.DefaultTableGenerator; |
| import org.eclipse.persistence.tools.schemaframework.SchemaManager; |
| import org.eclipse.persistence.tools.schemaframework.TableCreator; |
| |
| import eclipselink.example.mysports.admin.examples.ExampleLeagueDefinition; |
| import eclipselink.example.mysports.admin.services.AdminSchemaManager; |
| import eclipselink.example.mysports.admin.services.HostedLeagueRepository; |
| import eclipselink.example.mysports.test.common.ExamplePropertiesLoader; |
| |
| public class AdminPersistenceTesting { |
| |
| public static Map<String, Object> add(Map<String, Object> properties) { |
| Map<String, Object> props = properties; |
| |
| if (props == null) { |
| props = new HashMap<String, Object>(); |
| } |
| props.put(TRANSACTION_TYPE, "RESOURCE_LOCAL"); |
| props.put(NON_JTA_DATASOURCE, ""); |
| |
| props.put(CONNECTION_POOL_READ + CONNECTION_POOL_INITIAL, "1"); |
| props.put(CONNECTION_POOL_READ + CONNECTION_POOL_MIN, "1"); |
| props.put(CONNECTION_POOL + CONNECTION_POOL_INITIAL, "1"); |
| props.put(CONNECTION_POOL + CONNECTION_POOL_MIN, "1"); |
| |
| ExamplePropertiesLoader.loadProperties(props); |
| |
| return props; |
| } |
| |
| public static Map<String, Object> get() { |
| return add(null); |
| } |
| |
| public static HostedLeagueRepository createTestRepository(boolean replaceSchema) { |
| EntityManagerFactory emf = Persistence.createEntityManagerFactory("MySportsAdmin", get()); |
| if (replaceSchema) { |
| AdminSchemaManager.createTables(emf); |
| } |
| HostedLeagueRepository repository = (HostedLeagueRepository) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { HostedLeagueRepository.class }, new HostedLeagueRepositoryHandler(emf)); |
| ExampleLeagueDefinition.populateAll(repository); |
| return repository; |
| } |
| |
| public static EntityManagerFactory getEMF(HostedLeagueRepository repository) { |
| HostedLeagueRepositoryHandler handler = (HostedLeagueRepositoryHandler) Proxy.getInvocationHandler(repository); |
| return handler.getEmf(); |
| } |
| |
| public static void closeTestingRepository(HostedLeagueRepository repository, boolean dropTables) { |
| EntityManagerFactory emf = getEMF(repository); |
| if (dropTables) { |
| dropTables(emf); |
| } |
| emf.close(); |
| } |
| |
| public static void dropTables(EntityManagerFactory emf) { |
| Server session = JpaHelper.getServerSession(emf); |
| SchemaManager sm = new SchemaManager(session); |
| |
| TableCreator tc = new DefaultTableGenerator(session.getProject(), true).generateDefaultTableCreator(); |
| tc.setIgnoreDatabaseException(true); |
| tc.dropTables(session, sm, true); |
| } |
| |
| } |