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.ByteArrayInputStream;
11 import java.io.File;
12 import java.net.URL;
13 import junit.framework.TestCase;
14 import org.xml.sax.InputSource;
15
16 /***
17 * Basic unit tests for the catalog handler class.
18 *
19 * @author Peter Donald
20 */
21 public final class ComponentConfigUtilTestCase
22 extends TestCase
23 {
24 public void testInstantiateInstance()
25 {
26 new ComponentConfigUtil();
27 }
28
29 public void testCalculatedLocationThatIsSpecified()
30 throws Exception
31 {
32 final String resource =
33 ComponentConfigUtil.calculateLocation( "com.biz.Foo",
34 "Schema.xml" );
35 assertEquals( "/com/biz/Schema.xml", resource );
36 }
37
38 public void testCalculatedLocationThatIsNotSpecified()
39 throws Exception
40 {
41 final String resource =
42 ComponentConfigUtil.calculateLocation( "com.biz.Foo", null );
43 assertEquals( "/com/biz/Foo-schema.xml", resource );
44 }
45
46 public void testCalculatedLocationThatIsSpecifiedAsAnAbsolute()
47 throws Exception
48 {
49 final String resource =
50 ComponentConfigUtil.calculateLocation( "com.biz.Foo", "/Foo.xml" );
51 assertEquals( "/Foo.xml", resource );
52 }
53
54 public void testCalculatedLocationThatIsSpecifiedForClassInBasePackage()
55 throws Exception
56 {
57 final String resource =
58 ComponentConfigUtil.calculateLocation( "Baz", "Foo.xml" );
59 assertEquals( "/Foo.xml", resource );
60 }
61
62 public void testGetSchemaInputSource()
63 throws Exception
64 {
65 final String resource = "foo.dtd";
66 final URL url = new File( "." ).toURL();
67 final ByteArrayInputStream stream =
68 new ByteArrayInputStream( new byte[ 0 ] );
69 final MockClassLoader classLoader = new MockClassLoader( url, stream );
70 final InputSource source =
71 ComponentConfigUtil.getSchemaInputSource( resource, classLoader );
72 assertNotNull( source );
73 assertEquals( "systemID", url.toExternalForm(), source.getSystemId() );
74 }
75
76 public void testGetSchemaInputSourceWithNonExistent()
77 throws Exception
78 {
79 final String resource = "foo.dtd";
80 final MockClassLoader classLoader = new MockClassLoader( null, null );
81 final InputSource source =
82 ComponentConfigUtil.getSchemaInputSource( resource, classLoader );
83 assertNull( source );
84 }
85
86 public void testGetComponentConfigValidator()
87 throws Exception
88 {
89 final ConfigValidator validator =
90 ComponentConfigUtil.getComponentConfigValidator(
91 "org.codehaus.spice.configkit.test.Baz",
92 getClass().getClassLoader(),
93 "assembly.dtd",
94 null );
95 assertNotNull( validator );
96 }
97
98 public void testGetComponentConfigValidatorWithNullClassLoader()
99 throws Exception
100 {
101 try
102 {
103 ComponentConfigUtil.getComponentConfigValidator( "",
104 null,
105 "assembly.dtd",
106 null );
107 }
108 catch( final NullPointerException npe )
109 {
110 assertEquals( "npe.message", "classLoader", npe.getMessage() );
111 return;
112 }
113 fail( "Expected npe" );
114 }
115
116 public void testGetComponentConfigValidatorWithNullClassName()
117 throws Exception
118 {
119 try
120 {
121 ComponentConfigUtil.getComponentConfigValidator( null,
122 getClass()
123 .getClassLoader(),
124 "assembly.dtd",
125 null );
126 }
127 catch( final NullPointerException npe )
128 {
129 assertEquals( "npe.message", "classname", npe.getMessage() );
130 return;
131 }
132 fail( "Expected npe" );
133 }
134 }
This page was automatically generated by Maven