updated CDMServer and corresponding test files to use dbunit datasets with the possib...
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / 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.lang.reflect.Field;
12
13 import org.apache.log4j.Level;
14 import org.apache.log4j.Logger;
15 import org.junit.AfterClass;
16 import org.junit.Assert;
17 import org.junit.BeforeClass;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.springframework.core.io.ClassPathResource;
21 import org.springframework.core.io.Resource;
22 import org.unitils.UnitilsJUnit4;
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;
27
28 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
29 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
30 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
31 import eu.etaxonomy.cdm.remote.CdmPersistentRemoteSource;
32 import eu.etaxonomy.cdm.remote.CdmRemoteSource;
33 import eu.etaxonomy.cdm.remote.CdmRemoteSourceException;
34 import eu.etaxonomy.cdm.remote.ICdmRemoteSource;
35
36
37 /**
38 * Base class for remoting tests, responsible for
39 * - starting / stop the cdm server
40 * - running some basic connection tests
41 * - setting up the remote configuration.
42 *
43 */
44 @Transactional(TransactionMode.DISABLED)
45 @SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
46 public class BaseRemotingTest extends UnitilsJUnit4 {
47 //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
48
49 private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE =
50 new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
51
52 private static ICdmApplicationConfiguration remoteApplicationController;
53 private static ICdmRemoteSource cdmRemoteSource;
54 private static CdmPersistentRemoteSource remotePersistentSource;
55
56 public static boolean useManagedServer = false;
57
58 @BeforeClass
59 public static void initializeBaseRemotingTest() {
60 //Logger.getRootLogger().setLevel(Level.INFO);
61 useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
62 if(useManagedServer) {
63 try {
64 CDMServer.getInstance().start();
65 } catch (Exception e) {
66 e.printStackTrace();
67 Assert.fail("Server failed to start. Reason : " + e.getMessage());
68 }
69
70 initializeController(CDMServer.getInstance().getName(),
71 CDMServer.getInstance().getHost(),
72 CDMServer.getInstance().getPort(),
73 CDMServer.getInstance().getContextPath(),
74 NomenclaturalCode.ICNAFP);
75 }
76 }
77
78
79 public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode) {
80
81 cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
82 remoteApplicationController =
83 CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
84 false,
85 null,
86 null);
87
88 try {
89 remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
90 } catch (CdmRemoteSourceException e) {
91 Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
92 }
93
94 }
95
96
97
98 protected static ICdmApplicationConfiguration getRemoteApplicationController() {
99 return remoteApplicationController;
100 }
101
102 protected static ICdmRemoteSource getCdmRemoteSource() {
103 return cdmRemoteSource;
104 }
105
106 protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
107 return remotePersistentSource;
108 }
109
110 @AfterClass
111 public static void cleanup() {
112 try {
113 CDMServer.getInstance().stop();
114 } catch (Exception e) {
115 Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
116 }
117 }
118
119
120 protected static Object getFieldValueViaReflection(Object object, String fieldName) {
121 Class<?> clazz = object.getClass();
122 try {
123 Field field = clazz.getDeclaredField(fieldName);
124 field.setAccessible(true);
125 return field.get(object);
126 } catch (NoSuchFieldException e) {
127 e.printStackTrace();
128 } catch (SecurityException e) {
129 e.printStackTrace();
130 } catch (IllegalArgumentException e) {
131 e.printStackTrace();
132 } catch (IllegalAccessException e) {
133 e.printStackTrace();
134 }
135 return null;
136 }
137
138
139
140
141 }