Project

General

Profile

Download (6.32 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.cdm.database.update;
10

    
11

    
12
import java.lang.reflect.Method;
13
import java.sql.SQLException;
14

    
15
import org.apache.log4j.Logger;
16
import org.junit.Assert;
17
import org.junit.Before;
18
import org.junit.BeforeClass;
19
import org.junit.Ignore;
20
import org.junit.Test;
21

    
22
import eu.etaxonomy.cdm.common.AccountStore;
23
import eu.etaxonomy.cdm.database.CdmDataSource;
24
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
25
import eu.etaxonomy.cdm.database.ICdmDataSource;
26
import eu.etaxonomy.cdm.database.update.v24_25.SchemaUpdater_24_25;
27
import eu.etaxonomy.cdm.database.update.v24_25.TermUpdater_24_25;
28
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
29

    
30
/**
31
 * @author a.mueller
32
 * @date 14.09.2010
33
 *
34
 */
35
public class CdmUpdaterTest {
36
	@SuppressWarnings("unused")
37
	private static final Logger logger = Logger.getLogger(CdmUpdaterTest.class);
38

    
39
	/**
40
	 * @throws java.lang.Exception
41
	 */
42
	@BeforeClass
43
	public static void setUpBeforeClass() throws Exception {
44
	}
45

    
46
	/**
47
	 * @throws java.lang.Exception
48
	 */
49
	@Before
50
	public void setUp() throws Exception {
51
	}
52

    
53
// ******************** TESTS ****************************************************/
54

    
55
	@Ignore
56
	@Test
57
	public void testUpdateToCurrentVersion() {
58
		CdmUpdater cdmUpdater = new CdmUpdater();
59
		ICdmDataSource datasource = cdm_test_algaterra();
60
		try {
61
			boolean connectionAvailable = datasource.testConnection();
62
			Assert.assertTrue("Testdatabase is not available", connectionAvailable);
63
		} catch (ClassNotFoundException e) {
64
			Assert.fail();
65
		} catch (SQLException e) {
66
			Assert.fail();
67
		}
68
		cdmUpdater.updateToCurrentVersion(datasource, null);
69
	}
70

    
71
	private static ICdmDataSource cdm_test_andreasM(){
72
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
73
		String cdmServer = "192.168.2.10";
74
		String cdmDB = "cdm_test_andreasM";
75
		String cdmUserName = "edit";
76
		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
77
	}
78

    
79
	private static ICdmDataSource cdm_test_algaterra(){
80
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
81
		String cdmServer = "160.45.63.201";
82
		String cdmDB = "cdm_edit_algaterra";
83
		String cdmUserName = "edit";
84
		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
85
	}
86

    
87
//	private static ICdmDataSource cdm_import(){
88
//		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
89
//		String cdmServer = "160.45.63.151";
90
//		String cdmDB = "cdm_production_campanulaceae";
91
//		String cdmUserName = "edit";
92
//		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
93
//	}
94

    
95
	private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){
96
		//establish connection
97
		pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd);
98
		ICdmDataSource destination;
99
		if(dbType.equals(DatabaseTypeEnum.MySQL)){
100
			destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
101
		} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){
102
			destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
103
		} else {
104
			//TODO others
105
			throw new RuntimeException("Unsupported DatabaseType");
106
		}
107
		return destination;
108
	}
109

    
110
	@Test
111
	public void testRecursiveCallSchemaUpdater(){
112
		CdmUpdater updater = new CdmUpdater();
113
		ISchemaUpdater currentUpdater = null;
114
		try {
115
			Method method =  CdmUpdater.class.getDeclaredMethod("getCurrentSchemaUpdater");
116
			method.setAccessible(true);
117

    
118
			currentUpdater = (ISchemaUpdater)method.invoke(updater);
119
		} catch (Exception e) {
120
			Assert.fail("CdmUpdater.getCurrentSchemaUpdater not found:" + e.getMessage());;
121
		}
122
		ISchemaUpdater lastUpdater = null;
123
		ISchemaUpdater tmpUpdater = currentUpdater;
124

    
125
		int i = 0;
126
		//get very first schema updater available (= SchemaUpdater_24_25) by recursive call to getPreviousUpdater
127
		while (tmpUpdater.getPreviousUpdater() != null && i++<1000){
128
			tmpUpdater = tmpUpdater.getPreviousUpdater();
129
			lastUpdater = tmpUpdater;
130
		}
131
		Assert.assertNotNull("Current Updater must not be null", currentUpdater);
132
		Assert.assertEquals("Very first schema updater must be schemaUpdater_24_25. Something seems to be wrong in recursive call of getPreviousSchemaUpdater", SchemaUpdater_24_25.class, lastUpdater.getClass());
133

    
134
		i = 0;
135
		while (tmpUpdater.getNextUpdater() != null && i++<1000){
136
			tmpUpdater = tmpUpdater.getNextUpdater();
137
			lastUpdater = tmpUpdater;
138
		}
139
		Assert.assertEquals("Current schema updater not found by recursive call firstUpdater.getNextUpdater()", currentUpdater.getClass(), lastUpdater.getClass());
140

    
141
		//test correct schema version string
142
		Assert.assertEquals(CdmMetaData.getDbSchemaVersion(), currentUpdater.getTargetVersion());
143
	}
144

    
145
	@Test
146
	public void testRecursiveCallTermUpdater(){
147
		CdmUpdater updater = new CdmUpdater();
148
		ITermUpdater currentUpdater = null;
149
		try {
150
			Method method =  CdmUpdater.class.getDeclaredMethod("getCurrentTermUpdater");
151
			method.setAccessible(true);
152
			currentUpdater = (ITermUpdater)method.invoke(updater);
153
		} catch (Exception e) {
154
			Assert.fail("CdmUpdater.getCurrentTermUpdater not found:" + e.getMessage());;
155
		}
156
		Assert.assertNotNull("Current Updater must not be null", currentUpdater);
157
		ITermUpdater lastUpdater = null;
158
		ITermUpdater tmpUpdater = currentUpdater;
159
		int i = 0;
160
		//get very first term updater available (= TermUpdater_24_25) by recursive call to getPreviousUpdater
161
		while (tmpUpdater.getPreviousUpdater() != null && i++<1000){
162
			tmpUpdater = tmpUpdater.getPreviousUpdater();
163
			lastUpdater = tmpUpdater;
164
		}
165

    
166
		Assert.assertEquals(TermUpdater_24_25.class, lastUpdater.getClass());
167

    
168
		i = 0;
169
		while (tmpUpdater.getNextUpdater() != null && i++<1000){
170
			tmpUpdater = tmpUpdater.getNextUpdater();
171
			lastUpdater = tmpUpdater;
172
		}
173
		Assert.assertEquals("Current term updater not found by recursive call firstUpdater.getNextUpdater()", currentUpdater.getClass(), lastUpdater.getClass());
174

    
175
		//test correct schema version string
176
		Assert.assertEquals(CdmMetaData.getTermsVersion(), currentUpdater.getTargetVersion());
177
	}
178

    
179
}
    (1-1/1)