View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * This test makes sure all of our components, tags, renderers, 
42   * validators, converters, action listeners, phase listeners and
43   * core implementation classes are in the build.
44   * 
45   * This class has been copy and pasted into both tomahawk and core 
46   * in order to avoid a compile scoped dependency on junit in shared.
47   * 
48   * @see ClassElementHandler
49   * @author Dennis Byrne
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 }