PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 192 lines | 132 code | 35 blank | 25 comment | 0 complexity | 9309eb33bce771d0d427c3ac8609eebf 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. * SchemaLoaderTest.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.*;
  32. import org.xml.sax.helpers.*;
  33. import javax.xml.validation.ValidatorHandler;
  34. import com.thaiopensource.xml.sax.DraconianErrorHandler;
  35. /**
  36. * $Id: SchemaLoaderTest.java 18330 2010-08-11 19:53:08Z kerik-sf $
  37. */
  38. public class SchemaLoaderTest{
  39. private static File testData;
  40. private static SchemaLoader l;
  41. @BeforeClass
  42. public static void setUpjEdit() throws IOException{
  43. TestUtils.beforeClass();
  44. testData = new File(System.getProperty("test_data")).getCanonicalFile();
  45. assertTrue(testData.exists());
  46. l = SchemaLoader.instance();
  47. assertNotNull(l);
  48. }
  49. @AfterClass
  50. public static void tearDownjEdit() {
  51. TestUtils.afterClass();
  52. }
  53. @Test
  54. public void testRNG() throws SAXException, IOException{
  55. File rngSchema = new File(testData,"relax_ng/actions.rng");
  56. ValidatorHandler verifierFilter = l.loadJaxpGrammar(
  57. null,
  58. rngSchema.getPath(),
  59. new DraconianErrorHandler(),
  60. null);
  61. assertNotNull(verifierFilter);
  62. File badActions = new File(testData,"relax_ng/actions.xml");
  63. XMLReader reader = XMLReaderFactory.createXMLReader();
  64. javax.xml.parsers.SAXParserFactory factory = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
  65. factory.setNamespaceAware(true);
  66. reader.setContentHandler(verifierFilter);
  67. verifierFilter.setContentHandler(new DefaultHandler());
  68. verifierFilter.setErrorHandler(new DraconianErrorHandler());
  69. // test that it accepts valid content
  70. File goodActions = new File(testData,"relax_ng/valid_actions.xml");
  71. reader.parse(new InputSource(goodActions.toURL().toString()));
  72. // test that it throws errors
  73. verifierFilter = l.loadJaxpGrammar(
  74. null,
  75. rngSchema.getPath(),
  76. new DraconianErrorHandler(),
  77. null);
  78. reader.setContentHandler(verifierFilter);
  79. verifierFilter.setContentHandler(new DefaultHandler());
  80. verifierFilter.setErrorHandler(new DraconianErrorHandler());
  81. try{
  82. reader.parse(new InputSource(badActions.toURL().toString()));
  83. fail("should throw an exception");
  84. }catch(SAXParseException spe){
  85. assertNotNull(spe.getMessage());
  86. assertTrue(spe.getMessage(),spe.getMessage().contains("CODDE"));
  87. }
  88. }
  89. @Test
  90. public void testXSD() throws SAXException, IOException{
  91. File xsdSchema = new File(testData,"simple/actions.xsd");
  92. ValidatorHandler verifierFilter = l.loadJaxpGrammar(
  93. null,
  94. xsdSchema.getPath(),
  95. new DraconianErrorHandler(),
  96. null);
  97. assertNotNull(verifierFilter);
  98. File badActions = new File(testData,"relax_ng/actions.xml");
  99. XMLReader reader = XMLReaderFactory.createXMLReader();
  100. javax.xml.parsers.SAXParserFactory factory = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
  101. factory.setNamespaceAware(true);
  102. reader.setContentHandler(verifierFilter);
  103. verifierFilter.setContentHandler(new DefaultHandler());
  104. verifierFilter.setErrorHandler(new DraconianErrorHandler());
  105. // test that it accepts valid content
  106. File goodActions = new File(testData,"relax_ng/valid_actions.xml");
  107. reader.parse(new InputSource(goodActions.toURL().toString()));
  108. // test that it throws errors
  109. verifierFilter = l.loadJaxpGrammar(
  110. null,
  111. xsdSchema.getPath(),
  112. new DraconianErrorHandler(),
  113. null);
  114. reader.setContentHandler(verifierFilter);
  115. verifierFilter.setContentHandler(new DefaultHandler());
  116. verifierFilter.setErrorHandler(new DraconianErrorHandler());
  117. try{
  118. reader.parse(new InputSource(badActions.toURL().toString()));
  119. fail("should throw an exception");
  120. }catch(SAXParseException spe){
  121. assertNotNull(spe.getMessage());
  122. assertTrue(spe.getMessage(),spe.getMessage().contains("CODDE"));
  123. }
  124. }
  125. @Test
  126. public void testNoSchemaThere(){
  127. try{
  128. ValidatorHandler verifierFilter = l.loadJaxpGrammar(
  129. null,
  130. "file:/not_there",
  131. new DraconianErrorHandler(),
  132. null);
  133. fail("should throw an exception");
  134. }catch(IOException ioe){
  135. //fine
  136. }catch(SAXException se){
  137. fail("wrong exception :"+se);
  138. }
  139. }
  140. /** this test fails... */
  141. @Test
  142. public void testBrokenSchema(){
  143. File brokenSchema = new File(testData,"dir with space/actions.xsd");
  144. try{
  145. ValidatorHandler verifierFilter = l.loadJaxpGrammar(
  146. null,
  147. brokenSchema.toURI().toString(),
  148. new DraconianErrorHandler(),
  149. null);
  150. fail("should throw an exception");
  151. }catch(IOException ioe){
  152. fail("wrong exception :"+ioe);
  153. }catch(SAXException se){
  154. //fine
  155. }
  156. }
  157. }