Project

General

Profile

Download (8.64 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.test.function;
10

    
11
import java.util.UUID;
12

    
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15
import org.springframework.transaction.TransactionStatus;
16

    
17
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
18
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
19
import eu.etaxonomy.cdm.config.AccountStore;
20
import eu.etaxonomy.cdm.database.CdmDataSource;
21
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
22
import eu.etaxonomy.cdm.database.DbSchemaValidation;
23
import eu.etaxonomy.cdm.database.ICdmDataSource;
24
import eu.etaxonomy.cdm.database.update.CdmUpdater;
25
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
26
import eu.etaxonomy.cdm.model.description.TemporalData;
27

    
28
/**
29
 * This class is meant for functional testing of model changes. It is not meant
30
 * for running in maven.
31
 *
32
 * For testing
33
 *
34
 * 1. First run with CREATE first against H2, than MySQL, PostGreSQL, (SQLServer)
35
 * 2. Save old schema databases
36
 * 3. Run with VALIDATE
37
 *
38
 * @author a.mueller
39
 * @since 22.05.2015
40
 * @see CdmUpdater
41
 */
42
public class TestModelUpdate {
43

    
44
    @SuppressWarnings("unused")
45
	private static final Logger logger = LogManager.getLogger(TestModelUpdate.class);
46

    
47
	private void testSelectedDb(){
48
		DbSchemaValidation schema = DbSchemaValidation.VALIDATE;
49

    
50
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
51
		String database = (schema == DbSchemaValidation.VALIDATE  ? "cdm529" : "cdm532");
52
//		database = "cdm_test1";
53

    
54
		CdmDataSource dataSource = getDatasource(dbType, database);
55
 		try {
56
// 		    int n = dataSource.executeUpdate("UPDATE CdmMetaData SET value = '3.1.0.0.201607300000' WHERE propertyname = 0 ");
57
			CdmUpdater updater = new CdmUpdater();
58
			if (schema == DbSchemaValidation.VALIDATE){
59
				SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource,
60
				        DefaultProgressMonitor.NewInstance());
61
				String report = result.createReport().toString();
62
				System.out.println(report);
63
			}
64
		} catch (Exception e) {
65
			e.printStackTrace();
66
		}
67
 		try{
68
    		CdmApplicationController appCtr = CdmApplicationController.NewInstance(dataSource, schema);
69

    
70
    //		Classification classification = Classification.NewInstance("Me");
71
    //		Taxon taxon = Taxon.NewInstance(null, null);
72
    //		Person person = Person.NewInstance();
73
    //		TaxonNode node = classification.addChildTaxon(taxon, null, null);
74
    //		DefinedTerm lastScrutiny = (DefinedTerm)appCtr.getTermService().find(DefinedTerm.uuidLastScrutiny);
75
    //		TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
76
    //      appCtr.getClassificationService().save(classification);
77

    
78
    		if (schema == DbSchemaValidation.CREATE){
79
    		    System.out.println("fillData");
80
    		    appCtr.getCommonService().createFullSampleData();
81
    		    appCtr.getNameService().list(null, null, null, null, null);
82
    		    TransactionStatus tx = appCtr.startTransaction(false);
83
    		    TemporalData td = (TemporalData)appCtr.getDescriptionElementService().find(
84
    		            UUID.fromString("9a1c91c0-fc58-4310-94cb-8c26115985d3"));
85
    		    td.getFeature().setSupportsCategoricalData(true);
86
    		    appCtr.getTermService().saveOrUpdate(td.getFeature());
87
    		    System.out.println(td.getPeriod());
88
                appCtr.commitTransaction(tx);
89
    		}
90

    
91
    		appCtr.close();
92
 		}catch (Exception e) {
93
 		    e.printStackTrace();
94
 		}
95
 		System.out.println("Ready");
96
	}
97

    
98
    private CdmDataSource getDatasource(DatabaseTypeEnum dbType, String database) {
99
        String server = "localhost";
100
        String username = "edit";
101
        String serverSql = "130.133.70.26";
102
//        server = "160.45.63.175";
103

    
104
        if (dbType == DatabaseTypeEnum.MySQL){
105
            return CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
106
        }else if (dbType == DatabaseTypeEnum.H2){
107
            //H2
108
            String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_" + database;
109
//            String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_xyz";
110
            username = "sa";
111
            CdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", username, "", path);
112
            return dataSource;
113
        }else if (dbType == DatabaseTypeEnum.SqlServer2005){
114
            server = serverSql;
115
            username = "cdmupdater";
116
            CdmDataSource dataSource = CdmDataSource.NewSqlServer2005Instance(server, database, 1433, username, AccountStore.readOrStorePassword(server, database, username, null));
117
            return dataSource;
118
        }else if (dbType == DatabaseTypeEnum.PostgreSQL){
119
            server = serverSql;
120
            username = "postgres";
121
            CdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(server, database, 5432, username,  AccountStore.readOrStorePassword(server, database, username, null));
122
            return dataSource;
123
        }else{
124
            throw new IllegalArgumentException("dbType not supported:" + dbType);
125
        }
126
    }
127

    
128
	/**
129
	 * Updates the H2 test database in remote web-app.
130
	 * Requires that the local path to the database is adapted
131
	 */
132
	private void updateRemoteWebappTestH2(){
133
	    System.out.println("Update remoteWebappTestH2");
134
	    String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdmlib2\\cdmlib-remote-webapp\\";
135
	    updateH2(pathToProject);
136
	}
137

    
138
    /**
139
     * Updates the H2 test database in TaxEditor.
140
     * Requires that the local path to the database is adapted
141
     */
142
    private void updateTaxEditorH2(){
143
        System.out.println("Update TaxEditorTestH2");
144
        String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\taxeditor2\\eu.etaxonomy.taxeditor.test\\";
145
        updateH2(pathToProject);
146
    }
147

    
148
    /**
149
     * Updates the H2 test database in CDM vaadin.
150
     * Requires that the local path to the database is adapted
151
     */
152
    private void updateVaadinH2(){
153
        System.out.println("Update VaadinTestH2");
154
        String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdm-vaadin2\\";
155
        updateH2(pathToProject);
156
    }
157

    
158
    private void updateH2(String pathToProject) {
159
        String pathInProject = "src\\test\\resources\\h2";
160

    
161
	    String path = pathToProject + pathInProject;
162
		ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", "sa", "", path);
163

    
164
 		try {
165
			CdmUpdater updater = new CdmUpdater();
166
			SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
167
			System.out.println(result.createReport());
168
		} catch (Exception e) {
169
			e.printStackTrace();
170
		}
171

    
172
		//CdmPersistentDataSource.save(dataSource.getName(), dataSource);
173
		CdmApplicationController appCtr;
174
		appCtr = CdmApplicationController.NewInstance(dataSource,DbSchemaValidation.VALIDATE);
175
		appCtr.close();
176
		System.out.println("\nEnd Datasource");
177
    }
178

    
179
    @SuppressWarnings("unused")  //enable only if needed
180
    private void updateEdaphobasePostgres(){
181
       String serverSql = "130.133.70.26";
182
       String database = "cdm_edaphobase";
183
       int port = 5433;
184
       String username = "edaphobase";
185
       String password = AccountStore.readOrStorePassword(database, serverSql, username, null);
186

    
187
       ICdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(serverSql,
188
                database, port, username, password);
189
        try {
190
            CdmUpdater updater = new CdmUpdater();
191
            SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
192
            System.out.println(result.createReport());
193
        } catch (Exception e) {
194
            e.printStackTrace();
195
        }
196

    
197
        //CdmPersistentDataSource.save(dataSource.getName(), dataSource);
198
        CdmApplicationController appCtr;
199
        appCtr = CdmApplicationController.NewInstance(dataSource,DbSchemaValidation.VALIDATE);
200
        appCtr.close();
201
        System.out.println("\nEnd Datasource");
202
    }
203

    
204
	private void test(){
205
		System.out.println("Start TestModelUpdate");
206
		testSelectedDb();
207

    
208
//		updateRemoteWebappTestH2();
209
//		updateAllTestH2();
210
//		updateEdaphobasePostgres();
211

    
212
		System.out.println("\nEnd Datasource");
213
	}
214

    
215
	/**
216
     * Updates all H2 test DBs
217
     */
218
    private void updateAllTestH2() {
219
        updateRemoteWebappTestH2();
220
        updateTaxEditorH2();
221
        updateVaadinH2();
222
    }
223

    
224
	public static void  main(String[] args) {
225
	    TestModelUpdate cc = new TestModelUpdate();
226
		cc.test();
227
    	System.exit(0);
228
	}
229
}
(4-4/6)