View Javadoc
1   package org.apache.fulcrum.factory;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  
25  
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import java.util.ArrayList;
29  
30  import org.apache.fulcrum.testcontainer.BaseUnit5Test;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Basic tests of the fulcrum factory service
36   * 
37   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
38   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
39   * 
40   * @version $Id$ 
41   */
42  public class FactoryServiceTest extends BaseUnit5Test
43  {
44  	/** Default factory service **/
45      private FactoryService factoryService = null;
46  
47      @BeforeEach
48      public void setUp() throws Exception
49      {
50          setConfigurationFileName("src/test/TestComponentConfig.xml");
51          setRoleFileName("src/test/TestRoleConfig.xml");
52          factoryService = (FactoryService) this.lookup(FactoryService.class.getName());    	
53      }
54      
55      /**
56       * Class to test for Object getInstance(String)
57       * @throws Exception if factory fails to generate object
58       */
59      @Test
60      public void testGetInstanceString() throws Exception
61      {
62          Object object = factoryService.getInstance("java.lang.StringBuilder");
63          assertTrue(object instanceof StringBuilder);
64      }
65      
66      /**
67       * Class to test for Object getInstance(String, ClassLoader)
68       *
69       * @throws Exception Generic exception
70       */
71      @Test
72      public void testGetInstanceStringClassLoader() throws Exception
73      {
74          Object object = factoryService.getInstance("java.lang.StringBuilder", StringBuilder.class.getClassLoader());
75          assertTrue(object instanceof StringBuilder);
76      }
77      
78      /**
79       * Class to test for Object getInstance(String, Object[], String[])
80       * @throws Exception Generic exception
81       */
82      @Test
83      public void testGetInstanceStringObjectArrayStringArray() throws Exception
84      {
85      	String sourceValue = "testing";
86      	Object params[] = new Object[] { sourceValue };
87          String signature[] = new String[] { "java.lang.String" };
88  
89          Object object = factoryService.getInstance("java.lang.StringBuilder", params, signature);
90          assertTrue(object instanceof StringBuilder);
91          assertEquals(sourceValue, object.toString());
92      }
93      
94      /**
95       * Class to test for Object getInstance(String, ClassLoader, Object[], String[])
96       * 
97       * @throws Exception Generic exception
98       */    
99      @Test
100     public void testGetInstanceStringClassLoaderObjectArrayStringArray() throws Exception
101     {
102     	String sourceValue = "testing";
103     	Object params[] = new Object[] { sourceValue };
104         String signature[] = new String[] { "java.lang.String" };
105 
106         Object object =
107             factoryService.getInstance(
108                 "java.lang.StringBuilder",
109                 StringBuilder.class.getClassLoader(),
110                 params,
111                 signature);
112         assertTrue(object instanceof StringBuilder);
113         assertEquals(sourceValue, object.toString());
114 
115     }
116     
117     /**
118      * Test if the loader is supported
119      * 
120      * @throws Exception Generic exception
121      */
122     @Test
123     public void testIsLoaderSupported() throws Exception
124     {
125         // TODO Need to run a test where the loader is NOT supported.
126         assertTrue(factoryService.isLoaderSupported("java.lang.String"));
127     }
128     
129 
130     /**
131      * Test get signature
132      * 
133      * @throws Exception Generic exception
134      */
135     @Test
136     public void testGetSignature() throws Exception
137     {
138     	String sourceValue = "testing";
139     	Object params[] = new Object[] { sourceValue };
140         String signature[] = new String[] { "java.lang.String" };
141 
142         Class<?>[] results = factoryService.getSignature(StringBuilder.class, params, signature);
143         assertEquals(1, results.length);
144         assertTrue(results[0].equals(String.class));
145 
146         Integer sourceValueInteger = new Integer(10);
147         params[0] = sourceValueInteger;
148         signature[0] = "java.lang.Integer";
149         results = factoryService.getSignature(ArrayList.class, params, signature);
150         assertEquals(1, results.length);
151         assertTrue(results[0].equals(Integer.class));
152     }
153 }