1 package com.sourceforge.jpatterns.core.configuration.model;
2
3 import com.sourceforge.jpatterns.core.JPConstants;
4 import com.sourceforge.jpatterns.core.configuration.PropertiesProvider;
5 import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException;
6 import com.sourceforge.jpatterns.schema.CastorConfigType;
7 import com.sourceforge.jpatterns.schema.CastorItemType;
8 import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType;
9 import com.sourceforge.jpatterns.schema.CastorSectionType;
10 import com.sourceforge.jpatterns.schema.Item;
11 import com.sourceforge.jpatterns.schema.JPatternsConfig;
12 import com.sourceforge.jpatterns.utils.CastorUtils;
13 import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
14 import com.zmicer.utils.InputArgumentUtils;
15 import com.zmicer.utils.LoggingUtils;
16 import junit.framework.Test;
17 import junit.framework.TestCase;
18 import junit.framework.TestSuite;
19 import org.apache.log4j.Logger;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28
29 public class JPatternsConfigBeansBuilderImplTest extends TestCase
30 {
31
32
33
34 final public static Logger LOG = Logger.getLogger(JPatternsConfigBeansBuilderImplTest.class);
35
36
37
38
39 private JPatternsConfigBeansBuilderImpl m_builder = new JPatternsConfigBeansBuilderImpl();
40
41
42
43
44 public interface ConfigsForTest
45 {
46
47
48
49 String CONFIG_1 = "jpatterns_framework1.xml";
50
51
52
53
54 String CONFIG_2 = "jpatterns_framework2.xml";
55 }
56
57
58
59
60
61
62 public JPatternsConfigBeansBuilderImplTest(String name)
63 {
64 super(name);
65 }
66
67
68
69
70
71
72 public void setUp() throws Exception
73 {
74 super.setUp();
75 }
76
77
78
79
80
81
82 public void tearDown() throws Exception
83 {
84 super.tearDown();
85 }
86
87
88
89
90
91
92 public static Test suite()
93 {
94 return new TestSuite(JPatternsConfigBeansBuilderImplTest.class);
95 }
96
97
98
99
100
101
102
103 public void testSetGetOverridingDepth() throws Exception
104 {
105
106 try
107 {
108 assertNotNull(JPatternsConfigBeansBuilderImpl.getOverridingDepth());
109 }
110 catch (Exception ex)
111 {
112 LoggingUtils.logException(LOG, ex, null, null);
113 fail("Exception should not occur for that case " + ex.getMessage());
114 }
115
116 try
117 {
118 JPatternsConfigBeansBuilderImpl.setOverridingDepth(null);
119 fail("Exception should have been occuried before this line.");
120 }
121 catch (Exception ex)
122 {
123 assertTrue(ex instanceof IllegalArgumentException);
124 }
125
126 try
127 {
128 JPatternsConfigBeansBuilderImpl.setOverridingDepth(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_ITEM.toString());
129 assertEquals(JPatternsConfigBeansBuilderImpl.getOverridingDepth(),
130 PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_ITEM.toString());
131 JPatternsConfigBeansBuilderImpl.setOverridingDepth(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString());
132 assertEquals(JPatternsConfigBeansBuilderImpl.getOverridingDepth(),
133 PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString());
134 }
135 catch (Exception ex)
136 {
137 LoggingUtils.logException(LOG, ex, null, null);
138 fail("Exception should not occur for that case " + ex.getMessage());
139 }
140 }
141
142
143
144
145
146
147
148 public void testSetGetOverrideNotDependingOnPriority() throws Exception
149 {
150
151 try
152 {
153 assertTrue(JPatternsConfigBeansBuilderImpl.getOverrideNotDependingOnPriority());
154 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(false);
155 assertFalse(JPatternsConfigBeansBuilderImpl.getOverrideNotDependingOnPriority());
156 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(true);
157 assertTrue(JPatternsConfigBeansBuilderImpl.getOverrideNotDependingOnPriority());
158 }
159 catch (Exception ex)
160 {
161 LoggingUtils.logException(LOG, ex, null, null);
162 fail("Exception should not occur for that case " + ex.getMessage());
163 }
164 }
165
166
167
168
169
170
171
172 public void testChoiceOrMergeCastorNameScopePriorityTypes() throws Exception
173 {
174
175 try
176 {
177 m_builder.choiceOrMergeCastorNameScopePriorityTypes(null, 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 m_builder.choiceOrMergeCastorNameScopePriorityTypes(null, new CastorSectionType());
188 fail("Exception should have been occuried before this line.");
189 }
190 catch (Exception ex)
191 {
192 assertTrue(ex instanceof IllegalArgumentException);
193 }
194
195 try
196 {
197 m_builder.choiceOrMergeCastorNameScopePriorityTypes(new CastorSectionType(), new CastorSectionType());
198 fail("Exception should have been occuried before this line.");
199 }
200 catch (Exception ex)
201 {
202 assertTrue(ex instanceof IllegalArgumentException);
203 }
204
205 try
206 {
207 m_builder.choiceOrMergeCastorNameScopePriorityTypes(new CastorItemType(), new CastorSectionType());
208 fail("Exception should have been occuried before this line.");
209 }
210 catch (Exception ex)
211 {
212 assertTrue(ex instanceof IllegalArgumentException);
213 }
214
215 try
216 {
217 m_builder.choiceOrMergeCastorNameScopePriorityTypes(buildConfigSection("scope", "name", "-1"),
218 buildItem("scope", "name_", "-1"));
219 fail("Exception should have been occuried before this line.");
220 }
221 catch (Exception ex)
222 {
223 assertTrue(ex instanceof IllegalArgumentException);
224 }
225
226
227 try
228 {
229 m_builder.choiceOrMergeCastorNameScopePriorityTypes(buildItem("scope", "name", "-1"), buildItem("scope", "name", "-1"));
230 fail("Exception should have been occuried before this line.");
231 }
232 catch (Exception ex)
233 {
234 assertTrue(ex instanceof JPConfigException);
235 }
236
237 try
238 {
239 JPatternsConfigBeansBuilderImpl.setOverridingDepth(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString());
240 m_builder.choiceOrMergeCastorNameScopePriorityTypes(buildConfigSection("scope", "name", "-1"),
241 buildConfigSection("scope", "name", "-1"));
242 fail("Exception should have been occuried before this line.");
243 }
244 catch (Exception ex)
245 {
246 assertTrue(ex instanceof JPConfigException);
247 }
248
249 try
250 {
251 JPatternsConfigBeansBuilderImpl.setOverridingDepth(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_ITEM.toString());
252 final CastorConfigType section1 = buildConfigSection("scope", "name", "-1");
253 final Item item1 = new Item();
254 section1.addItem(item1);
255 final CastorConfigType section2 = buildConfigSection("scope", "name", "-1");
256 final Item item2 = new Item();
257 section2.addItem(item2);
258 final CastorNameScopePriorityType castorNameScopePriorityType =
259 m_builder.choiceOrMergeCastorNameScopePriorityTypes(section1, section2);
260 assertNotNull(castorNameScopePriorityType);
261 assertTrue(castorNameScopePriorityType instanceof CastorSectionType);
262 assertEquals(((CastorConfigType) castorNameScopePriorityType).getItem().length, 2);
263 }
264 catch (Exception ex)
265 {
266 LoggingUtils.logException(LOG, ex, null, null);
267 fail("Exception should not occur for that case " + ex.getMessage());
268 }
269
270 try
271 {
272
273 CastorNameScopePriorityType object1 = build("scope", "name", "-1");
274 CastorNameScopePriorityType object2 = build("scope", "name", "1");
275 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
276 object1 = build("scope", "name", "1");
277 object2 = build("scope", "name", "-1");
278 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
279
280 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "-1");
281 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
282 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
283 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
284 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "-1");
285 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
286
287
288 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(false);
289 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "10");
290 object2 = build("scope", "name", "1");
291 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
292 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
293 object2 = build("scope", "name", "10");
294 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
295
296 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(true);
297 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "10");
298 object2 = build("scope", "name", "1");
299 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
300 object1 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
301 object2 = build("scope", "name", "10");
302 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
303
304 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(false);
305 object1 = build("scope", "name", "1");
306 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "10");
307 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
308 object1 = build("scope", "name", "10");
309 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
310 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object1);
311
312 JPatternsConfigBeansBuilderImpl.setOverrideNotDependingOnPriority(true);
313 object1 = build("scope", "name", "10");
314 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "1");
315 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
316 object1 = build("scope", "name", "1");
317 object2 = build("scope", "name", JPConstants.PRIORITIZED_PRIOTITY_PREFIX + "10");
318 assertEquals(m_builder.choiceOrMergeCastorNameScopePriorityTypes(object1, object2), object2);
319 }
320 catch (Exception ex)
321 {
322 LoggingUtils.logException(LOG, ex, null, null);
323 fail("Exception should not occur for that case " + ex.getMessage());
324 }
325 }
326
327
328
329
330
331
332
333
334
335
336 private CastorConfigType buildConfigSection(final String scope, final String name, final String priority)
337 {
338 InputArgumentUtils.checkStrings(true, scope, name, priority);
339 final CastorConfigType result = new CastorConfigType();
340 result.setScope(scope);
341 result.setName(name);
342 result.setPriority(priority);
343
344 return result;
345 }
346
347
348
349
350
351
352
353
354
355
356 private CastorItemType buildItem(final String scope, final String name, final String priority)
357 {
358 InputArgumentUtils.checkStrings(true, scope, name, priority);
359 final CastorItemType result = new CastorItemType();
360 result.setScope(scope);
361 result.setName(name);
362 result.setPriority(priority);
363
364 return result;
365 }
366
367
368
369
370
371
372
373
374
375
376 private CastorNameScopePriorityType build(final String scope, final String name, final String priority)
377 {
378 InputArgumentUtils.checkStrings(true, scope, name, priority);
379 final CastorNameScopePriorityType result = new CastorNameScopePriorityType();
380 result.setScope(scope);
381 result.setName(name);
382 result.setPriority(priority);
383
384 return result;
385 }
386
387
388
389
390
391
392
393 public void testFill() throws Exception
394 {
395
396 try
397 {
398 m_builder.fill(null, null);
399 fail("Exception should have been occuried before this line.");
400 }
401 catch (Exception ex)
402 {
403 assertTrue(ex instanceof IllegalArgumentException);
404 }
405
406 try
407 {
408 m_builder.fill(new JPatternsConfigBaseBean(), null);
409 fail("Exception should have been occuried before this line.");
410 }
411 catch (Exception ex)
412 {
413 assertTrue(ex instanceof IllegalArgumentException);
414 }
415
416 try
417 {
418 m_builder.fill(null, new ArrayList<CastorSectionType>());
419 fail("Exception should have been occuried before this line.");
420 }
421 catch (Exception ex)
422 {
423 assertTrue(ex instanceof IllegalArgumentException);
424 }
425
426
427 try
428 {
429 final JPatternsConfig config = JPatternsTestUtils.getConfig(JPatternsConfigBeansBuilderImplTest.class, null,
430 ConfigsForTest.CONFIG_1);
431 CastorUtils.validateAndNormalizeScopesPriorities(config);
432 final List<CastorSectionType> sections = CastorUtils.extractCastorSectionTypeObjects(config);
433 final JPatternsConfigBaseBean bean = new JPatternsConfigBaseBean();
434 m_builder.fill(bean, sections);
435 String scopeName = JPConstants.DEFAULT_SCOPE_NAME;
436
437
438 assertNotNull(bean.getSection(scopeName, "GlobalSection1"));
439 assertNotNull(bean.getSection(scopeName, "GlobalSection2"));
440 assertNotNull(bean.getSection(scopeName, "GlobalSection3"));
441
442
443 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection1", scopeName, "GlobalSectionItem1"));
444 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem1"));
445 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem2"));
446 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem1"));
447 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem2"));
448
449
450 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", "GlobalItemScope1", "GlobalSectionItem2"));
451 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", "GlobalItemScope2", "GlobalSectionItem2"));
452 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", "GlobalItemScope2", "GlobalSectionItem3"));
453
454
455 scopeName = "GlobalScope1";
456
457 assertNotNull(bean.getSection(scopeName, "GlobalSection1"));
458 assertNotNull(bean.getSection(scopeName, "GlobalSection2"));
459 assertNotNull(bean.getSection(scopeName, "GlobalSection3"));
460
461
462 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection1", scopeName, "GlobalSectionItem1"));
463 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection1", "GlobalItemScope1", "GlobalSectionItem1"));
464 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem1"));
465 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem2"));
466 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem1"));
467 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem2"));
468
469
470 scopeName = JPConstants.DEFAULT_SCOPE_NAME;
471 CastorSectionType section = bean.getSection(scopeName, "GlobalSection4");
472 assertNotNull(section);
473 assertEquals(section.getPriority(), "1");
474 assertNull(bean.getBusinessItem(scopeName, "GlobalSection4", scopeName, "GlobalSectionItem4"));
475 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection4", scopeName, "GlobalSectionItem4_1"));
476 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection4", "GlobalItemScope4", "GlobalSectionItem4_2"));
477 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection4", "GlobalItemScope5", "GlobalSectionItem4_2"));
478 }
479 catch (Exception ex)
480 {
481 LoggingUtils.logException(LOG, ex, null, null);
482 fail("Exception should not occur for that case " + ex.getMessage());
483 }
484 }
485
486
487
488
489
490
491
492 public void testBuild() throws Exception
493 {
494
495 try
496 {
497 m_builder.build((JPatternsConfig) null);
498 fail("Exception should have been occuried before this line.");
499 }
500 catch (Exception ex)
501 {
502 assertTrue(ex instanceof IllegalArgumentException);
503 }
504
505 try
506 {
507 final JPatternsConfig config = JPatternsTestUtils.getConfig(JPatternsConfigBeansBuilderImplTest.class, null,
508 ConfigsForTest.CONFIG_1);
509 final JPatternsConfigBean bean = m_builder.build(config);
510 assertNotNull(bean);
511
512 assertEquals(bean.getCastorConfig(), config);
513 assertEquals(bean.getDefaultScope(), JPConstants.DEFAULT_SCOPE_NAME);
514
515
516 assertNotNull(bean.getSection(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection1"));
517 assertNotNull(bean.getSection("GlobalScope1", "GlobalSection1"));
518 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection1",
519 JPConstants.DEFAULT_SCOPE_NAME, "GlobalSectionItem1"));
520 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection2",
521 JPConstants.DEFAULT_SCOPE_NAME, "GlobalSectionItem1"));
522 assertNotNull(bean.getBusinessItem("GlobalScope1", "GlobalSection1", "GlobalScope1", "GlobalSectionItem1"));
523 assertNotNull(bean.getBusinessItem("GlobalScope1", "GlobalSection1", "GlobalItemScope1", "GlobalSectionItem1"));
524 assertNotNull(bean.getBusinessItem("GlobalScope1", "GlobalSection2", "GlobalScope1", "GlobalSectionItem1"));
525 assertNotNull(bean.getBusinessItem("GlobalScope1", "GlobalSection2", "GlobalScope1", "GlobalSectionItem2"));
526 }
527 catch (Exception ex)
528 {
529 LoggingUtils.logException(LOG, ex, null, null);
530 fail("Exception should not occur for that case " + ex.getMessage());
531 }
532 }
533
534
535
536
537
538
539
540 public void testBuildConfigsBean() throws Exception
541 {
542
543 try
544 {
545 m_builder.build((List<JPatternsConfigBaseBean>) null);
546 fail("Exception should have been occuried before this line.");
547 }
548 catch (Exception ex)
549 {
550 assertTrue(ex instanceof IllegalArgumentException);
551 }
552
553
554 try
555 {
556 final JPatternsConfig config1 = JPatternsTestUtils.getConfig(JPatternsConfigBeansBuilderImplTest.class, null,
557 ConfigsForTest.CONFIG_1);
558 final JPatternsConfigBean bean1 = m_builder.build(config1);
559 assertNotNull(bean1);
560 final JPatternsConfig config2 = JPatternsTestUtils.getConfig(JPatternsConfigBeansBuilderImplTest.class, null,
561 ConfigsForTest.CONFIG_2);
562 final JPatternsConfigBean bean2 = m_builder.build(config2);
563 assertNotNull(bean2);
564 final List<JPatternsConfigBaseBean> list = new ArrayList<JPatternsConfigBaseBean>();
565 list.add(bean1);
566 list.add(bean2);
567 final JPatternsConfigsBean bean = m_builder.build(list);
568 assertNotNull(bean);
569
570
571 String scopeName = JPConstants.DEFAULT_SCOPE_NAME;
572 assertNotNull(bean.getSection(scopeName, "GlobalSection1"));
573 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection1", scopeName, "GlobalSectionItem1"));
574 scopeName = "DEFAULT_SCOPE_2";
575 assertNotNull(bean.getSection(scopeName, "GlobalSection1"));
576 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection1", scopeName, "GlobalSectionItem1"));
577
578
579 scopeName = JPConstants.DEFAULT_SCOPE_NAME;
580 assertNotNull(bean.getSection(scopeName, "GlobalSection2"));
581 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem10"));
582 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem20"));
583
584 assertNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem1"));
585 assertNull(bean.getBusinessItem(scopeName, "GlobalSection2", scopeName, "GlobalSectionItem2"));
586
587
588 scopeName = "GlobalScope1";
589 assertNotNull(bean.getSection(scopeName, "GlobalSection3"));
590 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem1"));
591 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection3", scopeName, "GlobalSectionItem2"));
592
593 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection3",
594 JPConstants.DEFAULT_SCOPE_NAME, "GlobalSectionItem1"));
595 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection3",
596 JPConstants.DEFAULT_SCOPE_NAME, "GlobalSectionItem1"));
597 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection3", "GlobalItemScope1", "GlobalSectionItem2"));
598 assertNotNull(bean.getBusinessItem(JPConstants.DEFAULT_SCOPE_NAME, "GlobalSection3",
599 JPConstants.DEFAULT_SCOPE_NAME, "GlobalSectionItem2"));
600
601
602 scopeName = JPConstants.DEFAULT_SCOPE_NAME;
603 assertNotNull(bean.getSection(scopeName, "GlobalSection5"));
604 assertNotNull(bean.getBusinessItem(scopeName, "GlobalSection5", scopeName, "GlobalSectionItem5"));
605 assertNull(bean.getBusinessItem(scopeName, "GlobalSection5", scopeName, "GlobalSectionItem5_2"));
606 }
607 catch (Exception ex)
608 {
609 LoggingUtils.logException(LOG, ex, null, null);
610 fail("Exception should not occur for that case " + ex.getMessage());
611 }
612 }
613 }