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
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
36 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
37 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
38 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
42 * Base class for remoting tests, responsible for
43 * - starting / stop the cdm server
44 * - running some basic connection tests
45 * - setting up the remote configuration.
48 @Transactional(TransactionMode
.DISABLED
)
49 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
50 public abstract class BaseRemotingTest
extends ThreadedTest
{
51 @SuppressWarnings("unused")
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
,
70 } catch (Exception e
) {
72 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
78 public static void emptyAllCachesExceptModelCache() {
79 CacheManager cm
= CacheManager
.create();
80 String
[] cacheNames
= CacheManager
.create().getCacheNames();
81 for(String cacheName
: cacheNames
) {
82 if(!cacheName
.equals(CdmRemoteCacheManager
.CDM_MODEL_CACHE_NAME
)) {
83 cm
.removeCache(cacheName
);
89 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, String username
, String password
) {
91 if(CdmApplicationState
.getCurrentAppConfig() != null) {
94 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
);
95 CdmApplicationRemoteController remoteApplicationController
=
96 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
99 CdmApplicationState
.setCurrentAppConfig(remoteApplicationController
);
100 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
102 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
104 CdmApplicationState
.setCurrentDataChangeService(new CdmDataChangeService());
106 authenticate(username
, password
);
110 protected static void authenticate(String username
, String password
) {
112 //FIXME:Remoting the authentication code should be replaced by a method call which actually
113 // does the authentication in the editor code so that the 'real' authentication can be tested
114 SecurityContextHolder
.clearContext();
115 SecurityContextImpl sc
= new SecurityContextImpl();
116 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
117 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
118 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
119 sc
.setAuthentication(authentication
);
121 SecurityContextHolder
.setContext(sc
);
122 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
127 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
128 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
131 protected static ICdmRemoteSource
getCdmRemoteSource() {
132 return cdmRemoteSource
;
135 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
136 return remotePersistentSource
;
139 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
140 return cdmEntitySessionManager
;
144 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
145 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
146 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
147 return ownerSessionMap
.get(sessionOwner
);
150 protected static CdmEntitySession
getActiveSession() {
151 return ((InheritableThreadLocal
<CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "tlActiveSession")).get();
154 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
155 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
159 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
160 Class
<?
> clazz
= object
.getClass();
162 Field field
= clazz
.getDeclaredField(fieldName
);
163 field
.setAccessible(true);
164 return field
.get(object
);
165 } catch (NoSuchFieldException e
) {
167 } catch (SecurityException e
) {
169 } catch (IllegalArgumentException e
) {
171 } catch (IllegalAccessException e
) {
178 protected static void authenticateDefaultUser() {
179 authenticate(user
, password
);