1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.test;
|
11
|
|
|
12
|
|
import java.io.File;
|
13
|
|
import java.io.FileReader;
|
14
|
|
import java.net.URL;
|
15
|
|
import java.util.Iterator;
|
16
|
|
import java.util.List;
|
17
|
|
import java.util.Set;
|
18
|
|
|
19
|
|
import org.jbind.util.other.FileUtil;
|
20
|
|
import org.jbind.xml.core.cmp.ISchema;
|
21
|
|
import org.jbind.xml.facade.JBindFacade;
|
22
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
23
|
|
import org.jbind.xml.msg.XmlException;
|
24
|
|
import org.jbind.xml.msg.XmlMessages;
|
25
|
|
import org.jbind.xml.schema.cmp.Schemas;
|
26
|
|
import org.jbind.xml.schema.compiler.Compiler;
|
27
|
|
import org.jbind.xml.schema.compiler.ICompiler;
|
28
|
|
import org.jbind.xml.schema.compiler.IFileInfo;
|
29
|
|
import org.jbind.xml.schema.instantiation.ISchemaReader;
|
30
|
|
|
31
|
|
public class SchemaAndCodeTest extends XmlTest {
|
32
|
|
|
33
|
|
private String myScenarioName = null;
|
34
|
|
private File myScenarioDir = null;
|
35
|
|
private File myGenSrcRootDir = null;
|
36
|
|
private File myTmpSrcRootDir = null;
|
37
|
|
private File myBehaviourSrcRootDir = null;
|
38
|
|
private DescriptionInfo myInfo = null;
|
39
|
|
private boolean myCreateOnly;
|
40
|
|
|
41
|
24
|
public SchemaAndCodeTest(String aScenarioName, File aScenarioDir, File aGenSrcRootDir, File aBehaviourSrcRootDir, File aTmpSrcRootDir, DescriptionInfo anInfo, boolean aCreateOnly) {
|
42
|
24
|
super(aScenarioName + "/" + anInfo.schema, aScenarioName, aScenarioDir, anInfo);
|
43
|
24
|
myScenarioName = aScenarioName;
|
44
|
24
|
myScenarioDir = aScenarioDir;
|
45
|
24
|
myGenSrcRootDir = aGenSrcRootDir;
|
46
|
24
|
myBehaviourSrcRootDir = aBehaviourSrcRootDir;
|
47
|
24
|
myTmpSrcRootDir = aTmpSrcRootDir;
|
48
|
24
|
myInfo = anInfo;
|
49
|
24
|
myCreateOnly = aCreateOnly;
|
50
|
|
}
|
51
|
|
|
52
|
24
|
public void runTest() throws Exception {
|
53
|
24
|
IConstraintViolations violations = XmlMessages.constraintViolations();
|
54
|
|
|
55
|
|
//
|
56
|
|
// Instantiate the schema
|
57
|
|
//
|
58
|
24
|
File schemaFile = new File(myScenarioDir, myInfo.schema);
|
59
|
24
|
URL schemaUrl = schemaFile.toURL();
|
60
|
|
|
61
|
24
|
ISchemaReader reader = JBindFacade.createSchemaReader();
|
62
|
|
|
63
|
24
|
try {
|
64
|
|
|
65
|
24
|
ISchema schema = reader.readSchema(schemaUrl, true, myScenarioName);
|
66
|
|
|
67
|
24
|
if (violations.isEmpty()) {
|
68
|
|
//
|
69
|
|
// Compile the schema
|
70
|
|
//
|
71
|
24
|
ICompiler compiler = new Compiler();
|
72
|
24
|
List fileInfos = compiler.compileSchema(schema, violations);
|
73
|
|
|
74
|
24
|
if (violations.isEmpty()) {
|
75
|
24
|
if (myCreateOnly) {
|
76
|
0
|
for (Iterator i = fileInfos.iterator(); i.hasNext(); ) {
|
77
|
0
|
IFileInfo fileInfo = (IFileInfo)i.next();
|
78
|
0
|
File dir = fileInfo.overwrite() ? myTmpSrcRootDir : myBehaviourSrcRootDir;
|
79
|
0
|
fileInfo.output(dir, violations);
|
80
|
|
}
|
81
|
|
|
82
|
0
|
if (violations.isEmpty()) {
|
83
|
0
|
return;
|
84
|
|
} else {
|
85
|
0
|
assertTrue(violations.toString(), false);
|
86
|
|
}
|
87
|
|
|
88
|
|
}
|
89
|
|
|
90
|
|
//
|
91
|
|
// Check the generated files
|
92
|
|
//
|
93
|
24
|
File genSrcDir = new File(myGenSrcRootDir, myScenarioName);
|
94
|
24
|
Set srcFiles = FileUtil.collectJavaFiles(genSrcDir);
|
95
|
24
|
StringBuffer unknownSrcFiles = new StringBuffer();
|
96
|
24
|
StringBuffer differences = new StringBuffer();
|
97
|
24
|
for (Iterator i = fileInfos.iterator(); i.hasNext(); ) {
|
98
|
405
|
IFileInfo fileInfo = (IFileInfo)i.next();
|
99
|
405
|
if (!fileInfo.overwrite()) {
|
100
|
8
|
fileInfo.output(myBehaviourSrcRootDir, violations);
|
101
|
|
} else {
|
102
|
397
|
File srcFile = fileInfo.getDestinationFile(myGenSrcRootDir);
|
103
|
397
|
if (!srcFiles.contains(srcFile)) {
|
104
|
0
|
if (unknownSrcFiles.length() > 0) {
|
105
|
0
|
unknownSrcFiles.append("\n");
|
106
|
|
}
|
107
|
0
|
unknownSrcFiles.append("\t").append(srcFile);
|
108
|
0
|
fileInfo.output(myTmpSrcRootDir, violations);
|
109
|
|
} else {
|
110
|
397
|
srcFiles.remove(srcFile);
|
111
|
397
|
FileReader fileReader = new FileReader(srcFile);
|
112
|
397
|
if (!fileInfo.isUpToDate(fileReader)) {
|
113
|
0
|
if (differences.length() > 0) {
|
114
|
0
|
differences.append("\n");
|
115
|
|
}
|
116
|
0
|
differences.append("\t").append(srcFile);
|
117
|
0
|
fileInfo.output(myTmpSrcRootDir, violations);
|
118
|
|
}
|
119
|
|
}
|
120
|
|
}
|
121
|
|
}
|
122
|
|
|
123
|
24
|
StringBuffer missingSrcFiles = new StringBuffer();
|
124
|
24
|
for (Iterator i = srcFiles.iterator(); i.hasNext(); ) {
|
125
|
0
|
if (missingSrcFiles.length() > 0) {
|
126
|
0
|
missingSrcFiles.append("\n");
|
127
|
|
}
|
128
|
0
|
missingSrcFiles.append("\t").append(i.next());
|
129
|
|
}
|
130
|
24
|
StringBuffer msg = new StringBuffer();
|
131
|
24
|
if (unknownSrcFiles.length() > 0) {
|
132
|
0
|
msg.append("unknown source files:\n").append(unknownSrcFiles).append("\n");
|
133
|
|
}
|
134
|
24
|
if (differences.length() > 0) {
|
135
|
0
|
msg.append("differences in files:\n").append(differences).append("\n");
|
136
|
|
}
|
137
|
24
|
if (missingSrcFiles.length() > 0) {
|
138
|
0
|
msg.append("missing source files:\n").append(missingSrcFiles).append("\n");
|
139
|
|
}
|
140
|
|
|
141
|
24
|
assertTrue("\n" + msg.toString(), msg.length() == 0);
|
142
|
|
|
143
|
|
}
|
144
|
|
|
145
|
|
|
146
|
|
}
|
147
|
|
|
148
|
24
|
if (violations.isEmpty()) {
|
149
|
24
|
Schemas.getInstance().reset();
|
150
|
24
|
schema = reader.readSchema(schemaUrl, false, myScenarioName);// uses generated classes
|
151
|
24
|
Schemas.getInstance().setSchema(schema);
|
152
|
|
}
|
153
|
|
|
154
|
|
} catch (XmlException e) {
|
155
|
0
|
violations.add(e.getXmlMessage());
|
156
|
|
}
|
157
|
|
|
158
|
24
|
checkErrorMessages(violations);
|
159
|
|
|
160
|
|
}
|
161
|
|
|
162
|
|
}
|
163
|
|
|