1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.slick.configuration;
import java.beans.*;
import java.util.*;
import junit.framework.*;
import org.jitsi.service.configuration.*;
import org.osgi.framework.*;
/**
* Tests basic ConfiguratioService behaviour.
*
* @author Emil Ivov
*/
public class TestConfigurationService extends TestCase
{
/**
* The ConfigurationService that we will be testing.
*/
private ConfigurationService configurationService = null;
/**
* The PropertyChangeEvent that our test listeners will capture for testing.
* Make sure we null that upon tear down
*/
private PropertyChangeEvent propertyChangeEvent = null;
/**
* The name of a property that we will be using for testing.
*/
private final String propertyName = "my.test.property";
/**
* The name of a property that we will be using for testing custom event
* notification.
*/
private final String listenedPropertyName = "a.property.i.listen.to";
/**
* The value of the property with name propertyName.
*/
private final String propertyValue = "19200";
/**
* A new value for the property with name propertyName
*/
private final String propertyNewValue = "19201";
/**
* A PropertyChange listener impl that registers the last received event.
*/
private PropertyChangeListener pListener = new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent event)
{
propertyChangeEvent = event;
}
};
/**
* A straightforward Vetoable change listener that throws an exception
* upon any change
*/
ConfigVetoableChangeListener rudeVetoListener = new ConfigVetoableChangeListener()
{
public void vetoableChange(PropertyChangeEvent event) throws
ConfigPropertyVetoException
{
throw new ConfigPropertyVetoException("Just for the fun of it", event);
}
};
/**
* A straightforward implementation of a vetoable change listener that that
* does not throw a veto exception and only stored the last received event.
*/
ConfigVetoableChangeListener gentleVetoListener = new ConfigVetoableChangeListener()
{
public void vetoableChange(PropertyChangeEvent event) throws
ConfigPropertyVetoException
{
propertyChangeEvent = event;
}
};
/**
* Generic JUnit Constructor.
* @param name the name of the test
*/
public TestConfigurationService(String name)
{
super(name);
BundleContext context = ConfigurationServiceLick.bc;
ServiceReference ref = context.getServiceReference(
ConfigurationService.class.getName());
configurationService = (ConfigurationService)context.getService(ref);
}
/**
* Generic JUnit setUp method.
* @throws Exception if anything goes wrong.
*/
protected void setUp() throws Exception
{
configurationService.setProperty(propertyName, null);
configurationService.setProperty(listenedPropertyName, null);
super.setUp();
}
/**
* Generic JUnit tearDown method.
* @throws Exception if anything goes wrong.
*/
protected void tearDown() throws Exception
{
//first remove any remaining listeners
configurationService.removePropertyChangeListener(pListener);
configurationService.removeVetoableChangeListener(rudeVetoListener);
configurationService.removeVetoableChangeListener(gentleVetoListener);
//clear used properties.
configurationService.setProperty(propertyName, null);
configurationService.setProperty(listenedPropertyName, null);
propertyChangeEvent = null;
super.tearDown();
}
/**
* Tests whether setting and getting properties works correctly.
* @throws PropertyVetoException in case someone wrongfully vetoes the
* property change.
*/
public void testSetGetProperty() throws PropertyVetoException
{
String propertyName = "my.test.property";
Object property = new String("my.test.property's value");
configurationService.setProperty(propertyName, property);
Object actualReturn = configurationService.getProperty(propertyName);
assertEquals("a property was not properly stored",
property, actualReturn);
}
/**
* Tests whether setting and getting works alright for properties declared
* as system and whether resolving and retrieving from the system property
* set are done right.
* @throws PropertyVetoException in case someone wrongfully vetoes the
* property change.
*/
public void testSystemPoperties() throws PropertyVetoException
{
//first simply store and retrieve a system property
String propertyName = "my.test.system.property";
Object property = new String("sys.value.1");
configurationService.setProperty(propertyName, property, true);
Object actualReturn = configurationService.getProperty(propertyName);
assertEquals("a sys property was not properly stored",
property, actualReturn);
//now check whether you can also retrieve it from the sys property set.
actualReturn = System.getProperty(propertyName);
assertEquals("a property was not properly stored", property, actualReturn);
//verify that modifying it in the sys property set would affect the
//value in the configuration service
property = new String("second.sys.value");
System.setProperty(propertyName, property.toString());
actualReturn = configurationService.getProperty(propertyName);
assertEquals("a property was not properly stored", property, actualReturn);
//now make sure that modifying it in the configurationService would
//result in the corresponding change in the system property set.
property = new String("third.sys.value");
configurationService.setProperty(propertyName, property.toString());
actualReturn = System.getProperty(propertyName);
assertEquals("a property was not properly stored", property, actualReturn);
}
/**
* Tests whether getString properly returns string values and that it
* correctly handles corner cases.
*
* @throws PropertyVetoException if someone vetoes our change (which
* shouldn't happen)
*/
public void testGetString() throws PropertyVetoException
{
//test a basic scenario
String propertyName = "my.test.property";
Object property = new String("my.test.property's value");
configurationService.setProperty(propertyName, property);
String actualReturn = configurationService.getString(propertyName);
assertEquals("getString failed to retrieve a property",
property.toString(), actualReturn);
//verify that setting a non string object would not mess things up
property = new Integer(7121979);
configurationService.setProperty(propertyName, property);
actualReturn = configurationService.getString(propertyName);
assertEquals("getString failed to retrieve a property",
property.toString(), actualReturn);
//verify that setting a whitespace only string would return null
property = new String("\t\n ");
configurationService.setProperty(propertyName, property);
actualReturn = configurationService.getString(propertyName);
assertNull("getString did not trim a white space only string",
actualReturn);
}
/**
* Records a few properties with a similar prefix and verifies that they're
* all returned by the getPropertyNamesByPrefix() method.
*
* @throws PropertyVetoException if someone vetoes our change (which
* shouldn't happen)
*/
public void testGetPropertyNamesByPrefix() throws PropertyVetoException
{
String prefix = "this.is.a.prefix";
String exactPrefixProp1Name = prefix + ".PROP1";
String exactPrefixProp2Name = prefix + ".PROP3";
String longerPrefixProp3Name = prefix + ".which.is.longer.PROP3";
String completeMismatchProp4Name = "and.hereis.one.other.prefix.PROP4";
configurationService.setProperty(exactPrefixProp1Name, new Object());
configurationService.setProperty(exactPrefixProp2Name, new Object());
configurationService.setProperty(longerPrefixProp3Name, new Object());
configurationService.setProperty(completeMismatchProp4Name
, new Object());
//try an exact match first
List<String> propertyNames
= configurationService.getPropertyNamesByPrefix(prefix, true);
assertTrue("Returned list did not contain all property names. "
+ " MissingPropertyName: " + exactPrefixProp1Name
, propertyNames.contains(exactPrefixProp1Name));
assertTrue("Returned list did not contain all property names. "
+ " MissingPropertyName: " + exactPrefixProp2Name
, propertyNames.contains(exactPrefixProp2Name));
assertEquals("Returned list contains more properties than expected. "
+ " List was: " + propertyNames
, 2, propertyNames.size() );
//try a broader search
propertyNames
= configurationService.getPropertyNamesByPrefix(prefix, false);
assertTrue("Returned list did not contain all property names. "
+ " MissingPropertyName: " + exactPrefixProp1Name
, propertyNames.contains(exactPrefixProp1Name));
assertTrue("Returned list did not contain all property names. "
+ " MissingPropertyName: " + exactPrefixProp2Name
, propertyNames.contains(exactPrefixProp2Name));
assertTrue("Returned list did not contain all property names. "
+ " MissingPropertyName: " + longerPrefixProp3Name
, propertyNames.contains(longerPrefixProp3Name));
assertEquals("Returned list contains more properties than expected. "
+ " List was: " + propertyNames
, 3, propertyNames.size());
}
/**
* Tests event notification through multicast listeners (those that are
* registered for the whole configuration and not a single property only).
*/
public void testMulticastEventNotification()
{
propertyChangeEvent = null;
configurationService.addPropertyChangeListener(pListener);
// test the initial set of a property.
try
{
configurationService.setProperty(propertyName, propertyValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNotNull( "No PropertyChangeEvent was delivered upon setProperty",
propertyChangeEvent);
assertNull("oldValue must be null", propertyChangeEvent.getOldValue());
assertEquals( "newValue is not the value we just set!",
propertyValue,
propertyChangeEvent.getNewValue()
);
assertEquals( "propertyName is not the value we just set!",
propertyName,
propertyChangeEvent.getPropertyName()
);
//test setting a new value;
propertyChangeEvent = null;
try
{
configurationService.setProperty(propertyName, propertyNewValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNotNull( "No PropertyChangeEvent was delivered upon setProperty",
propertyChangeEvent);
assertEquals("incorrect oldValue",
propertyValue,
propertyChangeEvent.getOldValue());
assertEquals( "newValue is not the value we just set!",
propertyNewValue,
propertyChangeEvent.getNewValue());
//test remove
propertyChangeEvent = null;
configurationService.removePropertyChangeListener(pListener);
try
{
configurationService.setProperty(propertyName, propertyValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNull( "A PropertyChangeEvent after unregistering a listener.",
propertyChangeEvent);
}
/**
* Test event dispatch to vetoable listeners registered for the whole
* configuration.
*/
public void testMulticastEventNotificationToVetoableListeners()
{
String propertyValue = "19200";
String propertyNewValue = "19201";
propertyChangeEvent = null;
configurationService.addVetoableChangeListener(gentleVetoListener);
// test the initial set of a property.
try
{
configurationService.setProperty(propertyName, propertyValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNotNull( "No PropertyChangeEvent was delivered "
+"to VetoableListeners upon setProperty",
propertyChangeEvent);
assertNull("oldValue must be null", propertyChangeEvent.getOldValue());
assertEquals( "newValue is not the value we just set!",
propertyValue,
propertyChangeEvent.getNewValue());
assertEquals( "propertyName is not the value we just set!",
propertyName,
propertyChangeEvent.getPropertyName());
//test setting a new value;
propertyChangeEvent = null;
try
{
configurationService.setProperty(propertyName, propertyNewValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNotNull( "No PropertyChangeEvent was delivered to veto listener "
+"upon setProperty",
propertyChangeEvent);
assertEquals("incorrect oldValue",
propertyValue,
propertyChangeEvent.getOldValue());
assertEquals( "newValue is not the value we just set!",
propertyNewValue,
propertyChangeEvent.getNewValue());
//test remove
propertyChangeEvent = null;
configurationService.removeVetoableChangeListener(gentleVetoListener);
try
{
configurationService.setProperty(propertyName, propertyValue);
}
catch (ConfigPropertyVetoException ex)
{
fail("A PropertyVetoException came from nowhere. Exc="
+ ex.getMessage());
}
assertNull( "A PropertyChangeEvent after unregistering a listener.",
propertyChangeEvent);
}
/**
* Test whether vetoing changes works as it is supposed to.
*/
public void testVetos()
{
propertyChangeEvent = null;
configurationService.addVetoableChangeListener(rudeVetoListener);
configurationService.addPropertyChangeListener(pListener);
ConfigPropertyVetoException exception = null;
try
{
configurationService.setProperty(propertyName, propertyValue);
}
catch (ConfigPropertyVetoException ex)
{
exception = ex;
}
//make sure the exception was thrown
assertNotNull("A vetoable change event was not dispatched or an "
+"exception was not let through.",
exception);
//make sure no further event dispatching was done
assertNull("A property change event was delivered even after "+
"the property change was vetoed.",
propertyChangeEvent);
//make sure the property did not get modified after vetoing the change
assertNull( "A property was changed even avfter vetoing the change."
,configurationService.getProperty(propertyName));
// now let's make sure that we have the right order of event dispatching.
propertyChangeEvent = null;
configurationService.removeVetoableChangeListener(rudeVetoListener);
ConfigVetoableChangeListener vcListener = new ConfigVetoableChangeListener(){
public void vetoableChange(PropertyChangeEvent event)
{
assertNull("propertyChangeEvent was not null which means that it has "
+"bean delivered to the propertyChangeListener prior to "
+"being delivered to the vetoable change listener.",
propertyChangeEvent);
}
};
try
{
configurationService.setProperty(propertyName, propertyNewValue);
}
catch (ConfigPropertyVetoException ex1)
{
ex1.printStackTrace();
fail("unexpected veto exception. message:" + ex1.getMessage());
}
configurationService.removeVetoableChangeListener(vcListener);
}
/**
* Make sure that adding listeners for a single property name
* only gets us events for that listeners. Removing a listener for a
* specific property should also be proved to no obstruct event delivery to
* the same listener had it been registered for other properties.
*
* @throws PropertyVetoException if someone vetoes our change (which
* shouldn't happen)
*/
public void testSinglePropertyEventNotification()
throws PropertyVetoException
{
String listenedPropertyValue = "19.2598";
String listenedPropertyNewValue = "19.29581";
//test basic selective event dispatch
configurationService.addPropertyChangeListener(
listenedPropertyName, pListener);
propertyChangeEvent = null;
configurationService.setProperty(
propertyName, propertyValue);
assertNull("setting prop:"+propertyName + " caused an event notif. to "+
"listener registered for prop:" + listenedPropertyName,
propertyChangeEvent);
configurationService.setProperty(
listenedPropertyName, listenedPropertyValue);
assertNotNull("No event was dispatched upon modification of prop:"
+listenedPropertyName,
propertyChangeEvent );
assertNull("oldValue must be null", propertyChangeEvent.getOldValue());
assertEquals("wrong newValue",
listenedPropertyValue,
propertyChangeEvent.getNewValue());
//test that a generic remove only removes the generic listener
propertyChangeEvent = null;
configurationService.removePropertyChangeListener(pListener);
configurationService.setProperty(
listenedPropertyName, listenedPropertyNewValue);
assertNotNull("No event was dispatched upon modification of prop:"
+listenedPropertyName
+ ". The listener was wrongfully removed.",
propertyChangeEvent );
assertEquals("wrong oldValue",
listenedPropertyValue,
propertyChangeEvent.getOldValue());
assertEquals("wrong newValue",
listenedPropertyNewValue,
propertyChangeEvent.getNewValue());
//make sure that removing the listener properly - really removes it.
propertyChangeEvent = null;
configurationService.removePropertyChangeListener(
listenedPropertyName, pListener);
configurationService.setProperty(listenedPropertyName, propertyValue);
assertNull(
"An event was wrongfully dispatched after removing a listener",
propertyChangeEvent
);
}
/**
* Make sure that adding vetoable listeners for a single property name
* only gets us events for that listeners. Removing a listener for a
* specific property should also be proved to no obstruct event delivery to
* the same listener had it been registered for other properties.
*
* @throws PropertyVetoException if someone vetoes our change (which
* shouldn't happen)
*/
public void testSinglePropertyVetoEventNotification()
throws PropertyVetoException
{
String listenedPropertyValue = "19.2598";
String listenedPropertyNewValue = "19.29581";
ConfigVetoableChangeListener vetoListener = new ConfigVetoableChangeListener()
{
public void vetoableChange(PropertyChangeEvent event)
{
propertyChangeEvent = event;
}
};
//test basic selective event dispatch
configurationService.addVetoableChangeListener(
listenedPropertyName, vetoListener);
propertyChangeEvent = null;
configurationService.setProperty(
propertyName, propertyValue);
assertNull("setting prop:" + propertyName + " caused an event notif. to " +
"listener registered for prop:" + listenedPropertyName,
propertyChangeEvent);
configurationService.setProperty(
listenedPropertyName, listenedPropertyValue);
assertNotNull("No event was dispatched upon modification of prop:"
+ listenedPropertyName,
propertyChangeEvent);
assertNull("oldValue must be null", propertyChangeEvent.getOldValue());
assertEquals("wrong newValue",
listenedPropertyValue,
propertyChangeEvent.getNewValue());
//test that a generic remove only removes the generic listener
propertyChangeEvent = null;
configurationService.removeVetoableChangeListener(vetoListener);
configurationService.setProperty(
listenedPropertyName, listenedPropertyNewValue);
assertNotNull("No event was dispatched upon modification of prop:"
+ listenedPropertyName
+ ". The listener was wrongfully removed.",
propertyChangeEvent);
assertEquals("wrong oldValue",
listenedPropertyValue,
propertyChangeEvent.getOldValue());
assertEquals("wrong newValue",
listenedPropertyNewValue,
propertyChangeEvent.getNewValue());
//make sure that removing the listener properly - really removes it.
propertyChangeEvent = null;
configurationService.removeVetoableChangeListener(
listenedPropertyName, vetoListener);
configurationService.setProperty(
listenedPropertyName, listenedPropertyValue);
assertNull(
"An event was wrongfully dispatched after removing a listener",
propertyChangeEvent
);
//make sure that adding a generic listener, then adding a custom prop
//listener, then removing it - would not remove the generic listener.
propertyChangeEvent = null;
configurationService.addVetoableChangeListener(vetoListener);
configurationService.addVetoableChangeListener(
listenedPropertyName, vetoListener);
configurationService.removeVetoableChangeListener(
listenedPropertyName, vetoListener);
configurationService.setProperty(
listenedPropertyName, listenedPropertyNewValue);
assertNotNull("No event was dispatched upon modification of prop:"
+ listenedPropertyName
+ ". The global listener was wrongfully removed.",
propertyChangeEvent);
assertEquals("wrong propertyName",
listenedPropertyName,
propertyChangeEvent.getPropertyName());
assertEquals("wrong oldValue",
listenedPropertyValue,
propertyChangeEvent.getOldValue());
assertEquals("wrong newValue",
listenedPropertyNewValue,
propertyChangeEvent.getNewValue());
//fail("Testing failures! "
//+"Wanted to know whether cruisecontrol will notice the falure. emil.");
}
}
|