1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // TestComposite.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.structural.composite;
22  
23  import java.util.*;
24  
25  import junit.framework.*;
26  
27  import org.perfectjpattern.core.api.structural.composite.*;
28  import org.slf4j.*;
29  
30  /**
31   * Test Suite for the {@link Composite} implementation.
32   * 
33   * @see Composite
34   * 
35   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36   * @version $Revision: 1.0 $ $Date: Nov 18, 2007 11:58:58 PM $
37   */
38  public 
39  class TestComposite
40  extends TestCase
41  {
42      //------------------------------------------------------------------------
43      // public
44      //------------------------------------------------------------------------
45      public void
46      testComposition()
47      {
48          theLogger.debug("Step #1: Create four Component elements");
49          ISomeComponent myComponent1 = new SomeComponent();
50          ISomeComponent myComponent2 = new SomeComponent();
51          ISomeComponent myComponent3 = new SomeComponent();
52          ISomeComponent myComponent4 = new SomeComponent();
53                  
54          theLogger.debug("Step #2: Create Composite 1");
55          IComposite<ISomeComponent> myComposite1 = new Composite<ISomeComponent>(
56              ISomeComponent.class);  
57  
58          theLogger.debug("Step #3: Create Composite 2");
59          IComposite<ISomeComponent> myComposite2 = new Composite<ISomeComponent>(
60              ISomeComponent.class);  
61  
62          theLogger.debug("Step #4: Create compositions");
63          myComposite1.addAll(myComponent1, myComponent2);
64          
65          myComposite2.addAll(myComponent3, myComponent4, myComposite1.
66              getComponent());
67          
68          theLogger.debug("Step #5: Call a method on the Composite " +
69              "Component view that triggers execution over the whole " +
70              "composition");
71          myComposite2.getComponent().printValue("Hello World!");
72          
73          theLogger.debug("Prepare to run assertions");
74          Collection<ISomeComponent> myComponents = new ArrayList<
75              ISomeComponent>();
76          myComponents.add(myComponent1);
77          myComponents.add(myComponent2);
78          myComponents.add(myComponent3);
79          myComponents.add(myComponent4);        
80          
81          theLogger.debug("Running assertions");
82          for (ISomeComponent myComponent : myComponents)
83          {
84              assertTrue("All Elements must have been called.", ((SomeComponent) 
85                  myComponent).isCalled());
86          }
87      }
88      
89      //------------------------------------------------------------------------
90      // inner classes
91      //------------------------------------------------------------------------
92      /**
93       * Definition of an example Component interface.
94       */
95      public static
96      interface ISomeComponent
97      {
98          //--------------------------------------------------------------------
99          public void 
100         printValue(String aValue);
101     }    
102     
103     //------------------------------------------------------------------------
104     /**
105      * Implementation of the interface defined above. 
106      */
107     private static
108     class SomeComponent
109     implements ISomeComponent
110     {
111         //--------------------------------------------------------------------
112         public void 
113         printValue(String aValue)
114         {
115             theLogger.debug(aValue);
116             
117             theCalled = true;
118         }        
119         
120         //--------------------------------------------------------------------
121         public boolean
122         isCalled()
123         {
124             return theCalled;
125         }
126         
127         //--------------------------------------------------------------------
128         // members
129         //--------------------------------------------------------------------
130         private boolean theCalled;
131     }
132 
133     //------------------------------------------------------------------------
134     // members
135     //------------------------------------------------------------------------
136     /**
137      * Provides logging services for this class.
138      */
139     // CHECKSTYLE:OFF
140     private final static Logger theLogger = LoggerFactory.getLogger(
141         TestComposite.class);
142     // CHECKSTYLE:ON
143 }