1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package eu.clarin.cmd.toolkit; |
---|
6 | |
---|
7 | import java.io.File; |
---|
8 | import java.io.IOException; |
---|
9 | import java.nio.file.Paths; |
---|
10 | import java.util.HashMap; |
---|
11 | import java.util.List; |
---|
12 | import java.util.Map; |
---|
13 | import javax.xml.XMLConstants; |
---|
14 | import javax.xml.parsers.DocumentBuilder; |
---|
15 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
16 | import javax.xml.transform.Source; |
---|
17 | import javax.xml.transform.dom.DOMSource; |
---|
18 | import javax.xml.transform.stream.StreamSource; |
---|
19 | import javax.xml.validation.Schema; |
---|
20 | import javax.xml.validation.SchemaFactory; |
---|
21 | import javax.xml.validation.Validator; |
---|
22 | import net.sf.saxon.s9api.DOMDestination; |
---|
23 | import net.sf.saxon.s9api.QName; |
---|
24 | import net.sf.saxon.s9api.XPathCompiler; |
---|
25 | import net.sf.saxon.s9api.XPathExecutable; |
---|
26 | import net.sf.saxon.s9api.XPathSelector; |
---|
27 | import net.sf.saxon.s9api.XdmAtomicValue; |
---|
28 | import net.sf.saxon.s9api.XdmDestination; |
---|
29 | import net.sf.saxon.s9api.XdmNode; |
---|
30 | import net.sf.saxon.s9api.XdmValue; |
---|
31 | import net.sf.saxon.s9api.XsltTransformer; |
---|
32 | import nl.mpi.tla.schemanon.Message; |
---|
33 | import nl.mpi.tla.schemanon.SaxonUtils; |
---|
34 | import nl.mpi.tla.schemanon.SchemAnon; |
---|
35 | import org.junit.*; |
---|
36 | |
---|
37 | import static org.junit.Assert.*; |
---|
38 | import org.w3c.dom.Document; |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | * @author menwin |
---|
43 | */ |
---|
44 | public class TestCMDToolkit { |
---|
45 | |
---|
46 | XsltTransformer upgradeCMDSpec = null; |
---|
47 | XsltTransformer upgradeCMDRec = null; |
---|
48 | XsltTransformer transformCMDSpecInXSD = null; |
---|
49 | SchemAnon validateCMDSpec = null; |
---|
50 | SchemAnon validateCMDEnvelop = null; |
---|
51 | |
---|
52 | @Before |
---|
53 | public void setUp() { |
---|
54 | try { |
---|
55 | upgradeCMDSpec = SaxonUtils.buildTransformer(CMDToolkit.class.getResource("/toolkit/upgrade/cmd-component-1_1-to-1_2.xsl")).load(); |
---|
56 | upgradeCMDRec = SaxonUtils.buildTransformer(CMDToolkit.class.getResource("/toolkit/upgrade/cmd-record-1_1-to-1_2.xsl")).load(); |
---|
57 | transformCMDSpecInXSD = SaxonUtils.buildTransformer(CMDToolkit.class.getResource("/toolkit/xslt/comp2schema.xsl")).load(); |
---|
58 | validateCMDSpec = new SchemAnon(CMDToolkit.class.getResource("/toolkit/xsd/cmd-component.xsd").toURI().toURL()); |
---|
59 | validateCMDEnvelop = new SchemAnon(CMDToolkit.class.getResource("/toolkit/xsd/cmd-envelop.xsd").toURI().toURL()); |
---|
60 | } catch(Exception e) { |
---|
61 | System.err.println("!ERR: couldn't setup the testing environment!"); |
---|
62 | System.err.println(""+e); |
---|
63 | e.printStackTrace(System.err); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | @After |
---|
68 | public void tearDown() { |
---|
69 | } |
---|
70 | |
---|
71 | protected Document transform(XsltTransformer trans,Source src) throws Exception { |
---|
72 | try { |
---|
73 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
74 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
75 | Document doc = builder.newDocument(); |
---|
76 | DOMDestination dest = new DOMDestination(doc); |
---|
77 | trans.setSource(src); |
---|
78 | trans.setDestination(dest); |
---|
79 | // always set cmd-toolkit to the current working directory, which is expected to be where pom.xml lives |
---|
80 | trans.setParameter(new QName("cmd-toolkit"),new XdmAtomicValue(Paths.get("").toAbsolutePath().toString()+"/src/main/resources/toolkit")); |
---|
81 | trans.transform(); |
---|
82 | return doc; |
---|
83 | } catch (Exception e) { |
---|
84 | System.out.println("!ERR: failed transform: "+e); |
---|
85 | e.printStackTrace(System.out); |
---|
86 | throw e; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | protected boolean xpath(Document doc,String xpath) throws Exception { |
---|
91 | XPathCompiler xpc = SaxonUtils.getProcessor().newXPathCompiler(); |
---|
92 | xpc.declareNamespace("cmd","http://www.clarin.eu/cmd/1"); |
---|
93 | XPathExecutable xpe = xpc.compile(xpath); |
---|
94 | XPathSelector xps = xpe.load(); |
---|
95 | xps.setContextItem(SaxonUtils.getProcessor().newDocumentBuilder().wrap(doc)); |
---|
96 | return xps.effectiveBooleanValue(); |
---|
97 | } |
---|
98 | |
---|
99 | protected void printMessages(SchemAnon anon) throws Exception { |
---|
100 | for (Message msg : anon.getMessages()) { |
---|
101 | System.out.println("" + (msg.isError() ? "ERROR" : "WARNING") + (msg.getLocation() != null ? " at " + msg.getLocation() : "")); |
---|
102 | System.out.println(" " + msg.getText()); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | protected int countErrors(SchemAnon anon) throws Exception { |
---|
107 | int cnt = 0; |
---|
108 | for (Message msg : anon.getMessages()) |
---|
109 | cnt += (msg.isError()?1:0); |
---|
110 | return cnt; |
---|
111 | } |
---|
112 | |
---|
113 | protected Document upgradeCMDSpec(String spec) throws Exception { |
---|
114 | System.out.println("Upgrade CMD spec["+spec+"]"); |
---|
115 | return transform(upgradeCMDSpec,new javax.xml.transform.stream.StreamSource(new java.io.File(TestCMDToolkit.class.getResource(spec).toURI()))); |
---|
116 | } |
---|
117 | |
---|
118 | protected Document upgradeCMDRecord(String rec) throws Exception { |
---|
119 | System.out.println("Upgrade CMD spec["+rec+"]"); |
---|
120 | return transform(upgradeCMDRec,new javax.xml.transform.stream.StreamSource(new java.io.File(TestCMDToolkit.class.getResource(rec).toURI()))); |
---|
121 | } |
---|
122 | |
---|
123 | protected Document transformCMDSpecInXSD(String spec,Source src) throws Exception { |
---|
124 | System.out.println("Transform CMD spec["+spec+"] into XSD"); |
---|
125 | return transform(transformCMDSpecInXSD,src); |
---|
126 | } |
---|
127 | |
---|
128 | protected Document transformCMDSpecInXSD(String spec) throws Exception { |
---|
129 | return transformCMDSpecInXSD(spec,new javax.xml.transform.stream.StreamSource(new java.io.File(TestCMDToolkit.class.getResource(spec).toURI()))); |
---|
130 | } |
---|
131 | |
---|
132 | protected boolean validateCMDSpec(String spec,Source src) throws Exception { |
---|
133 | System.out.println("Validate CMD spec["+spec+"]"); |
---|
134 | boolean res = validateCMDSpec.validate(src); |
---|
135 | System.out.println("CMD spec["+spec+"]: "+(res?"VALID":"INVALID")); |
---|
136 | printMessages(validateCMDSpec); |
---|
137 | return res; |
---|
138 | } |
---|
139 | |
---|
140 | protected boolean validateCMDSpec(String spec) throws Exception { |
---|
141 | return validateCMDSpec(spec,new StreamSource(new java.io.File(TestCMDToolkit.class.getResource(spec).toURI()))); |
---|
142 | } |
---|
143 | |
---|
144 | protected boolean validateCMDRecord(String spec,SchemAnon anon,String rec,Source src) throws Exception { |
---|
145 | System.out.println("Validate CMD record["+rec+"] against spec["+spec+"]"); |
---|
146 | boolean res = anon.validate(src); |
---|
147 | System.out.println("CMD record["+rec+"]: "+(res?"VALID":"INVALID")); |
---|
148 | printMessages(anon); |
---|
149 | return res; |
---|
150 | } |
---|
151 | |
---|
152 | protected boolean validateCMDEnvelop(String rec,Source src) throws Exception { |
---|
153 | System.out.println("Validate envelop CMD rec["+rec+"]"); |
---|
154 | boolean res = validateCMDEnvelop.validate(src); |
---|
155 | System.out.println("CMD rec["+rec+"] evelop: "+(res?"VALID":"INVALID")); |
---|
156 | printMessages(validateCMDEnvelop); |
---|
157 | return res; |
---|
158 | } |
---|
159 | |
---|
160 | protected boolean validateCMDEnvelop(String rec) throws Exception { |
---|
161 | return validateCMDEnvelop(rec,new StreamSource(new java.io.File(TestCMDToolkit.class.getResource(rec).toURI()))); |
---|
162 | } |
---|
163 | |
---|
164 | @Test |
---|
165 | public void testAdelheid() throws Exception { |
---|
166 | String profile = "/toolkit/Adelheid/profiles/clarin.eu:cr1:p_1311927752306.xml"; |
---|
167 | String record = "/toolkit/Adelheid/records/Adelheid.cmdi"; |
---|
168 | |
---|
169 | // upgrade the profile from 1.1 to 1.2 |
---|
170 | Document upgradedProfile = upgradeCMDSpec(profile); |
---|
171 | |
---|
172 | // validate the 1.2 profile |
---|
173 | boolean validProfile = validateCMDSpec(profile+" (upgraded)",new DOMSource(upgradedProfile)); |
---|
174 | |
---|
175 | // assertions |
---|
176 | // the upgraded profile should be a valid CMDI 1.2 profile |
---|
177 | assertTrue(validProfile); |
---|
178 | // so there should be no errors |
---|
179 | assertEquals(0, countErrors(validateCMDSpec)); |
---|
180 | |
---|
181 | // transform the 1.2 profile into a XSD |
---|
182 | Document profileSchema = transformCMDSpecInXSD(profile+" (upgraded)",new DOMSource(upgradedProfile)); |
---|
183 | SchemAnon profileAnon = new SchemAnon(new DOMSource(profileSchema)); |
---|
184 | |
---|
185 | // upgrade the record from 1.1 to 1.2 |
---|
186 | Document upgradedRecord = upgradeCMDRecord(record); |
---|
187 | |
---|
188 | // validate the 1.2 record |
---|
189 | boolean validRecord = validateCMDRecord(profile+" (upgraded)",profileAnon,record+" (upgraded)",new DOMSource(upgradedRecord)); |
---|
190 | |
---|
191 | // assertions |
---|
192 | // the upgraded record should be a valid CMDI 1.2 record |
---|
193 | assertTrue(validRecord); |
---|
194 | // so there should be no errors |
---|
195 | assertEquals(0, countErrors(profileAnon)); |
---|
196 | } |
---|
197 | |
---|
198 | @Test |
---|
199 | public void testTEI() throws Exception { |
---|
200 | String record = "/toolkit/TEI/records/sundhed_dsn.teiHeader.ref.xml"; |
---|
201 | |
---|
202 | // upgrade the record from 1.1 to 1.2 |
---|
203 | Document upgradedRecord = upgradeCMDRecord(record); |
---|
204 | |
---|
205 | // assertions |
---|
206 | // the @ref attributes on the elements should stay as they are |
---|
207 | assertTrue(xpath(upgradedRecord,"//*:author/*:name/@ref")); |
---|
208 | } |
---|
209 | |
---|
210 | @Test |
---|
211 | public void testSuccessor() throws Exception { |
---|
212 | String validRecord = "/toolkit/successor/profiles/successor-valid.xml"; |
---|
213 | String invalidRecord = "/toolkit/successor/profiles/successor-invalid.xml"; |
---|
214 | |
---|
215 | // assertions |
---|
216 | assertTrue(validateCMDSpec(validRecord)); |
---|
217 | assertFalse(validateCMDSpec(invalidRecord)); |
---|
218 | } |
---|
219 | |
---|
220 | @Test |
---|
221 | public void testOLAC() throws Exception { |
---|
222 | String profile = "/toolkit/OLAC/profiles/OLAC-DcmiTerms.xml"; |
---|
223 | String record = "/toolkit/OLAC/records/org_rosettaproject-record.xml"; |
---|
224 | |
---|
225 | // upgrade the profile from 1.1 to 1.2 |
---|
226 | Document upgradedProfile = upgradeCMDSpec(profile); |
---|
227 | |
---|
228 | // validate the 1.2 profile |
---|
229 | boolean validProfile = validateCMDSpec(profile+" (upgraded)",new DOMSource(upgradedProfile)); |
---|
230 | |
---|
231 | // assertions |
---|
232 | // the upgraded profile should be a valid CMDI 1.2 profile |
---|
233 | assertTrue(validProfile); |
---|
234 | // so there should be no errors |
---|
235 | assertEquals(0, countErrors(validateCMDSpec)); |
---|
236 | |
---|
237 | // transform the 1.2 profile into a XSD |
---|
238 | Document profileSchema = transformCMDSpecInXSD(profile+" (upgraded)",new DOMSource(upgradedProfile)); |
---|
239 | SchemAnon profileAnon = new SchemAnon(new DOMSource(profileSchema)); |
---|
240 | |
---|
241 | // upgrade the record from 1.1 to 1.2 |
---|
242 | Document upgradedRecord = upgradeCMDRecord(record); |
---|
243 | |
---|
244 | // validate the 1.2 record |
---|
245 | boolean validRecord = validateCMDRecord(profile+" (upgraded)",profileAnon,record+" (upgraded)",new DOMSource(upgradedRecord)); |
---|
246 | |
---|
247 | // assertions |
---|
248 | assertFalse(validRecord); |
---|
249 | } |
---|
250 | } |
---|