Project

General

Profile

Download (6.22 KB) Statistics
| Branch: | Tag: | Revision:
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

    
14
import org.junit.AfterClass;
15
import org.junit.Assert;
16
import org.junit.BeforeClass;
17
import org.junit.Test;
18
import org.springframework.core.io.ClassPathResource;
19
import org.springframework.core.io.Resource;
20
import org.unitils.UnitilsJUnit4;
21
import org.unitils.database.DatabaseUnitils;
22
import org.unitils.database.annotations.Transactional;
23
import org.unitils.database.util.TransactionMode;
24
import org.unitils.spring.annotation.SpringApplicationContext;
25

    
26
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
27
import eu.etaxonomy.cdm.api.cache.CdmClientCacheException;
28
import eu.etaxonomy.cdm.api.cache.CdmTransientEntityCacher;
29
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
30
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
31
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
32
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
33
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
34
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
35
import eu.etaxonomy.taxeditor.session.ISessionEventListener;
36

    
37

    
38
/**
39
 * Base class for remoting tests, responsible for
40
 * - starting / stop the cdm server
41
 * - running some basic connection tests
42
 * - setting up the remote configuration.
43
 *
44
 */
45
@Transactional(TransactionMode.DISABLED)
46
@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
47
public class BaseRemotingTest extends UnitilsJUnit4 {
48
    //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
49

    
50
    private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE =
51
            new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
52

    
53
    private static CdmApplicationRemoteController remoteApplicationController;
54
    private static ICdmRemoteSource cdmRemoteSource;
55
    private static CdmPersistentRemoteSource remotePersistentSource;
56

    
57
    public static boolean useManagedServer = true;
58

    
59
    public static final Resource CDMLIB_DISK_STORE_RESOURCE =
60
            new ClassPathResource("/eu/etaxonomy/cache");
61
    
62
    protected static ICdmEntitySessionManager cdmEntitySessionManager;
63
    
64
    @BeforeClass
65
    public static void initializeBaseRemotingTest() {
66
        //NOTE: Run this the cdmTest H2 DB whenever it needs to be
67
    	//      recreated e.g. after a model change
68
    	//DatabaseUnitils.disableConstraints();
69

    
70
		try {
71
			System.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE.getFile().getAbsolutePath());
72
		} catch (IOException e) {
73
			throw new CdmClientCacheException(e);
74
		}
75
        
76
        useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
77
        if(useManagedServer) {
78
            try {
79
                CDMServer.getInstance().start();
80
            } catch (Exception e) {
81
                e.printStackTrace();
82
                Assert.fail("Server failed to start. Reason : " + e.getMessage());
83
            }
84

    
85
        initializeController(CDMServer.getInstance().getName(),
86
                CDMServer.getInstance().getHost(),
87
                CDMServer.getInstance().getPort(),
88
                CDMServer.getInstance().getContextPath(),
89
                NomenclaturalCode.ICNAFP);
90
        }
91
    }
92

    
93
    
94
    @Test
95
    public void disableConstraints() {
96
    	// To be run on the src/test/resources/h2/cdmTest h2 db after
97
    	// updating the unitils.properties 'database.url' property
98
    	DatabaseUnitils.disableConstraints();
99
    }
100
    public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode) {
101

    
102
        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
103
        remoteApplicationController =
104
                CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
105
                        false,
106
                        null,
107
                        null);
108

    
109
        try {
110
            remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
111
        } catch (CdmRemoteSourceException e) {
112
            Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
113
        }
114
        cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
115
    }
116

    
117

    
118

    
119
    protected static CdmApplicationRemoteController getRemoteApplicationController() {
120
        return remoteApplicationController;
121
    }
122

    
123
    protected static ICdmRemoteSource getCdmRemoteSource() {
124
        return cdmRemoteSource;
125
    }
126

    
127
    protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
128
        return remotePersistentSource;
129
    }
130
    
131
    protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
132
    	return cdmEntitySessionManager;
133
    }
134
    
135
    protected static CdmTransientEntityCacher getCacher(ISessionEventListener sessionOwner) {
136
    	return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager.getSession(sessionOwner), "cdmTransientEntityCacher");
137
    }
138

    
139
    @AfterClass
140
    public static void cleanup() {
141
        try {
142
            CDMServer.getInstance().stop();
143
        } catch (Exception e) {
144
            Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
145
        }
146
    }
147

    
148

    
149
    protected static Object getFieldValueViaReflection(Object object, String fieldName) {
150
        Class<?> clazz = object.getClass();
151
        try {
152
            Field field = clazz.getDeclaredField(fieldName);
153
            field.setAccessible(true);
154
            return field.get(object);
155
        } catch (NoSuchFieldException e) {
156
            e.printStackTrace();
157
        } catch (SecurityException e) {
158
            e.printStackTrace();
159
        } catch (IllegalArgumentException e) {
160
            e.printStackTrace();
161
        } catch (IllegalAccessException e) {
162
            e.printStackTrace();
163
        }
164
        return null;
165
    }
166

    
167

    
168

    
169

    
170
}
(1-1/7)