From 115e458f66ae440914c6c8f00a5c36752e1fea34 Mon Sep 17 00:00:00 2001 From: Cherian Mathew Date: Thu, 11 Jun 2015 17:50:02 +0200 Subject: [PATCH] fixed issues with resource loading for maven int tests --- eu.etaxonomy.taxeditor.test/build.properties | 6 +- eu.etaxonomy.taxeditor.test/pom.xml | 12 +- .../httpinvoker/BaseRemotingTest.java | 118 +++++++++++------- .../taxeditor/httpinvoker/CDMServer.java | 6 + .../writableResources/cdm.datasources.xml | 13 ++ .../eu/etaxonomy/cdm/cdm.datasources.xml | 20 --- 6 files changed, 104 insertions(+), 71 deletions(-) create mode 100644 eu.etaxonomy.taxeditor.test/src/test/resources/.cdmLibrary/writableResources/cdm.datasources.xml delete mode 100644 eu.etaxonomy.taxeditor.test/src/test/resources/eu/etaxonomy/cdm/cdm.datasources.xml diff --git a/eu.etaxonomy.taxeditor.test/build.properties b/eu.etaxonomy.taxeditor.test/build.properties index 0e7e33b0f..4c13ab916 100644 --- a/eu.etaxonomy.taxeditor.test/build.properties +++ b/eu.etaxonomy.taxeditor.test/build.properties @@ -3,12 +3,12 @@ source.. = src/test/java/,\ bin.includes = META-INF/,\ .,\ lib/byte-buddy-0.5.1.jar,\ - src/,\ lib/org.springframework.context-3.2.2.RELEASE.jar,\ lib/unitils-core-3.4.2.jar,\ lib/unitils-database-3.4.2.jar,\ lib/unitils-dbmaintainer-3.4.2.jar,\ lib/unitils-dbunit-3.4.2.jar,\ lib/unitils-spring-3.4.2.jar,\ - lib/dbunit-2.4.9.jar -output.. = bin/ + lib/dbunit-2.4.9.jar,\ + src/test/resources/ +output.. = target/classes diff --git a/eu.etaxonomy.taxeditor.test/pom.xml b/eu.etaxonomy.taxeditor.test/pom.xml index eef8eb161..740a38374 100644 --- a/eu.etaxonomy.taxeditor.test/pom.xml +++ b/eu.etaxonomy.taxeditor.test/pom.xml @@ -13,6 +13,16 @@ Holds all Tests for the Taxonomic Editor + + + + + + + + + + org.eclipse.tycho target-platform-configuration @@ -86,7 +96,7 @@ deploy-war - 0 + 0 diff --git a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java index 420dcb5f5..9b78aa287 100644 --- a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java +++ b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java @@ -8,16 +8,21 @@ */ package eu.etaxonomy.taxeditor.httpinvoker; +import java.io.File; import java.io.InputStream; import java.lang.reflect.Field; +import java.net.URL; import java.util.Map; import java.util.Properties; import org.apache.log4j.Logger; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Platform; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.osgi.framework.Bundle; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -58,6 +63,9 @@ public class BaseRemotingTest extends UnitilsJUnit4 { public static final Resource SERVER_PROPERTIES_FILE = new ClassPathResource("server.properties"); + public static final Resource EDITOR_DATASOURCES_FILE = + new ClassPathResource(".cdmLibrary/writableResources/cdm.datasources.xml"); + private static CdmApplicationRemoteController remoteApplicationController; private static ICdmRemoteSource cdmRemoteSource; private static CdmPersistentRemoteSource remotePersistentSource; @@ -70,71 +78,87 @@ public class BaseRemotingTest extends UnitilsJUnit4 { private final static String DEFAULT_USER = "admin"; private final static String DEFAULT_PASSWORD = "00000"; + private static String userHomeKey = "user.home"; + private static String user = DEFAULT_USER; private static String password = DEFAULT_PASSWORD; @BeforeClass public static void initializeBaseRemotingTest() { + //NOTE: Run this the cdmTest H2 DB whenever it needs to be - // recreated e.g. after a model change - DatabaseUnitils.disableConstraints(); + // recreated e.g. after a model change + DatabaseUnitils.disableConstraints(); + try { + String userHomeDirPath; + Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.test"); + // if bundle is null then we are running as junit test in eclipse + if(bundle == null) { + userHomeDirPath = EDITOR_DATASOURCES_FILE.getFile().getParentFile().getParentFile().getParent(); + } else { + // ... else we are running in maven which requires loading the dir via the + // classpath bundle + URL userHomeDirURL = bundle.getEntry("src/test/resources"); + File userHomeDir = new File(FileLocator.resolve(userHomeDirURL).toURI()); + userHomeDirPath = userHomeDir.getAbsolutePath(); + } - useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server")); - if(useManagedServer) { - try { - CDMServer cdmServer = CDMServer.getInstance(); - Properties prop = new Properties(); - InputStream inputStream = SERVER_PROPERTIES_FILE.getInputStream(); - if (inputStream != null) { - prop.load(inputStream); - } + logger.info("Setting user.home to " + userHomeDirPath); + System.setProperty(userHomeKey, userHomeDirPath); - if(prop.getProperty("httpPort") != null) { - cdmServer.setHttpPort(Integer.valueOf(prop.getProperty("httpPort"))); - } + CDMServer cdmServer = CDMServer.getInstance(); - if(prop.getProperty("stopPort") != null) { - cdmServer.setStopPort(Integer.valueOf(prop.getProperty("stopPort"))); - } - if(prop.getProperty("stopKey") != null) { - cdmServer.setStopKey(prop.getProperty("stopKey")); - } + Properties prop = new Properties(); + InputStream inputStream = SERVER_PROPERTIES_FILE.getInputStream(); - if(prop.getProperty("user") != null) { - user = prop.getProperty("user"); - } + if (inputStream != null) { + prop.load(inputStream); + inputStream.close(); + } - if(prop.getProperty("password") != null) { - password = prop.getProperty("password"); - } + if(prop.getProperty("httpPort") != null) { + cdmServer.setHttpPort(Integer.valueOf(prop.getProperty("httpPort"))); + } - cdmServer.start(); - } catch (Exception e) { - e.printStackTrace(); - Assert.fail("Server failed to start. Reason : " + e.getMessage()); + if(prop.getProperty("stopPort") != null) { + cdmServer.setStopPort(Integer.valueOf(prop.getProperty("stopPort"))); } - initializeController(CDMServer.getInstance().getName(), - CDMServer.getInstance().getHost(), - CDMServer.getInstance().getPort(), - CDMServer.getInstance().getContextPath(), - NomenclaturalCode.ICNAFP, - user, - password); - } + if(prop.getProperty("stopKey") != null) { + cdmServer.setStopKey(prop.getProperty("stopKey")); + } + + if(prop.getProperty("user") != null) { + user = prop.getProperty("user"); + } + if(prop.getProperty("password") != null) { + password = prop.getProperty("password"); + } + cdmServer.start(); + initializeController(CDMServer.getInstance().getName(), + CDMServer.getInstance().getHost(), + CDMServer.getInstance().getPort(), + CDMServer.getInstance().getContextPath(), + NomenclaturalCode.ICNAFP, + user, + password); + } catch (Exception e) { + e.printStackTrace(); + // Assert.fail("Server failed to start. Reason : " + e.getMessage()); + } } @Test public void disableConstraints() { - // To be run on the src/test/resources/h2/cdmTest h2 db after - // updating the unitils.properties 'database.url' property - DatabaseUnitils.disableConstraints(); + // To be run on the src/test/resources/h2/cdmTest h2 db after + // updating the unitils.properties 'database.url' property + DatabaseUnitils.disableConstraints(); } @@ -181,22 +205,22 @@ public class BaseRemotingTest extends UnitilsJUnit4 { } protected static ICdmEntitySessionManager getCdmEntitySessionManager() { - return cdmEntitySessionManager; + return cdmEntitySessionManager; } protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) { - Map ownerSessionMap = - (Map) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap"); - return ownerSessionMap.get(sessionOwner); + Map ownerSessionMap = + (Map) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap"); + return ownerSessionMap.get(sessionOwner); } protected static CdmTransientEntityCacher getActiveSession() { - return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession"); + return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession"); } protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) { - return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher"); + return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher"); } @AfterClass diff --git a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java index a6f5fd509..4fad3b373 100644 --- a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java +++ b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java @@ -75,6 +75,8 @@ public class CDMServer { private static CDMServer cdmServer = null; private static CDMServerException cdmse = null; + private boolean serverAlreadyRunning = false; + private void CDMServer() {} public static CDMServer getInstance() { @@ -162,6 +164,7 @@ public class CDMServer { */ if(isStarted(1)) { logger.info("[CDM-Server] Server already running @ " + host + ":" + httpPort ); + serverAlreadyRunning = true; return; } @@ -268,6 +271,9 @@ public class CDMServer { } } + if(serverAlreadyRunning) { + return; + } Thread t = new Thread() { @Override public void run() { diff --git a/eu.etaxonomy.taxeditor.test/src/test/resources/.cdmLibrary/writableResources/cdm.datasources.xml b/eu.etaxonomy.taxeditor.test/src/test/resources/.cdmLibrary/writableResources/cdm.datasources.xml new file mode 100644 index 000000000..7cb8fa81b --- /dev/null +++ b/eu.etaxonomy.taxeditor.test/src/test/resources/.cdmLibrary/writableResources/cdm.datasources.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/eu.etaxonomy.taxeditor.test/src/test/resources/eu/etaxonomy/cdm/cdm.datasources.xml b/eu.etaxonomy.taxeditor.test/src/test/resources/eu/etaxonomy/cdm/cdm.datasources.xml deleted file mode 100644 index f5d1afb0e..000000000 --- a/eu.etaxonomy.taxeditor.test/src/test/resources/eu/etaxonomy/cdm/cdm.datasources.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - -- 2.30.2