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
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.InputStream
;
14 import java
.lang
.reflect
.Field
;
17 import java
.util
.Properties
;
19 import net
.sf
.ehcache
.CacheManager
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.eclipse
.core
.runtime
.FileLocator
;
23 import org
.eclipse
.core
.runtime
.Platform
;
24 import org
.junit
.Assert
;
25 import org
.junit
.BeforeClass
;
26 import org
.osgi
.framework
.Bundle
;
27 import org
.springframework
.core
.io
.ClassPathResource
;
28 import org
.springframework
.core
.io
.Resource
;
29 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
30 import org
.springframework
.security
.core
.Authentication
;
31 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
32 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
33 import org
.unitils
.UnitilsJUnit4
;
34 import org
.unitils
.database
.DatabaseUnitils
;
35 import org
.unitils
.database
.annotations
.Transactional
;
36 import org
.unitils
.database
.util
.TransactionMode
;
37 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
39 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
40 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
41 import eu
.etaxonomy
.cdm
.api
.application
.CdmDataChangeService
;
42 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
43 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
44 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
45 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
46 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
47 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
48 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
49 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
50 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
51 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
55 * Base class for remoting tests, responsible for
56 * - starting / stop the cdm server
57 * - running some basic connection tests
58 * - setting up the remote configuration.
61 @Transactional(TransactionMode
.DISABLED
)
62 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
63 public abstract class BaseRemotingTest
extends UnitilsJUnit4
{
64 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
67 public static final Resource SERVER_PROPERTIES_FILE
=
68 new ClassPathResource("server.properties");
70 private static ICdmRemoteSource cdmRemoteSource
;
71 private static CdmPersistentRemoteSource remotePersistentSource
;
73 public static boolean useManagedServer
= true;
76 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
78 private final static String DEFAULT_USER
= "admin";
79 private final static String DEFAULT_PASSWORD
= "00000";
81 private static String userHomeKey
= "user.home";
83 private static String user
= DEFAULT_USER
;
84 private static String password
= DEFAULT_PASSWORD
;
86 protected static CDMServer cdmServer
;
89 public static void initializeBaseRemotingTest() {
91 DatabaseUnitils
.disableConstraints();
94 String userHomeDirPath
;
95 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.test");
97 URL userHomeDirURL
= bundle
.getEntry("src/test/resources");
98 File userHomeDir
= new File(FileLocator
.resolve(userHomeDirURL
).toURI());
99 userHomeDirPath
= userHomeDir
.getAbsolutePath();
101 URL serverPropertiesURL
= bundle
.getEntry("src/test/resources/server.properties");
102 File serverPropertiesFile
= new File(FileLocator
.resolve(serverPropertiesURL
).toURI());
103 InputStream inputStream
= new FileInputStream(serverPropertiesFile
);
105 Properties prop
= new Properties();
106 if (inputStream
!= null) {
107 prop
.load(inputStream
);
111 logger
.info("Setting user.home to " + userHomeDirPath
);
112 System
.setProperty(userHomeKey
, userHomeDirPath
);
115 cdmServer
= new CDMServer("cdmTest", serverPropertiesURL
);
118 if(prop
.getProperty("user") != null) {
119 user
= prop
.getProperty("user");
122 if(prop
.getProperty("password") != null) {
123 password
= prop
.getProperty("password");
126 initializeController(cdmServer
.getName(),
129 cdmServer
.getContextPath(),
130 NomenclaturalCode
.ICNAFP
,
133 } catch (Exception e
) {
135 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
138 logger
.info("Emptying all caches (except model cache) ");
139 //emptyAllCachesExceptModelCache();
142 public static void emptyAllCachesExceptModelCache() {
143 CacheManager cm
= CacheManager
.create();
144 String
[] cacheNames
= CacheManager
.create().getCacheNames();
145 for(String cacheName
: cacheNames
) {
146 if(!cacheName
.equals(CdmRemoteCacheManager
.CDM_MODEL_CACHE_NAME
)) {
147 cm
.removeCache(cacheName
);
153 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
155 if(CdmApplicationState
.getCurrentAppConfig() != null) {
158 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
159 CdmApplicationRemoteController remoteApplicationController
=
160 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
163 CdmApplicationState
.setCurrentAppConfig(remoteApplicationController
);
165 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
166 } catch (CdmRemoteSourceException e
) {
167 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
169 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
171 CdmApplicationState
.setCurrentDataChangeService(new CdmDataChangeService());
173 //FIXME:Remoting the authentication code should be replaced by a method call which actually
174 // does the authentication in the editor code so that the 'real' authentication can be tested
175 SecurityContextHolder
.clearContext();
176 SecurityContextImpl sc
= new SecurityContextImpl();
177 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
178 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
179 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
180 sc
.setAuthentication(authentication
);
182 SecurityContextHolder
.setContext(sc
);
183 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
188 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
189 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
192 protected static ICdmRemoteSource
getCdmRemoteSource() {
193 return cdmRemoteSource
;
196 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
197 return remotePersistentSource
;
200 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
201 return cdmEntitySessionManager
;
205 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
206 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
207 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
208 return ownerSessionMap
.get(sessionOwner
);
211 protected static CdmEntitySession
getActiveSession() {
212 return (CdmEntitySession
) getFieldValueViaReflection(cdmEntitySessionManager
, "activeSession");
215 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
216 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
220 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
221 Class
<?
> clazz
= object
.getClass();
223 Field field
= clazz
.getDeclaredField(fieldName
);
224 field
.setAccessible(true);
225 return field
.get(object
);
226 } catch (NoSuchFieldException e
) {
228 } catch (SecurityException e
) {
230 } catch (IllegalArgumentException e
) {
232 } catch (IllegalAccessException e
) {