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
.lang
.reflect
.Field
;
14 import net
.sf
.ehcache
.CacheManager
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.junit
.BeforeClass
;
18 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
19 import org
.springframework
.security
.core
.Authentication
;
20 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
21 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
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
.application
.CdmApplicationState
;
29 import eu
.etaxonomy
.cdm
.api
.application
.CdmDataChangeService
;
30 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
31 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
37 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
38 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
39 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
43 * Base class for remoting tests, responsible for
44 * - starting / stop the cdm server
45 * - running some basic connection tests
46 * - setting up the remote configuration.
49 @Transactional(TransactionMode
.DISABLED
)
50 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
51 public abstract class BaseRemotingTest
extends ThreadedTest
{
52 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
54 private static ICdmRemoteSource cdmRemoteSource
;
55 private static CdmPersistentRemoteSource remotePersistentSource
;
56 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
59 public static void initializeBaseRemotingTest() {
61 DatabaseUnitils
.disableConstraints();
64 initializeController(sourceName
,
68 NomenclaturalCode
.ICNAFP
,
71 } catch (Exception e
) {
73 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
79 public static void emptyAllCachesExceptModelCache() {
80 CacheManager cm
= CacheManager
.create();
81 String
[] cacheNames
= CacheManager
.create().getCacheNames();
82 for(String cacheName
: cacheNames
) {
83 if(!cacheName
.equals(CdmRemoteCacheManager
.CDM_MODEL_CACHE_NAME
)) {
84 cm
.removeCache(cacheName
);
90 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
92 if(CdmApplicationState
.getCurrentAppConfig() != null) {
95 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
96 CdmApplicationRemoteController remoteApplicationController
=
97 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
100 CdmApplicationState
.setCurrentAppConfig(remoteApplicationController
);
101 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
103 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
105 CdmApplicationState
.setCurrentDataChangeService(new CdmDataChangeService());
107 authenticate(username
, password
);
111 protected static void authenticate(String username
, String password
) {
113 //FIXME:Remoting the authentication code should be replaced by a method call which actually
114 // does the authentication in the editor code so that the 'real' authentication can be tested
115 SecurityContextHolder
.clearContext();
116 SecurityContextImpl sc
= new SecurityContextImpl();
117 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
118 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
119 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
120 sc
.setAuthentication(authentication
);
122 SecurityContextHolder
.setContext(sc
);
123 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
128 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
129 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
132 protected static ICdmRemoteSource
getCdmRemoteSource() {
133 return cdmRemoteSource
;
136 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
137 return remotePersistentSource
;
140 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
141 return cdmEntitySessionManager
;
145 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
146 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
147 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
148 return ownerSessionMap
.get(sessionOwner
);
151 protected static CdmEntitySession
getActiveSession() {
152 return ((InheritableThreadLocal
<CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "tlActiveSession")).get();
155 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
156 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
160 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
161 Class
<?
> clazz
= object
.getClass();
163 Field field
= clazz
.getDeclaredField(fieldName
);
164 field
.setAccessible(true);
165 return field
.get(object
);
166 } catch (NoSuchFieldException e
) {
168 } catch (SecurityException e
) {
170 } catch (IllegalArgumentException e
) {
172 } catch (IllegalAccessException e
) {
179 protected static void authenticateDefaultUser() {
180 authenticate(user
, password
);