View Javadoc

1   /*
2    * Copyright 2004-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.myfaces.test;
18  
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.logging.Logger;
23  
24  import javax.xml.parsers.SAXParser;
25  import javax.xml.parsers.SAXParserFactory;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.myfaces.shared.test.ClassElementHandler;
30  
31  /**
32   * This test makes sure all of our components, tags, renderers, validators, converters, action listeners, phase
33   * listeners and core implementation classes are in the build.
34   * 
35   * This class has been copy and pasted into both tomahawk and core in order to avoid a compile scoped dependency on
36   * junit in shared.
37   * 
38   * @see ClassElementHandler
39   * @author Dennis Byrne
40   */
41  
42  public abstract class AbstractClassElementTestCase extends TestCase
43  {
44  
45      //private Log log = LogFactory.getLog(AbstractClassElementTestCase.class);
46      private Logger log = Logger.getLogger(AbstractClassElementTestCase.class.getName());
47  
48      protected List<String> resource = new ArrayList<String>();
49      private List<String> className = new ArrayList<String>();
50  
51      protected void setUp() throws Exception
52      {
53          SAXParserFactory factory = SAXParserFactory.newInstance();
54          factory.setValidating(false);
55          factory.setNamespaceAware(false);
56  
57          SAXParser parser = factory.newSAXParser();
58          ClassElementHandler handler = new ClassElementHandler();
59  
60          for (String resourceName : resource)
61          {
62              InputStream is = getClass().getClassLoader().getResourceAsStream(resourceName);
63  
64              if (is == null)
65              {
66                  is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
67              }
68  
69              if (is == null)
70              {
71                  throw new Exception("Could not locate resource :" + resourceName);
72              }
73  
74              parser.parse(is, handler);
75  
76          }
77  
78          className.addAll(handler.getClassName());
79      }
80  
81      public void testClassPath()
82      {
83          int i = -1;
84          for (String clazz : className)
85          {
86              try
87              {
88                  i++;
89                  getClass().getClassLoader().loadClass(clazz);
90              }
91              catch (ClassNotFoundException e)
92              {
93                  try
94                  {
95                      Thread.currentThread().getContextClassLoader().loadClass(clazz);
96                  }
97                  catch (ClassNotFoundException e2)
98                  {
99                      assertFalse("Could not load " + clazz, true);
100                 }
101             }
102         }
103 
104         log.fine((i + 1) + " class found ");
105     }
106 }