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
.Assert
;
18 import org
.junit
.BeforeClass
;
19 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
20 import org
.springframework
.security
.core
.Authentication
;
21 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
22 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
23 import org
.unitils
.database
.DatabaseUnitils
;
24 import org
.unitils
.database
.annotations
.Transactional
;
25 import org
.unitils
.database
.util
.TransactionMode
;
26 import org
.unitils
.spring
.annotation
.SpringApplicationContext
;
28 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
29 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
30 import eu
.etaxonomy
.cdm
.api
.application
.CdmDataChangeService
;
31 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
32 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
33 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
34 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmPersistentRemoteSource
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSource
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
38 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
39 import eu
.etaxonomy
.taxeditor
.session
.CdmEntitySession
;
40 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
41 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
45 * Base class for remoting tests, responsible for
46 * - starting / stop the cdm server
47 * - running some basic connection tests
48 * - setting up the remote configuration.
51 @Transactional(TransactionMode
.DISABLED
)
52 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
53 public abstract class BaseRemotingTest
extends ThreadedTest
{
54 private static final Logger logger
= Logger
.getLogger(BaseRemotingTest
.class);
56 private static ICdmRemoteSource cdmRemoteSource
;
57 private static CdmPersistentRemoteSource remotePersistentSource
;
58 protected static ICdmEntitySessionManager cdmEntitySessionManager
;
61 public static void initializeBaseRemotingTest() {
63 DatabaseUnitils
.disableConstraints();
66 initializeController(sourceName
,
70 NomenclaturalCode
.ICNAFP
,
73 } catch (Exception e
) {
75 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
81 public static void emptyAllCachesExceptModelCache() {
82 CacheManager cm
= CacheManager
.create();
83 String
[] cacheNames
= CacheManager
.create().getCacheNames();
84 for(String cacheName
: cacheNames
) {
85 if(!cacheName
.equals(CdmRemoteCacheManager
.CDM_MODEL_CACHE_NAME
)) {
86 cm
.removeCache(cacheName
);
92 public static void initializeController(String sourceName
, String host
, int port
, String contextPath
, NomenclaturalCode ncode
, String username
, String password
) {
94 if(CdmApplicationState
.getCurrentAppConfig() != null) {
97 cdmRemoteSource
= CdmRemoteSource
.NewInstance(sourceName
, host
, port
, contextPath
, ncode
);
98 CdmApplicationRemoteController remoteApplicationController
=
99 CdmApplicationRemoteController
.NewInstance(cdmRemoteSource
,
102 CdmApplicationState
.setCurrentAppConfig(remoteApplicationController
);
103 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
105 remotePersistentSource
= CdmPersistentRemoteSource
.NewInstance(sourceName
);
106 } catch (CdmRemoteSourceException e
) {
107 Assert
.fail("Default Remote Persistent Source failed to load. Reason : " + e
.getMessage());
109 cdmEntitySessionManager
= getRemoteApplicationController().getCdmEntitySessionManager();
111 CdmApplicationState
.setCurrentDataChangeService(new CdmDataChangeService());
113 authenticate(username
, password
);
117 protected static void authenticate(String username
, String password
) {
119 //FIXME:Remoting the authentication code should be replaced by a method call which actually
120 // does the authentication in the editor code so that the 'real' authentication can be tested
121 SecurityContextHolder
.clearContext();
122 SecurityContextImpl sc
= new SecurityContextImpl();
123 Authentication token
= new UsernamePasswordAuthenticationToken(username
,password
);
124 Authentication authentication
= getRemoteApplicationController().getAuthenticationManager().authenticate(token
);
125 authentication
= new UsernamePasswordAuthenticationToken(username
,password
, authentication
.getAuthorities());
126 sc
.setAuthentication(authentication
);
128 SecurityContextHolder
.setContext(sc
);
129 CdmApplicationState
.setCurrentSecurityContext(SecurityContextHolder
.getContext());
134 protected static CdmApplicationRemoteController
getRemoteApplicationController() {
135 return (CdmApplicationRemoteController
) CdmApplicationState
.getCurrentAppConfig();
138 protected static ICdmRemoteSource
getCdmRemoteSource() {
139 return cdmRemoteSource
;
142 protected static CdmPersistentRemoteSource
getCdmPersistentRemoteSource() {
143 return remotePersistentSource
;
146 protected static ICdmEntitySessionManager
getCdmEntitySessionManager() {
147 return cdmEntitySessionManager
;
151 protected static CdmEntitySession
getSession(ICdmEntitySessionEnabled sessionOwner
) {
152 Map
<ICdmEntitySessionEnabled
, CdmEntitySession
> ownerSessionMap
=
153 (Map
<ICdmEntitySessionEnabled
, CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "ownerSessionMap");
154 return ownerSessionMap
.get(sessionOwner
);
157 protected static CdmEntitySession
getActiveSession() {
158 return ((InheritableThreadLocal
<CdmEntitySession
>) getFieldValueViaReflection(cdmEntitySessionManager
, "tlActiveSession")).get();
161 protected static CdmTransientEntityCacher
getCacher(ICdmEntitySessionEnabled sessionOwner
) {
162 return (CdmTransientEntityCacher
) getFieldValueViaReflection(getSession(sessionOwner
), "cdmTransientEntityCacher");
166 protected static Object
getFieldValueViaReflection(Object object
, String fieldName
) {
167 Class
<?
> clazz
= object
.getClass();
169 Field field
= clazz
.getDeclaredField(fieldName
);
170 field
.setAccessible(true);
171 return field
.get(object
);
172 } catch (NoSuchFieldException e
) {
174 } catch (SecurityException e
) {
176 } catch (IllegalArgumentException e
) {
178 } catch (IllegalAccessException e
) {
185 protected static void authenticateDefaultUser() {
186 authenticate(user
, password
);