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
|
|
10
|
package eu.etaxonomy.cdm.test.function;
|
11
|
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
17
|
import eu.etaxonomy.cdm.common.AccountStore;
|
18
|
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
|
19
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
20
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
21
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
22
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
23
|
import eu.etaxonomy.cdm.database.update.CdmUpdater;
|
24
|
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
|
25
|
import eu.etaxonomy.cdm.model.media.Media;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
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
|
*
|
39
|
* @author a.mueller
|
40
|
* @since 22.05.2015
|
41
|
* @see CdmUpdater
|
42
|
*/
|
43
|
public class TestModelUpdate {
|
44
|
@SuppressWarnings("unused")
|
45
|
private static final Logger logger = Logger.getLogger(TestModelUpdate.class);
|
46
|
|
47
|
|
48
|
private void testSelectedDb(){
|
49
|
DbSchemaValidation schema = DbSchemaValidation.VALIDATE;
|
50
|
|
51
|
DatabaseTypeEnum dbType = DatabaseTypeEnum.H2;
|
52
|
|
53
|
|
54
|
String database = (schema == DbSchemaValidation.VALIDATE ? "cdm47" : "cdm50");
|
55
|
CdmDataSource dataSource = getDatasource(dbType, database);
|
56
|
|
57
|
|
58
|
try {
|
59
|
// int n = dataSource.executeUpdate("UPDATE CdmMetaData SET value = '3.1.0.0.201607300000' WHERE propertyname = 0 ");
|
60
|
CdmUpdater updater = new CdmUpdater();
|
61
|
if (schema == DbSchemaValidation.VALIDATE){
|
62
|
SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource,
|
63
|
DefaultProgressMonitor.NewInstance());
|
64
|
String report = result.createReport().toString();
|
65
|
System.out.println(report);
|
66
|
}
|
67
|
} catch (Exception e) {
|
68
|
e.printStackTrace();
|
69
|
}
|
70
|
try{
|
71
|
CdmApplicationController appCtr = CdmApplicationController.NewInstance(dataSource, schema);
|
72
|
|
73
|
// Classification classification = Classification.NewInstance("Me");
|
74
|
// Taxon taxon = Taxon.NewInstance(null, null);
|
75
|
// Person person = Person.NewInstance();
|
76
|
// TaxonNode node = classification.addChildTaxon(taxon, null, null);
|
77
|
// DefinedTerm lastScrutiny = (DefinedTerm)appCtr.getTermService().find(DefinedTerm.uuidLastScrutiny);
|
78
|
// TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
|
79
|
// appCtr.getClassificationService().save(classification);
|
80
|
|
81
|
if (schema == DbSchemaValidation.CREATE){
|
82
|
System.out.println("fillData");
|
83
|
appCtr.getCommonService().createFullSampleData();
|
84
|
appCtr.getNameService().list(null, null, null, null, null);
|
85
|
}
|
86
|
List<Media> medias = appCtr.getMediaService().list(null, null, null, null, null);
|
87
|
for (Media media: medias){
|
88
|
if (media.getMediaCreated() != null){
|
89
|
System.out.println(media.getMediaCreated().toString());
|
90
|
}
|
91
|
}
|
92
|
List<Reference> references = appCtr.getReferenceService().list(null, null, null, null, null);
|
93
|
for (Reference reference: references){
|
94
|
if (reference.getDatePublished() != null){
|
95
|
System.out.println(reference.getDatePublished().toString());
|
96
|
}
|
97
|
}
|
98
|
|
99
|
|
100
|
appCtr.close();
|
101
|
}catch (Exception e) {
|
102
|
e.printStackTrace();
|
103
|
}
|
104
|
System.out.println("Ready");
|
105
|
System.exit(0);
|
106
|
}
|
107
|
|
108
|
|
109
|
|
110
|
/**
|
111
|
* @param dbType
|
112
|
* @param database
|
113
|
* @return
|
114
|
*/
|
115
|
private CdmDataSource getDatasource(DatabaseTypeEnum dbType, String database) {
|
116
|
String server = "localhost";
|
117
|
String username = "edit";
|
118
|
String serverSql = "130.133.70.26";
|
119
|
|
120
|
if (dbType == DatabaseTypeEnum.MySQL){
|
121
|
return CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
|
122
|
}else if (dbType == DatabaseTypeEnum.H2){
|
123
|
//H2
|
124
|
String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_" + database;
|
125
|
// String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_xyz";
|
126
|
username = "sa";
|
127
|
CdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", username, "", path);
|
128
|
return dataSource;
|
129
|
}else if (dbType == DatabaseTypeEnum.SqlServer2005){
|
130
|
server = serverSql;
|
131
|
username = "cdmupdater";
|
132
|
CdmDataSource dataSource = CdmDataSource.NewSqlServer2005Instance(server, database, 1433, username, AccountStore.readOrStorePassword(server, database, username, null));
|
133
|
return dataSource;
|
134
|
}else if (dbType == DatabaseTypeEnum.PostgreSQL){
|
135
|
server = serverSql;
|
136
|
username = "postgres";
|
137
|
CdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(server, database, 5432, username, AccountStore.readOrStorePassword(server, database, username, null));
|
138
|
return dataSource;
|
139
|
}else{
|
140
|
throw new IllegalArgumentException("dbType not supported:" + dbType);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
|
145
|
|
146
|
/**
|
147
|
* Updates the H2 test database in remote web-app.
|
148
|
* Requires that the local path to the database is adapted
|
149
|
*/
|
150
|
@SuppressWarnings("unused") //enable only if needed
|
151
|
private void updateRemoteWebappTestH2(){
|
152
|
String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdmlib\\cdmlib-remote-webapp\\";
|
153
|
// String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdm-vaadin\\";
|
154
|
// String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\taxeditor\\eu.etaxonomy.taxeditor.test\\";
|
155
|
|
156
|
String pathInProject = "src\\test\\resources\\h2";
|
157
|
|
158
|
String path = pathToProject + pathInProject;
|
159
|
ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", "sa", "", path);
|
160
|
|
161
|
|
162
|
try {
|
163
|
CdmUpdater updater = new CdmUpdater();
|
164
|
SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
|
165
|
System.out.println(result.createReport());
|
166
|
} catch (Exception e) {
|
167
|
e.printStackTrace();
|
168
|
}
|
169
|
|
170
|
//CdmPersistentDataSource.save(dataSource.getName(), dataSource);
|
171
|
CdmApplicationController appCtr;
|
172
|
appCtr = CdmApplicationController.NewInstance(dataSource,DbSchemaValidation.VALIDATE);
|
173
|
appCtr.close();
|
174
|
System.out.println("\nEnd Datasource");
|
175
|
System.exit(0);
|
176
|
}
|
177
|
|
178
|
|
179
|
private void test(){
|
180
|
System.out.println("Start Datasource");
|
181
|
testSelectedDb();
|
182
|
|
183
|
// updateRemoteWebappTestH2(); //also updates vaadin and taxedtior model
|
184
|
|
185
|
System.out.println("\nEnd Datasource");
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* @param args
|
190
|
*/
|
191
|
public static void main(String[] args) {
|
192
|
TestModelUpdate cc = new TestModelUpdate();
|
193
|
cc.test();
|
194
|
}
|
195
|
|
196
|
}
|