merge from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / BaseRemotingTest.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.httpinvoker;
10
11 import java.io.IOException;
12 import java.lang.reflect.Field;
13 import java.util.Map;
14
15 import org.junit.AfterClass;
16 import org.junit.Assert;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.springframework.core.io.ClassPathResource;
20 import org.springframework.core.io.Resource;
21 import org.unitils.UnitilsJUnit4;
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;
26
27 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
28 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
29 import eu.etaxonomy.taxeditor.remoting.cache.CdmClientCacheException;
30 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
31 import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
32 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
33 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
34 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
35 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
36 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
37 import eu.etaxonomy.taxeditor.session.ISessionEventListener;
38
39
40 /**
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.
45 *
46 */
47 @Transactional(TransactionMode.DISABLED)
48 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
49 public class BaseRemotingTest extends UnitilsJUnit4 {
50 //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
51
52 private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE =
53 new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
54
55 private static CdmApplicationRemoteController remoteApplicationController;
56 private static ICdmRemoteSource cdmRemoteSource;
57 private static CdmPersistentRemoteSource remotePersistentSource;
58
59 public static boolean useManagedServer = true;
60
61 public static final Resource CDMLIB_DISK_STORE_RESOURCE =
62 new ClassPathResource("/eu/etaxonomy/cache");
63
64 protected static ICdmEntitySessionManager cdmEntitySessionManager;
65
66 @BeforeClass
67 public static void initializeBaseRemotingTest() {
68 //NOTE: Run this the cdmTest H2 DB whenever it needs to be
69 // recreated e.g. after a model change
70 //DatabaseUnitils.disableConstraints();
71
72 try {
73 System.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE.getFile().getAbsolutePath());
74 } catch (IOException e) {
75 throw new CdmClientCacheException(e);
76 }
77
78 useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
79 if(useManagedServer) {
80 try {
81 CDMServer.getInstance().start();
82 } catch (Exception e) {
83 e.printStackTrace();
84 Assert.fail("Server failed to start. Reason : " + e.getMessage());
85 }
86
87 initializeController(CDMServer.getInstance().getName(),
88 CDMServer.getInstance().getHost(),
89 CDMServer.getInstance().getPort(),
90 CDMServer.getInstance().getContextPath(),
91 NomenclaturalCode.ICNAFP);
92 }
93 }
94
95
96 @Test
97 public void disableConstraints() {
98 // To be run on the src/test/resources/h2/cdmTest h2 db after
99 // updating the unitils.properties 'database.url' property
100 DatabaseUnitils.disableConstraints();
101 }
102
103 public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode) {
104
105 cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
106 remoteApplicationController =
107 CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
108 false,
109 null,
110 null);
111
112 try {
113 remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
114 } catch (CdmRemoteSourceException e) {
115 Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
116 }
117 cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
118 }
119
120
121
122 protected static CdmApplicationRemoteController getRemoteApplicationController() {
123 return remoteApplicationController;
124 }
125
126 protected static ICdmRemoteSource getCdmRemoteSource() {
127 return cdmRemoteSource;
128 }
129
130 protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
131 return remotePersistentSource;
132 }
133
134 protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
135 return cdmEntitySessionManager;
136 }
137
138
139 protected static CdmEntitySession getSession(ISessionEventListener sessionOwner) {
140 Map<ISessionEventListener, CdmEntitySession> ownerSessionMap =
141 (Map<ISessionEventListener, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
142 return ownerSessionMap.get(sessionOwner);
143 }
144
145 protected static CdmTransientEntityCacher getActiveSession() {
146 return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
147 }
148
149 protected static CdmTransientEntityCacher getCacher(ISessionEventListener sessionOwner) {
150 return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
151 }
152
153 @AfterClass
154 public static void cleanup() {
155 try {
156 CDMServer.getInstance().stop();
157 } catch (Exception e) {
158 Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
159 }
160 }
161
162
163 protected static Object getFieldValueViaReflection(Object object, String fieldName) {
164 Class<?> clazz = object.getClass();
165 try {
166 Field field = clazz.getDeclaredField(fieldName);
167 field.setAccessible(true);
168 return field.get(object);
169 } catch (NoSuchFieldException e) {
170 e.printStackTrace();
171 } catch (SecurityException e) {
172 e.printStackTrace();
173 } catch (IllegalArgumentException e) {
174 e.printStackTrace();
175 } catch (IllegalAccessException e) {
176 e.printStackTrace();
177 }
178 return null;
179 }
180
181
182
183
184 }