1 package com.sourceforge.jpatterns.core.configuration;
2
3 import com.sourceforge.jpatterns.core.JPConstants;
4 import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException;
5 import com.sourceforge.jpatterns.core.configuration.model.IJPatternsConfigBeansBuilder;
6 import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBeansBuilderImpl;
7 import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBeansBuilderMockImpl;
8 import com.sourceforge.jpatterns.utils.JPatternsPropsUtils;
9 import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
10 import com.zmicer.utils.LoggingUtils;
11 import com.zmicer.utils.ReflexionUtils;
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15 import org.apache.log4j.Logger;
16
17 import java.util.Set;
18
19
20
21
22
23
24
25
26 public class PropertiesManagerImplTest extends TestCase
27 {
28
29
30
31 final private static Logger LOG = Logger.getLogger(PropertiesManagerImplTest.class);
32
33
34
35
36 private IPropertiesManager m_propertiesManager = PropertiesBasedFactory.getInstance().getPropertiesManager();
37
38
39
40
41 final private static String KEY_FOR_TEST = ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class);
42
43
44
45
46 final private static String VALUE_FOR_TEST = JPatternsConfigBeansBuilderImpl.class.getName();
47
48
49
50
51 final private static String OVERRIDEN_VALUE_FOR_TEST = JPatternsConfigBeansBuilderMockImpl.class.getName();
52
53
54
55
56 interface Cases
57 {
58 String ALL_PROPS = "allPropertiesCase";
59 String CUSTOM_PROP = "customPropertyCase";
60 String DEFAULT_PROP = "defaultPropertyCase";
61 String NO_PROPS = "noPropertiesCase";
62 }
63
64
65
66
67
68
69 public PropertiesManagerImplTest(String name)
70 {
71 super(name);
72 }
73
74
75
76
77
78
79 public void setUp() throws Exception
80 {
81 super.setUp();
82 }
83
84
85
86
87
88
89 public void tearDown() throws Exception
90 {
91 super.tearDown();
92 }
93
94
95
96
97
98
99 public static Test suite()
100 {
101 return new TestSuite(PropertiesManagerImplTest.class);
102 }
103
104
105
106
107
108
109
110 public void testCustomConfigPresents() throws Exception
111 {
112 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
113 assertTrue(m_propertiesManager.customConfigPresents());
114 JPatternsTestUtils.resetJVMProps();
115
116
117
118
119 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, "not_existed", true);
120 assertFalse(m_propertiesManager.customConfigPresents());
121 JPatternsTestUtils.resetJVMProps();
122 }
123
124
125
126
127
128
129
130 public void testDefaultConfigPresents() throws Exception
131 {
132 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
133 assertTrue(m_propertiesManager.defaultConfigPresents());
134 JPatternsTestUtils.resetJVMProps();
135
136 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
137 assertTrue(m_propertiesManager.defaultConfigPresents());
138 JPatternsTestUtils.resetJVMProps();
139
140
141 try
142 {
143 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.CUSTOM_PROP, null, null, true);
144
145 fail("Exception should have been occuried before this line.");
146 }
147 catch (Exception ex)
148 {
149 assertTrue(ex instanceof JPInitializationException);
150 }
151 finally
152 {
153 JPatternsTestUtils.resetJVMProps();
154 }
155 }
156
157
158
159
160
161
162
163 public void testInitConfigs() throws Exception
164 {
165
166 try
167 {
168 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
169
170 assertTrue(m_propertiesManager.customConfigPresents());
171 assertTrue(m_propertiesManager.defaultConfigPresents());
172
173 assertTrue(JPatternsPropsUtils.getDefaultBundleName().contains(
174 JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME));
175 assertTrue(JPatternsPropsUtils.getCustomBundleName().contains(
176 JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME));
177
178 assertNotNull(m_propertiesManager.getBundle(KEY_FOR_TEST));
179 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), OVERRIDEN_VALUE_FOR_TEST);
180 assertNotNull(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST));
181 assertEquals(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST), VALUE_FOR_TEST);
182
183 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, null,
184 "PropertiesManagerTest_default", "PropertiesManagerTest_custom", true);
185 assertNotNull(m_propertiesManager.getBundle(KEY_FOR_TEST));
186 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), OVERRIDEN_VALUE_FOR_TEST);
187 assertNotNull(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST));
188 assertEquals(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST), VALUE_FOR_TEST);
189 }
190 catch (Exception ex)
191 {
192 LoggingUtils.logException(LOG, ex, null, null);
193 fail("Exception should not occur for that case params" + ex.getMessage());
194 }
195 finally
196 {
197 JPatternsTestUtils.resetJVMProps();
198 }
199 }
200
201
202
203
204
205
206
207 public void testGetDefaultBundle() throws Exception
208 {
209
210 try
211 {
212 m_propertiesManager.getDefaultBundle(null);
213 fail("Exception should have been occuried before this line.");
214 }
215 catch (Exception ex)
216 {
217 assertTrue(ex instanceof IllegalArgumentException);
218 }
219
220 try
221 {
222 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
223
224 assertNotNull(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST));
225 assertEquals(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST), VALUE_FOR_TEST);
226
227
228 assertNull(m_propertiesManager.getDefaultBundle(KEY_FOR_TEST + "1"));
229 }
230 catch (Exception ex)
231 {
232 LoggingUtils.logException(LOG, ex, null, null);
233 fail("Exception should not occur for that case params" + ex.getMessage());
234 }
235 finally
236 {
237 JPatternsTestUtils.resetJVMProps();
238 }
239
240 try
241 {
242 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.NO_PROPS, null, null, true);
243
244 fail("Exception should have been occuried before this line.");
245 }
246 catch (Exception ex)
247 {
248 JPatternsTestUtils.resetJVMProps();
249 assertTrue(ex instanceof JPInitializationException);
250 }
251
252 }
253
254
255
256
257
258
259
260 public void testGetBundle() throws Exception
261 {
262
263 try
264 {
265 m_propertiesManager.getBundle((String) null);
266 fail("Exception should have been occuried before this line.");
267 }
268 catch (Exception ex)
269 {
270 assertTrue(ex instanceof IllegalArgumentException);
271 }
272
273
274 try
275 {
276 m_propertiesManager.getBundle("");
277 fail("Exception should have been occuried before this line.");
278 }
279 catch (Exception ex)
280 {
281 assertTrue(ex instanceof IllegalArgumentException);
282 }
283
284
285 try
286 {
287 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
288
289 assertFalse(m_propertiesManager.customConfigPresents());
290
291
292 assertNull(m_propertiesManager.getCustomBundle(KEY_FOR_TEST));
293 final String result = m_propertiesManager.getDefaultBundle(KEY_FOR_TEST);
294 assertNotNull(result);
295 assertEquals(result, VALUE_FOR_TEST);
296 assertEquals(result, m_propertiesManager.getBundle(KEY_FOR_TEST));
297
298 JPatternsTestUtils.resetJVMProps();
299 }
300 catch (Exception ex)
301 {
302 LoggingUtils.logException(LOG, ex, null, null);
303 fail("Exception should not occur for that case params" + ex.getMessage());
304 }
305
306
307 try
308 {
309 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
310 assertNotNull(m_propertiesManager.getBundle(KEY_FOR_TEST));
311 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), OVERRIDEN_VALUE_FOR_TEST);
312 }
313 catch (Exception ex)
314 {
315 LoggingUtils.logException(LOG, ex, null, null);
316 fail("Exception should not occur for that case params" + ex.getMessage());
317 }
318 finally
319 {
320 JPatternsTestUtils.resetJVMProps();
321 }
322 }
323
324
325
326
327
328
329
330 public void testGetBundleByClass() throws Exception
331 {
332
333 try
334 {
335 m_propertiesManager.getBundle((Class) null);
336 fail("Exception should have been occuried before this line.");
337 }
338 catch (Exception ex)
339 {
340 assertTrue(ex instanceof IllegalArgumentException);
341 }
342 try
343 {
344 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
345
346 assertNotNull(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class));
347 assertEquals(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class), OVERRIDEN_VALUE_FOR_TEST);
348 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), OVERRIDEN_VALUE_FOR_TEST);
349
350 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
351
352 assertNotNull(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class));
353 assertEquals(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class), VALUE_FOR_TEST);
354 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), VALUE_FOR_TEST);
355 }
356 catch (Exception ex)
357 {
358 LoggingUtils.logException(LOG, ex, null, null);
359 fail("Exception should not occur for that case params" + ex.getMessage());
360 }
361 finally
362 {
363 JPatternsTestUtils.resetJVMProps();
364 }
365
366
367 try
368 {
369 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
370
371 assertNotNull(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class));
372 assertEquals(m_propertiesManager.getBundle(IJPatternsConfigBeansBuilder.class), VALUE_FOR_TEST);
373 assertEquals(m_propertiesManager.getBundle(KEY_FOR_TEST), VALUE_FOR_TEST);
374
375 JPatternsTestUtils.resetJVMProps();
376 }
377 catch (Exception ex)
378 {
379 LoggingUtils.logException(LOG, ex, null, null);
380 fail("Exception should not occur for that case params" + ex.getMessage());
381 }
382
383 }
384
385
386
387
388
389
390
391 public void testGetInstance() throws Exception
392 {
393 assertNotNull(m_propertiesManager);
394 }
395
396
397
398
399
400
401
402 public void testGetCustomBundle() throws Exception
403 {
404
405 try
406 {
407 m_propertiesManager.getDefaultBundle(null);
408 fail("Exception should have been occuried before this line.");
409 }
410 catch (Exception ex)
411 {
412 assertTrue(ex instanceof IllegalArgumentException);
413 }
414
415 try
416 {
417 m_propertiesManager.getDefaultBundle("");
418 fail("Exception should have been occuried before this line.");
419 }
420 catch (Exception ex)
421 {
422 assertTrue(ex instanceof IllegalArgumentException);
423 }
424
425 try
426 {
427 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
428 final String value = m_propertiesManager.getCustomBundle(KEY_FOR_TEST);
429 assertNotNull(value);
430 assertEquals(value, OVERRIDEN_VALUE_FOR_TEST);
431 }
432 catch (Exception ex)
433 {
434 LoggingUtils.logException(LOG, ex, null, null);
435 fail("Exception should not occur for that case params" + ex.getMessage());
436 }
437 finally
438 {
439 JPatternsTestUtils.resetJVMProps();
440 }
441
442 try
443 {
444 assertNull(m_propertiesManager.getCustomBundle(KEY_FOR_TEST + "notexistedkey"));
445 }
446 catch (Exception ex)
447 {
448 LoggingUtils.logException(LOG, ex, null, null);
449 fail("Exception should not occur for that case params" + ex.getMessage());
450 }
451
452 try
453 {
454 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
455
456
457 assertNull(m_propertiesManager.getCustomBundle(KEY_FOR_TEST));
458
459 JPatternsTestUtils.resetJVMProps();
460 }
461 catch (Exception ex)
462 {
463 LoggingUtils.logException(LOG, ex, null, null);
464 fail("Exception should not occur for that case params" + ex.getMessage());
465 }
466 }
467
468
469
470
471
472
473
474 public void testGetBundledObject() throws Exception
475 {
476
477 try
478 {
479 m_propertiesManager.getBundledObject((Class) null);
480 fail("Exception should have been occuried before this line.");
481 }
482 catch (Exception ex)
483 {
484 assertTrue(ex instanceof IllegalArgumentException);
485 }
486
487
488 try
489 {
490 m_propertiesManager.getBundledObject((String) null);
491 fail("Exception should have been occuried before this line.");
492 }
493 catch (Exception ex)
494 {
495 assertTrue(ex instanceof IllegalArgumentException);
496 }
497
498
499 try
500 {
501 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
502 m_propertiesManager.getBundledObject(PropertiesManagerImplTest.class);
503 fail("Exception should have been occuried before this line.");
504 }
505 catch (Exception ex)
506 {
507 assertTrue(ex instanceof JPInitializationException);
508 }
509 finally
510 {
511 JPatternsTestUtils.resetJVMProps();
512 }
513
514 try
515 {
516 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
517 m_propertiesManager.getBundledObject(TestCase.class);
518 fail("Exception should have been occuried before this line.");
519 }
520 catch (Exception ex)
521 {
522 assertTrue(ex instanceof JPInitializationException);
523 }
524 finally
525 {
526 JPatternsTestUtils.resetJVMProps();
527 }
528
529 try
530 {
531 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
532 m_propertiesManager.getBundledObject(PropertiesBasedFactory.class);
533 fail("Exception should have been occuried before this line.");
534 }
535 catch (Exception ex)
536 {
537 assertTrue(ex instanceof JPInitializationException);
538 }
539 finally
540 {
541 JPatternsTestUtils.resetJVMProps();
542 }
543
544
545 try
546 {
547 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
548 final Object object = m_propertiesManager.getBundledObject(IJPatternsConfigBeansBuilder.class);
549 assertNotNull(object);
550 assertTrue(object instanceof JPatternsConfigBeansBuilderMockImpl);
551 }
552 catch (Exception ex)
553 {
554 LoggingUtils.logException(LOG, ex, null, null);
555 fail("Exception should not occur for that case params" + ex.getMessage());
556 }
557 finally
558 {
559 JPatternsTestUtils.resetJVMProps();
560 }
561
562 try
563 {
564 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
565 final Object object = m_propertiesManager.getBundledObject(IJPatternsConfigBeansBuilder.class);
566 assertNotNull(object);
567 assertTrue(object instanceof JPatternsConfigBeansBuilderMockImpl);
568 }
569 catch (Exception ex)
570 {
571 LoggingUtils.logException(LOG, ex, null, null);
572 fail("Exception should not occur for that case params" + ex.getMessage());
573 }
574 finally
575 {
576 JPatternsTestUtils.resetJVMProps();
577 }
578 }
579
580
581
582
583
584
585
586 public void testGetMergedKeys() throws Exception
587 {
588
589 try
590 {
591 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
592
593 final Set<String> keys = m_propertiesManager.getMergedKeys();
594 assertNotNull(keys);
595
596 assertTrue(keys.contains(KEY_FOR_TEST));
597 assertTrue(keys.contains(ReflexionUtils.getBaseName(TestCase.class)));
598 assertTrue(keys.contains(ReflexionUtils.getBaseName(PropertiesBasedFactory.class)));
599
600 JPatternsTestUtils.resetJVMProps();
601 }
602 catch (Exception ex)
603 {
604 LoggingUtils.logException(LOG, ex, null, null);
605 fail("Exception should not occur for that case params" + ex.getMessage());
606 }
607
608
609 try
610 {
611 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.DEFAULT_PROP, null, null, true);
612
613 final Set<String> keys = m_propertiesManager.getMergedKeys();
614 assertNotNull(keys);
615
616 assertTrue(keys.contains(KEY_FOR_TEST));
617 assertFalse(keys.contains(ReflexionUtils.getBaseName(TestCase.class)));
618 assertFalse(keys.contains(ReflexionUtils.getBaseName(PropertiesBasedFactory.class)));
619
620 JPatternsTestUtils.resetJVMProps();
621 }
622 catch (Exception ex)
623 {
624 LoggingUtils.logException(LOG, ex, null, null);
625 fail("Exception should not occur for that case params" + ex.getMessage());
626 }
627
628
629 try
630 {
631 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.CUSTOM_PROP, null, null, true);
632 final Set<String> keys = m_propertiesManager.getMergedKeys();
633 fail("Exception should have been occuried before this line.");
634 }
635 catch (Exception ex)
636 {
637 JPatternsTestUtils.resetJVMProps();
638 assertTrue(ex instanceof JPInitializationException);
639 }
640 }
641
642
643
644
645
646
647
648 public void testInitUseOnlyCustomConfigIfPresent() throws Exception
649 {
650
651 try
652 {
653 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
654
655 ((PropertiesManagerImpl) m_propertiesManager).initUseOnlyCustomConfigIfPresent();
656 assertFalse(m_propertiesManager.useOnlyCustomConfigIfPresent());
657
658
659 String result = m_propertiesManager.getBundle(ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class));
660 assertNotNull(result);
661 assertEquals(result, JPatternsConfigBeansBuilderMockImpl.class.getName());
662
663
664 result = m_propertiesManager.getDefaultBundle(ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class));
665 assertNotNull(result);
666 assertEquals(result, JPatternsConfigBeansBuilderImpl.class.getName());
667
668 JPatternsTestUtils.resetJVMProps();
669 }
670 catch (Exception ex)
671 {
672 LoggingUtils.logException(LOG, ex, null, null);
673 fail("Exception should not occur for that case params" + ex.getMessage());
674 }
675
676 try
677 {
678 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, null,
679 "PropertiesManagerTest_default", "PropertiesManagerTest_custom", true);
680
681
682 String result = m_propertiesManager.getDefaultBundle(ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class));
683 assertNotNull(result);
684 assertEquals(result, JPatternsConfigBeansBuilderImpl.class.getName());
685
686
687 result = m_propertiesManager.getBundle(ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class));
688 assertNotNull(result);
689 assertEquals(result, JPatternsConfigBeansBuilderMockImpl.class.getName());
690 }
691 catch (Exception ex)
692 {
693 LoggingUtils.logException(LOG, ex, null, null);
694 fail("Exception should not occur for that case params" + ex.getMessage());
695 }
696
697
698 }
699
700
701
702
703
704
705
706 public void testUseOnlyCustomConfigIfPresent() throws Exception
707 {
708
709 try
710 {
711 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
712 assertFalse(m_propertiesManager.useOnlyCustomConfigIfPresent());
713 JPatternsTestUtils.resetJVMProps();
714 }
715 catch (Exception ex)
716 {
717 LoggingUtils.logException(LOG, ex, null, null);
718 fail("Exception should not occur for that case params" + ex.getMessage());
719 }
720
721 }
722
723
724
725
726
727
728
729 public void testCheckIsInitialized() throws Exception
730 {
731
732 try
733 {
734 JPatternsTestUtils.setJVMPropsParams(PropertiesManagerImplTest.class, false, Cases.ALL_PROPS, null, null, true);
735 assertTrue(m_propertiesManager.customConfigPresents());
736 ((PropertiesManagerImpl) m_propertiesManager).checkIsInitialized();
737 }
738 catch (Exception ex)
739 {
740 LoggingUtils.logException(LOG, ex, null, null);
741 fail("Exception should not occur for that case params" + ex.getMessage());
742 }
743
744 }
745 }