1   package org.apache.stratum.component;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation or its licensors,
5    * as applicable.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.configuration.Configuration;
23  import org.apache.commons.configuration.ConfigurationException;
24  import org.apache.commons.configuration.PropertiesConfiguration;
25  import org.apache.log4j.Category;
26  
27  /***
28   * TODO: DOCUMENT ME!
29   *
30   * @author $author$
31   * @version $Id: TestComponentLoader.java 264191 2005-08-29 18:07:52Z henning $
32   */
33  public class TestComponentLoader
34          extends TestCase
35  {
36      /*** TODO: DOCUMENT ME! */
37      private static Category log = Category.getInstance(TestComponentLoader.class);
38  
39      /*** TODO: DOCUMENT ME! */
40      private static String CONFIG = "src/test-conf/ComponentLoader.properties";
41  
42      /*** TODO: DOCUMENT ME! */
43      private static Configuration config;
44  
45      static
46      {
47          try
48          {
49              config = new PropertiesConfiguration(CONFIG);
50          }
51          catch (ConfigurationException ce)
52          {
53              log.error("unable to configure ComponentLoader from '" + CONFIG + "'.");
54          }
55      }
56  
57      /*** TODO: DOCUMENT ME! */
58      private ComponentLoader cl = new ComponentLoader(config);
59  
60      /***
61       * Creates a new TestComponentLoader object.
62       *
63       * @param name TODO: DOCUMENT ME!
64       */
65      public TestComponentLoader(String name)
66      {
67          super(name);
68      }
69  
70      /***
71       * TODO: DOCUMENT ME!
72       */
73      public void testConstructor()
74      {
75          ComponentLoader loader = new ComponentLoader(config);
76          Configuration loaderConfig = loader.getConfiguration();
77          assertNotNull(loaderConfig);
78          assertEquals("org.apache.stratum.component.MockComponent", loaderConfig.getString("component.testing.classname"));
79      }
80  
81      /***
82       * TODO: DOCUMENT ME!
83       */
84      public void testLoadComponent()
85      {
86          String goodClassName = "org.apache.stratum.component.MockComponent";
87          String goodConfigFile = "src/test-conf/TestComponentLoader.properties";
88          String badClassName = "org.apache.stratum.component.MissingMockComponent";
89          String badConfigFile = "src/test-conf/MissingTestComponentLoader.properties";
90  
91          // test loading with a mock component
92          MockComponent comp = (MockComponent) cl.loadComponent(goodClassName, goodConfigFile, null);
93  
94          // test that loadComponent() catches the IOException
95          cl.loadComponent(badClassName, goodConfigFile, null);
96  
97          // test that loadComponent() catches the ClassNotFoundException
98          cl.loadComponent(goodClassName, badConfigFile, null);
99      }
100 
101     /***
102      * TODO: DOCUMENT ME!
103      */
104     public void testLoad()
105     {
106         assertNotNull(cl);
107 
108         Object [] comps = cl.load();
109 
110         int key1FinderCount = 0;
111 
112         for (int i = 0; i < comps.length; i++)
113         {
114             if (comps[i] instanceof MockComponent)
115             {
116                 MockComponent mc = (MockComponent) comps[i];
117                 Configuration config = mc.getConfiguration();
118 
119                 if (config.getProperty("key1") != null)
120                 {
121                     assertEquals("ensure key2 is present and correct", "value2", config.getProperty("key2"));
122                     key1FinderCount++;
123                 }
124             }
125         }
126 
127         assertEquals("should only find key1 once", key1FinderCount, 1);
128     }
129 }