View Javadoc
1   package org.codehaus.plexus.component.factory.java;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  import org.codehaus.plexus.ContainerConfiguration;
21  import org.codehaus.plexus.DefaultContainerConfiguration;
22  import org.codehaus.plexus.DefaultPlexusContainer;
23  import org.codehaus.plexus.PlexusContainer;
24  import org.codehaus.plexus.classworlds.ClassWorld;
25  import org.codehaus.plexus.component.factory.Component;
26  import org.codehaus.plexus.component.factory.ComponentImplA;
27  import org.codehaus.plexus.component.factory.ComponentImplB;
28  import org.codehaus.plexus.component.factory.ComponentImplC;
29  import org.codehaus.plexus.component.factory.ComponentInstantiationException;
30  import org.codehaus.plexus.component.repository.ComponentDescriptor;
31  
32  /**
33   * @author Jason van Zyl
34   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
35   */
36  public class JavaComponentFactoryTest extends TestCase {
37      public void testComponentCreation() throws Exception {
38          JavaComponentFactory factory = new JavaComponentFactory();
39  
40          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
41  
42          componentDescriptor.setRole(Component.class.getName());
43  
44          componentDescriptor.setImplementation(ComponentImplA.class.getName());
45  
46          ClassWorld classWorld = new ClassWorld();
47  
48          classWorld.newRealm("core", Thread.currentThread().getContextClassLoader());
49  
50          PlexusContainer container = new DefaultPlexusContainer(containerConfiguration(classWorld));
51  
52          Object component = factory.newInstance(componentDescriptor, classWorld.getRealm("core"), container);
53  
54          assertNotNull(component);
55      }
56  
57      public void testComponentCreationWithNotMatchingRoleAndImplemenation() throws Exception {
58          JavaComponentFactory factory = new JavaComponentFactory();
59  
60          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
61  
62          componentDescriptor.setRole(Component.class.getName());
63  
64          componentDescriptor.setImplementation(ComponentImplB.class.getName());
65  
66          ClassWorld classWorld = new ClassWorld();
67  
68          classWorld.newRealm("core", Thread.currentThread().getContextClassLoader());
69  
70          PlexusContainer container = new DefaultPlexusContainer(containerConfiguration(classWorld));
71  
72          factory.newInstance(componentDescriptor, classWorld.getRealm("core"), container);
73      }
74  
75      public void testInstanciationOfAAbstractComponent() throws Exception {
76          JavaComponentFactory factory = new JavaComponentFactory();
77  
78          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
79  
80          componentDescriptor.setRole(Component.class.getName());
81  
82          componentDescriptor.setImplementation(ComponentImplC.class.getName());
83  
84          ClassWorld classWorld = new ClassWorld();
85  
86          classWorld.newRealm("core", Thread.currentThread().getContextClassLoader());
87  
88          PlexusContainer container = new DefaultPlexusContainer(containerConfiguration(classWorld));
89  
90          try {
91              factory.newInstance(componentDescriptor, classWorld.getRealm("core"), container);
92  
93              fail("Expected ComponentInstantiationException when instanciating a abstract class.");
94          } catch (ComponentInstantiationException ex) {
95              assertTrue(true);
96          }
97      }
98  
99      private ContainerConfiguration containerConfiguration(ClassWorld classWorld) {
100         ContainerConfiguration c = new DefaultContainerConfiguration().setClassWorld(classWorld);
101 
102         return c;
103     }
104 }