1 package org.codehaus.classworlds; 2 3 /* 4 $Id: ConfiguratorTest.java,v 1.3 2003/09/23 18:11:30 jvanzyl Exp $ 5 6 Copyright 2002 (C) The Werken Company. All Rights Reserved. 7 8 Redistribution and use of this software and associated documentation 9 ("Software"), with or without modification, are permitted provided 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "classworlds" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Werken Company. For written permission, 24 please contact bob@werken.com. 25 26 4. Products derived from this Software may not be called "classworlds" 27 nor may "classworlds" appear in their names without prior written 28 permission of The Werken Company. "classworlds" is a registered 29 trademark of The Werken Company. 30 31 5. Due credit should be given to The Werken Company. 32 (http://classworlds.werken.com/). 33 34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 import junit.framework.TestCase; 50 51 import java.io.File; 52 import java.io.FileInputStream; 53 import java.io.FileNotFoundException; 54 import java.net.URL; 55 import java.util.Collection; 56 57 public class ConfiguratorTest extends TestCase 58 { 59 private Launcher launcher; 60 private Configurator configurator; 61 62 public ConfiguratorTest(String name) 63 { 64 super( name ); 65 } 66 67 public void setUp() 68 { 69 this.launcher = new Launcher(); 70 this.configurator = new Configurator( this.launcher ); 71 } 72 73 public void tearDown() 74 { 75 this.launcher = null; 76 this.configurator = null; 77 } 78 79 public void testConfigure_Nonexistant() throws Exception 80 { 81 try 82 { 83 this.configurator.configure( getConfigPath( "notfound.conf" ) ); 84 fail( "throw FileNotFoundException" ); 85 } 86 catch (FileNotFoundException e) 87 { 88 // expected and correct 89 } 90 } 91 92 public void testConfigure_DuplicateMain() throws Exception 93 { 94 try 95 { 96 this.configurator.configure( getConfigPath( "dupe-main.conf" ) ); 97 fail( "throw ConfigurationException" ); 98 } 99 catch (ConfigurationException e) 100 { 101 // expected and correct 102 assertTrue( e.getMessage().startsWith( "Duplicate main" ) ); 103 } 104 } 105 106 public void testConfigure_DuplicateRealm() throws Exception 107 { 108 try 109 { 110 this.configurator.configure( getConfigPath( "dupe-realm.conf" ) ); 111 fail( "throw DuplicateRealmException" ); 112 } 113 catch (DuplicateRealmException e) 114 { 115 // expected and correct 116 assertEquals( "dupe.realm", 117 e.getId() ); 118 } 119 } 120 121 public void testConfigure_EarlyImport() throws Exception 122 { 123 try 124 { 125 this.configurator.configure( getConfigPath( "early-import.conf" ) ); 126 fail( "throw ConfigurationException" ); 127 } 128 catch (ConfigurationException e) 129 { 130 // expected and correct 131 assertTrue( e.getMessage().startsWith( "Unhandled import" ) ); 132 } 133 } 134 135 public void testConfigure_RealmSyntax() throws Exception 136 { 137 try 138 { 139 this.configurator.configure( getConfigPath( "realm-syntax.conf" ) ); 140 fail( "throw ConfigurationException" ); 141 } 142 catch (ConfigurationException e) 143 { 144 // expected and correct 145 assertTrue( e.getMessage().startsWith( "Invalid realm" ) ); 146 } 147 } 148 149 public void testConfigure_Valid() throws Exception 150 { 151 this.configurator.configure( getConfigPath( "valid.conf" ) ); 152 153 assertEquals( "org.apache.maven.app.App", 154 this.launcher.getMainClassName() ); 155 156 assertEquals( "maven", 157 this.launcher.getMainRealmName() ); 158 159 ClassWorld world = this.launcher.getWorld(); 160 161 Collection realms = world.getRealms(); 162 163 assertEquals( 4, 164 realms.size() ); 165 166 assertNotNull( world.getRealm( "ant" ) ); 167 assertNotNull( world.getRealm( "maven" ) ); 168 assertNotNull( world.getRealm( "xml" ) ); 169 170 DefaultClassRealm antRealm = world.getRealmImpl( "ant" ); 171 DefaultClassRealm mavenRealm = world.getRealmImpl( "maven" ); 172 DefaultClassRealm xmlRealm = world.getRealmImpl( "xml" ); 173 DefaultClassRealm globRealm = world.getRealmImpl( "glob" ); 174 175 assertSame( antRealm, 176 antRealm.locateSourceRealm( "org.apache.tools.Ant" ) ); 177 178 assertSame( xmlRealm, 179 antRealm.locateSourceRealm( "org.xml.sax.SAXException" ) ); 180 181 assertSame( mavenRealm, 182 mavenRealm.locateSourceRealm( "org.apache.maven.app.App" ) ); 183 184 assertSame( xmlRealm, 185 mavenRealm.locateSourceRealm( "org.xml.sax.SAXException" ) ); 186 187 // Test the glob support 188 RealmClassLoader cl = (RealmClassLoader) globRealm.getClassLoader(); 189 URL[] urls = cl.getURLs(); 190 191 assertArrayContains(urls, new File(System.getProperty("basedir") + "/target/test-data/nested.jar").toURL()); 192 assertArrayContains(urls, new File(System.getProperty("basedir") + "/target/test-data/a.jar").toURL()); 193 assertArrayContains(urls, new File(System.getProperty("basedir") + "/target/test-data/b.jar").toURL()); 194 assertArrayContains(urls, new File(System.getProperty("basedir") + "/target/test-data/c.jar").toURL()); 195 } 196 197 public void testConfigure_Unhandled() throws Exception 198 { 199 try 200 { 201 this.configurator.configure( getConfigPath( "unhandled.conf" ) ); 202 fail( "throw ConfigurationException" ); 203 } 204 catch (ConfigurationException e) 205 { 206 // expected and correct 207 assertTrue( e.getMessage().startsWith( "Unhandled configuration" ) ); 208 } 209 } 210 211 public void testFilter_Unterminated() throws Exception 212 { 213 try 214 { 215 this.configurator.filter( "${cheese" ); 216 fail( "throw ConfigurationException" ); 217 } 218 catch (ConfigurationException e) 219 { 220 // expected and correct 221 assertTrue( e.getMessage().startsWith( "Unterminated" ) ); 222 } 223 } 224 225 public void testFilter_Solitary() throws Exception 226 { 227 System.setProperty( "classworlds.test.prop", 228 "test prop value" ); 229 230 String result = this.configurator.filter( "${classworlds.test.prop}" ); 231 232 assertEquals( "test prop value", 233 result ); 234 } 235 236 public void testFilter_AtStart() throws Exception 237 { 238 System.setProperty( "classworlds.test.prop", 239 "test prop value" ); 240 241 String result = this.configurator.filter( "${classworlds.test.prop}cheese" ); 242 243 assertEquals( "test prop valuecheese", 244 result ); 245 } 246 247 public void testFilter_AtEnd() throws Exception 248 { 249 System.setProperty( "classworlds.test.prop", 250 "test prop value" ); 251 252 String result = this.configurator.filter( "cheese${classworlds.test.prop}" ); 253 254 assertEquals( "cheesetest prop value", 255 result ); 256 } 257 258 public void testFilter_Multiple() throws Exception 259 { 260 System.setProperty( "classworlds.test.prop.one", 261 "test prop value one" ); 262 263 System.setProperty( "classworlds.test.prop.two", 264 "test prop value two" ); 265 266 String result = this.configurator.filter( "I like ${classworlds.test.prop.one} and ${classworlds.test.prop.two} a lot" ); 267 268 assertEquals( "I like test prop value one and test prop value two a lot", 269 result ); 270 } 271 272 public void testFilter_NonExistent() throws Exception 273 { 274 try 275 { 276 this.configurator.filter( "${gollygeewillikers}" ); 277 fail( "throw ConfigurationException" ); 278 } 279 catch (ConfigurationException e) 280 { 281 // expected and correct 282 assertTrue( e.getMessage().startsWith( "No such property" ) ); 283 } 284 } 285 286 public void testFilter_InMiddle() throws Exception 287 { 288 System.setProperty( "classworlds.test.prop", 289 "test prop value" ); 290 291 String result = this.configurator.filter( "cheese${classworlds.test.prop}toast" ); 292 293 assertEquals( "cheesetest prop valuetoast", 294 result ); 295 } 296 297 private FileInputStream getConfigPath(String name) 298 throws Exception 299 { 300 return new FileInputStream( new File( new File( System.getProperty( "basedir" ), "target/test-data" ), name ) ) ; 301 } 302 303 private void assertArrayContains(URL[] array, URL url) throws Exception { 304 for (int i = 0; i < array.length; ++i) 305 if (url.equals(array[i])) 306 return; 307 fail("URL (" + url + ") not found in array of URLs"); 308 } 309 }

This page was automatically generated by Maven