001/* 002 * Copyright (c) 2004-2012, Willem Cazander 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 006 * that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the 009 * following disclaimer. 010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 011 * the following disclaimer in the documentation and/or other materials provided with the distribution. 012 * 013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 022 */ 023 024package org.x4o.xml.core; 025 026import java.io.IOException; 027 028import org.x4o.xml.core.config.X4OLanguageProperty; 029import org.x4o.xml.eld.EldParserSupportCore; 030import org.x4o.xml.element.ElementLanguage; 031import org.xml.sax.EntityResolver; 032import org.xml.sax.InputSource; 033import org.xml.sax.SAXException; 034 035import junit.framework.TestCase; 036 037/** 038 * X4OLanguageClassLoaderTest test classloader. 039 * 040 * @author Willem Cazander 041 * @version 1.0 Aug 27, 2012 042 */ 043public class X4OEntityResolverTest extends TestCase { 044 045 public void testElementLangugeNull() throws Exception { 046 Exception e = null; 047 try { 048 new X4OEntityResolver(null); 049 } catch (Exception catchE) { 050 e = catchE; 051 } 052 assertNotNull(e); 053 assertEquals(NullPointerException.class, e.getClass()); 054 assertTrue(e.getMessage().contains("null")); 055 } 056 057 public void testResolve() throws Exception { 058 EldParserSupportCore support = new EldParserSupportCore(); 059 X4OEntityResolver resolver = new X4OEntityResolver(support.loadElementLanguageSupport()); 060 InputSource input = resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd"); 061 assertNotNull(input); 062 } 063 064 public void testResolveMissing() throws Exception { 065 EldParserSupportCore support = new EldParserSupportCore(); 066 X4OEntityResolver resolver = new X4OEntityResolver(support.loadElementLanguageSupport()); 067 Exception e = null; 068 try { 069 resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd-missing-resource"); 070 } catch (Exception catchE) { 071 e = catchE; 072 } 073 assertNotNull(e); 074 assertEquals(SAXException.class, e.getClass()); 075 assertTrue(e.getMessage().contains("missing-resource")); 076 } 077 078 public void testResolveProperty() throws Exception { 079 EldParserSupportCore support = new EldParserSupportCore(); 080 ElementLanguage language = support.loadElementLanguageSupport(); 081 language.getLanguageConfiguration().setLanguageProperty(X4OLanguageProperty.CONFIG_ENTITY_RESOLVER, new TestEntityResolver()); 082 X4OEntityResolver resolver = new X4OEntityResolver(language); 083 Exception e = null; 084 InputSource input = null; 085 try { 086 input = resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd"); 087 } catch (Exception catchE) { 088 e = catchE; 089 } 090 assertNull(e); 091 assertNotNull(input); 092 } 093 094 public void testResolvePropertyNull() throws Exception { 095 EldParserSupportCore support = new EldParserSupportCore(); 096 ElementLanguage language = support.loadElementLanguageSupport(); 097 language.getLanguageConfiguration().setLanguageProperty(X4OLanguageProperty.CONFIG_ENTITY_RESOLVER, new TestEntityResolver()); 098 X4OEntityResolver resolver = new X4OEntityResolver(language); 099 Exception e = null; 100 try { 101 resolver.resolveEntity("","http://cel.x4o.org/xml/ns/cel-root-1.0.xsd-null"); 102 } catch (Exception catchE) { 103 e = catchE; 104 } 105 assertNotNull(e); 106 assertEquals(SAXException.class, e.getClass()); 107 assertTrue(e.getMessage().contains("null")); 108 } 109 110 public class TestEntityResolver implements EntityResolver { 111 112 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 113 if (systemId.contains("null")) { 114 return null; 115 } else { 116 return new InputSource(); 117 } 118 } 119 120 } 121}