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
31
32
33 public class CastorUtilsTest extends TestCase
34 {
35
36
37
38 final public static Logger LOG = Logger.getLogger(CastorUtilsTest.class);
39
40
41
42
43 final public static String GLOBAL_CONFIG_NAME = "JPatternsGlobalConfiguration";
44
45
46
47
48
49
50 public CastorUtilsTest(String name)
51 {
52 super(name);
53 }
54
55
56
57
58
59
60 public void setUp() throws Exception
61 {
62 super.setUp();
63 }
64
65
66
67
68
69
70 public void tearDown() throws Exception
71 {
72 super.tearDown();
73 }
74
75
76
77
78
79
80 public static Test suite()
81 {
82 return new TestSuite(CastorUtilsTest.class);
83 }
84
85
86
87
88
89
90
91 public void testCastorIdioms() throws Exception
92 {
93
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
122
123
124
125
126 public void testGetJPatternsConfig() throws Exception
127 {
128
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
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
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
168
169
170
171
172 public void testExtractCastorSectionTypeObjects() throws Exception
173 {
174
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
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
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
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
241
242
243
244
245 public void testValidateAndNormalizeScopesPriorities() throws Exception
246 {
247
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
258
259
260 try
261 {
262
263
264
265 JPatternsConfig config = new JPatternsConfig();
266
267 Config global1 = new Config();
268 CastorUtils.addConfig(config, global1);
269 Item item1 = new Item();
270 global1.addItem(item1);
271
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
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
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
305 assertEquals(item4.getScope(), "scope");
306 assertEquals(item4.getPriority(), String.valueOf(JPConstants.DEFAULT_PRIORITY));
307
308
309
310
311 ds = "defaultConfigScope";
312 config = new JPatternsConfig();
313 config.setDefaultScope(ds);
314
315 global1 = new Config();
316 CastorUtils.addConfig(config, global1);
317 item1 = new Item();
318 global1.addItem(item1);
319
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
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
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
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
354
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
377
378
379
380
381 public void testMakePrioritized() throws Exception
382 {
383
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
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
410 try
411 {
412
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
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
447
448
449
450
451 public void testSetValidatePriority() throws Exception
452 {
453
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
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
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
494
495
496
497
498 public void testGetPriority() throws Exception
499 {
500
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
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
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
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
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
564
565
566
567
568 public void testExtractCastorNameScopePriorityTypeObjects() throws Exception
569 {
570
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
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
647
648
649
650
651 public void testConstructGroupItem() throws Exception
652 {
653
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
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
687
688
689
690
691 public void testToString() throws Exception
692 {
693
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
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
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
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 }