Project

General

Profile

Download (4.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.config.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.model.metadata.CdmMetaData;
28

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

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

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

    
52
// ******************** TESTS ****************************************************/
53

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

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

    
78
	private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){
79
		//establish connection
80
		pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd);
81
		ICdmDataSource destination;
82
		if(dbType.equals(DatabaseTypeEnum.MySQL)){
83
			destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
84
		} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){
85
			destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
86
		} else {
87
			//TODO others
88
			throw new RuntimeException("Unsupported DatabaseType");
89
		}
90
		return destination;
91
	}
92

    
93
	@Test
94
	public void testRecursiveCallSchemaUpdater(){
95
		CdmUpdater updater = new CdmUpdater();
96
		ISchemaUpdater currentUpdater = null;
97
		try {
98
			Method method =  CdmUpdater.class.getDeclaredMethod("getCurrentSchemaUpdater");
99
			method.setAccessible(true);
100

    
101
			currentUpdater = (ISchemaUpdater)method.invoke(updater);
102
		} catch (Exception e) {
103
			Assert.fail("CdmUpdater.getCurrentSchemaUpdater not found:" + e.getMessage());
104
			return;
105
		}
106
		ISchemaUpdater lastUpdater = null;
107
		ISchemaUpdater tmpUpdater = currentUpdater;
108

    
109
		int i = 0;
110
		//get very first schema updater available (= SchemaUpdater_24_25) by recursive call to getPreviousUpdater
111
		while (tmpUpdater.getPreviousUpdater() != null && i++<1000){
112
			tmpUpdater = tmpUpdater.getPreviousUpdater();
113
			lastUpdater = tmpUpdater;
114
		}
115
		Assert.assertNotNull("Current Updater must not be null", currentUpdater);
116
		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());
117

    
118
		i = 0;
119
		while (tmpUpdater.getNextUpdater() != null && i++<1000){
120
			tmpUpdater = tmpUpdater.getNextUpdater();
121
			lastUpdater = tmpUpdater;
122
		}
123
		Assert.assertEquals("Current schema updater not found by recursive call firstUpdater.getNextUpdater()", currentUpdater.getClass(), lastUpdater.getClass());
124

    
125
		//test correct schema version string
126
		Assert.assertEquals(CdmMetaData.getDbSchemaVersion(), currentUpdater.getTargetVersion());
127
	}
128

    
129
}
    (1-1/1)