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
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
22 import org
.springframework
.security
.core
.Authentication
;
23 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
24 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
25 import org
.unitils
.UnitilsJUnit4
;
26 import org
.unitils
.database
.DatabaseUnitils
;
27 import org
.unitils
.database
.annotations
.Transactional
;
28 import org
.unitils
.database
.util
.TransactionMode
;
29 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
31 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmClientCacheException
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
38 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
39 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
40 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
41 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
45 * Base class for remoting tests, responsible for
46 * - starting / stop the cdm server
47 * - running some basic connection tests
48 * - setting up the remote configuration.
51 @Transactional(TransactionMode
.DISABLED
)
52 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
53 public class BaseRemotingTest
extends UnitilsJUnit4
{
54 //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
56 private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE
=
57 new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
59 private static CdmApplicationRemoteController remoteApplicationController
;
60 private static ICdmRemoteSource cdmRemoteSource
;
61 private static CdmPersistentRemoteSource remotePersistentSource
;
63 public static boolean useManagedServer
= false;
65 public static final Resource CDMLIB_DISK_STORE_RESOURCE
=
66 new ClassPathResource("/eu/etaxonomy/cache");
68 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
72 public static void initializeBaseRemotingTest() {
73 //NOTE: Run this the cdmTest H2 DB whenever it needs to be
74 // recreated e.g. after a model change
75 DatabaseUnitils
.disableConstraints();
78 System
.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE
.getFile().getAbsolutePath());
79 } catch (IOException e
) {
80 throw new CdmClientCacheException(e
);
83 useManagedServer
= (System
.getProperty("use.managed.server") == null) ? useManagedServer
: Boolean
.valueOf(System
.getProperty("use.managed.server"));
84 if(useManagedServer
) {
86 CDMServer
.getInstance().start();
87 } catch (Exception e
) {
89 Assert
.fail("Server failed to start. Reason : " + e
.getMessage());
92 initializeController(CDMServer
.getInstance().getName(),
93 CDMServer
.getInstance().getHost(),
94 CDMServer
.getInstance().getPort(),
95 CDMServer
.getInstance().getContextPath(),
96 NomenclaturalCode
.ICNAFP
,
106 public void disableConstraints() {
107 // To be run on the src/test/resources/h2/cdmTest h2 db after
108 // updating the unitils.properties 'database.url' property
109 DatabaseUnitils
.disableConstraints();
112 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
114 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
115 remoteApplicationController
=
116 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
122 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
123 } catch (CdmRemoteSourceException e
) {
124 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
126 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
128 SecurityContextHolder
.clearContext();
129 SecurityContextImpl sc
= new SecurityContextImpl();
130 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
131 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
132 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
133 sc
.setAuthentication(authentication
);
135 SecurityContextHolder
.setContext(sc
);
140 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
141 return remoteApplicationController
;
144 protected static ICdmRemoteSource
getCdmRemoteSource() {
145 return cdmRemoteSource
;
148 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
149 return remotePersistentSource
;
152 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
153 return cdmEntitySessionManager
;
157 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
158 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
159 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
160 return ownerSessionMap
.get(sessionOwner
);
163 protected static CdmTransientEntityCacher
getActiveSession() {
164 return (CdmTransientEntityCacher
) getFieldValueViaReflection(cdmEntitySessionManager
, "activeSession");
167 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
168 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
172 public static void cleanup() {
174 CDMServer
.getInstance().stop();
175 } catch (Exception e
) {
176 Assert
.fail("Server could not be stopped. Reason : " + e
.getMessage());
181 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
182 Class
<?
> clazz
= object
.getClass();
184 Field field
= clazz
.getDeclaredField(fieldName
);
185 field
.setAccessible(true);
186 return field
.get(object
);
187 } catch (NoSuchFieldException e
) {
189 } catch (SecurityException e
) {
191 } catch (IllegalArgumentException e
) {
193 } catch (IllegalAccessException e
) {