PageRenderTime 140ms CodeModel.GetById 93ms app.highlight 42ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 584 lines | 385 code | 130 blank | 69 comment | 0 complexity | 51c428cab531438e840d8b42054b5cd1 MD5 | raw file
  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 */
 14package xml.parser;
 15
 16// {{{ jUnit imports 
 17import java.util.concurrent.TimeUnit;
 18
 19import org.junit.*;
 20import static org.junit.Assert.*;
 21
 22import org.fest.swing.fixture.*;
 23import org.fest.swing.core.*;
 24import org.fest.swing.finder.*;
 25import org.fest.swing.edt.*;
 26import org.fest.swing.timing.*;
 27
 28import static org.fest.assertions.Assertions.*;
 29
 30import org.gjt.sp.jedit.testframework.Log;
 31import org.gjt.sp.jedit.testframework.TestUtils;
 32
 33import static xml.XMLTestUtils.*;
 34// }}}
 35
 36import java.io.*;
 37import java.net.*;
 38import org.xml.sax.SAXException;
 39import org.xml.sax.SAXParseException;
 40import org.xml.sax.helpers.DefaultHandler;
 41
 42import static xml.parser.SchemaMapping.*;
 43
 44/**
 45 * $Id: SchemaMappingTest.java 21326 2012-03-11 11:37:15Z kerik-sf $
 46 */
 47public class SchemaMappingTest{
 48	private static File testData;
 49
 50    @BeforeClass
 51    public static void setUpjEdit() throws IOException{
 52        TestUtils.beforeClass();
 53        testData = new File(System.getProperty("test_data")).getCanonicalFile();
 54        assertTrue(testData.exists());
 55    }
 56
 57    @AfterClass
 58    public static void tearDownjEdit() {
 59        TestUtils.afterClass();
 60    }
 61
 62    @Test
 63    public void testEmptyMapping(){
 64    	SchemaMapping m = new SchemaMapping();
 65    	assertNull(m.getBaseURI());
 66    	assertNull(m.getSchemaForDocument(null, "actions.xml",
 67		null,null,"ACTIONS",true));
 68    }
 69    
 70    @Test
 71    public void testBuiltInSchema()  throws URISyntaxException, IllegalArgumentException, IOException, SAXException{
 72    	URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
 73    	SchemaMapping m = SchemaMapping.fromDocument(builtin.toString(), new DefaultHandler());
 74    	assertNotNull(m);
 75    	
 76    	// 2 built-in rules
 77    	assertEquals(new Result(builtin,"locate.rng"),m.getSchemaForDocument(null, "schemas.xml",
 78		null,null,"locatingRules",true));
 79    	assertEquals(new Result(builtin,"locate.rng"),m.getSchemaForDocument(null, "schemas.xml",
 80		null,null,"a",true));
 81		
 82		// non-matching rule
 83    	assertNull(m.getSchemaForDocument(null, "actions.xml",
 84		null,null,"ACTIONS",true));
 85		
 86    }
 87    
 88    @Test
 89    public void testAddRule() throws MalformedURLException, URISyntaxException{
 90    	SchemaMapping m = new SchemaMapping();
 91
 92    	
 93    	// test taken into account
 94    	m.addRule(new DocumentElementRule(null,null, "ACTIONS", "actions.rng", false));
 95    	assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
 96		null,null,"ACTIONS",true));
 97		
 98    	m.addRule(new URIResourceRule(new URI("file:///"),"actions.xml", "else.rng", false));
 99
100    	// test taken into account
101    	assertEquals(new Result(new URI("file:///"),"else.rng"),m.getSchemaForDocument(null, "actions.xml",
102		null,null,"TOTO",true));
103
104		// test precedence ( both match, but first one wins)
105    	assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
106		null,null,"ACTIONS",true));
107		
108    }
109
110    @Test
111    public void testAddTypeId(){
112    	SchemaMapping m = new SchemaMapping();
113    	
114    	// test undefined typeId
115    	m.addRule(new DocumentElementRule(null,null, "ACTIONS", "actions", true));
116
117    	assertEquals(null,m.getSchemaForDocument(null, "actions.xml",
118		null,null,"ACTIONS",true));
119    	
120		m.addTypeId("actions","actions.rng",false);
121
122    	// test taken into account
123    	assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
124		null,null,"ACTIONS",true));
125		
126    	m.addTypeId("actions", "else.rng",false);
127
128		// test precedence ( the first one wins)
129    	assertEquals(new Result(null,"actions.rng"),m.getSchemaForDocument(null, "actions.xml",
130		null,null,"ACTIONS",true));
131		
132    }
133    
134     @Test
135    public void testDefaultRule(){
136    	Rule r;
137    	
138    	try{
139    		new DefaultRule(null,null,false);
140    		fail("should throw an exception");
141    	}catch(RuntimeException e){
142    		//fine
143    	}
144    	
145    	try{
146    		new DefaultRule(null,"",false);
147    		fail("should throw an exception");
148    	}catch(RuntimeException e){
149    		//fine
150    	}
151    	
152    	r = new DefaultRule(null,"target",false);
153    	assertTrue(r.matchNamespace("urn:ns"));
154    	assertTrue(r.matchURL("test.txt"));
155    	assertTrue(r.matchDoctype("book"));
156    	assertTrue(r.matchDocumentElement(null,"test"));
157    	assertTrue(r.matchDocumentElement("t","test"));
158    }
159
160    @Test
161    public void testDocumentElementRule(){
162    	Rule r;
163    	
164    	try{
165    		new DocumentElementRule(null,null,null,"target",false);
166    		fail("should throw an exception");
167    	}catch(RuntimeException e){
168    		//fine
169    	}
170    	
171    	try{
172    		new DocumentElementRule(null,null,"","target",false);
173    		fail("should throw an exception");
174    	}catch(RuntimeException e){
175    		//fine
176    	}
177    	
178    	r = new DocumentElementRule(null,null,"test","target",false);
179    	assertFalse(r.matchNamespace("urn:ns"));
180    	assertFalse(r.matchURL("test.txt"));
181    	assertFalse(r.matchDoctype("book"));
182    	
183    	// no prefix
184    	assertTrue(r.matchDocumentElement(null,"test"));
185    	
186    	// with prefix
187    	assertTrue(r.matchDocumentElement("t","test"));
188    	
189    	// no match
190    	assertFalse(r.matchDocumentElement(null,"test2"));
191    	
192    	r = new DocumentElementRule(null, "t","test","target",false);
193    	
194    	// no prefix
195    	assertFalse(r.matchDocumentElement(null,"test"));
196    	
197    	// with prefix
198    	assertTrue(r.matchDocumentElement("t","test"));
199    	
200    	// no match
201    	assertFalse(r.matchDocumentElement("tt","test"));
202    	
203    	
204    }
205    
206    @Test
207    public void testDoctypeRule(){
208    	Rule r;
209    	
210    	try{
211    		new DoctypeRule(null, null,"target",false);
212    		fail("should throw an exception");
213    	}catch(RuntimeException e){
214    		//fine
215    	}
216    	
217    	try{
218    		new DoctypeRule(null,"","target",false);
219    		fail("should throw an exception");
220    	}catch(RuntimeException e){
221    		//fine
222    	}
223    	
224    	r = new DoctypeRule(null,"test","target",false);
225    	assertFalse(r.matchNamespace("urn:ns"));
226    	assertFalse(r.matchURL("test.txt"));
227    	assertFalse(r.matchDocumentElement(null,"test"));
228    	
229    	assertTrue(r.matchDoctype("test"));
230    	assertFalse(r.matchDoctype("test2"));
231    }
232    
233    @Test
234    public void testNamespaceRule(){
235    	Rule r;
236    	
237    	try{
238    		new NamespaceRule(null,null,"target",false);
239    		fail("should throw an exception");
240    	}catch(RuntimeException e){
241    		//fine
242    	}
243    	
244    	r = new NamespaceRule(null,"urn:ns","target",false);
245    	
246    	assertFalse(r.matchDoctype("test"));
247    	assertFalse(r.matchURL("test.txt"));
248    	assertFalse(r.matchDocumentElement(null,"test"));
249    	
250    	assertTrue(r.matchNamespace("urn:ns"));
251    	assertFalse(r.matchNamespace("urn:ns2"));
252    	
253    	// default namespace
254    	r = new NamespaceRule(null,"","target",false);
255    	assertTrue(r.matchNamespace(""));
256    	assertFalse(r.matchNamespace("urn:ns2"));
257    	
258    }
259    
260    @Test
261    public void testURIPatternRule(){
262    	Rule r;
263    	
264    	try{
265    		new URIPatternRule(null,null,"target",false);
266    		fail("should throw an exception");
267    	}catch(RuntimeException e){
268    		//fine
269    	}
270    	
271    	r = new URIPatternRule(null,"test.txt","target",false);
272    	
273    	assertFalse(r.matchDoctype("test"));
274    	assertFalse(r.matchDocumentElement(null,"test"));
275    	assertFalse(r.matchNamespace("urn:ns"));
276    	
277    	assertTrue(r.matchURL("test.txt"));
278    	assertFalse(r.matchURL("test2.txt"));
279    	
280    	r = new URIPatternRule(null,"*.txt","target",false);
281    	assertTrue(r.matchURL("test.txt"));
282    	assertFalse(r.matchURL("test.txt.2"));
283    	
284    	//relative/absolute uri :
285    	r = new URIPatternRule(null,"*.txt","target",false);
286    	assertTrue(r.matchURL("test/test.txt"));
287    	assertTrue(r.matchURL("file:///c:/test.txt"));
288    	
289    	r = new URIPatternRule(null,"*/A.txt","target",false);
290    	assertTrue(r.matchURL("test/A.txt"));
291    	assertTrue(r.matchURL("file:///c:/A.txt"));
292    	
293    	assertFalse(r.matchURL("A.txt"));
294    	
295    	//opaque URI
296    	r = new URIPatternRule(null,"*jedit*","target",false);
297    	assertTrue(r.matchURL("mailto:jedit-devel@sourceforge.net"));
298    	assertFalse(r.matchURL("mailto:test:test"));
299    }
300    
301    
302    @Test
303    public void testURIResourceRule() throws MalformedURLException, URISyntaxException{
304    	Rule r;
305    	
306    	try{
307    		new URIResourceRule(null,null,"target",false);
308    		fail("should throw an exception");
309    	}catch(RuntimeException e){
310    		//fine
311    	}
312
313    	try{
314    		new URIResourceRule(null,"test","target",false);
315    		fail("should throw an exception");
316    	}catch(RuntimeException e){
317    		//fine
318    	}
319    	
320    	// relative url
321		try{
322			new URIResourceRule(null, "relative","target",false);
323			fail("should throw an exception");
324		}catch(IllegalArgumentException iae){
325			//fine
326		}
327		
328		//illegal URI
329		try{
330			new URIResourceRule(null, "\\//","target",false);
331			fail("should throw an exception");
332		}catch(IllegalArgumentException iae){
333			//fine
334		}
335
336    	r = new URIResourceRule(null,"file:///test.txt","target",false);
337    	
338    	assertFalse(r.matchDoctype("test"));
339    	assertFalse(r.matchDocumentElement(null,"test"));
340    	assertFalse(r.matchNamespace("urn:ns"));
341    	
342    	// absolute uri as pattern
343
344    	assertTrue(r.matchURL("file:///test.txt"));
345    	assertTrue(r.matchURL("file:///A/../test.txt")); // same path
346    	
347    	// relative uri as pattern
348    	r = new URIResourceRule(new URI("file:///A/schemas.xml"),"test.txt","target",false);
349    	assertTrue(r.matchURL("file:///A/test.txt"));
350    	assertFalse(r.matchURL("file:///test.txt"));
351    	
352    	//relative URI to match
353    	assertTrue(r.matchURL("test.txt"));
354    }
355 
356	@Test
357    public void testBase() throws URISyntaxException, IllegalArgumentException, IOException, SAXException{
358     	URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
359    	SchemaMapping m = SchemaMapping.fromDocument(builtin.toString(), new DefaultHandler());
360    	assertNotNull(m);
361
362    	
363    	// schemas.xml base
364    	assertEquals(new Result(builtin,"relaxng.rng"),m.getSchemaForDocument(null, "actions.rng",
365		null,null,"ACTIONS",true));
366		
367    	m.addRule(new URIResourceRule(new URI("file:///schemas.xml"),"actions.xml", "else.rng", false));
368
369    	// rule base
370    	assertEquals(new Result(new URI("file:///schemas.xml"),"else.rng"),
371    		m.getSchemaForDocument(null, "actions.xml",null,null,"TOTO",true));
372
373	}
374	
375	
376	@Test
377	public void testIncludeMapping() throws MalformedURLException, URISyntaxException, SAXException{
378     	
379		try{
380			new SchemaMapping.IncludeMapping(null, (String)null, new DefaultHandler());
381			fail("should throw an exception");
382		}catch(IllegalArgumentException iae){
383			//fine
384		}
385     	
386		try{
387			new SchemaMapping.IncludeMapping(null, (SchemaMapping)null);
388			fail("should throw an exception");
389		}catch(IllegalArgumentException iae){
390			//fine
391		}
392		
393		try{
394			new SchemaMapping.IncludeMapping(null, "relative", new DefaultHandler());
395			fail("should throw an exception");
396		}catch(IllegalArgumentException iae){
397			//fine
398		}
399		
400		try{
401			new SchemaMapping.IncludeMapping(null, "\\//", new DefaultHandler());
402			fail("should throw an exception");
403		}catch(IllegalArgumentException iae){
404			//fine
405		}
406
407		URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
408     	
409    	SchemaMapping m = new SchemaMapping();
410    	
411    	m.addRule(new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler()));
412    	    	
413    	// used the include rule AND used the base uri of the included schema
414    	assertEquals(new Result(builtin,"relaxng.rng"),m.getSchemaForDocument(null, "actions.rng",
415		null,null,"ACTIONS",true));
416		
417		// this rule will override the default included rule
418    	m.insertRuleAt(0,new URIPatternRule(null,"actions.*", "else.rng", false));
419
420    	// rule base
421    	assertEquals(new Result(null,"else.rng"),
422    		m.getSchemaForDocument(null, "actions.xml",null,null,"TOTO",true));
423	}
424	
425	@Test
426	public void testIncludeMappingIncludedMappingBroken() throws MalformedURLException, IllegalArgumentException, SAXException{
427		File broken = new File(testData, "broken_schemas_xml/malformed/schemas.xml");
428     	
429    	SchemaMapping m = new SchemaMapping();
430    	final Object[] errors = new Object[1];
431    	new SchemaMapping.IncludeMapping(null, broken.toURI().toURL().toString(), new DefaultHandler(){
432    		@Override
433    		public void fatalError(SAXParseException e) throws SAXException {
434    			errors[0] = e;
435    		}
436    	});
437    	    	
438	}
439	
440	@Test
441	public void testTransformURI() throws MalformedURLException, URISyntaxException, IOException{
442		Mapping r;
443		
444    	try{
445    		new TransformURI(null,null,"target");
446    		fail("should throw an exception");
447    	}catch(RuntimeException e){
448    		//fine
449    	}
450    	try{
451    		new TransformURI(null,"from",null);
452    		fail("should throw an exception");
453    	}catch(RuntimeException e){
454    		//fine
455    	}
456
457    	try{
458    		new TransformURI(null,"*.xml","*/*.xsd");
459    		fail("should throw an exception");
460    	}catch(RuntimeException e){
461    		//fine
462    	}
463    	
464    	System.err.println("hello");
465    	r = new TransformURI(null,"*.xml","*.xsd");
466    	
467    	File existsXML = new File(testData,"simple/actions.xml");
468    	File existsXSD = new File(testData,"simple/actions.xsd");
469
470    	// pattern matches and result exists
471    	assertEquals(new Result(null,existsXSD.toURL().toString()),
472    		r.getSchemaForDocument(null, existsXML.toURL().toString(),null,null,null,true));
473    	
474    	// test doesn't match
475    	assertNull(r.getSchemaForDocument(null, "test.txt",null,null,null,true));
476
477    	// test no result resource
478    	File noXSD = new File(testData,"dtd/actions.xml");
479    	assertNull(r.getSchemaForDocument(null, noXSD.toURL().toString(),null,null,null,true));
480
481    	// test result relative to base
482    	r = new TransformURI(existsXML.toURL().toURI(),"*.xml","../simple/*.xsd");
483    	
484    	assertEquals(new Result(existsXML.toURL().toURI(),"../simple/actions.xsd"),
485    		r.getSchemaForDocument(null, "actions.xml",null,null,null,true));
486
487    	r = new TransformURI(null,"*.xml","../simple/*.xsd");
488
489    	assertEquals(new Result(null,new File(noXSD.getParentFile(),"../simple/actions.xsd").toURL().toString()),
490    		r.getSchemaForDocument(null, noXSD.toURL().toString(),null,null,null,true));
491	}
492	
493	@Test
494	public void testToDocument() throws MalformedURLException, URISyntaxException, IOException, IllegalArgumentException, SAXException{
495     	URI builtin = getClass().getClassLoader().getResource("xml/dtds/schemas.xml").toURI();
496    	SchemaMapping m = new SchemaMapping();
497
498		Mapping r;
499    	r = new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler());
500    	m.addRule(r);
501    	r = new SchemaMapping.IncludeMapping(null, builtin.toString(), new DefaultHandler());
502    	r.setExplicitBase("file:///a.xml");
503    	m.addRule(r);
504    	
505    	r = new DocumentElementRule(null,null,"test","target",true);
506    	m.addRule(r);
507    	r = new DocumentElementRule(null,null,"test","target",false);
508    	r.setExplicitBase("");
509    	m.addRule(r);
510
511     	r = new DoctypeRule(null,"test","target",false);
512     	m.addRule(r);
513     	r = new DoctypeRule(null,"test","target",true);
514    	r.setExplicitBase("file:///b.xml");
515    	m.addRule(r);
516     	
517    	r = new NamespaceRule(null,"urn:ns","target",false);
518     	m.addRule(r);
519    	r = new NamespaceRule(null,"urn:ns","target",true);
520    	r.setExplicitBase("file:///c.xml");
521    	m.addRule(r);
522
523    	r = new URIPatternRule(null,"*.txt","target",false);
524     	m.addRule(r);
525    	r = new URIPatternRule(null,"*.txt","target",true);
526    	r.setExplicitBase("file:///d.xml");
527    	m.addRule(r);
528
529    	r = new URIResourceRule(null,"file:///test.txt","target",false);
530     	m.addRule(r);
531    	r = new URIResourceRule(null,"file:///test.txt","target",true);
532    	r.setExplicitBase("file:///e.xml");
533    	m.addRule(r);
534
535     	r = new DefaultRule(null,"targetDefault",false);
536     	m.addRule(r);
537     	r = new DefaultRule(null,"targetDefault",true);
538    	r.setExplicitBase("file:///f.xml");
539    	m.addRule(r);
540    	
541    	r = new TransformURI(null,"*.xml","*.xsd");
542     	m.addRule(r);
543    	r = new TransformURI(null,"*.xml","*.xsd");
544    	r.setExplicitBase("g.xml");
545    	m.addRule(r);
546
547    	m.addTypeId("toto","titi",true);
548    	m.addTypeId("titi","relax.rng",false);
549    	
550    	File f = File.createTempFile("schemamappingtest",".xml");
551    	try{
552    		m.toDocument(f.toURI().toURL().toString());
553    	}catch(IOException ioe){
554    		fail("should not throw an exception : "+ioe);
555    	}
556    	
557    	SchemaMapping m2 = null;
558    	try{
559    		m2 = SchemaMapping.fromDocument(f.toURL().toString(), new DefaultHandler());
560    	}catch(IOException ioe){
561    		fail("should not throw an exception : "+ioe);
562    	}
563    	
564    	File f2 = File.createTempFile("schemamappingtest",".xml");
565    	try{
566    		m2.toDocument(f2.getPath());
567    	}catch(IOException ioe){
568    		fail("should not throw an exception : "+ioe);
569    	}
570    	
571    	InputStream in1 = new FileInputStream(f);
572    	InputStream in2 = new FileInputStream(f2);
573    	
574    	try{
575    	assertInputStreamEquals(in1,in2);
576    	}finally{
577    		in1.close();
578    		in2.close();
579    	}
580    	//they are deleted only on success => easier debugging
581    	f.delete();
582    	f2.delete();
583	}
584}