View Javadoc
1   package org.codehaus.plexus.component.composition;
2   
3   import org.codehaus.plexus.PlexusTestCase;
4   import static org.codehaus.plexus.PlexusConstants.PLEXUS_DEFAULT_HINT;
5   import org.codehaus.plexus.classworlds.realm.ClassRealm;
6   
7   import java.io.File;
8   import java.net.URL;
9   import java.util.Map;
10  import java.util.List;
11  import java.util.TreeMap;
12  
13  /** @author Jason van Zyl */
14  public class ComponentRealmCompositionTest
15      extends PlexusTestCase
16  {
17      //
18      // Component archives
19      //
20      private static final String PLUGIN_0_JAR = "src/test/test-components/plugin0-1.0-SNAPSHOT.jar";
21      private static final String PLUGIN_1_JAR = "src/test/test-components/plugin1-1.0-SNAPSHOT.jar";
22      private static final String COMPONENT_A_JAR = "src/test/test-components/component-a-1.0-SNAPSHOT.jar";
23      private static final String COMPONENT_B_JAR = "src/test/test-components/component-b-1.0-SNAPSHOT.jar";
24      private static final String COMPONENT_C_JAR = "src/test/test-components/component-c-1.0-SNAPSHOT.jar";
25      private static final String ARCHIVER_JAR = "src/test/test-components/plexus-archiver-1.0-alpha-8.jar";
26  
27      //
28      // Component roles
29      //
30      private static final String PLUGIN_0_ROLE = "org.codehaus.plexus.plugins.Plugin0";
31      private static final String PLUGIN_1_ROLE = "org.codehaus.plexus.plugins.Plugin1";
32  
33      //
34      // Component realms
35      //
36      private static final String PLUGIN_0_REALM = "plugin0Realm";
37      private static final String PLUGIN_1_REALM = "plugin1Realm";
38  
39      protected void setUp() throws Exception
40      {
41          super.setUp();
42  
43          // Create ClassRealm plugin0 with plugin0 -> A, plugin0 -> B
44          createClassRealm( PLUGIN_0_REALM,
45              PLUGIN_0_JAR,
46              COMPONENT_A_JAR,
47              COMPONENT_B_JAR,
48              ARCHIVER_JAR );
49  
50          // Create ClassRealm plugin1 with plugin1 -> A, plugin1 -> C
51          createClassRealm( PLUGIN_1_REALM,
52              PLUGIN_1_JAR,
53              COMPONENT_A_JAR,
54              COMPONENT_C_JAR,
55              ARCHIVER_JAR );
56      }
57  
58      /*
59      * We are testing that when the same component implementation exists in more then one
60      * realm and components depend on those implementations, that the right realm is used
61      * to wire up the components.
62      *
63      * An example of this in practice are Maven plugins where each plugin is loaded into
64      * a separate realm and the plugin may have dependencies on other components. We want
65      * to make sure that a requirement, say a JarArchiver, for a given component, say the
66      * maven-jar-plugin, is wired up with a JarArchiver taken from the same realm as the
67      * maven-jar-plugin and not a different realm.
68      */
69  
70      public void testCompositionWhereTheSameImplementationExistsInDifferentRealms()
71          throws Exception
72      {
73          // Plugin0
74          getContainer().lookup( PLUGIN_0_ROLE );
75  
76  
77          // Plugin1
78          getContainer().lookup( PLUGIN_1_ROLE );
79  
80          // Plugin0(alt)
81          getContainer().lookup( PLUGIN_0_ROLE, "alt" );
82  
83  
84          // Plugin1(alt)
85          getContainer().lookup( PLUGIN_1_ROLE, "alt" );
86      }
87  
88      public void testThatASingletonComponentIntheCoreRealmWhenLookedUpInComponentRealmsYieldsTheSameInstance()
89          throws Exception
90      {
91      }
92  
93      public void testMultiRealmLookupMap()
94          throws Exception
95      {
96          Map<String,Object> plugin0Map = getContainer().lookupMap( PLUGIN_0_ROLE );
97          assertNotNull("plugin0Map is null", plugin0Map );
98          assertNotNull("plugin0Map does not contain a DefaultPlugin0", plugin0Map.get( PLEXUS_DEFAULT_HINT));
99          assertNotNull("plugin0Map does not contain a AltPlugin0", plugin0Map.get( "alt"));
100         assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size());
101 
102         Map<String,Object> plugin1Map = getContainer().lookupMap( PLUGIN_1_ROLE );
103         assertNotNull("plugin1Map is null", plugin1Map);
104         assertNotNull("plugin1Map does not contain a DefaultPlugin1", plugin1Map.get( PLEXUS_DEFAULT_HINT));
105         assertNotNull("plugin1Map does not contain a AltPlugin1", plugin1Map.get( "alt"));
106         assertEquals("Expected only 2 components in plugin1Map", 2, plugin1Map.size());
107 
108     }
109 
110     public void testMultiRealmLookupList()
111         throws Exception
112     {
113         List<Object> plugin0List = getContainer().lookupList( PLUGIN_0_ROLE );
114         assertNotNull("plugin0List is null", plugin0List );
115         Map<String, Object> plugin0Map = mapByClassSimpleName( plugin0List );
116         assertNotNull("plugin0List does not contain a DefaultPlugin0", plugin0Map.get( "DefaultPlugin0"));
117         assertNotNull("plugin0List does not contain a AltPlugin0", plugin0Map.get( "AltPlugin0"));
118         assertEquals("Expected only 2 components in plugin0Map", 2, plugin0Map.size());
119 
120         List<Object> plugin1List = getContainer().lookupList( PLUGIN_1_ROLE );
121         assertNotNull("plugin1List is null", plugin1List );
122         Map<String, Object> plugin1Map = mapByClassSimpleName( plugin1List );
123         assertNotNull("plugin1List does not contain a DefaultPlugin1", plugin1Map.get( "DefaultPlugin1"));
124         assertNotNull("plugin1List does not contain a AltPlugin1", plugin1Map.get( "AltPlugin1"));
125         assertEquals("Expected only 2 components in plugin0Map", 2, plugin1Map.size());
126     }
127 
128     private ClassRealm createClassRealm(String id, String... jars)
129         throws Exception
130     {
131         // create the realm
132         ClassRealm classRealm = getContainer().createChildRealm( id );
133 
134         // populate the realm
135         for ( String jar : jars )
136         {
137             File file = new File(jar);
138             assertTrue( jar + " is not a file", file.isFile() );
139 
140             URL url = file.toURI().toURL();
141             classRealm.addURL( url );
142         }
143 
144         // descover all component definitions in the realm and register them with the repository
145         getContainer().discoverComponents( classRealm );
146 
147         return classRealm;
148     }
149 
150     private Map<String, Object> mapByClassSimpleName(List<Object> objects)
151     {
152         Map<String, Object> map = new TreeMap<String, Object>();
153         for ( Object object : objects )
154         {
155             map.put(object.getClass().getSimpleName(), object);
156         }
157         return map;
158     }
159 }