1   package com.sourceforge.jpatterns.utils;
2   
3   import com.sourceforge.jpatterns.core.JPConstants;
4   import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException;
5   import com.sourceforge.jpatterns.schema.CastorConfigType;
6   import com.sourceforge.jpatterns.schema.CastorFactoryType;
7   import com.sourceforge.jpatterns.schema.CastorGroupType;
8   import com.sourceforge.jpatterns.schema.CastorGroupTypeItem;
9   import com.sourceforge.jpatterns.schema.CastorItemType;
10  import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType;
11  import com.sourceforge.jpatterns.schema.CastorSectionType;
12  import com.sourceforge.jpatterns.schema.Config;
13  import com.sourceforge.jpatterns.schema.Factory;
14  import com.sourceforge.jpatterns.schema.Item;
15  import com.sourceforge.jpatterns.schema.JPatternsConfig;
16  import com.sourceforge.jpatterns.schema.JPatternsConfigItem;
17  import com.sourceforge.jpatterns.schema.Mediator;
18  import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
19  import com.zmicer.utils.LoggingUtils;
20  import com.zmicer.utils.MagicNumbers;
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import org.apache.log4j.Logger;
25  
26  import java.io.File;
27  import java.util.List;
28  
29  /**
30   * $Author:: zmicer             $<br/>
31   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
32   */
33  public class CastorUtilsTest extends TestCase
34  {
35      /**
36       * Logger instance.
37       */
38      final public static Logger LOG = Logger.getLogger(CastorUtilsTest.class);
39  
40      /**
41       * The name of the global configuration to be used at the tests
42       */
43      final public static String GLOBAL_CONFIG_NAME = "JPatternsGlobalConfiguration";
44  
45      /**
46       * Contructor with name of test attribute.
47       *
48       * @param name name of the test
49       */
50      public CastorUtilsTest(String name)
51      {
52          super(name);
53      }
54  
55      /**
56       * Perform the set up functionality for the test.
57       *
58       * @throws Exception may occur in the case of some problems
59       */
60      public void setUp() throws Exception
61      {
62          super.setUp();
63      }
64  
65      /**
66       * Perform the tear down functionality for the test
67       *
68       * @throws Exception may occur in the case of some problems
69       */
70      public void tearDown() throws Exception
71      {
72          super.tearDown();
73      }
74  
75      /**
76       * Test suite method
77       *
78       * @return the built test suite
79       */
80      public static Test suite()
81      {
82          return new TestSuite(CastorUtilsTest.class);
83      }
84  
85      /**
86       * Check some castor idioms
87       *
88       * @throws Exception in the case smth. wrong occuried.
89       * @forversion 1.0
90       */
91      public void testCastorIdioms() throws Exception
92      {
93          // normal sub-case: no exception: not null is returned for the arrays
94          try
95          {
96              JPatternsConfig config = new JPatternsConfig();
97              assertNotNull(config.getJPatternsConfigItem());
98              assertEquals(config.getJPatternsConfigItem().length, 0);
99  
100             JPatternsConfigItem configItem = new JPatternsConfigItem();
101             assertNotNull(configItem);
102             assertNull(configItem.getConfig());
103             assertNull(configItem.getFactory());
104             assertNull(configItem.getChoiceValue());
105 
106             final Config castorConfig = new Config();
107             configItem.setConfig(castorConfig);
108             assertNotNull(configItem.getConfig());
109             assertNotNull(configItem.getChoiceValue());
110             assertTrue(configItem.getChoiceValue() instanceof Config);
111             assertEquals(configItem.getChoiceValue(), castorConfig);
112         }
113         catch (Exception ex)
114         {
115             LoggingUtils.logException(LOG, ex, null, null);
116             fail("Exception should not occur for that case " + ex.getMessage());
117         }
118     }
119 
120     /**
121      * Tests {@link CastorUtils#getJPatternsConfig}
122      *
123      * @throws Exception in the case smth. wrong occuried.
124      * @forversion 1.0
125      */
126     public void testGetJPatternsConfig() throws Exception
127     {
128         // failed sub-case: exception occuried
129         try
130         {
131             CastorUtils.getJPatternsConfig(null);
132             fail("Exception should have been occuried before this line.");
133         }
134         catch (Exception ex)
135         {
136             assertTrue(ex instanceof IllegalArgumentException);
137         }
138         // failed sub-case: exception occuried
139         try
140         {
141             CastorUtils.getJPatternsConfig(new File(""));
142             fail("Exception should have been occuried before this line.");
143         }
144         catch (Exception ex)
145         {
146             assertTrue(ex instanceof IllegalArgumentException);
147         }
148         // normal sub-case: no exception
149         try
150         {
151             final JPatternsConfig config = JPatternsTestUtils.getConfig(CastorUtilsTest.class, null, "jpatterns.xml");
152             assertNotNull(config);
153             assertNotNull(config.getJPatternsConfigItem());
154             assertEquals(CastorUtils.getConfig(config).size(), 1);
155             assertEquals(CastorUtils.getConfig(config).get(0).getName(), GLOBAL_CONFIG_NAME);
156         }
157         catch (Exception ex)
158         {
159             LoggingUtils.logException(LOG, ex, null, null);
160             fail("Exception should not occur for that case params" + ex.getMessage());
161         }
162 
163 
164     }
165 
166     /**
167      * Tests {@link CastorUtils#extractCastorSectionTypeObjects}
168      *
169      * @throws Exception in the case smth. wrong occuried.
170      * @forversion 1.0
171      */
172     public void testExtractCastorSectionTypeObjects() throws Exception
173     {
174         // failed sub-case: exception occuried: incorrect argument
175         try
176         {
177             CastorUtils.extractCastorSectionTypeObjects(null);
178             fail("Exception should have been occuried before this line.");
179         }
180         catch (Exception ex)
181         {
182             assertTrue(ex instanceof IllegalArgumentException);
183         }
184         // normal sub-case: no exception: no such objects we are interested in.
185         try
186         {
187             final List<CastorSectionType> result = CastorUtils.extractCastorSectionTypeObjects(new JPatternsConfig());
188             assertNotNull(result);
189             assertTrue(result.isEmpty());
190         }
191         catch (Exception ex)
192         {
193             LoggingUtils.logException(LOG, ex, null, null);
194             fail("Exception should not occur for that case " + ex.getMessage());
195         }
196         // 00: normal sub-case: no exception: usual case - check if it is works correctly
197         try
198         {
199             final JPatternsConfig config = new JPatternsConfig();
200             final Factory factory1 = new Factory();
201             final Factory factory2 = new Factory();
202             final Factory factory3 = new Factory();
203             JPatternsConfigItem configItem = new JPatternsConfigItem();
204             configItem.setFactory(factory1);
205             config.addJPatternsConfigItem(configItem);
206             configItem = new JPatternsConfigItem();
207             configItem.setFactory(factory2);
208             config.addJPatternsConfigItem(configItem);
209             configItem = new JPatternsConfigItem();
210             configItem.setFactory(factory3);
211             config.addJPatternsConfigItem(configItem);
212             List<CastorSectionType> result = CastorUtils.extractCastorSectionTypeObjects(config);
213             assertNotNull(result);
214             assertEquals(result.size(), MagicNumbers.THREE);
215             assertTrue(result.contains(factory1));
216             assertTrue(result.contains(factory2));
217             assertTrue(result.contains(factory3));
218 
219             // 00:A check the case there are instances of the several different childs of CastorSectionType
220             final Config global1 = new Config();
221             final Config global2 = new Config();
222             final Config global3 = new Config();
223             CastorUtils.addConfig(config, global1);
224             CastorUtils.addConfig(config, global2);
225             CastorUtils.addConfig(config, global3);
226             result = CastorUtils.extractCastorSectionTypeObjects(config);
227             assertEquals(result.size(), MagicNumbers.SIX);
228             assertTrue(result.contains(global1));
229             assertTrue(result.contains(global2));
230             assertTrue(result.contains(global3));
231         }
232         catch (Exception ex)
233         {
234             LoggingUtils.logException(LOG, ex, null, null);
235             fail("Exception should not occur for that case " + ex.getMessage());
236         }
237     }
238 
239     /**
240      * Tests {@link CastorUtils#validateAndNormalizeScopes}
241      *
242      * @throws Exception in the case smth. wrong occuried.
243      * @forversion 1.1
244      */
245     public void testValidateAndNormalizeScopesPriorities() throws Exception
246     {
247         // failed sub-case: exception occuried: null argument
248         try
249         {
250             CastorUtils.validateAndNormalizeScopesPriorities(null);
251             fail("Exception should have been occuried before this line.");
252         }
253         catch (Exception ex)
254         {
255             assertTrue(ex instanceof IllegalArgumentException);
256         }
257         // 01: normal sub-case: no exception: the default scope is not set - check that the
258         // com.sourceforge.jpatterns.core.JPConstants.DEFAULT_SCOPE_NAME would be set to the both config root object and all the not
259         // scoped objects - sections and items.
260         try
261         {
262             // A. all is operated without the global scope
263             // [inits]
264             // 00:a without default scope
265             JPatternsConfig config = new JPatternsConfig();
266             // 00:b without default scope too (and appropriate business item)
267             Config global1 = new Config();
268             CastorUtils.addConfig(config, global1);
269             Item item1 = new Item();
270             global1.addItem(item1);
271             // 00:c one item has, one has not, and the parent has not
272             Config global2 = new Config();
273             CastorUtils.addConfig(config, global2);
274             Item item2 = new Item();
275             global2.addItem(item2);
276             Item item3 = new Item();
277             item3.setScope("scope");
278             global2.addItem(item3);
279             // 00:d section has the scope, and one of its childs has not
280             Config global3 = new Config();
281             global3.setScope("scope");
282             CastorUtils.addConfig(config, global3);
283             Item item4 = new Item();
284             global3.addItem(item4);
285 
286             CastorUtils.validateAndNormalizeScopesPriorities(config);
287 
288             // [results checks]
289             String ds = com.sourceforge.jpatterns.core.JPConstants.DEFAULT_SCOPE_NAME;
290             assertEquals(config.getDefaultScope(), ds);
291             assertEquals(global1.getScope(), ds);
292             assertEquals(global1.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
293             assertEquals(global2.getScope(), ds);
294             assertEquals(global2.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
295             assertEquals(global3.getScope(), "scope");
296             assertEquals(global2.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
297 
298             assertEquals(item1.getScope(), ds);
299             assertEquals(item1.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
300             assertEquals(item2.getScope(), ds);
301             assertEquals(item2.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
302             assertEquals(item3.getScope(), "scope");
303             assertEquals(item3.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
304             // this scopes should have been set here
305             assertEquals(item4.getScope(), "scope");
306             assertEquals(item4.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
307 
308             // B. global default scope is set
309             // [inits]
310             // 00:a without default scope
311             ds = "defaultConfigScope";
312             config = new JPatternsConfig();
313             config.setDefaultScope(ds);
314             // 00:b without default scope too (and appropriate business item)
315             global1 = new Config();
316             CastorUtils.addConfig(config, global1);
317             item1 = new Item();
318             global1.addItem(item1);
319             // 00:c one item has, one has not, and the parent has not
320             global2 = new Config();
321             CastorUtils.addConfig(config, global2);
322             item2 = new Item();
323             global2.addItem(item2);
324             item3 = new Item();
325             item3.setScope("scope");
326             global2.addItem(item3);
327             // 00:d section has the scope, and one of its childs has not
328             global3 = new Config();
329             global3.setScope("scope");
330             CastorUtils.addConfig(config, global3);
331             item4 = new Item();
332             global3.addItem(item4);
333 
334             CastorUtils.validateAndNormalizeScopesPriorities(config);
335 
336             // [results checks]
337             assertEquals(config.getDefaultScope(), ds);
338             assertEquals(global1.getScope(), ds);
339             assertEquals(global2.getScope(), ds);
340             assertEquals(global3.getScope(), "scope");
341 
342             assertEquals(item1.getScope(), ds);
343             assertEquals(item2.getScope(), ds);
344             assertEquals(item3.getScope(), "scope");
345             // this scopes should have been set here
346             assertEquals(item4.getScope(), "scope");
347         }
348         catch (Exception ex)
349         {
350             LoggingUtils.logException(LOG, ex, null, null);
351             fail("Exception should not occur for that case " + ex.getMessage());
352         }
353         // normal sub-case: no exception: check the case when parent and child have different scopes (and the scope of the parent differs
354         // from the default one). It is normal case, no exceptions would appear.
355         try
356         {
357             final JPatternsConfig config = new JPatternsConfig();
358             config.setDefaultScope("scope1");
359             final Config global = new Config();
360             global.setScope("scope2");
361             CastorUtils.addConfig(config, global);
362             final Item item = new Item();
363             item.setScope("scope3");
364             global.addItem(item);
365             CastorUtils.validateAndNormalizeScopesPriorities(config);
366         }
367         catch (Exception ex)
368         {
369             final String excMessage = "Exception should not occur for that case ";
370             LoggingUtils.logException(LOG, ex, excMessage, CastorUtilsTest.class);
371             fail(excMessage + ex.getMessage());
372         }
373     }
374 
375     /**
376      * Tests {@link CastorUtils#makePrioritized}
377      *
378      * @throws Exception in the case smth. wrong occuried.
379      * @forversion 1.0
380      */
381     public void testMakePrioritized() throws Exception
382     {
383         // failed sub-case: exception occuried: null input argument
384         try
385         {
386             CastorUtils.makePrioritized((JPatternsConfig) null);
387             fail("Exception should have been occuried before this line.");
388         }
389         catch (Exception ex)
390         {
391             assertTrue(ex instanceof IllegalArgumentException);
392         }
393 
394         // failed sub-case: exception occuried: incorrect priority
395         try
396         {
397             final JPatternsConfig config = new JPatternsConfig();
398             final Config global = new Config();
399             global.setPriority("lalala");
400             CastorUtils.addConfig(config, global);
401             CastorUtils.makePrioritized(config);
402 
403             fail("Exception should have been occuried before this line.");
404         }
405         catch (Exception ex)
406         {
407             assertTrue(ex instanceof JPConfigException);
408         }
409         // normal sub-case: no exception: just check the algorithm
410         try
411         {
412             // [init]
413             JPatternsConfig config = new JPatternsConfig();
414             Config global1 = new Config();
415             global1.setPriority("10");
416             JPatternsConfigItem patternsConfigItem = new JPatternsConfigItem();
417             CastorUtils.addConfig(config, global1);
418             Item item1 = new Item();
419             global1.addItem(item1);
420             Config global2 = new Config();
421             global2.setPriority(JPConstants.PRIORITIZED_PRIOTITY_PREFIX + MagicNumbers.ONE);
422             CastorUtils.addConfig(config, global2);
423             Item item2 = new Item();
424             global2.addItem(item2);
425 
426             CastorUtils.makePrioritized(config);
427 
428             // [checks]
429             assertNotNull(global1.getPriority());
430             assertEquals(global1.getPriority(), JPConstants.PRIORITIZED_PRIOTITY_PREFIX + MagicNumbers.TEN);
431             assertNotNull(item1.getPriority());
432             assertEquals(item1.getPriority(), JPConstants.PRIORITIZED_PRIOTITY_PREFIX + JPConstants.DEFAULT_PRIORITY);
433             assertNotNull(global2.getPriority());
434             assertEquals(global2.getPriority(), JPConstants.PRIORITIZED_PRIOTITY_PREFIX + MagicNumbers.ONE);
435             assertNotNull(item2.getPriority());
436             assertEquals(item2.getPriority(), JPConstants.PRIORITIZED_PRIOTITY_PREFIX + JPConstants.DEFAULT_PRIORITY);
437         }
438         catch (Exception ex)
439         {
440             LoggingUtils.logException(LOG, ex, null, null);
441             fail("Exception should not occur for that case " + ex.getMessage());
442         }
443     }
444 
445     /**
446      * Tests {@link CastorUtils#setValidatePriority}
447      *
448      * @throws Exception in the case smth. wrong occuried.
449      * @forversion 1.0
450      */
451     public void testSetValidatePriority() throws Exception
452     {
453         // failed sub-case: exception occuried: null argument
454         try
455         {
456             CastorUtils.setValidatePriority(null, true);
457             fail("Exception should have been occuried before this line.");
458         }
459         catch (Exception ex)
460         {
461             assertTrue(ex instanceof IllegalArgumentException);
462         }
463         // failed sub-case: exception occuried: incorrect priority
464         try
465         {
466             final Config global = new Config();
467             global.setPriority("la-la-la");
468             CastorUtils.setValidatePriority(global, false);
469             fail("Exception should have been occuried before this line.");
470         }
471         catch (Exception ex)
472         {
473             assertTrue(ex instanceof JPConfigException);
474         }
475         // normal sub-case: no exception: just normal working
476         try
477         {
478             Config global = new Config();
479             CastorUtils.setValidatePriority(global, false);
480             assertEquals(global.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
481             global = new Config();
482             CastorUtils.setValidatePriority(global, true);
483             assertEquals(global.getPriority(), JPConstants.PRIORITIZED_PRIOTITY_PREFIX + JPConstants.DEFAULT_PRIORITY);
484         }
485         catch (Exception ex)
486         {
487             LoggingUtils.logException(LOG, ex, null, null);
488             fail("Exception should not occur for that case " + ex.getMessage());
489         }
490     }
491 
492     /**
493      * Tests {@link CastorUtils#getPriority}
494      *
495      * @throws Exception in the case smth. wrong occuried.
496      * @forversion 1.0
497      */
498     public void testGetPriority() throws Exception
499     {
500         // failed sub-case: exception occuried: null input
501         try
502         {
503             CastorUtils.getPriority(null);
504             fail("Exception should have been occuried before this line.");
505         }
506         catch (Exception ex)
507         {
508             assertTrue(ex instanceof IllegalArgumentException);
509         }
510         // failed sub-case: exception occuried: input without priority
511         try
512         {
513             CastorUtils.getPriority(new Config());
514             fail("Exception should have been occuried before this line.");
515         }
516         catch (Exception ex)
517         {
518             assertTrue(ex instanceof IllegalArgumentException);
519         }
520         // failed sub-case: exception occuried: wrong priority
521         try
522         {
523             CastorNameScopePriorityType castor = new CastorNameScopePriorityType();
524             castor.setPriority("la-la-la");
525             CastorUtils.getPriority(castor);
526 
527             fail("Exception should have been occuried before this line.");
528         }
529         catch (Exception ex)
530         {
531             assertTrue(ex instanceof IllegalArgumentException);
532         }
533         // failed sub-case: exception occuried: wrong priority
534         try
535         {
536             CastorNameScopePriorityType castor = new CastorNameScopePriorityType();
537             castor.setPriority(JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "la-la-la");
538             CastorUtils.getPriority(castor);
539 
540             fail("Exception should have been occuried before this line.");
541         }
542         catch (Exception ex)
543         {
544             assertTrue(ex instanceof IllegalArgumentException);
545         }
546         // normal sub-case: no exception
547         try
548         {
549             CastorNameScopePriorityType castor = new CastorNameScopePriorityType();
550             castor.setPriority("" + MagicNumbers.TEN);
551             assertEquals(CastorUtils.getPriority(castor), MagicNumbers.TEN);
552             castor.setPriority(JPConstants.PRIORITIZED_PRIOTITY_PREFIX + MagicNumbers.TEN);
553             assertEquals(CastorUtils.getPriority(castor), MagicNumbers.TEN);
554         }
555         catch (Exception ex)
556         {
557             LoggingUtils.logException(LOG, ex, null, null);
558             fail("Exception should not occur for that case " + ex.getMessage());
559         }
560     }
561 
562     /**
563      * Tests {@link CastorUtils#extractCastorNameScopePriorityTypeObjects}
564      *
565      * @throws Exception in the case smth. wrong occuried.
566      * @forversion 1.0
567      */
568     public void testExtractCastorNameScopePriorityTypeObjects() throws Exception
569     {
570         // failed sub-case: exception occuried: null input argument
571         try
572         {
573             CastorUtils.extractCastorNameScopePriorityTypeObjects(null);
574             fail("Exception should have been occuried before this line.");
575         }
576         catch (Exception ex)
577         {
578             assertTrue(ex instanceof IllegalArgumentException);
579         }
580         // normal sub-case: no exception: all the possible cases.
581         try
582         {
583             CastorSectionType sectionType = new CastorSectionType();
584             List<CastorNameScopePriorityType> result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
585             assertNotNull(result);
586             assertTrue(result.isEmpty());
587 
588             Item item1 = new Item();
589             sectionType = new CastorFactoryType();
590             ((CastorFactoryType) sectionType).addItem(item1);
591             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
592             assertNotNull(result);
593             assertEquals(result.size(), 1);
594             assertTrue(result.contains(item1));
595 
596             Item item2 = new Item();
597             ((CastorFactoryType) sectionType).addItem(item2);
598             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
599             assertNotNull(result);
600             assertEquals(result.size(), MagicNumbers.TWO);
601             assertTrue(result.contains(item1));
602             assertTrue(result.contains(item2));
603 
604             sectionType = new CastorConfigType();
605             ((CastorConfigType) sectionType).addItem(item1);
606             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
607             assertNotNull(result);
608             assertEquals(result.size(), 1);
609             assertTrue(result.contains(item1));
610 
611             ((CastorConfigType) sectionType).addItem(item2);
612             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
613             assertNotNull(result);
614             assertEquals(result.size(), MagicNumbers.TWO);
615             assertTrue(result.contains(item1));
616             assertTrue(result.contains(item2));
617 
618             sectionType = new CastorGroupType();
619             CastorGroupTypeItem groupItem1 = new CastorGroupTypeItem();
620             Mediator mediator1 = new Mediator();
621             groupItem1.setMediator(mediator1);
622             ((CastorGroupType) sectionType).addCastorGroupTypeItem(groupItem1);
623             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
624             assertNotNull(result);
625             assertEquals(result.size(), 1);
626             assertTrue(result.contains(mediator1));
627 
628             CastorGroupTypeItem groupItem2 = new CastorGroupTypeItem();
629             Mediator mediator2 = new Mediator();
630             groupItem2.setMediator(mediator2);
631             ((CastorGroupType) sectionType).addCastorGroupTypeItem(groupItem2);
632             result = CastorUtils.extractCastorNameScopePriorityTypeObjects(sectionType);
633             assertNotNull(result);
634             assertEquals(result.size(), MagicNumbers.TWO);
635             assertTrue(result.contains(mediator1));
636             assertTrue(result.contains(mediator2));
637         }
638         catch (Exception ex)
639         {
640             LoggingUtils.logException(LOG, ex, null, null);
641             fail("Exception should not occur for that case " + ex.getMessage());
642         }
643     }
644 
645     /**
646      * Tests {@link CastorUtils#constructGroupItem}
647      *
648      * @throws Exception in the case smth. wrong occuried.
649      * @forversion 1.0
650      */
651     public void testConstructGroupItem() throws Exception
652     {
653         // failed sub-case: exception occuried: null as input parameter
654         try
655         {
656             CastorUtils.constructGroupItem(null);
657             fail("Exception should have been occuried before this line.");
658         }
659         catch (Exception ex)
660         {
661             assertTrue(ex instanceof IllegalArgumentException);
662         }
663         // normal sub-case: no exception: business cases
664         try
665         {
666             CastorGroupTypeItem groupItem = CastorUtils.constructGroupItem(new CastorNameScopePriorityType());
667             assertNull(groupItem);
668             groupItem = CastorUtils.constructGroupItem(new Item());
669             assertNull(groupItem);
670             groupItem = CastorUtils.constructGroupItem(new CastorItemType());
671             assertNull(groupItem);
672             final Mediator mediator = new Mediator();
673             groupItem = CastorUtils.constructGroupItem(mediator);
674             assertNotNull(groupItem);
675             assertNotNull(groupItem.getChoiceValue());
676             assertEquals(groupItem.getChoiceValue(), mediator);
677         }
678         catch (Exception ex)
679         {
680             LoggingUtils.logException(LOG, ex, null, null);
681             fail("Exception should not occur for that case " + ex.getMessage());
682         }
683     }
684 
685     /**
686      * Tests {@link CastorUtils#toString}
687      *
688      * @throws Exception in the case smth. wrong occuried.
689      * @forversion 1.0
690      */
691     public void testToString() throws Exception
692     {
693         // failed sub-case: exception occuried: item is not passed
694         try
695         {
696             CastorUtils.toString(null, null);
697             fail("Exception should have been occuried before this line.");
698         }
699         catch (Exception ex)
700         {
701             assertTrue(ex instanceof IllegalArgumentException);
702         }
703         // failed sub-case: exception occuried: item is not passed
704         try
705         {
706             CastorUtils.toString(new CastorSectionType(), null);
707             fail("Exception should have been occuried before this line.");
708         }
709         catch (Exception ex)
710         {
711             assertTrue(ex instanceof IllegalArgumentException);
712         }
713         // failed sub-case: exception occuried: item is not passed
714         try
715         {
716             CastorUtils.toString(new CastorSectionType(), new CastorNameScopePriorityType());
717             fail("Exception should have been occuried before this line.");
718         }
719         catch (Exception ex)
720         {
721             assertTrue(ex instanceof IllegalArgumentException);
722         }
723         // normal sub-case: no exception:
724         CastorNameScopePriorityType item = new CastorNameScopePriorityType();
725         item.setName("itemname");
726         item.setScope("itemscope");
727         CastorSectionType section = new CastorSectionType();
728         section.setName("sectionname");
729         section.setScope("sectionscope");
730         try
731         {
732 
733             String res = CastorUtils.toString(null, item);
734             assertNotNull(res);
735             assertTrue(res.contains("itemname"));
736             assertTrue(res.contains("itemscope"));
737 
738             res = CastorUtils.toString(section, item);
739             assertNotNull(res);
740             assertTrue(res.contains("itemname"));
741             assertTrue(res.contains("itemscope"));
742             assertTrue(res.contains("sectionname"));
743             assertTrue(res.contains("sectionscope"));
744         }
745         catch (Exception ex)
746         {
747             final String excMessage = "Exception should not occur for that case ";
748             LoggingUtils.logException(LOG, ex, excMessage, null);
749             fail(excMessage + ex.getMessage());
750         }
751     }
752 }