1 /* 2 * Copyright The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE.txt file. 7 */ 8 package org.codehaus.spice.configkit; 9 10 import java.io.IOException; 11 import junit.framework.TestCase; 12 import org.xml.sax.InputSource; 13 import org.xml.sax.SAXException; 14 15 /*** 16 * Basic unit tests for the multiplexing streams. 17 * 18 * @author Peter Donald 19 */ 20 public final class ConfigKitEntityResolverTestCase 21 extends TestCase 22 { 23 private ClassLoader m_contextClassLoader; 24 25 protected void setUp() 26 throws Exception 27 { 28 m_contextClassLoader = Thread.currentThread().getContextClassLoader(); 29 Thread.currentThread().setContextClassLoader( null ); 30 } 31 32 protected void tearDown() 33 throws Exception 34 { 35 Thread.currentThread().setContextClassLoader( m_contextClassLoader ); 36 } 37 38 public void testNullInfos() 39 { 40 try 41 { 42 new ConfigKitEntityResolver( null, getClassLoader() ); 43 } 44 catch( NullPointerException npe ) 45 { 46 assertEquals( "NPE for null infos", "infos", npe.getMessage() ); 47 return; 48 } 49 50 fail( "Expected NPE due to null infos" ); 51 } 52 53 public void testBadResource() 54 { 55 final EntityInfo info = 56 new EntityInfo( TestData.PUBLIC_ID, 57 TestData.SYSTEM_ID, 58 "noexist.txt" ); 59 final ConfigKitEntityResolver resolver = 60 new ConfigKitEntityResolver( new EntityInfo[]{info}, 61 getClassLoader() ); 62 try 63 { 64 resolver.resolveEntity( TestData.PUBLIC_ID, TestData.SYSTEM_ID ); 65 fail( "Expected IOException as resource no exist" ); 66 } 67 catch( final SAXException se ) 68 { 69 fail( "Unexpected SAXException: " + se ); 70 } 71 catch( final IOException ioe ) 72 { 73 return; 74 } 75 } 76 77 public void testNullClassLoader() 78 { 79 new ConfigKitEntityResolver( new EntityInfo[]{TestData.INFO}, null ); 80 } 81 82 public void testEmptyResolver() 83 { 84 new ConfigKitEntityResolver( new EntityInfo[ 0 ], getClassLoader() ); 85 } 86 87 public void testRetrieveBySystemId() 88 { 89 doRetrievalTest( null, TestData.SYSTEM_ID, true, getClassLoader() ); 90 } 91 92 public void testRetrieveByPublicId() 93 { 94 doRetrievalTest( TestData.PUBLIC_ID, null, true, getClassLoader() ); 95 } 96 97 public void testNonRetrieveByPublicId() 98 { 99 doRetrievalTest( "no exist", null, false, getClassLoader() ); 100 } 101 102 public void testNonRetrieveBySystemId() 103 { 104 doRetrievalTest( null, "no exist", false, getClassLoader() ); 105 } 106 107 public void testNoClassLoaderRetrieveBySystemId() 108 { 109 doRetrievalTest( null, TestData.SYSTEM_ID, true, null ); 110 } 111 112 public void testNoClassLoaderRetrieveByPublicId() 113 { 114 doRetrievalTest( TestData.PUBLIC_ID, null, true, null ); 115 } 116 117 public void testNoClassLoaderNonRetrieveByPublicId() 118 { 119 doRetrievalTest( "no exist", null, false, null ); 120 } 121 122 public void testNoClassLoaderNonRetrieveBySystemId() 123 { 124 doRetrievalTest( null, "no exist", false, null ); 125 } 126 127 public void testCtxClassLoaderRetrieveBySystemId() 128 { 129 Thread.currentThread().setContextClassLoader( getClassLoader() ); 130 doRetrievalTest( null, TestData.SYSTEM_ID, true, null ); 131 } 132 133 public void testCtxClassLoaderRetrieveByPublicId() 134 { 135 Thread.currentThread().setContextClassLoader( getClassLoader() ); 136 doRetrievalTest( TestData.PUBLIC_ID, null, true, null ); 137 } 138 139 public void testCtxClassLoaderNonRetrieveByPublicId() 140 { 141 Thread.currentThread().setContextClassLoader( getClassLoader() ); 142 doRetrievalTest( "no exist", null, false, null ); 143 } 144 145 public void testCtxClassLoaderNonRetrieveBySystemId() 146 { 147 Thread.currentThread().setContextClassLoader( getClassLoader() ); 148 doRetrievalTest( null, "no exist", false, null ); 149 } 150 151 private void doRetrievalTest( final String publicID, 152 final String systemID, 153 final boolean shouldExist, 154 final ClassLoader classLoader ) 155 { 156 final ConfigKitEntityResolver resolver = 157 new ConfigKitEntityResolver( new EntityInfo[]{TestData.INFO}, 158 classLoader ); 159 try 160 { 161 final InputSource inputSource = resolver.resolveEntity( publicID, 162 systemID ); 163 if( shouldExist ) 164 { 165 assertNotNull( 166 "Expected to find resource for " + 167 getIdString( publicID, systemID ), 168 inputSource ); 169 } 170 else 171 { 172 assertNull( 173 "Not expected to find resource for " + 174 getIdString( publicID, systemID ), 175 inputSource ); 176 } 177 } 178 catch( final IOException ioe ) 179 { 180 fail( "Error loading resource for " + 181 getIdString( publicID, systemID ) + 182 " due to " + ioe ); 183 } 184 catch( final SAXException se ) 185 { 186 fail( "Error finding resolver resource for " + 187 getIdString( publicID, systemID ) + 188 " due to " + se ); 189 } 190 } 191 192 private String getIdString( final String publicId, final String systemId ) 193 { 194 if( null == publicId ) 195 { 196 return "systemID " + systemId; 197 } 198 else if( null == systemId ) 199 { 200 return "publicID " + publicId; 201 } 202 else 203 { 204 return "id " + publicId + "/" + systemId; 205 } 206 } 207 208 private ClassLoader getClassLoader() 209 { 210 return getClass().getClassLoader(); 211 } 212 } 213

This page was automatically generated by Maven