PageRenderTime 68ms CodeModel.GetById 51ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

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