Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0eefb8e16eebcdbc4209cc6da73ce4e6c1bb434b (plain) (blame)
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/**
 * 
 */
package org.eclipse.papyrus.infra.core.services;

import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.papyrus.infra.core.services.IService;
import org.eclipse.papyrus.infra.core.services.ServiceDescriptor;
import org.eclipse.papyrus.infra.core.services.FakeServiceFactory.FakeCreatedService;
import org.eclipse.papyrus.infra.core.services.ServiceA.TraceKind;
import org.eclipse.papyrus.infra.core.services.ServiceDescriptor.ServiceTypeKind;
import org.eclipse.papyrus.infra.core.services.ServiceMultiException;
import org.eclipse.papyrus.infra.core.services.ServiceStartKind;
import org.eclipse.papyrus.infra.core.services.ServiceState;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;


/**
 * @author dumoulin
 * 
 */
public class ServicesRegistryTest extends TestCase {

	/** The registry to test */
	ServicesRegistry servicesRegistry;

	ServiceDescriptor serviceADesc = new LazyServiceADescriptor();

	ServiceDescriptor serviceA10Desc = new LazyServiceA10Descriptor();

	ServiceDescriptor serviceBDesc = new LazyServiceBDescriptor();

	ServiceDescriptor serviceCDesc = new ServiceCDescriptor();

	ServiceDescriptor servicePojoADesc = new LazyServicePojoADescriptor();

	public ServicesRegistryTest(String name) {
		super(name);
	}

	/**
	 * @throws java.lang.Exception
	 */
	protected void setUp() throws Exception {
		servicesRegistry = new ServicesRegistry();
	}

	/**
	 * @throws java.lang.Exception
	 */
	protected void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link org.eclipse.papyrus.infra.core.services.ServicesRegistry#add(org.eclipse.papyrus.infra.core.services.ServiceDescriptor)} .
	 * @throws ServiceMultiException 
	 */
	public void testAdd() throws ServiceMultiException {

		servicesRegistry.add(serviceADesc);
		servicesRegistry.add(serviceBDesc);
		servicesRegistry.add(serviceCDesc);
		servicesRegistry.add(servicePojoADesc);

		// Test entries creation
//		try {
//			assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey()));
//			assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey()));
//			assertFalse("serviceC stopped", servicesRegistry.isStarted(serviceCDesc.getKey()));
//			assertFalse("servicePojoA stopped", servicesRegistry.isStarted(servicePojoADesc.getKey()));
//		} catch (ServiceNotFoundException e) {
//			fail("Service should exist." + e.getMessage());
//		}

		// Test startup
		servicesRegistry.startRegistry();

		// Test always started
		try {
			assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey()));
			assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey()));
			assertEquals("serviceC state", ServiceState.started, servicesRegistry.serviceState(serviceCDesc.getKey()));
			assertTrue("serviceC started", servicesRegistry.isStarted(serviceCDesc.getKey()));
			assertFalse("servicePojoA stopped", servicesRegistry.isStarted(servicePojoADesc.getKey()));
		} catch (ServiceNotFoundException e) {
			fail("Service should exist.");
		}


	}

	/**
	 * Test method for {@link org.eclipse.papyrus.infra.core.services.ServicesRegistry#getService(java.lang.Object)}.
	 * 
	 * @throws ServiceException
	 */
	public void testGetService() throws ServiceException {
		servicesRegistry.add(serviceADesc);
		servicesRegistry.add(serviceBDesc);
		servicesRegistry.add(serviceCDesc);
		servicesRegistry.add(servicePojoADesc);


		servicesRegistry.startRegistry();
		
		// Test lazy service
		Object serviceA = servicesRegistry.getService(serviceADesc.getKey());
		assertNotNull("service created", serviceA);
		assertEquals("right class", ServiceA.class, serviceA.getClass());

		Object serviceA2 = servicesRegistry.getService(serviceADesc.getKey());
		assertEquals("Second retrieve get the same service", serviceA, serviceA2);

		// test startup service
		Object serviceC = servicesRegistry.getService(serviceCDesc.getKey());
		assertNotNull("service created", serviceC);
		assertEquals("right class", ServiceC.class, serviceC.getClass());

		Object serviceC2 = servicesRegistry.getService(serviceCDesc.getKey());
		assertEquals("Second retrieve get the same service", serviceC, serviceC2);

		// test pojo service
		Object servicePojo = servicesRegistry.getService(servicePojoADesc.getKey());
		assertNotNull("service created", servicePojo);
		assertEquals("right class", ServicePojoA.class, servicePojo.getClass());

		Object servicePojo2 = servicesRegistry.getService(servicePojoADesc.getKey());
		assertEquals("Second retrieve get the same service", servicePojo, servicePojo2);


	}

	/**
	 * Test method for {@link org.eclipse.papyrus.infra.core.services.ServicesRegistry#getService(java.lang.Object)}.
	 * 
	 * @throws ServiceException
	 */
	public void testGetServiceByClass() throws ServiceException {
		servicesRegistry.add(serviceADesc);
		servicesRegistry.add(serviceBDesc);
		servicesRegistry.add(serviceCDesc);
		servicesRegistry.add(servicePojoADesc);


		servicesRegistry.startRegistry();
		
		// Test lazy service
		ServiceA serviceA = servicesRegistry.getService(ServiceA.class);
		assertNotNull("service created", serviceA);
		assertEquals("right class", ServiceA.class, serviceA.getClass());

		ServiceA serviceA2 = servicesRegistry.getService(ServiceA.class);
		assertEquals("Second retrieve get the same service", serviceA, serviceA2);

		// test startup service
		ServiceC serviceC = servicesRegistry.getService(ServiceC.class);
		assertNotNull("service created", serviceC);
		assertEquals("right class", ServiceC.class, serviceC.getClass());

		// test pojo service
		ServicePojoA servicePojo = servicesRegistry.getService(ServicePojoA.class);
		assertNotNull("service created", servicePojo);
		assertEquals("right class", ServicePojoA.class, servicePojo.getClass());

		Object servicePojo2 = servicesRegistry.getService(ServicePojoA.class);
		assertEquals("Second retrieve get the same service", servicePojo, servicePojo2);


	}

	/**
	 * Try to register 2 services under the same name, but with different priorities.
	 * 
	 * @throws ServiceException
	 */
	public void testPriority() throws ServiceException {
		servicesRegistry.add(serviceADesc);
		servicesRegistry.add(serviceA10Desc);
		servicesRegistry.add(serviceBDesc);

		// Test creation
//		try {
//			assertFalse("serviceA stopped", servicesRegistry.isStarted(serviceADesc.getKey()));
//			assertFalse("serviceB stopped", servicesRegistry.isStarted(serviceBDesc.getKey()));
//		} catch (ServiceNotFoundException e) {
//			fail("Service should exist.");
//		}

		servicesRegistry.startRegistry();
		
		Object serviceA = servicesRegistry.getService(serviceADesc.getKey());

		assertNotNull("Service created", serviceA);
		assertEquals("right class", ServiceA10.class, serviceA.getClass());


	}

	/**
	 * Test add for a direct instance of service.
	 * 
	 * @throws ServiceException
	 */
	public void testAddDirectInstance() throws ServiceException {
		IService instanciatedService = new ServiceA();
		String key = instanciatedService.getClass().getName();

		servicesRegistry.add(key, 1, instanciatedService);

		servicesRegistry.add(serviceCDesc);
		servicesRegistry.add(serviceBDesc);

		servicesRegistry.startRegistry();

		Object serviceA = servicesRegistry.getService(key);

		assertNotNull("Service created", serviceA);
		assertEquals("right object", instanciatedService, serviceA);
		assertEquals("right class", ServiceA.class, serviceA.getClass());


	}

	/**
	 * Test life cycle for directly registered services
	 * @throws ServiceException
	 */
	public void testCallsOrder() throws ServiceException {
		// Register services as STARTUP !
		servicesRegistry.add(ServiceA.class, 1, new ServiceA() );
		// Lazy - generate no trace
		servicesRegistry.add(ServiceB.class, 1, new ServiceB(), ServiceStartKind.LAZY);
		servicesRegistry.add(ServiceC.class, 1, new ServiceC());
		servicesRegistry.add(ServicePojoA.class, 1, new ServicePojoA());
		

		ServiceA.resetTrace();
		servicesRegistry.startRegistry();
		
		// Check orders
		// 
		int i=0;
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
		
		// Now, start lazy service
		ServiceB service = servicesRegistry.getService(ServiceB.class);
		assertNotNull("service found", service);
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
			
		
		// Check dispose
		servicesRegistry.disposeRegistry();
		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
		
		// 
	}
	
	/**
	 * Test life cycle for   services registered wih descriptors
	 * @throws ServiceException
	 */
	public void testCallsOrderForDescriptors() throws ServiceException {
		// Register services as STARTUP !
		// Lazy - generate no trace
		servicesRegistry.add( new LazyServiceBDescriptor() );
		servicesRegistry.add( new ServiceCDescriptor());
		

		ServiceA.resetTrace();
		servicesRegistry.startRegistry();

		// Check orders
		// 
		int i=0;
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
		
		// Now, start lazy service
		ServiceB service = (ServiceB)servicesRegistry.getService(ServiceB.class.getName());
		assertNotNull("service found", service);
		assertEquals("service", TraceKind.init, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.start, ServiceA.getEvent(i++));
			
		
		// Check dispose
		servicesRegistry.disposeRegistry();
		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
//		assertEquals("service", TraceKind.dispose, ServiceA.getEvent(i++));
		
		// 
	}
	
	/**
	 * Test the start order for services of type Start and Service
	 * @throws ServiceException
	 */
	public void testStartDependantOrder() throws ServiceException {
		
		String A = "A";
		String B = "B";
		String C = "C";
		String D = "D";
		String E = "E";
//		String F = "F";
		
		// Create services E --> D --> C --> B --> A
		FakeService.getTrace().reset();
		
		servicesRegistry.add( new ServiceDesc( E, ServiceStartKind.STARTUP, Arrays.asList(D) ));
		servicesRegistry.add( new ServiceDesc( D, ServiceStartKind.STARTUP, Arrays.asList(C) ));
		servicesRegistry.add( new ServiceDesc( C, ServiceStartKind.STARTUP, Arrays.asList(B) ));
		servicesRegistry.add( new ServiceDesc( B, ServiceStartKind.STARTUP, Arrays.asList(A) ));
		servicesRegistry.add( new ServiceDesc( A, ServiceStartKind.STARTUP ));
//		servicesRegistry.add( new ServiceDesc( F, ServiceStartKind.STARTUP, Arrays.asList(E) ));
		
		// Start services
		servicesRegistry.startRegistry();
		
		// check services
		assertTrue("service started", servicesRegistry.isStarted(A));
		assertTrue("service started", servicesRegistry.isStarted(B));
		assertNotSame("services are different", servicesRegistry.getService(A), servicesRegistry.getService(B));

		// Get names of created services
		String nameA = ((FakeService)servicesRegistry.getService(A)).getName();
		String nameB = ((FakeService)servicesRegistry.getService(B)).getName();
		String nameC = ((FakeService)servicesRegistry.getService(C)).getName();
		String nameD = ((FakeService)servicesRegistry.getService(D)).getName();
		String nameE = ((FakeService)servicesRegistry.getService(E)).getName();
//		String nameF = ((FakeService)servicesRegistry.getService(F)).getName();
		
		
		// check order	(E and F order are not guaranteed)	
		// order should be A, B, C, D, E
		TestTrace trace = FakeService.getTrace();
		int i = 0;
//		assertEquals("order", nameF + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameD + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameE + ",create", trace.getNameTrace(i++));

//		assertEquals("order", nameF + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameD + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameE + ",init", trace.getNameTrace(i++));

//		assertEquals("order", nameF + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameD + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameE + ",start", trace.getNameTrace(i++));

	}

	/**
	 * Start regular services, then add a new service and try to start it. 
	 * This should work.
	 * @throws ServiceException
	 */
	public void testStartRegistryAndThenAddNewServiceAndStartIt() throws ServiceException {
		
		// Register some services
		String A = "A";
		String B = "B";
		String C = "C";
		servicesRegistry.add( new ServiceFactoryDesc( C, ServiceStartKind.STARTUP ));
		servicesRegistry.add( new ServiceFactoryDesc( B, ServiceStartKind.STARTUP, Arrays.asList(C) ));
		servicesRegistry.add( new ServiceFactoryDesc( A, ServiceStartKind.STARTUP, Arrays.asList(B) ));

		// Start them
		servicesRegistry.startRegistry();
		
		// Register another services as pojo
		IService instanciatedService = new ServiceA();
		String key = instanciatedService.getClass().getName();
		servicesRegistry.add(key, 1, instanciatedService);

		// Try to start it
		servicesRegistry.startServices(key);
		
		// check services
		assertTrue("service started", servicesRegistry.isStarted(A));
		assertTrue("service started", servicesRegistry.isStarted(B));
		assertTrue("service started", servicesRegistry.isStarted(C));
		
		assertTrue("service started", servicesRegistry.isStarted(key));
		
		assertEquals("get registered service", instanciatedService, servicesRegistry.getService(key));

	}

	/**
	 * Test Service Factory creation
	 * @throws ServiceException
	 */
	public void testServiceFactoryCreation() throws ServiceException {
		
		String A = "A";
		String B = "B";
		String C = "C";
		
		// Create services
		FakeService.getTrace().reset();
		
		servicesRegistry.add( new ServiceFactoryDesc( C, ServiceStartKind.STARTUP ));
		servicesRegistry.add( new ServiceFactoryDesc( B, ServiceStartKind.STARTUP, Arrays.asList(C) ));
		servicesRegistry.add( new ServiceFactoryDesc( A, ServiceStartKind.STARTUP, Arrays.asList(B) ));
		
		// Start services
		servicesRegistry.startRegistry();
		
		// check services
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass());
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass());
		
		// Get names of created services
		String nameA = ((FakeCreatedService)servicesRegistry.getService(A)).getName();
		String nameB = ((FakeCreatedService)servicesRegistry.getService(B)).getName();
		String nameC = ((FakeCreatedService)servicesRegistry.getService(C)).getName();
		
		
		// check order	
		// Order should be: C, B, A
		TestTrace trace = FakeService.getTrace();
		int i = 0;
		assertEquals("order", nameC + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",create", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",init", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",start", trace.getNameTrace(i++));

		// next order is the order of getService()
		assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++));
	}

	/**
	 * Test Creation of Service Factory used with class as key.
	 * @throws ServiceException
	 */
	public void testServiceFactoryWithClassKey() throws ServiceException {
		
		//
		Class<?> A = ServiceA.class;
		Class<?> B = ServiceB.class;
		Class<?> C = ServiceC.class;
		
		// Specify service types as String
		String serviceClassname1 = FakeServiceFactory.class.getName();;
		String serviceClassname2 = FakeServiceFactory.class.getName();
		String serviceClassname3 = FakeServiceFactory.class.getName();
		
		// Create services
		FakeService.getTrace().reset();
		
		servicesRegistry.add( new ServiceFactoryDesc( C, serviceClassname3, ServiceStartKind.STARTUP ));
		servicesRegistry.add( new ServiceFactoryDesc( B, serviceClassname2, ServiceStartKind.STARTUP , Arrays.asList(C.getName()) ));
		servicesRegistry.add( new ServiceFactoryDesc( A, serviceClassname1, ServiceStartKind.STARTUP , Arrays.asList(B.getName()) ));
		
		// Start services
		servicesRegistry.startRegistry();
		
		// check services
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass());
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass());
		
		// Get names of created services
		String nameA = ((FakeCreatedService)servicesRegistry.getService(A)).getName();
		String nameB = ((FakeCreatedService)servicesRegistry.getService(B)).getName();
		String nameC = ((FakeCreatedService)servicesRegistry.getService(C)).getName();
		
		
		// check order	(E and F order are not guaranteed)	
		// Order should be
		TestTrace trace = FakeService.getTrace();
		int i = 0;
		assertEquals("order", nameC + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",create", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",init", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",start", trace.getNameTrace(i++));

		// next order is the order of getService()
		assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++));
	}
	
	/**
	 * Test Service Factory creation order.
	 * One of the factory try to get the required service from its init() method.
	 * @throws ServiceException
	 */
	public void testServiceFactoryWithRequiredInInit() throws ServiceException {
		
		String A = "A";
		String B = "B";
		String C = "C";
		
		// Create services C --> A --> B
		FakeService.getTrace().reset();
		
		// FakeWithRequiredServiceFactory try to retrieve service "C"
		servicesRegistry.add( new ServiceFactoryDesc( B, FakeWithRequiredServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(C) ));
		servicesRegistry.add( new ServiceFactoryDesc( A, FakeWithRequiredServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1, Arrays.asList(B, C) ));
		servicesRegistry.add( new ServiceFactoryDesc( C, FakeServiceFactory.class.getName(), ServiceStartKind.STARTUP, 1 ));
		
		// Start services
		servicesRegistry.startRegistry();
		
		// check services
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(A).getClass());
		assertSame("service type", FakeCreatedService.class, servicesRegistry.getService(B).getClass());
		
		// Get names of created services
		String nameA = ((FakeCreatedService)servicesRegistry.getService(A)).getName();
		String nameB = ((FakeCreatedService)servicesRegistry.getService(B)).getName();
		String nameC = ((FakeCreatedService)servicesRegistry.getService(C)).getName();
		
		
		// check order	
		// Order should be: C, B, A
		TestTrace trace = FakeService.getTrace();
		int i = 0;
		assertEquals("order", nameC + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",create", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",init", trace.getNameTrace(i++));
		
		assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++));
		assertEquals("value", servicesRegistry.getService(C), trace.getValue(i));
		assertEquals("order", nameB + ",initEnd", trace.getNameTrace(i++));
		
		assertEquals("order", nameA + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",initEnd", trace.getNameTrace(i++));

		assertEquals("order", nameC + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameA + ",start", trace.getNameTrace(i++));

		// next order is the order of getService()
		assertEquals("order", nameA + ",createInstance", trace.getNameTrace(i++));
//		assertEquals("order", nameB + ",createInstance", trace.getNameTrace(i++));
//		assertEquals("order", nameC + ",createInstance", trace.getNameTrace(i++));
	}

	/**
	 * Test  {@link ServicesRegistry#startServicesByClassKeys(List)}. 
	 * Check that services are started.
	 * 
	 * @throws ServiceException
	 */
	public void testStartService() throws ServiceException {
		
		String A = "A";
		String B = "B";
		String C = "C";
		String D = "D";
		String E = "E";
		
		// Create services E --> D --> C --> B --> A
		FakeService.getTrace().reset();
		
		servicesRegistry.add( new ServiceDesc( E, ServiceStartKind.STARTUP, Arrays.asList(D) ));
		servicesRegistry.add( new ServiceDesc( D, ServiceStartKind.STARTUP, Arrays.asList(C) ));
		servicesRegistry.add( new ServiceDesc( C, ServiceStartKind.STARTUP, Arrays.asList(A, B) ));
		servicesRegistry.add( new ServiceDesc( B, ServiceStartKind.STARTUP, Arrays.asList(A) ));
		servicesRegistry.add( new ServiceDesc( A, ServiceStartKind.STARTUP ));
		
		// Start services
		servicesRegistry.startServices(Arrays.asList(C));
		
		// check services
		assertTrue("service started", servicesRegistry.isStarted(A));
		assertTrue("service started", servicesRegistry.isStarted(B));
		assertTrue("service started", servicesRegistry.isStarted(C));

		// Get names of created services
		String nameA = ((FakeService)servicesRegistry.getService(A)).getName();
		String nameB = ((FakeService)servicesRegistry.getService(B)).getName();
		String nameC = ((FakeService)servicesRegistry.getService(C)).getName();
//		String nameD = ((FakeService)servicesRegistry.getService(D)).getName();
//		String nameE = ((FakeService)servicesRegistry.getService(E)).getName();
//		String nameF = ((FakeService)servicesRegistry.getService(F)).getName();
		
		
		// check order	(E and F order are not guaranteed)	
		// order should be A, B, C
		TestTrace trace = FakeService.getTrace();
		int i = 0;
		assertEquals("order", nameA + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",create", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",create", trace.getNameTrace(i++));
//		assertEquals("order", nameD + ",create", trace.getNameTrace(i++));
//		assertEquals("order", nameE + ",create", trace.getNameTrace(i++));

		assertEquals("order", nameA + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",init", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",init", trace.getNameTrace(i++));
//		assertEquals("order", nameD + ",init", trace.getNameTrace(i++));
//		assertEquals("order", nameE + ",init", trace.getNameTrace(i++));

		assertEquals("order", nameA + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameB + ",start", trace.getNameTrace(i++));
		assertEquals("order", nameC + ",start", trace.getNameTrace(i++));
//		assertEquals("order", nameD + ",start", trace.getNameTrace(i++));
//		assertEquals("order", nameE + ",start", trace.getNameTrace(i++));

	}

	/* **************************************** */





	/**
	 * General purpose descriptor.
	 */
	public class ServiceDesc extends ServiceDescriptor {


		public ServiceDesc( String key, ServiceStartKind startKind) {
			super(key, FakeService.class.getName(), startKind, 1 );
		}
		
		public ServiceDesc( String key, ServiceStartKind startKind, List<String> requiredService) {
			super(key, FakeService.class.getName(), startKind, 1, requiredService );
		}
	}

	/**
	 * General purpose descriptor.
	 */
	public class ServiceFactoryDesc extends ServiceDescriptor {


		public ServiceFactoryDesc( String key, ServiceStartKind startKind) {
			super(key, FakeServiceFactory.class.getName(), startKind, 1 );
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}
		
		public ServiceFactoryDesc( String key, ServiceStartKind startKind, List<String> requiredService) {
			super(key, FakeServiceFactory.class.getName(), startKind, 1, requiredService );
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}
		/**
		 * Constructor with a classname and {@link ServiceDescriptor#isStartAtStartup()} = true.
		 * Constructor.
		 *
		 * @param key
		 * @param startKind
		 * @param requiredService
		 */
		public ServiceFactoryDesc( Class<?> key, String classname, ServiceStartKind startKind, List<String> requiredService) {
			super(key, classname, startKind, 1, requiredService );
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}
		/**
		 * Constructor with a classname and {@link ServiceDescriptor#isStartAtStartup()} = true.
		 * Constructor.
		 *
		 * @param key
		 * @param startKind
		 * @param requiredService
		 */
		public ServiceFactoryDesc( Class<?>  key, String classname, ServiceStartKind startKind) {
			super(key, classname, startKind, 1 );
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}

		public ServiceFactoryDesc(String key, String serviceClassname, ServiceStartKind serviceStartKind, int priority, List<String> requiredServices) {
			super(key, serviceClassname, serviceStartKind, priority, requiredServices);
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}

		public ServiceFactoryDesc(String key, String serviceClassname, ServiceStartKind serviceStartKind, int priority) {
			super(key, serviceClassname, serviceStartKind, priority);
			setServiceTypeKind(ServiceTypeKind.serviceFactory);
		}
	}

	/**
	 * 
	 * @author dumoulin
	 *
	 */
	public class LazyServiceADescriptor extends ServiceDescriptor {


		public LazyServiceADescriptor() {
			super(ServiceA.class.getName(), ServiceStartKind.LAZY, 1);
		}
	}

	public class LazyServiceA10Descriptor extends ServiceDescriptor {


		public LazyServiceA10Descriptor() {
			super(ServiceA.class.getName(), ServiceA10.class.getName(), ServiceStartKind.LAZY, 10);
		}
	}

	public class LazyServiceBDescriptor extends ServiceDescriptor {


		public LazyServiceBDescriptor() {
			super(ServiceB.class.getName(), ServiceStartKind.LAZY, 1);
		}
	}

	public class ServiceCDescriptor extends ServiceDescriptor {


		public ServiceCDescriptor() {
			super(ServiceC.class.getName(), ServiceStartKind.STARTUP, 1);
		}
	}

	public class LazyServicePojoADescriptor extends ServiceDescriptor {


		public LazyServicePojoADescriptor() {
			super(ServicePojoA.class.getName(), ServiceStartKind.LAZY, 1);
		}
	}

	public class PojoServiceDescriptor extends ServiceDescriptor {


		public PojoServiceDescriptor() {
			super(Object.class.getName(), ServiceStartKind.STARTUP, 1);
		}
	}


}

Back to the top