1   /*
2    * Copyright (C) The DNA Group. All rights reserved.
3    *
4    * This software is published under the terms of the DNA
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.dna.impl;
9   
10  import junit.framework.TestCase;
11  
12  import org.codehaus.dna.ResourceLocator;
13  import org.codehaus.dna.impl.ConsoleLogger;
14  import org.codehaus.dna.impl.ContainerUtil;
15  import org.codehaus.dna.impl.DefaultConfiguration;
16  import org.codehaus.dna.impl.DefaultResourceLocator;
17  
18  public class ContainerUtilTestCase
19      extends TestCase
20  {
21      public void testEnableLoggingOnComponentNotImplementingStage()
22          throws Exception
23      {
24          final Object object = new Object();
25          ContainerUtil.enableLogging( object, null );
26      }
27  
28      public void testEnableLoggingOnComponentImplementingStage()
29          throws Exception
30      {
31          final MockComponent object = new MockComponent();
32          final ConsoleLogger logger = new ConsoleLogger();
33  
34          ContainerUtil.enableLogging( object, logger );
35  
36          assertEquals( logger, object.getLogger() );
37      }
38  
39      public void testEnableLoggingOnComponentImplementingStageButNullLogger()
40          throws Exception
41      {
42          final MockComponent object = new MockComponent();
43          final ConsoleLogger logger = null;
44  
45          try
46          {
47              ContainerUtil.enableLogging( object, logger );
48          }
49          catch( IllegalArgumentException iae )
50          {
51              return;
52          }
53          fail( "Expected stage to fail as passing in null " +
54                "resource but object implements stage." );
55      }
56  
57      public void testComposeOnComponentNotImplementingStage()
58          throws Exception
59      {
60          final Object object = new Object();
61          ContainerUtil.compose( object, null );
62      }
63  
64      public void testComposeOnComponentImplementingStage()
65          throws Exception
66      {
67          final MockComponent object = new MockComponent();
68          final ResourceLocator resource = new DefaultResourceLocator();
69  
70          ContainerUtil.compose( object, resource );
71  
72          assertEquals( resource, object.getServices() );
73      }
74  
75      public void testComposeOnComponentImplementingStageButNullLogger()
76          throws Exception
77      {
78          final MockComponent object = new MockComponent();
79          final ResourceLocator resource = null;
80  
81          try
82          {
83              ContainerUtil.compose( object, resource );
84          }
85          catch( IllegalArgumentException iae )
86          {
87              return;
88          }
89          fail( "Expected stage to fail as passing in null " +
90                "resource but object implements stage." );
91      }
92  
93      public void testConfigureOnComponentNotImplementingStage()
94          throws Exception
95      {
96          final Object object = new Object();
97          ContainerUtil.configure( object, null );
98      }
99  
100     public void testConfigureOnComponentImplementingStage()
101         throws Exception
102     {
103         final MockComponent object = new MockComponent();
104         final DefaultConfiguration resource = new DefaultConfiguration( "s", "", "" );
105 
106         ContainerUtil.configure( object, resource );
107 
108         assertEquals( resource, object.getConfiguration() );
109     }
110 
111     public void testConfigureOnComponentImplementingStageButNullLogger()
112         throws Exception
113     {
114         final MockComponent object = new MockComponent();
115         final DefaultConfiguration resource = null;
116 
117         try
118         {
119             ContainerUtil.configure( object, resource );
120         }
121         catch( IllegalArgumentException iae )
122         {
123             return;
124         }
125         fail( "Expected stage to fail as passing in null " +
126               "resource but object implements stage." );
127     }
128 
129     public void testInitializeOnComponentNotImplementingStage()
130         throws Exception
131     {
132         final Object object = new Object();
133         ContainerUtil.initialize( object );
134     }
135 
136     public void testInitializeOnComponentImplementingStage()
137         throws Exception
138     {
139         final MockComponent object = new MockComponent();
140         ContainerUtil.initialize( object );
141         assertEquals( true, object.isInitialized() );
142     }
143 
144     public void testDisposeOnComponentNotImplementingStage()
145         throws Exception
146     {
147         final Object object = new Object();
148         ContainerUtil.dispose( object );
149     }
150 
151     public void testDisposeOnComponentImplementingStage()
152         throws Exception
153     {
154         final MockComponent object = new MockComponent();
155         ContainerUtil.dispose( object );
156         assertEquals( true, object.isDisposed() );
157     }
158 }