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.InputStream;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.xml.parsers.SAXParserFactory;
14 import junit.framework.TestCase;
15 import org.xml.sax.InputSource;
16 import org.xml.sax.SAXException;
17 import org.xml.sax.XMLReader;
18
19 /***
20 * Basic unit tests for the catalog handler class.
21 *
22 * @author Peter Donald
23 */
24 public final class CatalogHandlerTestCase
25 extends TestCase
26 {
27 public void testNullEntityList()
28 {
29 try
30 {
31 new CatalogHandler( null );
32 }
33 catch( final NullPointerException npe )
34 {
35 assertEquals( npe.getMessage(), "entitys" );
36 return;
37 }
38 fail( "Expected Null pointer due to null entitys" );
39 }
40
41 public void testEmptyCatalog()
42 throws Exception
43 {
44 final List entitys = new ArrayList();
45 parseCatalog( 1, entitys );
46 assertEquals( "entity count: " + entitys, 0, entitys.size() );
47 }
48
49 public void testSingleWithPublicID()
50 throws Exception
51 {
52 final List entitys = new ArrayList();
53 parseCatalog( 2, entitys );
54 assertEquals( "entity count: " + entitys, 1, entitys.size() );
55 final EntityInfo info = (EntityInfo)entitys.get( 0 );
56 assertEquals( "info.getPublicId()",
57 "-//PHOENIX/Assembly DTD Version 1.0//EN",
58 info.getPublicId() );
59 assertEquals( "info.getSystemId()", null, info.getSystemId() );
60 assertEquals( "info.getResource()",
61 "org/apache/avalon/phoenix/tools/assembly.dtd",
62 info.getResource() );
63 }
64
65 public void testSingleWithSystemID()
66 throws Exception
67 {
68 final List entitys = new ArrayList();
69 parseCatalog( 3, entitys );
70 assertEquals( "entity count: " + entitys, 1, entitys.size() );
71 final EntityInfo info = (EntityInfo)entitys.get( 0 );
72 assertEquals( "info.getPublicId()",
73 null,
74 info.getPublicId() );
75 assertEquals( "info.getSystemId()",
76 "http://jakarta.apache.org/phoenix/assembly_1_0.dtd",
77 info.getSystemId() );
78 assertEquals( "info.getResource()",
79 "org/apache/avalon/phoenix/tools/assembly.dtd",
80 info.getResource() );
81 }
82
83 public void testSingleWithBothID()
84 throws Exception
85 {
86 final List entitys = new ArrayList();
87 parseCatalog( 4, entitys );
88 assertEquals( "entity count: " + entitys, 1, entitys.size() );
89 final EntityInfo info = (EntityInfo)entitys.get( 0 );
90 assertEquals( "info.getPublicId()",
91 "-//PHOENIX/Assembly DTD Version 1.0//EN",
92 info.getPublicId() );
93 assertEquals( "info.getSystemId()",
94 "http://jakarta.apache.org/phoenix/assembly_1_0.dtd",
95 info.getSystemId() );
96 assertEquals( "info.getResource()",
97 "org/apache/avalon/phoenix/tools/assembly.dtd",
98 info.getResource() );
99 }
100
101 public void testTriple()
102 throws Exception
103 {
104 final List entitys = new ArrayList();
105 parseCatalog( 5, entitys );
106 assertEquals( "entity count: " + entitys, 3, entitys.size() );
107
108 final EntityInfo info1 = (EntityInfo)entitys.get( 0 );
109 assertEquals( "info1.getPublicId()",
110 "-//PHOENIX/Assembly DTD Version 1.0//EN",
111 info1.getPublicId() );
112 assertEquals( "info1.getSystemId()", null, info1.getSystemId() );
113 assertEquals( "info1.getResource()",
114 "org/apache/avalon/phoenix/tools/assembly.dtd",
115 info1.getResource() );
116
117 final EntityInfo info2 = (EntityInfo)entitys.get( 1 );
118 assertEquals( "info2.getPublicId()",
119 null,
120 info2.getPublicId() );
121 assertEquals( "info2.getSystemId()",
122 "http://jakarta.apache.org/phoenix/assembly_1_0.dtd",
123 info2.getSystemId() );
124 assertEquals( "info2.getResource()",
125 "org/apache/avalon/phoenix/tools/assembly.dtd",
126 info2.getResource() );
127
128 final EntityInfo info3 = (EntityInfo)entitys.get( 2 );
129 assertEquals( "info3.getPublicId()",
130 "-//PHOENIX/Assembly DTD Version 1.0//EN",
131 info3.getPublicId() );
132 assertEquals( "info3.getSystemId()",
133 "http://jakarta.apache.org/phoenix/assembly_1_0.dtd",
134 info3.getSystemId() );
135 assertEquals( "info3.getResource()",
136 "org/apache/avalon/phoenix/tools/assembly.dtd",
137 info3.getResource() );
138 }
139
140 public void testNullVersion()
141 {
142 try
143 {
144 final List entitys = new ArrayList();
145 parseCatalog( 6, entitys );
146 fail( "Expected exception due to null version" );
147 }
148 catch( final SAXException se )
149 {
150 return;
151 }
152 catch( final Exception e )
153 {
154 fail( "Unexpected exception " + e );
155 }
156 }
157
158 public void testBadVersion()
159 {
160 try
161 {
162 final List entitys = new ArrayList();
163 parseCatalog( 7, entitys );
164 fail( "Expected exception due to bad version" );
165 }
166 catch( final SAXException se )
167 {
168 return;
169 }
170 catch( final Exception e )
171 {
172 fail( "Unexpected exception " + e );
173 }
174 }
175
176 public void testNullResource()
177 {
178 try
179 {
180 final List entitys = new ArrayList();
181 parseCatalog( 8, entitys );
182 fail( "Expected exception due to null resource" );
183 }
184 catch( final SAXException se )
185 {
186 return;
187 }
188 catch( final Exception e )
189 {
190 fail( "Unexpected exception " + e );
191 }
192 }
193
194 public void testNullIds()
195 {
196 try
197 {
198 final List entitys = new ArrayList();
199 parseCatalog( 9, entitys );
200 fail( "Expected exception due to null ids" );
201 }
202 catch( final SAXException se )
203 {
204 return;
205 }
206 catch( final Exception e )
207 {
208 fail( "Unexpected exception " + e );
209 }
210 }
211
212 public void testBadElements()
213 {
214 try
215 {
216 final List entitys = new ArrayList();
217 parseCatalog( 10, entitys );
218 fail( "Expected exception due to bad elements" );
219 }
220 catch( final SAXException se )
221 {
222 return;
223 }
224 catch( final Exception e )
225 {
226 fail( "Unexpected exception " + e );
227 }
228 }
229
230 private void parseCatalog( final int number, final List entitys )
231 throws Exception
232 {
233 final XMLReader xmlReader = createXMLReader();
234 final CatalogHandler handler = new CatalogHandler( entitys );
235 xmlReader.setContentHandler( handler );
236 xmlReader.setErrorHandler( handler );
237 final InputStream inputStream =
238 getClass().getResourceAsStream( "test/catalog" + number + ".xml" );
239
240 xmlReader.parse( new InputSource( inputStream ) );
241 }
242
243 /***
244 * Create an XMLReader.
245 *
246 * @return the created XMLReader
247 */
248 private static XMLReader createXMLReader()
249 {
250 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
251 saxParserFactory.setNamespaceAware( false );
252 try
253 {
254 return saxParserFactory.newSAXParser().getXMLReader();
255 }
256 catch( final Exception e )
257 {
258 throw new IllegalStateException( e.toString() );
259 }
260 }
261 }
262
This page was automatically generated by Maven