1   package com.sourceforge.jpatterns.core.configuration.model;
2   
3   import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType;
4   import com.sourceforge.jpatterns.schema.CastorSectionType;
5   import com.zmicer.utils.LoggingUtils;
6   import junit.framework.Test;
7   import junit.framework.TestCase;
8   import junit.framework.TestSuite;
9   import org.apache.log4j.Logger;
10  
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   * $Author:: zmicer             $<br/>
17   * $Rev:: 57                    $<br/> * $Date:: 2007-08-23 09:16:37 #$<br/>
18   * $Date:: 2007-08-23 09:16:37 #$<br/>
19   */
20  public class JPatternsConfigBaseBeanTest extends TestCase
21  {
22      /**
23       * Logger instance.
24       */
25      final public static Logger LOG = Logger.getLogger(JPatternsConfigBaseBeanTest.class);
26  
27      /**
28       * Instance of the covered class to be used below in the tests
29       */
30      private JPatternsConfigBaseBean m_jPatternsConfigBaseBean;
31  
32      /**
33       * Contructor with name of test attribute.
34       *
35       * @param name name of the test
36       */
37      public JPatternsConfigBaseBeanTest(String name)
38      {
39          super(name);
40      }
41  
42      /**
43       * Perform the set up functionality for the test.
44       *
45       * @throws Exception may occur in the case of some problems
46       */
47      public void setUp() throws Exception
48      {
49          super.setUp();
50          m_jPatternsConfigBaseBean = new JPatternsConfigBaseBean();
51      }
52  
53      /**
54       * Perform the tear down functionality for the test
55       *
56       * @throws Exception may occur in the case of some problems
57       */
58      public void tearDown() throws Exception
59      {
60          super.tearDown();
61      }
62  
63      /**
64       * Test suite method
65       *
66       * @return the built test suite
67       */
68      public static Test suite()
69      {
70          return new TestSuite(JPatternsConfigBaseBeanTest.class);
71      }
72  
73      /**
74       * Tests {@link JPatternsConfigBaseBean#set/getBusinessItems}
75       *
76       * @throws Exception in the case smth. wrong occuried.
77       * @forversion 1.0
78       */
79      public void testSetGetBusinessItems() throws Exception
80      {
81          // normal sub-case: no exception: get the default value of the business items - empty map should be present
82          try
83          {
84              assertNotNull(m_jPatternsConfigBaseBean.getBusinessItems());
85              assertEquals(m_jPatternsConfigBaseBean.getBusinessItems().size(), 0);
86          }
87          catch (Exception ex)
88          {
89              LoggingUtils.logException(LOG, ex, null, null);
90              fail("Exception should not occur for that case params" + ex.getMessage());
91          }
92  
93          // failed sub-case: exception occuried: set the incorrect values
94          try
95          {
96              m_jPatternsConfigBaseBean.setBusinessItems(null);
97              fail("Exception should have been occuried before this line.");
98          }
99          catch (Exception ex)
100         {
101             assertTrue(ex instanceof IllegalArgumentException);
102         }
103 
104         // normal sub-case: no exception: all is ok. set and get methods check.
105         try
106         {
107             final Map<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>> mapForTest =
108                 new HashMap<String, Map<String, Map<String, Map<String, CastorNameScopePriorityType>>>>();
109             m_jPatternsConfigBaseBean.setBusinessItems(mapForTest);
110             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItems());
111             assertEquals(m_jPatternsConfigBaseBean.getBusinessItems(), mapForTest);
112         }
113         catch (Exception ex)
114         {
115             LoggingUtils.logException(LOG, ex, null, null);
116             fail("Exception should not occur for that case params" + ex.getMessage());
117         }
118     }
119 
120     /**
121      * Tests {@link JPatternsConfigBaseBean#set/getSectionItems}
122      *
123      * @throws Exception in the case smth. wrong occuried.
124      * @forversion 1.0
125      */
126     public void testSetGetSectionItems() throws Exception
127     {
128         // normal sub-case: no exception: check the default state of the appropriate collection
129         try
130         {
131             assertNotNull(m_jPatternsConfigBaseBean.getSectionItems());
132             assertEquals(m_jPatternsConfigBaseBean.getSectionItems().size(), 0);
133         }
134         catch (Exception ex)
135         {
136             LoggingUtils.logException(LOG, ex, null, null);
137             fail("Exception should not occur for that case params" + ex.getMessage());
138         }
139 
140         // failed sub-case: exception occuried: incorrect param
141         try
142         {
143             m_jPatternsConfigBaseBean.setSectionItems(null);
144             fail("Exception should have been occuried before this line.");
145         }
146         catch (Exception ex)
147         {
148             assertTrue(ex instanceof IllegalArgumentException);
149         }
150 
151         // normal sub-case: no exception: really check the get/set on the not "limit" values.
152         try
153         {
154             final Map<String, Map<String, CastorSectionType>> mapForTest = new HashMap<String, Map<String, CastorSectionType>>();
155             m_jPatternsConfigBaseBean.setSectionItems(mapForTest);
156             assertNotNull(m_jPatternsConfigBaseBean.getSectionItems());
157             assertEquals(m_jPatternsConfigBaseBean.getSectionItems(), mapForTest);
158         }
159         catch (Exception ex)
160         {
161             LoggingUtils.logException(LOG, ex, null, null);
162             fail("Exception should not occur for that case params" + ex.getMessage());
163         }
164     }
165 
166     /**
167      * Tests {@link JPatternsConfigBaseBean#check}
168      *
169      * @throws Exception in the case smth. wrong occuried.
170      * @forversion 1.0
171      */
172     public void testCheck() throws Exception
173     {
174         // normal sub-case: no exception: just to prove after the constructor calling the composition is correct (all the maps are
175         // initialized)
176         try
177         {
178             assertTrue(m_jPatternsConfigBaseBean.check());
179         }
180         catch (Exception ex)
181         {
182             LoggingUtils.logException(LOG, ex, null, null);
183             fail("Exception should not occur for that case params" + ex.getMessage());
184         }
185 
186     }
187 
188     /**
189      * Tests {@link JPatternsConfigBaseBean#set/getBusinessItem}
190      *
191      * @throws Exception in the case smth. wrong occuried.
192      * @forversion 1.0
193      */
194     public void testSetGetBusinessItem() throws Exception
195     {
196         // failed sub-case: exception occuried: wrong input arguments
197         assertSetIncorrectBusinessItem(null, null, null);
198         assertSetIncorrectBusinessItem(null, "temp", new CastorNameScopePriorityType());
199         assertSetIncorrectBusinessItem("temp", null, new CastorNameScopePriorityType());
200         assertSetIncorrectBusinessItem("temp", "temp", null);
201         assertSetIncorrectBusinessItem("", "temp", new CastorNameScopePriorityType());
202         assertSetIncorrectBusinessItem("temp", "", new CastorNameScopePriorityType());
203         assertSetIncorrectBusinessItem("", "", new CastorNameScopePriorityType());
204 
205         // normal sub-case: no exception: normal case
206         try
207         {
208             // initial: check the structure on default (after the instantiating the bean.)
209             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItems());
210             assertEquals(m_jPatternsConfigBaseBean.getBusinessItems().size(), 0);
211             //A. all the maps are empty
212             final String name1 = "name1";
213             final String name2 = "name2";
214             final String scope1 = "scope1";
215             final String scope2 = "scope2";
216             final CastorNameScopePriorityType castorNameScopePriorityType1 = new CastorNameScopePriorityType();
217             castorNameScopePriorityType1.setName(name1);
218             castorNameScopePriorityType1.setScope(scope1);
219             m_jPatternsConfigBaseBean.setBusinessItem("scope", "section", castorNameScopePriorityType1);
220             assertEquals(m_jPatternsConfigBaseBean.getBusinessItems().size(), 1);
221             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItems().get("scope"));
222             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItems().get("scope").get("section"));
223             assertEquals(m_jPatternsConfigBaseBean.getBusinessItems().get("scope").get("section").size(), 1);
224             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1));
225             assertEquals(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1), castorNameScopePriorityType1);
226             // B. just one more item
227             final CastorNameScopePriorityType castorNameScopePriorityType2 = new CastorNameScopePriorityType();
228             castorNameScopePriorityType2.setName(name2);
229             castorNameScopePriorityType2.setScope(scope2);
230             m_jPatternsConfigBaseBean.setBusinessItem("scope", "section", castorNameScopePriorityType2);
231             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope2, name2));
232             assertEquals(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope2, name2), castorNameScopePriorityType2);
233             // C: check if item already put
234             final CastorNameScopePriorityType castorNameScopePriorityType3 = new CastorNameScopePriorityType();
235             castorNameScopePriorityType3.setName(name1);
236             castorNameScopePriorityType3.setScope(scope1);
237             m_jPatternsConfigBaseBean.setBusinessItem("scope", "section", castorNameScopePriorityType3);
238             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1));
239             // the item was replaced
240             assertEquals(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1), castorNameScopePriorityType3);
241 
242             // D: check if the items with identical names and different scopes are placed to different maps
243             final CastorNameScopePriorityType castorNameScopePriorityType4 = new CastorNameScopePriorityType();
244             castorNameScopePriorityType4.setName(name1);
245             castorNameScopePriorityType4.setScope(scope2);
246             m_jPatternsConfigBaseBean.setBusinessItem("scope", "section", castorNameScopePriorityType4);
247             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1));
248             assertNotNull(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope2, name1));
249             assertNotSame(m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope1, name1), 
250                 m_jPatternsConfigBaseBean.getBusinessItem("scope", "section", scope2, name1));
251         }
252         catch (Exception ex)
253         {
254             LoggingUtils.logException(LOG, ex, null, null);
255             fail("Exception should not occur for that case params" + ex.getMessage());
256         }
257     }
258 
259     /**
260      * Assert settings the incorrect values for business items. Introduced only to avoid copy-pasting at the
261      * <code>testSetGetBusinessItem</code> method
262      *
263      * @param scope       scope
264      * @param sectionName section name
265      * @param item        item
266      */
267     private void assertSetIncorrectBusinessItem(final String scope, final String sectionName, final CastorNameScopePriorityType item)
268     {
269         try
270         {
271             m_jPatternsConfigBaseBean.setBusinessItem(scope, sectionName, item);
272             fail("Exception should have been occuried before this line.");
273         }
274         catch (Exception ex)
275         {
276             assertTrue(ex instanceof IllegalArgumentException);
277         }
278     }
279 
280     /**
281      * Tests {@link JPatternsConfigBaseBean#set/getSection}
282      *
283      * @throws Exception in the case smth. wrong occuried.
284      * @forversion 1.0
285      */
286     public void testSetGetSection() throws Exception
287     {
288         // failed sub-case: exception occuried: wrong parameters
289         assertSetIncorrectSection(null, null);
290         assertSetIncorrectSection(null, new CastorSectionType());
291         assertSetIncorrectSection("", new CastorSectionType());
292         assertSetIncorrectSection("temp", null);
293 
294         // normal sub-case: no exception: normal case
295         try
296         {
297             // initial: check the default composition
298             assertNotNull(m_jPatternsConfigBaseBean.getSectionItems());
299             assertEquals(m_jPatternsConfigBaseBean.getSectionItems().size(), 0);
300             // A: map is not created
301             final String name1 = "name1";
302             final String name2 = "name2";
303             final CastorSectionType castorSectionType1 = new CastorSectionType();
304             castorSectionType1.setName(name1);
305             m_jPatternsConfigBaseBean.setSection("scope", castorSectionType1);
306             assertNotNull(m_jPatternsConfigBaseBean.getSection("scope", name1));
307             assertEquals(m_jPatternsConfigBaseBean.getSection("scope", name1), castorSectionType1);
308             // B: create one more section, for the tests in any case.
309             final CastorSectionType castorSectionType2 = new CastorSectionType();
310             castorSectionType2.setName(name2);
311             m_jPatternsConfigBaseBean.setSection("scope", castorSectionType2);
312             assertNotNull(m_jPatternsConfigBaseBean.getSection("scope", name2));
313             assertEquals(m_jPatternsConfigBaseBean.getSection("scope", name2), castorSectionType2);
314             // C: override the item1 using another castor section type with the "name1"
315             final CastorSectionType castorSectionType3 = new CastorSectionType();
316             castorSectionType3.setName(name1);
317             m_jPatternsConfigBaseBean.setSection("scope", castorSectionType3);
318             assertNotNull(m_jPatternsConfigBaseBean.getSection("scope", name1));
319             assertEquals(m_jPatternsConfigBaseBean.getSection("scope", name1), castorSectionType3);
320         }
321         catch (Exception ex)
322         {
323             LoggingUtils.logException(LOG, ex, null, null);
324             fail("Exception should not occur for that case params" + ex.getMessage());
325         }
326 
327     }
328 
329     /**
330      * Assert setting incorrect parameters for the section
331      *
332      * @param scope   scope name
333      * @param section castor object representing section
334      */
335     private void assertSetIncorrectSection(final String scope, final CastorSectionType section)
336     {
337         try
338         {
339             m_jPatternsConfigBaseBean.setSection(scope, section);
340             fail("Exception should have been occuried before this line.");
341         }
342         catch (Exception ex)
343         {
344             assertTrue(ex instanceof IllegalArgumentException);
345         }
346     }
347 
348     /**
349      * Tests {@link JPatternsConfigBaseBean#getListOfSectionItems}
350      *
351      * @throws Exception in the case smth. wrong occuried.
352      * @forversion 1.0
353      */
354     public void testGetListOfSectionItems() throws Exception
355     {
356         // normal sub-case: no exception: valid case
357         try
358         {
359             final CastorSectionType section1 = new CastorSectionType();
360             section1.setScope("scope1");
361             section1.setName("name1");
362             section1.setPriority("-1");
363             final CastorSectionType section2 = new CastorSectionType();
364             section2.setScope("scope2");
365             section2.setName("name2");
366             section2.setPriority("1");
367             m_jPatternsConfigBaseBean.setSection("scope1", section1);
368             m_jPatternsConfigBaseBean.setSection("scope2", section2);
369             final List<CastorSectionType> result = m_jPatternsConfigBaseBean.getListOfSectionItems();
370             assertNotNull(result);
371             assertEquals(result.size(), 2);
372         }
373         catch (Exception ex)
374         {
375             LoggingUtils.logException(LOG, ex, null, null);
376             fail("Exception should not occur for that case " + ex.getMessage());
377         }
378     }
379 }