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
.api
.cache
.CdmServiceCacher
;
43 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
44 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
45 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
46 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
47 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
48 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
49 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
50 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
51 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
52 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
56 * Base class for remoting tests, responsible for
57 * - starting / stop the cdm server
58 * - running some basic connection tests
59 * - setting up the remote configuration.
62 @Transactional(TransactionMode
.DISABLED
)
63 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
64 public abstract class BaseRemotingTest
extends UnitilsJUnit4
{
65 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
68 public static final Resource SERVER_PROPERTIES_FILE
=
69 new ClassPathResource("server.properties");
71 private static ICdmRemoteSource cdmRemoteSource
;
72 private static CdmPersistentRemoteSource remotePersistentSource
;
74 public static boolean useManagedServer
= true;
77 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
79 private final static String DEFAULT_USER
= "admin";
80 private final static String DEFAULT_PASSWORD
= "00000";
82 private static String userHomeKey
= "user.home";
84 private static String user
= DEFAULT_USER
;
85 private static String password
= DEFAULT_PASSWORD
;
87 protected static CDMServer cdmServer
;
90 public static void initializeBaseRemotingTest() {
92 DatabaseUnitils
.disableConstraints();
95 String userHomeDirPath
;
96 Bundle bundle
= Platform
.getBundle("eu.etaxonomy.taxeditor.test");
98 URL userHomeDirURL
= bundle
.getEntry("src/test/resources");
99 File userHomeDir
= new File(FileLocator
.resolve(userHomeDirURL
).toURI());
100 userHomeDirPath
= userHomeDir
.getAbsolutePath();
102 URL serverPropertiesURL
= bundle
.getEntry("src/test/resources/server.properties");
103 File serverPropertiesFile
= new File(FileLocator
.resolve(serverPropertiesURL
).toURI());
104 InputStream inputStream
= new FileInputStream(serverPropertiesFile
);
106 Properties prop
= new Properties();
107 if (inputStream
!= null) {
108 prop
.load(inputStream
);
112 logger
.info("Setting user.home to " + userHomeDirPath
);
113 System
.setProperty(userHomeKey
, userHomeDirPath
);
116 cdmServer
= new CDMServer("cdmTest", serverPropertiesURL
);
119 if(prop
.getProperty("user") != null) {
120 user
= prop
.getProperty("user");
123 if(prop
.getProperty("password") != null) {
124 password
= prop
.getProperty("password");
127 initializeController(cdmServer
.getName(),
130 cdmServer
.getContextPath(),
131 NomenclaturalCode
.ICNAFP
,
134 } catch (Exception e
) {
136 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
139 logger
.info("Emptying all caches (except model cache) ");
140 //emptyAllCachesExceptModelCache();
143 public static void emptyAllCachesExceptModelCache() {
144 CacheManager cm
= CacheManager
.create();
145 String
[] cacheNames
= CacheManager
.create().getCacheNames();
146 for(String cacheName
: cacheNames
) {
147 if(!cacheName
.equals(CdmRemoteCacheManager
.CDM_MODEL_CACHE_NAME
)) {
148 cm
.removeCache(cacheName
);
154 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
156 if(CdmApplicationState
.getCurrentAppConfig() != null) {
159 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
160 CdmApplicationRemoteController remoteApplicationController
=
161 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
164 CdmApplicationState
.setCurrentAppConfig(remoteApplicationController
);
165 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
167 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
168 } catch (CdmRemoteSourceException e
) {
169 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
171 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
173 CdmApplicationState
.setCurrentDataChangeService(new CdmDataChangeService());
175 //FIXME:Remoting the authentication code should be replaced by a method call which actually
176 // does the authentication in the editor code so that the 'real' authentication can be tested
177 SecurityContextHolder
.clearContext();
178 SecurityContextImpl sc
= new SecurityContextImpl();
179 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
180 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
181 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
182 sc
.setAuthentication(authentication
);
184 SecurityContextHolder
.setContext(sc
);
185 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
190 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
191 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
194 protected static ICdmRemoteSource
getCdmRemoteSource() {
195 return cdmRemoteSource
;
198 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
199 return remotePersistentSource
;
202 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
203 return cdmEntitySessionManager
;
207 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
208 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
209 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
210 return ownerSessionMap
.get(sessionOwner
);
213 protected static CdmEntitySession
getActiveSession() {
214 return (CdmEntitySession
) getFieldValueViaReflection(cdmEntitySessionManager
, "activeSession");
217 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
218 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
222 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
223 Class
<?
> clazz
= object
.getClass();
225 Field field
= clazz
.getDeclaredField(fieldName
);
226 field
.setAccessible(true);
227 return field
.get(object
);
228 } catch (NoSuchFieldException e
) {
230 } catch (SecurityException e
) {
232 } catch (IllegalArgumentException e
) {
234 } catch (IllegalAccessException e
) {