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   * @version $Id$
36   */
37  public class JavaComponentFactoryTest
38      extends TestCase
39  {
40      public void testComponentCreation()
41          throws Exception
42      {
43          JavaComponentFactory factory = new JavaComponentFactory();
44  
45          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
46  
47          componentDescriptor.setRole( Component.class.getName() );
48  
49          componentDescriptor.setImplementation( ComponentImplA.class.getName() );
50  
51          ClassWorld classWorld = new ClassWorld();
52  
53          classWorld.newRealm( "core", Thread.currentThread().getContextClassLoader() );
54  
55          PlexusContainer container = new DefaultPlexusContainer( containerConfiguration( classWorld ));
56  
57          Object component = factory.newInstance( componentDescriptor, classWorld.getRealm( "core" ), container );
58  
59          assertNotNull( component );
60      }
61  
62      public void testComponentCreationWithNotMatchingRoleAndImplemenation()
63          throws Exception
64      {
65          JavaComponentFactory factory = new JavaComponentFactory();
66  
67          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
68  
69          componentDescriptor.setRole( Component.class.getName() );
70  
71          componentDescriptor.setImplementation( ComponentImplB.class.getName() );
72  
73          ClassWorld classWorld = new ClassWorld();
74  
75          classWorld.newRealm( "core", Thread.currentThread().getContextClassLoader() );
76  
77          PlexusContainer container = new DefaultPlexusContainer( containerConfiguration( classWorld ));
78  
79          factory.newInstance( componentDescriptor, classWorld.getRealm( "core" ), container );
80      }
81  
82      public void testInstanciationOfAAbstractComponent()
83          throws Exception
84      {
85          JavaComponentFactory factory = new JavaComponentFactory();
86  
87          ComponentDescriptor componentDescriptor = new ComponentDescriptor();
88  
89          componentDescriptor.setRole( Component.class.getName() );
90  
91          componentDescriptor.setImplementation( ComponentImplC.class.getName() );
92  
93          ClassWorld classWorld = new ClassWorld();
94  
95          classWorld.newRealm( "core", Thread.currentThread().getContextClassLoader() );
96  
97          PlexusContainer container = new DefaultPlexusContainer( containerConfiguration( classWorld ) );
98  
99          try
100         {
101             factory.newInstance( componentDescriptor, classWorld.getRealm( "core" ), container );
102 
103             fail( "Expected ComponentInstantiationException when instanciating a abstract class." );
104         }
105         catch( ComponentInstantiationException ex )
106         {
107             assertTrue( true );
108         }
109     }
110 
111     private ContainerConfiguration containerConfiguration( ClassWorld classWorld )
112     {
113         ContainerConfiguration c = new DefaultContainerConfiguration()
114             .setClassWorld( classWorld );
115 
116         return c;
117     }
118 }