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 org
.apache
.log4j
.Logger
;
15 import org
.junit
.BeforeClass
;
16 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
17 import org
.springframework
.security
.core
.Authentication
;
18 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
19 import org
.unitils
.database
.DatabaseUnitils
;
20 import org
.unitils
.database
.annotations
.Transactional
;
21 import org
.unitils
.database
.util
.TransactionMode
;
22 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
24 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
26 import eu
.etaxonomy
.cdm
.api
.application
.CdmDataChangeService
;
27 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
28 import eu
.etaxonomy
.cdm
.cache
.CdmRemoteCacheManager
;
29 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
30 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.ConversationalTransientEntityCacher
;
31 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
32 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
34 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
35 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
36 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
37 import net
.sf
.ehcache
.CacheManager
;
41 * Base class for remoting tests, responsible for
42 * - starting / stop the cdm server
43 * - running some basic connection tests
44 * - setting up the remote configuration.
47 @Transactional(TransactionMode
.DISABLED
)
48 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
49 public abstract class BaseRemotingTest
extends ThreadedTest
{
50 @SuppressWarnings("unused")
51 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
53 private static ICdmRemoteSource cdmRemoteSource
;
54 private static CdmPersistentRemoteSource remotePersistentSource
;
55 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
58 public static void initializeBaseRemotingTest() {
60 DatabaseUnitils
.disableConstraints();
63 initializeController(sourceName
,
69 } catch (Exception e
) {
70 System
.err
.println("user: " + user
+ " password: " + password
);
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();
116 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(username
, password
);
117 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
120 User user
= (User
) authentication
.getPrincipal();
121 /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
122 * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
123 user
.initAuthorities();
124 authentication
= new UsernamePasswordAuthenticationToken(user
,password
, authentication
.getAuthorities());
125 SecurityContextHolder
.getContext().setAuthentication(authentication
);
126 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
132 // SecurityContextHolder.clearContext();
133 // SecurityContextImpl sc = new SecurityContextImpl();
134 // Authentication token = new UsernamePasswordAuthenticationToken(username,password);
135 // Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
137 // authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
138 // sc.setAuthentication(authentication);
140 // SecurityContextHolder.setContext(sc);
141 // CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
146 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
147 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
150 protected static ICdmRemoteSource
getCdmRemoteSource() {
151 return cdmRemoteSource
;
154 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
155 return remotePersistentSource
;
158 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
159 return cdmEntitySessionManager
;
163 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
164 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
165 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
166 return ownerSessionMap
.get(sessionOwner
);
169 protected static CdmEntitySession
getActiveSession() {
170 return ((InheritableThreadLocal
<CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "tlActiveSession")).get();
173 protected static ConversationalTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
174 return (ConversationalTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
178 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
179 Class
<?
> clazz
= object
.getClass();
181 Field field
= clazz
.getDeclaredField(fieldName
);
182 field
.setAccessible(true);
183 return field
.get(object
);
184 } catch (NoSuchFieldException e
) {
186 } catch (SecurityException e
) {
188 } catch (IllegalArgumentException e
) {
190 } catch (IllegalAccessException e
) {
197 protected static void authenticateDefaultUser() {
198 authenticate(user
, password
);