blob: b7a769e9885635e0c484c069e0b7d34f8dbd84a7 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010-2012 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.3 - MySports Demo Bug 344608
******************************************************************************/
package example.mysports.tests.admin;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.Assert;
import org.eclipse.persistence.jpa.JpaHelper;
import org.eclipse.persistence.sessions.server.Server;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import example.mysports.admin.model.DataIsolation;
import example.mysports.admin.model.Datasource;
import example.mysports.admin.model.HostedLeague;
import example.mysports.admin.services.DatasourceRepository;
import example.mysports.tests.TestingLeagueRepository;
/**
* Create initial league entities.
*
* @author dclarke
* @since EclipseLink 2.3.0
*/
public class CreateLeagues {
private static EntityManagerFactory emf;
private static DatasourceRepository dsRepo;
@Test
public void verifySession() {
Assert.assertNotNull(emf);
Server session = JpaHelper.getServerSession(emf);
Assert.assertNotNull(session);
Assert.assertTrue(session.isServerSession());
}
@Test
public void createLeagues() {
EntityManager em = emf.createEntityManager();
try {
Datasource ds = em.find(Datasource.class, "jdbc/MySports");
em.getTransaction().begin();
HostedLeague osl = new HostedLeague("OSL", "Ottawa Soccer League", "black");
osl.setLogoUrl("/logos/osl.png");
osl.setDatasource(ds);
osl.addPlayerExtension("allergies", "java.lang.String", "flex_1", "allergies/text()");
em.persist(osl);
HostedLeague hthl = new HostedLeague("HTHL", "High Tech Hockey League", "red");
hthl.setLogoUrl("/logos/hthl.png");
hthl.setDatasource(ds);
hthl.addPlayerExtension( "penaltyMinutes", "java.lang.Integer", "flex_1", "penalty-minutes/text()");
hthl.addPlayerExtension( "position", "java.lang.String", "flex_2", "position/text()");
em.persist(hthl);
HostedLeague kfl = new HostedLeague("KFL", "Kid's Football League", "green");
kfl.setLogoUrl("/logos/kfl.png");
kfl.setDatasource(ds);
kfl.addPlayerExtension("position", "java.lang.String", "flex_1", "penalty-minutes/text()");
em.persist(kfl);
HostedLeague mhl = new HostedLeague("MHL", "Minor Hockey League", "red");
mhl.setShared(false);
mhl.setDataIsolation(DataIsolation.DATABASE);
mhl.addTableName("Player", "mys_player_mhl");
mhl.addTableName("Division", "mys_div_mhl");
mhl.addTableName("Team", "mys_team_mhl");
mhl.setLogoUrl("/logos/hthl.png");
mhl.setDatasource(ds);
mhl.addPlayerExtension("position", "java.lang.String", "flex_1", "position/text()");
em.persist(mhl);
em.getTransaction().commit();
} finally {
em.close();
}
}
@BeforeClass
public static void createEMF() {
emf = Persistence.createEntityManagerFactory("mysports-admin", TestingLeagueRepository.get());
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.createNativeQuery("DELETE FROM mys_admin_player_ext").executeUpdate();
em.createNativeQuery("DELETE FROM mys_admin_team_ext").executeUpdate();
em.createNativeQuery("DELETE FROM mys_admin_div_ext").executeUpdate();
em.createQuery("DELETE FROM HostedLeague").executeUpdate();
em.getTransaction().commit();
} finally {
em.close();
}
dsRepo = new DatasourceRepository();
}
@AfterClass
public static void closeEMF() {
emf.close();
}
}