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
.InputStream
;
12 import java
.lang
.reflect
.Field
;
14 import java
.util
.Properties
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.junit
.AfterClass
;
18 import org
.junit
.Assert
;
19 import org
.junit
.BeforeClass
;
20 import org
.junit
.Test
;
21 import org
.springframework
.core
.io
.ClassPathResource
;
22 import org
.springframework
.core
.io
.Resource
;
23 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
24 import org
.springframework
.security
.core
.Authentication
;
25 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
26 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
27 import org
.unitils
.UnitilsJUnit4
;
28 import org
.unitils
.database
.DatabaseUnitils
;
29 import org
.unitils
.database
.annotations
.Transactional
;
30 import org
.unitils
.database
.util
.TransactionMode
;
31 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
33 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
38 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
39 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
40 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
41 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
42 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
46 * Base class for remoting tests, responsible for
47 * - starting / stop the cdm server
48 * - running some basic connection tests
49 * - setting up the remote configuration.
52 @Transactional(TransactionMode
.DISABLED
)
53 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
54 public class BaseRemotingTest
extends UnitilsJUnit4
{
55 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
58 public static final Resource SERVER_PROPERTIES_FILE
=
59 new ClassPathResource("server.properties");
61 private static CdmApplicationRemoteController remoteApplicationController
;
62 private static ICdmRemoteSource cdmRemoteSource
;
63 private static CdmPersistentRemoteSource remotePersistentSource
;
65 public static boolean useManagedServer
= true;
68 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
70 private final static String DEFAULT_USER
= "admin";
71 private final static String DEFAULT_PASSWORD
= "00000";
73 private static String user
= DEFAULT_USER
;
74 private static String password
= DEFAULT_PASSWORD
;
77 public static void initializeBaseRemotingTest() {
78 //NOTE: Run this the cdmTest H2 DB whenever it needs to be
79 // recreated e.g. after a model change
80 DatabaseUnitils
.disableConstraints();
83 useManagedServer
= (System
.getProperty("use.managed.server") == null) ? useManagedServer
: Boolean
.valueOf(System
.getProperty("use.managed.server"));
84 if(useManagedServer
) {
86 CDMServer cdmServer
= CDMServer
.getInstance();
87 Properties prop
= new Properties();
88 InputStream inputStream
= SERVER_PROPERTIES_FILE
.getInputStream();
90 if (inputStream
!= null) {
91 prop
.load(inputStream
);
94 if(prop
.getProperty("httpPort") != null) {
95 cdmServer
.setHttpPort(Integer
.valueOf(prop
.getProperty("httpPort")));
98 if(prop
.getProperty("stopPort") != null) {
99 cdmServer
.setStopPort(Integer
.valueOf(prop
.getProperty("stopPort")));
102 if(prop
.getProperty("stopKey") != null) {
103 cdmServer
.setStopKey(prop
.getProperty("stopKey"));
106 if(prop
.getProperty("user") != null) {
107 user
= prop
.getProperty("user");
110 if(prop
.getProperty("password") != null) {
111 password
= prop
.getProperty("password");
115 } catch (Exception e
) {
117 Assert
.fail("Server failed to start. Reason : " + e
.getMessage());
120 initializeController(CDMServer
.getInstance().getName(),
121 CDMServer
.getInstance().getHost(),
122 CDMServer
.getInstance().getPort(),
123 CDMServer
.getInstance().getContextPath(),
124 NomenclaturalCode
.ICNAFP
,
134 public void disableConstraints() {
135 // To be run on the src/test/resources/h2/cdmTest h2 db after
136 // updating the unitils.properties 'database.url' property
137 DatabaseUnitils
.disableConstraints();
141 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
143 if(remoteApplicationController
!= null) {
146 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
147 remoteApplicationController
=
148 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
153 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
154 } catch (CdmRemoteSourceException e
) {
155 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
157 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
159 SecurityContextHolder
.clearContext();
160 SecurityContextImpl sc
= new SecurityContextImpl();
161 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
162 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
163 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
164 sc
.setAuthentication(authentication
);
166 SecurityContextHolder
.setContext(sc
);
171 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
172 return remoteApplicationController
;
175 protected static ICdmRemoteSource
getCdmRemoteSource() {
176 return cdmRemoteSource
;
179 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
180 return remotePersistentSource
;
183 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
184 return cdmEntitySessionManager
;
188 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
189 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
190 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
191 return ownerSessionMap
.get(sessionOwner
);
194 protected static CdmTransientEntityCacher
getActiveSession() {
195 return (CdmTransientEntityCacher
) getFieldValueViaReflection(cdmEntitySessionManager
, "activeSession");
198 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
199 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
203 public static void cleanup() {
205 CDMServer
.getInstance().stop();
206 } catch (Exception e
) {
207 Assert
.fail("Server could not be stopped. Reason : " + e
.getMessage());
212 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
213 Class
<?
> clazz
= object
.getClass();
215 Field field
= clazz
.getDeclaredField(fieldName
);
216 field
.setAccessible(true);
217 return field
.get(object
);
218 } catch (NoSuchFieldException e
) {
220 } catch (SecurityException e
) {
222 } catch (IllegalArgumentException e
) {
224 } catch (IllegalAccessException e
) {