2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.httpinvoker
;
11 import java
.io
.IOException
;
12 import java
.lang
.reflect
.Field
;
15 import org
.junit
.AfterClass
;
16 import org
.junit
.Assert
;
17 import org
.junit
.BeforeClass
;
18 import org
.junit
.Test
;
19 import org
.springframework
.core
.io
.ClassPathResource
;
20 import org
.springframework
.core
.io
.Resource
;
21 import org
.unitils
.UnitilsJUnit4
;
22 import org
.unitils
.database
.DatabaseUnitils
;
23 import org
.unitils
.database
.annotations
.Transactional
;
24 import org
.unitils
.database
.util
.TransactionMode
;
25 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
28 import eu
.etaxonomy
.cdm
.api
.cache
.CdmClientCacheException
;
29 import eu
.etaxonomy
.cdm
.api
.cache
.CdmTransientEntityCacher
;
30 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
31 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
35 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
36 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
37 import eu
.etaxonomy
.taxeditor
.session
.ISessionEventListener
;
41 * Base class for remoting tests, responsible for
42 * - starting / stop the cdm server
43 * - running some basic connection tests
44 * - setting up the remote configuration.
47 @Transactional(TransactionMode
.DISABLED
)
48 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
49 public class BaseRemotingTest
extends UnitilsJUnit4
{
50 //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
52 private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
53 new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
55 private static CdmApplicationRemoteController remoteApplicationController
;
56 private static ICdmRemoteSource cdmRemoteSource
;
57 private static CdmPersistentRemoteSource remotePersistentSource
;
59 public static boolean useManagedServer
= true;
61 public static final Resource CDMLIB_DISK_STORE_RESOURCE
=
62 new ClassPathResource("/eu/etaxonomy/cache");
64 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
67 public static void initializeBaseRemotingTest() {
68 //NOTE: Run this the cdmTest H2 DB whenever it needs to be
69 // recreated e.g. after a model change
70 //DatabaseUnitils.disableConstraints();
73 System
.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE
.getFile().getAbsolutePath());
74 } catch (IOException e
) {
75 throw new CdmClientCacheException(e
);
78 useManagedServer
= (System
.getProperty("use.managed.server") == null) ? useManagedServer
: Boolean
.valueOf(System
.getProperty("use.managed.server"));
79 if(useManagedServer
) {
81 CDMServer
.getInstance().start();
82 } catch (Exception e
) {
84 Assert
.fail("Server failed to start. Reason : " + e
.getMessage());
87 initializeController(CDMServer
.getInstance().getName(),
88 CDMServer
.getInstance().getHost(),
89 CDMServer
.getInstance().getPort(),
90 CDMServer
.getInstance().getContextPath(),
91 NomenclaturalCode
.ICNAFP
);
97 public void disableConstraints() {
98 // To be run on the src/test/resources/h2/cdmTest h2 db after
99 // updating the unitils.properties 'database.url' property
100 DatabaseUnitils
.disableConstraints();
102 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
) {
104 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
105 remoteApplicationController
=
106 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
112 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
113 } catch (CdmRemoteSourceException e
) {
114 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
116 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
121 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
122 return remoteApplicationController
;
125 protected static ICdmRemoteSource
getCdmRemoteSource() {
126 return cdmRemoteSource
;
129 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
130 return remotePersistentSource
;
133 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
134 return cdmEntitySessionManager
;
138 protected static CdmEntitySession
getSession(ISessionEventListener sessionOwner
) {
139 Map
<ISessionEventListener
, CdmEntitySession
> ownerSessionMap
=
140 (Map
<ISessionEventListener
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
141 return ownerSessionMap
.get(sessionOwner
);
144 protected static CdmTransientEntityCacher
getActiveSession() {
145 return (CdmTransientEntityCacher
) getFieldValueViaReflection(cdmEntitySessionManager
, "activeSession");
148 protected static CdmTransientEntityCacher
getCacher(ISessionEventListener sessionOwner
) {
149 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
153 public static void cleanup() {
155 CDMServer
.getInstance().stop();
156 } catch (Exception e
) {
157 Assert
.fail("Server could not be stopped. Reason : " + e
.getMessage());
162 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
163 Class
<?
> clazz
= object
.getClass();
165 Field field
= clazz
.getDeclaredField(fieldName
);
166 field
.setAccessible(true);
167 return field
.get(object
);
168 } catch (NoSuchFieldException e
) {
170 } catch (SecurityException e
) {
172 } catch (IllegalArgumentException e
) {
174 } catch (IllegalAccessException e
) {