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.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.apache.myfaces.shared_tomahawk.test.ClassElementHandler;
36  import org.xml.sax.EntityResolver;
37  import org.xml.sax.InputSource;
38  import org.xml.sax.SAXException;
39  import org.xml.sax.XMLReader;
40  
41  /**
42   * This test makes sure all of our components, tags, renderers, 
43   * validators, converters, action listeners, phase listeners and
44   * core implementation classes are in the build.
45   * 
46   * This class has been copy and pasted into both tomahawk and core 
47   * in order to avoid a compile scoped dependency on junit in shared.
48   * 
49   * @see ClassElementHandler
50   * @author Dennis Byrne
51   */
52  
53  public abstract class AbstractClassElementTestCase extends TestCase
54  {
55  
56      private Log log = LogFactory.getLog(AbstractClassElementTestCase.class);
57      
58      protected List resource = new ArrayList();
59      private List className = new ArrayList();
60          
61      public class DelegateEntityResolver extends ClassElementHandler
62      {
63          public DelegateEntityResolver()
64          {
65              super();
66          }
67  
68          public InputSource resolveEntity(String publicId, String systemId)
69                  throws SAXException, IOException
70          {
71              if (publicId.equals("-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"))
72              {
73                  return new InputSource(Thread.currentThread()
74                          .getContextClassLoader().getResourceAsStream(
75                                  "META-INF/dtd/web-jsptaglibrary_1_2.dtd"));
76              }
77              else if (publicId.equals("-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"))
78              {
79                  return new InputSource(Thread.currentThread()
80                          .getContextClassLoader().getResourceAsStream(
81                                  "org/apache/myfaces/resource/web-facesconfig_1_1.dtd"));                
82              }
83              else
84              {
85                  return super.resolveEntity(publicId, systemId);
86              }
87          }
88      }    
89      
90      protected void setUp() throws Exception
91      {
92          SAXParserFactory factory = SAXParserFactory.newInstance();
93          factory.setValidating(false);
94          factory.setNamespaceAware(false);
95  
96          SAXParser parser = factory.newSAXParser();
97          ClassElementHandler handler = new DelegateEntityResolver();
98          
99          Iterator iterator = resource.iterator();
100         
101         while(iterator.hasNext()){
102             
103             String resourceName = (String) iterator.next();
104             
105             InputStream is = getClass().getClassLoader()
106                 .getResourceAsStream(resourceName);
107         
108             if(is == null)
109                 is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
110         
111             if(is == null)
112                 throw new Exception("Could not locate resource :" + resourceName);
113         
114             parser.parse(is, handler);
115             
116         }
117         
118         className.addAll(handler.getClassName());
119         
120     }
121     
122     public void testClassPath(){
123         
124         int i = 0;
125         for(  ; i < className.size() ; i++){
126             
127             String clazz = (String) className.get(i);
128             
129             try
130             {
131                 getClass().getClassLoader().loadClass(clazz);
132                 
133             }
134             catch (ClassNotFoundException e)
135             {
136                 
137                 try{
138                     
139                     Thread.currentThread().getContextClassLoader().loadClass(clazz);
140                     
141                 }catch(ClassNotFoundException e2){
142                     
143                     assertFalse("Could not load " + clazz, true); 
144                     
145                 }
146                 
147             }
148             
149         }
150         
151         log.debug(( i + 1 ) + " class found ");
152         
153     }
154     
155 }