1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.commons.test;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.xml.parsers.SAXParser;
29 import javax.xml.parsers.SAXParserFactory;
30
31 import junit.framework.TestCase;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.xml.sax.EntityResolver;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.XMLReader;
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public abstract class AbstractClassElementTestCase extends TestCase
53 {
54
55 private Log log = LogFactory.getLog(AbstractClassElementTestCase.class);
56
57 protected List resource = new ArrayList();
58 private List className = new ArrayList();
59
60 public class DelegateEntityResolver extends ClassElementHandler
61 {
62 public DelegateEntityResolver()
63 {
64 super();
65 }
66
67 public InputSource resolveEntity(String publicId, String systemId)
68 throws SAXException, IOException
69 {
70 if (publicId.equals("-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"))
71 {
72 return new InputSource(Thread.currentThread()
73 .getContextClassLoader().getResourceAsStream(
74 "META-INF/dtd/web-jsptaglibrary_1_2.dtd"));
75 }
76 else if (publicId.equals("-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"))
77 {
78 return new InputSource(Thread.currentThread()
79 .getContextClassLoader().getResourceAsStream(
80 "META-INF/dtd/web-facesconfig_1_1.dtd"));
81 }
82 else
83 {
84 return super.resolveEntity(publicId, systemId);
85 }
86 }
87 }
88
89 protected void setUp() throws Exception
90 {
91 SAXParserFactory factory = SAXParserFactory.newInstance();
92 factory.setValidating(false);
93 factory.setNamespaceAware(false);
94
95 SAXParser parser = factory.newSAXParser();
96 ClassElementHandler handler = new DelegateEntityResolver();
97
98 Iterator iterator = resource.iterator();
99
100 while (iterator.hasNext())
101 {
102 String resourceName = (String) iterator.next();
103
104 InputStream is = getClass().getClassLoader().getResourceAsStream(
105 resourceName);
106
107 if (is == null)
108 is = Thread.currentThread().getContextClassLoader()
109 .getResourceAsStream(resourceName);
110
111 if (is == null)
112 throw new Exception("Could not locate resource :"
113 + resourceName);
114
115 parser.parse(is, handler);
116 }
117
118 className.addAll(handler.getClassName());
119 }
120
121 public void testClassPath()
122 {
123 int i = 0;
124 for (; i < className.size(); i++)
125 {
126 String clazz = (String) className.get(i);
127
128 try
129 {
130 getClass().getClassLoader().loadClass(clazz);
131 }
132 catch (ClassNotFoundException e)
133 {
134 try
135 {
136 Thread.currentThread().getContextClassLoader().loadClass(
137 clazz);
138 }
139 catch (ClassNotFoundException e2)
140 {
141 assertFalse("Could not load " + clazz, true);
142 }
143 }
144 }
145 log.debug((i + 1) + " class found ");
146 }
147 }