editorApplicationContext : reverted stand-alone app context conf
[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 public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode) {
103
104 cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
105 remoteApplicationController =
106 CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
107 false,
108 null,
109 null);
110
111 try {
112 remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
113 } catch (CdmRemoteSourceException e) {
114 Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
115 }
116 cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
117 }
118
119
120
121 protected static CdmApplicationRemoteController getRemoteApplicationController() {
122 return remoteApplicationController;
123 }
124
125 protected static ICdmRemoteSource getCdmRemoteSource() {
126 return cdmRemoteSource;
127 }
128
129 protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
130 return remotePersistentSource;
131 }
132
133 protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
134 return cdmEntitySessionManager;
135 }
136
137
138 protected static CdmEntitySession getSession(ISessionEventListener sessionOwner) {
139 Map<ISessionEventListener, CdmEntitySession> ownerSessionMap =
140 (Map<ISessionEventListener, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
141 return ownerSessionMap.get(sessionOwner);
142 }
143
144 protected static CdmTransientEntityCacher getActiveSession() {
145 return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
146 }
147
148 protected static CdmTransientEntityCacher getCacher(ISessionEventListener sessionOwner) {
149 return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
150 }
151
152 @AfterClass
153 public static void cleanup() {
154 try {
155 CDMServer.getInstance().stop();
156 } catch (Exception e) {
157 Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
158 }
159 }
160
161
162 protected static Object getFieldValueViaReflection(Object object, String fieldName) {
163 Class<?> clazz = object.getClass();
164 try {
165 Field field = clazz.getDeclaredField(fieldName);
166 field.setAccessible(true);
167 return field.get(object);
168 } catch (NoSuchFieldException e) {
169 e.printStackTrace();
170 } catch (SecurityException e) {
171 e.printStackTrace();
172 } catch (IllegalArgumentException e) {
173 e.printStackTrace();
174 } catch (IllegalAccessException e) {
175 e.printStackTrace();
176 }
177 return null;
178 }
179
180
181
182
183 }