PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/parser/SchemaMappingTest.java

#
Java | 584 lines | 385 code | 130 blank | 69 comment | 0 complexity | 51c428cab531438e840d8b42054b5cd1 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * SchemaMappingTest.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2009 Eric Le Lay
  6. *
  7. * The XML plugin is licensed under the GNU General Public License, with
  8. * the following exception:
  9. *
  10. * "Permission is granted to link this code with software released under
  11. * the Apache license version 1.1, for example used by the Xerces XML
  12. * parser package."
  13. */
  14. package xml.parser;
  15. // {{{ jUnit imports
  16. import java.util.concurrent.TimeUnit;
  17. import org.junit.*;
  18. import static org.junit.Assert.*;
  19. import org.fest.swing.fixture.*;
  20. import org.fest.swing.core.*;
  21. import org.fest.swing.finder.*;
  22. import org.fest.swing.edt.*;
  23. import org.fest.swing.timing.*;
  24. import static org.fest.assertions.Assertions.*;
  25. import org.gjt.sp.jedit.testframework.Log;
  26. import org.gjt.sp.jedit.testframework.TestUtils;
  27. import static xml.XMLTestUtils.*;
  28. // }}}
  29. import java.io.*;
  30. import java.net.*;
  31. import org.xml.sax.SAXException;
  32. import org.xml.sax.SAXParseException;
  33. import org.xml.sax.helpers.DefaultHandler;
  34. import static xml.parser.SchemaMapping.*;
  35. /**
  36. * $Id: SchemaMappingTest.java 21326 2012-03-11 11:37:15Z kerik-sf $
  37. */
  38. public class SchemaMappingTest{
  39. private static File testData;
  40. @BeforeClass
  41. public static void setUpjEdit() throws IOException{
  42. TestUtils.beforeClass();
  43. testData = new File(System.getProperty("test_data")).getCanonicalFile();
  44. assertTrue(testData.exists());
  45. }
  46. @AfterClass
  47. public static void tearDownjEdit() {
  48. TestUtils.afterClass();
  49. }
  50. @Test
  51. public void testEmptyMapping(){
  52. SchemaMapping m = new SchemaMapping();
  53. assertNull(m.getBaseURI());
  54. assertNull(m.getSchemaForDocument(null, "actions.xml",
  55. null,null,"ACTIONS",true));
  56. }
  57. @Test
  58. public void testBuiltInSchema() throws URISyntaxException, IllegalArgumentException, IOException, SAXException{
  59. URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
  60. SchemaMapping m = SchemaMapping.fromDocument(builtin.toString(), new DefaultHandler());
  61. assertNotNull(m);
  62. // 2 built-in rules
  63. assertEquals(new Result(builtin,"locate.rng"),m.getSchemaForDocument(null, "schemas.xml",
  64. null,null,"locatingRules",true));
  65. assertEquals(new Result(builtin,"locate.rng"),m.getSchemaForDocument(null, "schemas.xml",
  66. null,null,"a",true));
  67. // non-matching rule
  68. assertNull(m.getSchemaForDocument(null, "actions.xml",
  69. null,null,"ACTIONS",true));
  70. }
  71. @Test
  72. public void testAddRule() throws MalformedURLException, URISyntaxException{
  73. SchemaMapping m = new SchemaMapping();
  74. // test taken into account
  75. m.addRule(new DocumentElementRule(null,null, "ACTIONS", "actions.rng", false));
  76. assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
  77. null,null,"ACTIONS",true));
  78. m.addRule(new URIResourceRule(new URI("file:///"),"actions.xml", "else.rng", false));
  79. // test taken into account
  80. assertEquals(new Result(new URI("file:///"),"else.rng"),m.getSchemaForDocument(null, "actions.xml",
  81. null,null,"TOTO",true));
  82. // test precedence ( both match, but first one wins)
  83. assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
  84. null,null,"ACTIONS",true));
  85. }
  86. @Test
  87. public void testAddTypeId(){
  88. SchemaMapping m = new SchemaMapping();
  89. // test undefined typeId
  90. m.addRule(new DocumentElementRule(null,null, "ACTIONS", "actions", true));
  91. assertEquals(null,m.getSchemaForDocument(null, "actions.xml",
  92. null,null,"ACTIONS",true));
  93. m.addTypeId("actions","actions.rng",false);
  94. // test taken into account
  95. assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
  96. null,null,"ACTIONS",true));
  97. m.addTypeId("actions", "else.rng",false);
  98. // test precedence ( the first one wins)
  99. assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
  100. null,null,"ACTIONS",true));
  101. }
  102. @Test
  103. public void testDefaultRule(){
  104. Rule r;
  105. try{
  106. new DefaultRule(null,null,false);
  107. fail("should throw an exception");
  108. }catch(RuntimeException e){
  109. //fine
  110. }
  111. try{
  112. new DefaultRule(null,"",false);
  113. fail("should throw an exception");
  114. }catch(RuntimeException e){
  115. //fine
  116. }
  117. r = new DefaultRule(null,"target",false);
  118. assertTrue(r.matchNamespace("urn:ns"));
  119. assertTrue(r.matchURL("test.txt"));
  120. assertTrue(r.matchDoctype("book"));
  121. assertTrue(r.matchDocumentElement(null,"test"));
  122. assertTrue(r.matchDocumentElement("t","test"));
  123. }
  124. @Test
  125. public void testDocumentElementRule(){
  126. Rule r;
  127. try{
  128. new DocumentElementRule(null,null,null,"target",false);
  129. fail("should throw an exception");
  130. }catch(RuntimeException e){
  131. //fine
  132. }
  133. try{
  134. new DocumentElementRule(null,null,"","target",false);
  135. fail("should throw an exception");
  136. }catch(RuntimeException e){
  137. //fine
  138. }
  139. r = new DocumentElementRule(null,null,"test","target",false);
  140. assertFalse(r.matchNamespace("urn:ns"));
  141. assertFalse(r.matchURL("test.txt"));
  142. assertFalse(r.matchDoctype("book"));
  143. // no prefix
  144. assertTrue(r.matchDocumentElement(null,"test"));
  145. // with prefix
  146. assertTrue(r.matchDocumentElement("t","test"));
  147. // no match
  148. assertFalse(r.matchDocumentElement(null,"test2"));
  149. r = new DocumentElementRule(null, "t","test","target",false);
  150. // no prefix
  151. assertFalse(r.matchDocumentElement(null,"test"));
  152. // with prefix
  153. assertTrue(r.matchDocumentElement("t","test"));
  154. // no match
  155. assertFalse(r.matchDocumentElement("tt","test"));
  156. }
  157. @Test
  158. public void testDoctypeRule(){
  159. Rule r;
  160. try{
  161. new DoctypeRule(null, null,"target",false);
  162. fail("should throw an exception");
  163. }catch(RuntimeException e){
  164. //fine
  165. }
  166. try{
  167. new DoctypeRule(null,"","target",false);
  168. fail("should throw an exception");
  169. }catch(RuntimeException e){
  170. //fine
  171. }
  172. r = new DoctypeRule(null,"test","target",false);
  173. assertFalse(r.matchNamespace("urn:ns"));
  174. assertFalse(r.matchURL("test.txt"));
  175. assertFalse(r.matchDocumentElement(null,"test"));
  176. assertTrue(r.matchDoctype("test"));
  177. assertFalse(r.matchDoctype("test2"));
  178. }
  179. @Test
  180. public void testNamespaceRule(){
  181. Rule r;
  182. try{
  183. new NamespaceRule(null,null,"target",false);
  184. fail("should throw an exception");
  185. }catch(RuntimeException e){
  186. //fine
  187. }
  188. r = new NamespaceRule(null,"urn:ns","target",false);
  189. assertFalse(r.matchDoctype("test"));
  190. assertFalse(r.matchURL("test.txt"));
  191. assertFalse(r.matchDocumentElement(null,"test"));
  192. assertTrue(r.matchNamespace("urn:ns"));
  193. assertFalse(r.matchNamespace("urn:ns2"));
  194. // default namespace
  195. r = new NamespaceRule(null,"","target",false);
  196. assertTrue(r.matchNamespace(""));
  197. assertFalse(r.matchNamespace("urn:ns2"));
  198. }
  199. @Test
  200. public void testURIPatternRule(){
  201. Rule r;
  202. try{
  203. new URIPatternRule(null,null,"target",false);
  204. fail("should throw an exception");
  205. }catch(RuntimeException e){
  206. //fine
  207. }
  208. r = new URIPatternRule(null,"test.txt","target",false);
  209. assertFalse(r.matchDoctype("test"));
  210. assertFalse(r.matchDocumentElement(null,"test"));
  211. assertFalse(r.matchNamespace("urn:ns"));
  212. assertTrue(r.matchURL("test.txt"));
  213. assertFalse(r.matchURL("test2.txt"));
  214. r = new URIPatternRule(null,"*.txt","target",false);
  215. assertTrue(r.matchURL("test.txt"));
  216. assertFalse(r.matchURL("test.txt.2"));
  217. //relative/absolute uri :
  218. r = new URIPatternRule(null,"*.txt","target",false);
  219. assertTrue(r.matchURL("test/test.txt"));
  220. assertTrue(r.matchURL("file:///c:/test.txt"));
  221. r = new URIPatternRule(null,"*/A.txt","target",false);
  222. assertTrue(r.matchURL("test/A.txt"));
  223. assertTrue(r.matchURL("file:///c:/A.txt"));
  224. assertFalse(r.matchURL("A.txt"));
  225. //opaque URI
  226. r = new URIPatternRule(null,"*jedit*","target",false);
  227. assertTrue(r.matchURL("mailto:jedit-devel@sourceforge.net"));
  228. assertFalse(r.matchURL("mailto:test:test"));
  229. }
  230. @Test
  231. public void testURIResourceRule() throws MalformedURLException, URISyntaxException{
  232. Rule r;
  233. try{
  234. new URIResourceRule(null,null,"target",false);
  235. fail("should throw an exception");
  236. }catch(RuntimeException e){
  237. //fine
  238. }
  239. try{
  240. new URIResourceRule(null,"test","target",false);
  241. fail("should throw an exception");
  242. }catch(RuntimeException e){
  243. //fine
  244. }
  245. // relative url
  246. try{
  247. new URIResourceRule(null, "relative","target",false);
  248. fail("should throw an exception");
  249. }catch(IllegalArgumentException iae){
  250. //fine
  251. }
  252. //illegal URI
  253. try{
  254. new URIResourceRule(null, "\\//","target",false);
  255. fail("should throw an exception");
  256. }catch(IllegalArgumentException iae){
  257. //fine
  258. }
  259. r = new URIResourceRule(null,"file:///test.txt","target",false);
  260. assertFalse(r.matchDoctype("test"));
  261. assertFalse(r.matchDocumentElement(null,"test"));
  262. assertFalse(r.matchNamespace("urn:ns"));
  263. // absolute uri as pattern
  264. assertTrue(r.matchURL("file:///test.txt"));
  265. assertTrue(r.matchURL("file:///A/../test.txt")); // same path
  266. // relative uri as pattern
  267. r = new URIResourceRule(new URI("file:///A/schemas.xml"),"test.txt","target",false);
  268. assertTrue(r.matchURL("file:///A/test.txt"));
  269. assertFalse(r.matchURL("file:///test.txt"));
  270. //relative URI to match
  271. assertTrue(r.matchURL("test.txt"));
  272. }
  273. @Test
  274. public void testBase() throws URISyntaxException, IllegalArgumentException, IOException, SAXException{
  275. URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
  276. SchemaMapping m = SchemaMapping.fromDocument(builtin.toString(), new DefaultHandler());
  277. assertNotNull(m);
  278. // schemas.xml base
  279. assertEquals(new Result(builtin,"relaxng.rng"),m.getSchemaForDocument(null, "actions.rng",
  280. null,null,"ACTIONS",true));
  281. m.addRule(new URIResourceRule(new URI("file:///schemas.xml"),"actions.xml", "else.rng", false));
  282. // rule base
  283. assertEquals(new Result(new URI("file:///schemas.xml"),"else.rng"),
  284. m.getSchemaForDocument(null, "actions.xml",null,null,"TOTO",true));
  285. }
  286. @Test
  287. public void testIncludeMapping() throws MalformedURLException, URISyntaxException, SAXException{
  288. try{
  289. new SchemaMapping.IncludeMapping(null, (String)null, new DefaultHandler());
  290. fail("should throw an exception");
  291. }catch(IllegalArgumentException iae){
  292. //fine
  293. }
  294. try{
  295. new SchemaMapping.IncludeMapping(null, (SchemaMapping)null);
  296. fail("should throw an exception");
  297. }catch(IllegalArgumentException iae){
  298. //fine
  299. }
  300. try{
  301. new SchemaMapping.IncludeMapping(null, "relative", new DefaultHandler());
  302. fail("should throw an exception");
  303. }catch(IllegalArgumentException iae){
  304. //fine
  305. }
  306. try{
  307. new SchemaMapping.IncludeMapping(null, "\\//", new DefaultHandler());
  308. fail("should throw an exception");
  309. }catch(IllegalArgumentException iae){
  310. //fine
  311. }
  312. URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
  313. SchemaMapping m = new SchemaMapping();
  314. m.addRule(new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler()));
  315. // used the include rule AND used the base uri of the included schema
  316. assertEquals(new Result(builtin,"relaxng.rng"),m.getSchemaForDocument(null, "actions.rng",
  317. null,null,"ACTIONS",true));
  318. // this rule will override the default included rule
  319. m.insertRuleAt(0,new URIPatternRule(null,"actions.*", "else.rng", false));
  320. // rule base
  321. assertEquals(new Result(null,"else.rng"),
  322. m.getSchemaForDocument(null, "actions.xml",null,null,"TOTO",true));
  323. }
  324. @Test
  325. public void testIncludeMappingIncludedMappingBroken() throws MalformedURLException, IllegalArgumentException, SAXException{
  326. File broken = new File(testData, "broken_schemas_xml/malformed/schemas.xml");
  327. SchemaMapping m = new SchemaMapping();
  328. final Object[] errors = new Object[1];
  329. new SchemaMapping.IncludeMapping(null, broken.toURI().toURL().toString(), new DefaultHandler(){
  330. @Override
  331. public void fatalError(SAXParseException e) throws SAXException {
  332. errors[0] = e;
  333. }
  334. });
  335. }
  336. @Test
  337. public void testTransformURI() throws MalformedURLException, URISyntaxException, IOException{
  338. Mapping r;
  339. try{
  340. new TransformURI(null,null,"target");
  341. fail("should throw an exception");
  342. }catch(RuntimeException e){
  343. //fine
  344. }
  345. try{
  346. new TransformURI(null,"from",null);
  347. fail("should throw an exception");
  348. }catch(RuntimeException e){
  349. //fine
  350. }
  351. try{
  352. new TransformURI(null,"*.xml","*/*.xsd");
  353. fail("should throw an exception");
  354. }catch(RuntimeException e){
  355. //fine
  356. }
  357. System.err.println("hello");
  358. r = new TransformURI(null,"*.xml","*.xsd");
  359. File existsXML = new File(testData,"simple/actions.xml");
  360. File existsXSD = new File(testData,"simple/actions.xsd");
  361. // pattern matches and result exists
  362. assertEquals(new Result(null,existsXSD.toURL().toString()),
  363. r.getSchemaForDocument(null, existsXML.toURL().toString(),null,null,null,true));
  364. // test doesn't match
  365. assertNull(r.getSchemaForDocument(null, "test.txt",null,null,null,true));
  366. // test no result resource
  367. File noXSD = new File(testData,"dtd/actions.xml");
  368. assertNull(r.getSchemaForDocument(null, noXSD.toURL().toString(),null,null,null,true));
  369. // test result relative to base
  370. r = new TransformURI(existsXML.toURL().toURI(),"*.xml","../simple/*.xsd");
  371. assertEquals(new Result(existsXML.toURL().toURI(),"../simple/actions.xsd"),
  372. r.getSchemaForDocument(null, "actions.xml",null,null,null,true));
  373. r = new TransformURI(null,"*.xml","../simple/*.xsd");
  374. assertEquals(new Result(null,new File(noXSD.getParentFile(),"../simple/actions.xsd").toURL().toString()),
  375. r.getSchemaForDocument(null, noXSD.toURL().toString(),null,null,null,true));
  376. }
  377. @Test
  378. public void testToDocument() throws MalformedURLException, URISyntaxException, IOException, IllegalArgumentException, SAXException{
  379. URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
  380. SchemaMapping m = new SchemaMapping();
  381. Mapping r;
  382. r = new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler());
  383. m.addRule(r);
  384. r = new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler());
  385. r.setExplicitBase("file:///a.xml");
  386. m.addRule(r);
  387. r = new DocumentElementRule(null,null,"test","target",true);
  388. m.addRule(r);
  389. r = new DocumentElementRule(null,null,"test","target",false);
  390. r.setExplicitBase("");
  391. m.addRule(r);
  392. r = new DoctypeRule(null,"test","target",false);
  393. m.addRule(r);
  394. r = new DoctypeRule(null,"test","target",true);
  395. r.setExplicitBase("file:///b.xml");
  396. m.addRule(r);
  397. r = new NamespaceRule(null,"urn:ns","target",false);
  398. m.addRule(r);
  399. r = new NamespaceRule(null,"urn:ns","target",true);
  400. r.setExplicitBase("file:///c.xml");
  401. m.addRule(r);
  402. r = new URIPatternRule(null,"*.txt","target",false);
  403. m.addRule(r);
  404. r = new URIPatternRule(null,"*.txt","target",true);
  405. r.setExplicitBase("file:///d.xml");
  406. m.addRule(r);
  407. r = new URIResourceRule(null,"file:///test.txt","target",false);
  408. m.addRule(r);
  409. r = new URIResourceRule(null,"file:///test.txt","target",true);
  410. r.setExplicitBase("file:///e.xml");
  411. m.addRule(r);
  412. r = new DefaultRule(null,"targetDefault",false);
  413. m.addRule(r);
  414. r = new DefaultRule(null,"targetDefault",true);
  415. r.setExplicitBase("file:///f.xml");
  416. m.addRule(r);
  417. r = new TransformURI(null,"*.xml","*.xsd");
  418. m.addRule(r);
  419. r = new TransformURI(null,"*.xml","*.xsd");
  420. r.setExplicitBase("g.xml");
  421. m.addRule(r);
  422. m.addTypeId("toto","titi",true);
  423. m.addTypeId("titi","relax.rng",false);
  424. File f = File.createTempFile("schemamappingtest",".xml");
  425. try{
  426. m.toDocument(f.toURI().toURL().toString());
  427. }catch(IOException ioe){
  428. fail("should not throw an exception : "+ioe);
  429. }
  430. SchemaMapping m2 = null;
  431. try{
  432. m2 = SchemaMapping.fromDocument(f.toURL().toString(), new DefaultHandler());
  433. }catch(IOException ioe){
  434. fail("should not throw an exception : "+ioe);
  435. }
  436. File f2 = File.createTempFile("schemamappingtest",".xml");
  437. try{
  438. m2.toDocument(f2.getPath());
  439. }catch(IOException ioe){
  440. fail("should not throw an exception : "+ioe);
  441. }
  442. InputStream in1 = new FileInputStream(f);
  443. InputStream in2 = new FileInputStream(f2);
  444. try{
  445. assertInputStreamEquals(in1,in2);
  446. }finally{
  447. in1.close();
  448. in2.close();
  449. }
  450. //they are deleted only on success => easier debugging
  451. f.delete();
  452. f2.delete();
  453. }
  454. }