Package: LookupEngineTest$TestServicesProvider1

LookupEngineTest$TestServicesProvider1

nameinstructionbranchcomplexitylinemethod
LookupEngineTest.TestServicesProvider1(CrossReferenceProvider)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getService(Method)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
notAService(EClassifier)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
service1(EClassifier)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.tests.runtime.lookup.basic;
12:
13: import java.lang.reflect.Constructor;
14: import java.lang.reflect.InvocationTargetException;
15: import java.lang.reflect.Method;
16: import java.util.Arrays;
17: import java.util.Collection;
18: import java.util.Iterator;
19: import java.util.LinkedHashMap;
20: import java.util.LinkedHashSet;
21: import java.util.List;
22: import java.util.Map;
23: import java.util.Map.Entry;
24: import java.util.Set;
25:
26: import org.eclipse.acceleo.query.runtime.CrossReferenceProvider;
27: import org.eclipse.acceleo.query.runtime.ILookupEngine;
28: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
29: import org.eclipse.acceleo.query.runtime.IService;
30: import org.eclipse.acceleo.query.runtime.Query;
31: import org.eclipse.acceleo.query.runtime.ServiceRegistrationResult;
32: import org.eclipse.acceleo.query.runtime.ServiceUtils;
33: import org.eclipse.acceleo.query.runtime.impl.AbstractServiceProvider;
34: import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
35: import org.eclipse.acceleo.query.runtime.lookup.basic.BasicLookupEngine;
36: import org.eclipse.acceleo.query.runtime.lookup.basic.CacheLookupEngine;
37: import org.eclipse.acceleo.query.runtime.lookup.basic.ServiceStore;
38: import org.eclipse.acceleo.query.services.AnyServices;
39: import org.eclipse.acceleo.query.services.BooleanServices;
40: import org.eclipse.acceleo.query.services.CollectionServices;
41: import org.eclipse.acceleo.query.services.ComparableServices;
42: import org.eclipse.acceleo.query.services.EObjectServices;
43: import org.eclipse.acceleo.query.services.NumberServices;
44: import org.eclipse.acceleo.query.services.ResourceServices;
45: import org.eclipse.acceleo.query.services.StringServices;
46: import org.eclipse.acceleo.query.services.XPathServices;
47: import org.eclipse.acceleo.query.validation.type.ClassType;
48: import org.eclipse.acceleo.query.validation.type.EClassifierType;
49: import org.eclipse.acceleo.query.validation.type.IType;
50: import org.eclipse.emf.ecore.EClass;
51: import org.eclipse.emf.ecore.EClassifier;
52: import org.eclipse.emf.ecore.EObject;
53: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
54: import org.eclipse.emf.ecore.EcorePackage;
55: import org.junit.Test;
56: import org.junit.runner.RunWith;
57: import org.junit.runners.Parameterized;
58: import org.junit.runners.Parameterized.Parameters;
59:
60: import static org.junit.Assert.assertEquals;
61: import static org.junit.Assert.assertFalse;
62: import static org.junit.Assert.assertNotNull;
63: import static org.junit.Assert.assertTrue;
64:
65: @RunWith(Parameterized.class)
66: public class LookupEngineTest {
67:
68:         public static final class TestCrossReferenceProvider implements CrossReferenceProvider {
69:
70:                 @Override
71:                 public Collection<Setting> getInverseReferences(EObject self) {
72:                         return null;
73:                 }
74:
75:         }
76:
77:         private interface ITestLookupEngine extends ILookupEngine {
78:
79:                 ServiceRegistrationResult registerService(IService service);
80:
81:                 IService removeService(IService service);
82:
83:                 ServiceStore getServices();
84:
85:                 IReadOnlyQueryEnvironment getQueryEnvironment();
86:
87:         }
88:
89:         private static class TestBasicLookupEngine extends BasicLookupEngine implements ITestLookupEngine {
90:
91:                 public TestBasicLookupEngine(CrossReferenceProvider crossReferencer) {
92:                         super(Query.newEnvironmentWithDefaultServices(crossReferencer));
93:                 }
94:
95:                 @Override
96:                 public ServiceStore getServices() {
97:                         return super.getServices();
98:                 }
99:
100:                 @Override
101:                 public IReadOnlyQueryEnvironment getQueryEnvironment() {
102:                         return queryEnvironment;
103:                 }
104:
105:         }
106:
107:         private static class TestCacheLookupEngine extends CacheLookupEngine implements ITestLookupEngine {
108:
109:                 public TestCacheLookupEngine(CrossReferenceProvider crossReferencer) {
110:                         super(Query.newEnvironmentWithDefaultServices(crossReferencer));
111:                 }
112:
113:                 @Override
114:                 public ServiceStore getServices() {
115:                         return super.getServices();
116:                 }
117:
118:                 @Override
119:                 public IReadOnlyQueryEnvironment getQueryEnvironment() {
120:                         return queryEnvironment;
121:                 }
122:
123:         }
124:
125:         /**
126:          * TestService {@link Class}.
127:          *
128:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
129:          */
130:         public static class TestServices1 {
131:
132:                 public final CrossReferenceProvider crossReferencer;
133:
134:                 public TestServices1(CrossReferenceProvider crossReferencer) {
135:                         this.crossReferencer = crossReferencer;
136:                 }
137:
138:                 public Boolean service1(EClassifier eObj) {
139:                         return Boolean.FALSE;
140:                 }
141:
142:                 @SuppressWarnings("unused")
143:                 private Boolean notAService(EClassifier eObj) {
144:                         return Boolean.FALSE;
145:                 }
146:
147:         }
148:
149:         public static final class TestStaticServices {
150:
151:                 private TestStaticServices() {
152:                         // nothing to do here;
153:                 }
154:
155:                 public static Boolean service1(EClassifier eObj) {
156:                         return Boolean.FALSE;
157:                 }
158:
159:                 @SuppressWarnings("unused")
160:                 private Boolean notAService(EClassifier eObj) {
161:                         return Boolean.FALSE;
162:                 }
163:
164:         }
165:
166:         /**
167:          * TestService {@link Class}.
168:          *
169:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
170:          */
171:         public static class TestDuplicateServices1 {
172:
173:                 public Boolean service1(EClassifier eObj) {
174:                         return Boolean.FALSE;
175:                 }
176:
177:         }
178:
179:         /**
180:          * TestService {@link Class}.
181:          *
182:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
183:          */
184:         public static class TestMaskServices1 {
185:
186:                 public Boolean service1(EClass eCls) {
187:                         return Boolean.FALSE;
188:                 }
189:
190:         }
191:
192:         /**
193:          * TestService {@link Class}.
194:          *
195:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
196:          */
197:         public static class ExtendedTestServices1 extends TestServices1 {
198:
199:                 public ExtendedTestServices1(CrossReferenceProvider crossReferencer) {
200:                         super(crossReferencer);
201:                 }
202:
203:                 public Boolean service2(EClassifier eObj) {
204:                         return Boolean.FALSE;
205:                 }
206:
207:         }
208:
209:         /**
210:          * TestService {@link Class}.
211:          *
212:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
213:          */
214:         public static class TestServicesProvider1 extends AbstractServiceProvider {
215:
216:                 private IService service1;
217:
218:                 public final CrossReferenceProvider crossReferencer;
219:
220:                 public TestServicesProvider1(CrossReferenceProvider crossReferencer) {
221:                         this.crossReferencer = crossReferencer;
222:                 }
223:
224:                 public Boolean service1(EClassifier eObj) {
225:                         return Boolean.FALSE;
226:                 }
227:
228:                 @SuppressWarnings("unused")
229:                 private Boolean notAService(EClassifier eObj) {
230:                         return Boolean.FALSE;
231:                 }
232:
233:                 @Override
234:                 protected IService getService(Method method) {
235:                         IService result = new JavaMethodService(method, this);
236:
237:                         service1 = result;
238:
239:                         return result;
240:                 }
241:
242:         }
243:
244:         /**
245:          * TestService {@link Class}.
246:          *
247:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
248:          */
249:         public static class TestDuplicateServicesProvider1 extends AbstractServiceProvider {
250:
251:                 private IService service1;
252:
253:                 public Boolean service1(EClassifier eObj) {
254:                         return Boolean.FALSE;
255:                 }
256:
257:                 @Override
258:                 protected IService getService(Method method) {
259:                         IService result = new JavaMethodService(method, this);
260:
261:                         service1 = result;
262:
263:                         return result;
264:                 }
265:
266:         }
267:
268:         /**
269:          * TestService {@link Class}.
270:          *
271:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
272:          */
273:         public static class TestMaskServicesProvider1 extends AbstractServiceProvider {
274:
275:                 private IService service1;
276:
277:                 public Boolean service1(EClass eCls) {
278:                         return Boolean.FALSE;
279:                 }
280:
281:                 @Override
282:                 protected IService getService(Method method) {
283:                         IService result = new JavaMethodService(method, this);
284:
285:                         service1 = result;
286:
287:                         return result;
288:                 }
289:
290:         }
291:
292:         /**
293:          * TestService {@link Class}.
294:          *
295:          * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
296:          */
297:         public static class ExtendedTestServicesProvider1 extends TestServicesProvider1 {
298:
299:                 public ExtendedTestServicesProvider1(CrossReferenceProvider crossReferencer) {
300:                         super(crossReferencer);
301:                 }
302:
303:                 private IService service2;
304:
305:                 public Boolean service2(EClassifier eObj) {
306:                         return Boolean.FALSE;
307:                 }
308:
309:                 @Override
310:                 protected IService getService(Method method) {
311:                         final IService result;
312:
313:                         if ("service2".equals(method.getName())) {
314:                                 result = new JavaMethodService(method, this);
315:                                 service2 = result;
316:                         } else {
317:                                 result = super.getService(method);
318:                         }
319:
320:                         return result;
321:                 }
322:
323:         }
324:
325:         private final Class<ITestLookupEngine> cls;
326:
327:         public LookupEngineTest(Class<ITestLookupEngine> cls) {
328:                 this.cls = cls;
329:         }
330:
331:         @Parameters(name = "{0}")
332:         public static Collection<Object[]> classes() {
333:                 return Arrays.asList(new Object[][] { {TestBasicLookupEngine.class, },
334:                                 {TestCacheLookupEngine.class, }, });
335:         }
336:
337:         ITestLookupEngine instanciate(CrossReferenceProvider provider) {
338:                 try {
339:                         final Constructor<ITestLookupEngine> constructor = cls
340:                                         .getConstructor(CrossReferenceProvider.class);
341:                         return constructor.newInstance(provider);
342:                 } catch (NoSuchMethodException e) {
343:                         e.printStackTrace();
344:                 } catch (SecurityException e) {
345:                         e.printStackTrace();
346:                 } catch (InstantiationException e) {
347:                         e.printStackTrace();
348:                 } catch (IllegalAccessException e) {
349:                         e.printStackTrace();
350:                 } catch (IllegalArgumentException e) {
351:                         e.printStackTrace();
352:                 } catch (InvocationTargetException e) {
353:                         e.printStackTrace();
354:                 }
355:
356:                 return null;
357:         }
358:
359:         @Test
360:         public void simpleRegistration() throws NoSuchMethodException, SecurityException {
361:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
362:                 final ITestLookupEngine engine = instanciate(provider);
363:
364:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
365:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
366:                                 provider))) {
367:                         result.merge(engine.registerService(service));
368:                 }
369:                 assertEquals(0, result.getDuplicated().size());
370:                 assertEquals(0, result.getMasked().size());
371:                 assertEquals(0, result.getIsMaskedBy().size());
372:
373:                 assertEquals(1, engine.getRegisteredServices().size());
374:
375:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
376:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
377:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), service.getMethod());
378:
379:                 assertEquals(1, result.getRegistered().size());
380:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
381:                                 .getRegistered().get(0)).getMethod());
382:         }
383:
384:         @Test
385:         public void staticRegistration() throws NoSuchMethodException, SecurityException {
386:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
387:                 final ITestLookupEngine engine = instanciate(provider);
388:
389:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
390:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
391:                                 TestStaticServices.class)) {
392:                         result.merge(engine.registerService(service));
393:                 }
394:                 assertEquals(0, result.getDuplicated().size());
395:                 assertEquals(0, result.getMasked().size());
396:                 assertEquals(0, result.getIsMaskedBy().size());
397:
398:                 assertEquals(1, engine.getRegisteredServices().size());
399:
400:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
401:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
402:                 assertEquals(TestStaticServices.class.getMethod("service1", EClassifier.class), service.getMethod());
403:
404:                 assertEquals(1, result.getRegistered().size());
405:                 assertEquals(TestStaticServices.class.getMethod("service1", EClassifier.class),
406:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
407:         }
408:
409:         @Test
410:         public void extendedRegistration() throws NoSuchMethodException, SecurityException {
411:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
412:                 final ITestLookupEngine engine = instanciate(provider);
413:
414:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
415:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
416:                                 new ExtendedTestServices1(provider))) {
417:                         result.merge(engine.registerService(service));
418:                 }
419:                 assertEquals(0, result.getDuplicated().size());
420:                 assertEquals(0, result.getMasked().size());
421:                 assertEquals(0, result.getIsMaskedBy().size());
422:
423:                 assertEquals(2, engine.getRegisteredServices().size());
424:
425:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
426:                                 engine.getQueryEnvironment(), EClassifier.class) });
427:                 assertEquals(ExtendedTestServices1.class.getMethod("service1", EClassifier.class), service
428:                                 .getMethod());
429:                 service = (JavaMethodService)engine.lookup("service2", new IType[] {new ClassType(engine
430:                                 .getQueryEnvironment(), EClassifier.class) });
431:                 assertEquals(ExtendedTestServices1.class.getMethod("service2", EClassifier.class), service
432:                                 .getMethod());
433:
434:                 assertEquals(2, result.getRegistered().size());
435:                 assertEquals(ExtendedTestServices1.class.getMethod("service2", EClassifier.class),
436:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
437:                 assertEquals(ExtendedTestServices1.class.getMethod("service1", EClassifier.class),
438:                                 ((JavaMethodService)result.getRegistered().get(1)).getMethod());
439:         }
440:
441:         @Test
442:         public void doubleRegistration() throws NoSuchMethodException, SecurityException {
443:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
444:                 final ITestLookupEngine engine = instanciate(provider);
445:
446:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
447:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
448:                                 provider))) {
449:                         result.merge(engine.registerService(service));
450:                 }
451:                 assertEquals(1, result.getRegistered().size());
452:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
453:                                 .getRegistered().get(0)).getMethod());
454:
455:                 result = new ServiceRegistrationResult();
456:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
457:                                 provider))) {
458:                         result.merge(engine.registerService(service));
459:                 }
460:                 assertEquals(1, result.getDuplicated().size());
461:                 assertEquals(0, result.getMasked().size());
462:                 assertEquals(0, result.getIsMaskedBy().size());
463:
464:                 assertEquals(1, engine.getRegisteredServices().size());
465:
466:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
467:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
468:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), service.getMethod());
469:
470:                 assertEquals(0, result.getRegistered().size());
471:         }
472:
473:         @Test
474:         public void duplicateRegistration() throws NoSuchMethodException, SecurityException {
475:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
476:                 final ITestLookupEngine engine = instanciate(provider);
477:
478:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
479:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
480:                                 provider))) {
481:                         result.merge(engine.registerService(service));
482:                 }
483:
484:                 assertEquals(1, result.getRegistered().size());
485:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
486:                                 .getRegistered().get(0)).getMethod());
487:
488:                 result = new ServiceRegistrationResult();
489:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
490:                                 TestDuplicateServices1.class)) {
491:                         result.merge(engine.registerService(service));
492:                 }
493:                 assertEquals(1, result.getDuplicated().size());
494:                 final Method expectedDuplicatedMethod = TestServices1.class.getMethod("service1", EClassifier.class);
495:
496:                 final Entry<IService, List<IService>> entry = result.getDuplicated().entrySet().iterator().next();
497:                 assertEquals(TestDuplicateServices1.class.getMethod("service1", EClassifier.class),
498:                                 ((JavaMethodService)entry.getKey()).getMethod());
499:                 assertEquals(1, entry.getValue().size());
500:                 final Method actualDuplicatedMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
501:
502:                 assertEquals(expectedDuplicatedMethod, actualDuplicatedMethod);
503:                 assertEquals(0, result.getMasked().size());
504:                 assertEquals(0, result.getIsMaskedBy().size());
505:
506:                 assertEquals(2, engine.getRegisteredServices().size());
507:
508:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
509:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
510:                 assertEquals(TestDuplicateServices1.class.getMethod("service1", EClassifier.class), service
511:                                 .getMethod());
512:
513:                 assertEquals(1, result.getRegistered().size());
514:                 assertEquals(TestDuplicateServices1.class.getMethod("service1", EClassifier.class),
515:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
516:         }
517:
518:         @Test
519:         public void maskRegistration() throws NoSuchMethodException, SecurityException {
520:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
521:                 final ITestLookupEngine engine = instanciate(provider);
522:
523:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
524:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
525:                                 provider))) {
526:                         result.merge(engine.registerService(service));
527:                 }
528:
529:                 assertEquals(1, result.getRegistered().size());
530:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
531:                                 .getRegistered().get(0)).getMethod());
532:
533:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
534:                                 TestMaskServices1.class)) {
535:                         result.merge(engine.registerService(service));
536:                 }
537:                 assertEquals(0, result.getDuplicated().size());
538:                 assertEquals(1, result.getMasked().size());
539:                 final Method expectedMaskMethod = TestServices1.class.getMethod("service1", EClassifier.class);
540:                 assertEquals(1, result.getMasked().entrySet().size());
541:                 final Entry<IService, List<IService>> entry = result.getMasked().entrySet().iterator().next();
542:                 assertEquals(TestMaskServices1.class.getMethod("service1", EClass.class), ((JavaMethodService)entry
543:                                 .getKey()).getMethod());
544:                 assertEquals(1, entry.getValue().size());
545:                 final Method actualMaskMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
546:
547:                 assertEquals(expectedMaskMethod, actualMaskMethod);
548:                 assertEquals(0, result.getIsMaskedBy().size());
549:
550:                 assertEquals(2, engine.getRegisteredServices().size());
551:
552:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
553:                                 engine.getQueryEnvironment(), EClassifier.class) });
554:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), service.getMethod());
555:                 service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(engine
556:                                 .getQueryEnvironment(), EClass.class) });
557:                 assertEquals(TestMaskServices1.class.getMethod("service1", EClass.class), service.getMethod());
558:
559:                 assertEquals(2, result.getRegistered().size());
560:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
561:                                 .getRegistered().get(0)).getMethod());
562:                 assertEquals(TestMaskServices1.class.getMethod("service1", EClass.class), ((JavaMethodService)result
563:                                 .getRegistered().get(1)).getMethod());
564:         }
565:
566:         @Test
567:         public void maskedByRegistration() throws NoSuchMethodException, SecurityException {
568:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
569:                 final ITestLookupEngine engine = instanciate(provider);
570:
571:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
572:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
573:                                 TestMaskServices1.class)) {
574:                         result.merge(engine.registerService(service));
575:                 }
576:
577:                 assertEquals(1, result.getRegistered().size());
578:                 assertEquals(TestMaskServices1.class.getMethod("service1", EClass.class), ((JavaMethodService)result
579:                                 .getRegistered().get(0)).getMethod());
580:
581:                 result = new ServiceRegistrationResult();
582:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
583:                                 provider))) {
584:                         result.merge(engine.registerService(service));
585:                 }
586:                 assertEquals(0, result.getDuplicated().size());
587:                 assertEquals(0, result.getMasked().size());
588:                 assertEquals(1, result.getIsMaskedBy().size());
589:                 final Method expectedIsMaskedByMethod = TestMaskServices1.class.getMethod("service1", EClass.class);
590:
591:                 final Entry<IService, List<IService>> entry = result.getIsMaskedBy().entrySet().iterator().next();
592:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)entry
593:                                 .getKey()).getMethod());
594:                 assertEquals(1, entry.getValue().size());
595:                 final Method actualIsMaskedByMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
596:
597:                 assertEquals(expectedIsMaskedByMethod, actualIsMaskedByMethod);
598:
599:                 assertEquals(2, engine.getRegisteredServices().size());
600:
601:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
602:                                 engine.getQueryEnvironment(), EClassifier.class) });
603:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), service.getMethod());
604:                 service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(engine
605:                                 .getQueryEnvironment(), EClass.class) });
606:                 assertEquals(TestMaskServices1.class.getMethod("service1", EClass.class), service.getMethod());
607:
608:                 assertEquals(1, result.getRegistered().size());
609:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
610:                                 .getRegistered().get(0)).getMethod());
611:         }
612:
613:         @Test
614:         public void withCrossReferencerRegistration() throws NoSuchMethodException, SecurityException {
615:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
616:                 final ITestLookupEngine engine = instanciate(provider);
617:
618:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
619:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
620:                                 provider))) {
621:                         result.merge(engine.registerService(service));
622:                 }
623:                 assertEquals(0, result.getDuplicated().size());
624:                 assertEquals(0, result.getMasked().size());
625:                 assertEquals(0, result.getIsMaskedBy().size());
626:
627:                 assertEquals(1, engine.getRegisteredServices().size());
628:                 final Iterator<IService> iterator = engine.getRegisteredServices().iterator();
629:                 final JavaMethodService javaMethodService = (JavaMethodService)iterator.next();
630:                 assertTrue(javaMethodService.getInstance() instanceof TestServices1);
631:                 TestServices1 instance = (TestServices1)javaMethodService.getInstance();
632:                 assertEquals(provider, instance.crossReferencer);
633:
634:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
635:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
636:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), service.getMethod());
637:
638:                 assertEquals(1, result.getRegistered().size());
639:                 assertEquals(TestServices1.class.getMethod("service1", EClassifier.class), ((JavaMethodService)result
640:                                 .getRegistered().get(0)).getMethod());
641:         }
642:
643:         @Test
644:         public void simpleRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
645:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
646:                 final ITestLookupEngine engine = instanciate(provider);
647:
648:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
649:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
650:                                 new TestServicesProvider1(provider))) {
651:                         result.merge(engine.registerService(service));
652:                 }
653:                 assertEquals(0, result.getDuplicated().size());
654:                 assertEquals(0, result.getMasked().size());
655:                 assertEquals(0, result.getIsMaskedBy().size());
656:
657:                 assertEquals(1, engine.getRegisteredServices().size());
658:
659:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
660:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
661:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
662:
663:                 assertEquals(1, result.getRegistered().size());
664:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
665:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
666:         }
667:
668:         @Test
669:         public void extendedRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
670:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
671:                 final ITestLookupEngine engine = instanciate(provider);
672:
673:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
674:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
675:                                 new ExtendedTestServicesProvider1(provider))) {
676:                         result.merge(engine.registerService(service));
677:                 }
678:                 assertEquals(0, result.getDuplicated().size());
679:                 assertEquals(0, result.getMasked().size());
680:                 assertEquals(0, result.getIsMaskedBy().size());
681:
682:                 assertEquals(2, engine.getRegisteredServices().size());
683:
684:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
685:                                 engine.getQueryEnvironment(), EClassifier.class) });
686:                 assertEquals(ExtendedTestServicesProvider1.class.getMethod("service1", EClassifier.class), service
687:                                 .getMethod());
688:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
689:                 service = (JavaMethodService)engine.lookup("service2", new IType[] {new ClassType(engine
690:                                 .getQueryEnvironment(), EClassifier.class) });
691:                 assertEquals(ExtendedTestServicesProvider1.class.getMethod("service2", EClassifier.class), service
692:                                 .getMethod());
693:                 assertEquals(((ExtendedTestServicesProvider1)service.getInstance()).service2, service);
694:
695:                 assertEquals(2, result.getRegistered().size());
696:                 assertEquals(ExtendedTestServicesProvider1.class.getMethod("service2", EClassifier.class),
697:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
698:                 assertEquals(ExtendedTestServicesProvider1.class.getMethod("service1", EClassifier.class),
699:                                 ((JavaMethodService)result.getRegistered().get(1)).getMethod());
700:         }
701:
702:         @Test
703:         public void doubleRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
704:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
705:                 final ITestLookupEngine engine = instanciate(provider);
706:
707:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
708:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
709:                                 new TestServicesProvider1(provider))) {
710:                         result.merge(engine.registerService(service));
711:                 }
712:
713:                 assertEquals(1, result.getRegistered().size());
714:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
715:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
716:
717:                 result = new ServiceRegistrationResult();
718:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
719:                                 new TestServicesProvider1(provider))) {
720:                         result.merge(engine.registerService(service));
721:                 }
722:                 assertEquals(1, result.getDuplicated().size());
723:                 assertEquals(0, result.getMasked().size());
724:                 assertEquals(0, result.getIsMaskedBy().size());
725:
726:                 assertEquals(1, engine.getRegisteredServices().size());
727:
728:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
729:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
730:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class), service
731:                                 .getMethod());
732:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
733:
734:                 assertEquals(0, result.getRegistered().size());
735:         }
736:
737:         @Test
738:         public void duplicateRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
739:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
740:                 final ITestLookupEngine engine = instanciate(provider);
741:
742:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
743:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
744:                                 new TestServicesProvider1(provider))) {
745:                         result.merge(engine.registerService(service));
746:                 }
747:
748:                 assertEquals(1, result.getRegistered().size());
749:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
750:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
751:
752:                 result = new ServiceRegistrationResult();
753:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
754:                                 TestDuplicateServicesProvider1.class)) {
755:                         result.merge(engine.registerService(service));
756:                 }
757:                 assertEquals(1, result.getDuplicated().size());
758:                 final Method expectedDuplicatedMethod = TestServicesProvider1.class.getMethod("service1",
759:                                 EClassifier.class);
760:
761:                 final Entry<IService, List<IService>> entry = result.getDuplicated().entrySet().iterator().next();
762:                 assertEquals(TestDuplicateServicesProvider1.class.getMethod("service1", EClassifier.class),
763:                                 ((JavaMethodService)entry.getKey()).getMethod());
764:                 assertEquals(1, entry.getValue().size());
765:                 final Method actualDuplicatedMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
766:
767:                 assertEquals(expectedDuplicatedMethod, actualDuplicatedMethod);
768:                 assertEquals(0, result.getMasked().size());
769:                 assertEquals(0, result.getIsMaskedBy().size());
770:
771:                 assertEquals(2, engine.getRegisteredServices().size());
772:
773:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
774:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
775:                 assertEquals(TestDuplicateServicesProvider1.class.getMethod("service1", EClassifier.class), service
776:                                 .getMethod());
777:                 assertEquals(((TestDuplicateServicesProvider1)service.getInstance()).service1, service);
778:
779:                 assertEquals(1, result.getRegistered().size());
780:                 assertEquals(TestDuplicateServicesProvider1.class.getMethod("service1", EClassifier.class),
781:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
782:         }
783:
784:         @Test
785:         public void maskRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
786:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
787:                 final ITestLookupEngine engine = instanciate(provider);
788:
789:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
790:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
791:                                 new TestServicesProvider1(provider))) {
792:                         result.merge(engine.registerService(service));
793:                 }
794:
795:                 assertEquals(1, result.getRegistered().size());
796:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
797:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
798:
799:                 result = new ServiceRegistrationResult();
800:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
801:                                 TestMaskServicesProvider1.class)) {
802:                         result.merge(engine.registerService(service));
803:                 }
804:                 assertEquals(0, result.getDuplicated().size());
805:                 assertEquals(1, result.getMasked().size());
806:                 final Method expectedMaskMethod = TestServicesProvider1.class
807:                                 .getMethod("service1", EClassifier.class);
808:
809:                 final Entry<IService, List<IService>> entry = result.getMasked().entrySet().iterator().next();
810:                 assertEquals(TestMaskServicesProvider1.class.getMethod("service1", EClass.class),
811:                                 ((JavaMethodService)entry.getKey()).getMethod());
812:                 assertEquals(1, entry.getValue().size());
813:                 final Method actualMaskMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
814:
815:                 assertEquals(expectedMaskMethod, actualMaskMethod);
816:                 assertEquals(0, result.getIsMaskedBy().size());
817:
818:                 assertEquals(2, engine.getRegisteredServices().size());
819:
820:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
821:                                 engine.getQueryEnvironment(), EClassifier.class) });
822:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class), service
823:                                 .getMethod());
824:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
825:                 service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(engine
826:                                 .getQueryEnvironment(), EClass.class) });
827:                 assertEquals(TestMaskServicesProvider1.class.getMethod("service1", EClass.class), service.getMethod());
828:                 assertEquals(((TestMaskServicesProvider1)service.getInstance()).service1, service);
829:
830:                 assertEquals(1, result.getRegistered().size());
831:                 assertEquals(TestMaskServicesProvider1.class.getMethod("service1", EClass.class),
832:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
833:         }
834:
835:         @Test
836:         public void maskedByRegistrationServiceProvider() throws NoSuchMethodException, SecurityException {
837:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
838:                 final ITestLookupEngine engine = instanciate(provider);
839:
840:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
841:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
842:                                 TestMaskServicesProvider1.class)) {
843:                         result.merge(engine.registerService(service));
844:                 }
845:
846:                 assertEquals(1, result.getRegistered().size());
847:                 assertEquals(TestMaskServicesProvider1.class.getMethod("service1", EClass.class),
848:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
849:
850:                 result = new ServiceRegistrationResult();
851:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
852:                                 new TestServicesProvider1(provider))) {
853:                         result.merge(engine.registerService(service));
854:                 }
855:                 assertEquals(0, result.getDuplicated().size());
856:                 assertEquals(0, result.getMasked().size());
857:                 assertEquals(1, result.getIsMaskedBy().size());
858:                 final Method expectedIsMaskedByMethod = TestMaskServicesProvider1.class.getMethod("service1",
859:                                 EClass.class);
860:
861:                 final Entry<IService, List<IService>> entry = result.getIsMaskedBy().entrySet().iterator().next();
862:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
863:                                 ((JavaMethodService)entry.getKey()).getMethod());
864:                 assertEquals(1, entry.getValue().size());
865:                 final Method actualIsMaskedByMethod = ((JavaMethodService)entry.getValue().get(0)).getMethod();
866:
867:                 assertEquals(expectedIsMaskedByMethod, actualIsMaskedByMethod);
868:
869:                 assertEquals(2, engine.getRegisteredServices().size());
870:
871:                 JavaMethodService service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(
872:                                 engine.getQueryEnvironment(), EClassifier.class) });
873:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class), service
874:                                 .getMethod());
875:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
876:                 service = (JavaMethodService)engine.lookup("service1", new IType[] {new ClassType(engine
877:                                 .getQueryEnvironment(), EClass.class) });
878:                 assertEquals(TestMaskServicesProvider1.class.getMethod("service1", EClass.class), service.getMethod());
879:                 assertEquals(((TestMaskServicesProvider1)service.getInstance()).service1, service);
880:
881:                 assertEquals(1, result.getRegistered().size());
882:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
883:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
884:         }
885:
886:         @Test
887:         public void withCrossReferencerRegistrationServiceProvider() throws NoSuchMethodException,
888:                         SecurityException {
889:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
890:                 final ITestLookupEngine engine = instanciate(provider);
891:
892:                 final ServiceRegistrationResult result = new ServiceRegistrationResult();
893:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
894:                                 new TestServicesProvider1(provider))) {
895:                         result.merge(engine.registerService(service));
896:                 }
897:                 assertEquals(0, result.getDuplicated().size());
898:                 assertEquals(0, result.getMasked().size());
899:                 assertEquals(0, result.getIsMaskedBy().size());
900:
901:                 assertEquals(1, engine.getRegisteredServices().size());
902:                 final Iterator<IService> iterator = engine.getRegisteredServices().iterator();
903:                 final JavaMethodService javaMethodService = (JavaMethodService)iterator.next();
904:                 assertTrue(javaMethodService.getInstance() instanceof TestServicesProvider1);
905:                 TestServicesProvider1 instance = (TestServicesProvider1)javaMethodService.getInstance();
906:                 assertEquals(provider, instance.crossReferencer);
907:
908:                 final JavaMethodService service = (JavaMethodService)engine.lookup("service1",
909:                                 new IType[] {new ClassType(engine.getQueryEnvironment(), EClassifier.class) });
910:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class), service
911:                                 .getMethod());
912:                 assertEquals(((TestServicesProvider1)service.getInstance()).service1, service);
913:
914:                 assertEquals(1, result.getRegistered().size());
915:                 assertEquals(TestServicesProvider1.class.getMethod("service1", EClassifier.class),
916:                                 ((JavaMethodService)result.getRegistered().get(0)).getMethod());
917:         }
918:
919:         @Test
920:         public void isServiceMethodFromObject() throws NoSuchMethodException, SecurityException {
921:                 final Method method = Object.class.getMethod("equals", Object.class);
922:                 assertNotNull(method);
923:                 assertFalse(ServiceUtils.isServiceMethod(this, method));
924:         }
925:
926:         @Test
927:         public void isServiceMethodNoParameter() throws NoSuchMethodException, SecurityException {
928:                 final Method method = MethodHolder.class.getMethod("testNotServiceMethodNoParameter");
929:                 assertNotNull(method);
930:                 assertFalse(ServiceUtils.isServiceMethod(this, method));
931:         }
932:
933:         @Test
934:         public void isServiceMethodNotStaticNoInstance() throws NoSuchMethodException, SecurityException {
935:                 final Method method = MethodHolder.class.getMethod("testServiceMethod", Object.class);
936:                 assertNotNull(method);
937:                 assertFalse(ServiceUtils.isServiceMethod(null, method));
938:         }
939:
940:         @Test
941:         public void isServiceMethodNotStaticInstance() throws NoSuchMethodException, SecurityException {
942:                 final Method method = MethodHolder.class.getMethod("testServiceMethod", Object.class);
943:                 assertNotNull(method);
944:                 assertTrue(ServiceUtils.isServiceMethod(this, method));
945:         }
946:
947:         @Test
948:         public void isServiceMethodStaticNoInstance() throws NoSuchMethodException, SecurityException {
949:                 final Method method = MethodHolder.class.getMethod("testStaticServiceMethod", Object.class);
950:                 assertNotNull(method);
951:                 assertTrue(ServiceUtils.isServiceMethod(null, method));
952:         }
953:
954:         @Test
955:         public void isServiceMethodStaticInstance() throws NoSuchMethodException, SecurityException {
956:                 final Method method = MethodHolder.class.getMethod("testStaticServiceMethod", Object.class);
957:                 assertNotNull(method);
958:                 assertTrue(ServiceUtils.isServiceMethod(this, method));
959:         }
960:
961:         @Test
962:         public void isRegisteredService() {
963:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
964:                 final ITestLookupEngine engine = instanciate(provider);
965:
966:                 final Set<IService> services = ServiceUtils.getServices(engine.getQueryEnvironment(),
967:                                 TestServices1.class);
968:                 for (IService service : services) {
969:                         engine.removeService(service);
970:                 }
971:
972:                 for (IService service : services) {
973:                         assertFalse(engine.isRegisteredService(service));
974:                 }
975:                 ServiceRegistrationResult result = new ServiceRegistrationResult();
976:                 for (IService service : services) {
977:                         result.merge(engine.registerService(service));
978:                 }
979:                 for (IService service : services) {
980:                         assertTrue(engine.isRegisteredService(service));
981:                 }
982:         }
983:
984:         @Test
985:         public void removeServiceEmpty() {
986:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
987:                 final ITestLookupEngine engine = instanciate(provider);
988:
989:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(), new TestServices1(
990:                                 provider))) {
991:                         engine.removeService(service);
992:                 }
993:
994:                 assertEquals(0, engine.getRegisteredServices().size());
995:                 assertEquals(0, engine.getServices().size());
996:         }
997:
998:         @Test
999:         public void removeService() {
1000:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1001:                 final ITestLookupEngine engine = instanciate(provider);
1002:
1003:                 final Set<IService> services = ServiceUtils.getServices(engine.getQueryEnvironment(),
1004:                                 new ExtendedTestServices1(provider));
1005:                 for (IService service : services) {
1006:                         engine.registerService(service);
1007:                 }
1008:
1009:                 assertEquals(2, engine.getRegisteredServices().size());
1010:                 assertEquals(2, engine.getServices().size());
1011:
1012:                 for (IService service : services) {
1013:                         engine.removeService(service);
1014:                 }
1015:
1016:                 assertEquals(0, engine.getRegisteredServices().size());
1017:                 assertEquals(0, engine.getServices().size());
1018:         }
1019:
1020:         @Test
1021:         public void getServicesEmpty() {
1022:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1023:                 final ITestLookupEngine engine = instanciate(provider);
1024:                 final Set<IType> types = new LinkedHashSet<IType>();
1025:                 types.add(new ClassType(engine.getQueryEnvironment(), EClassifier.class));
1026:
1027:                 assertEquals(0, engine.getServices(types).size());
1028:         }
1029:
1030:         @Test
1031:         public void getServices() throws NoSuchMethodException, SecurityException {
1032:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1033:                 final ITestLookupEngine engine = instanciate(provider);
1034:                 final Set<IType> types = new LinkedHashSet<IType>();
1035:                 types.add(new ClassType(engine.getQueryEnvironment(), EClassifier.class));
1036:
1037:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1038:                                 new ExtendedTestServices1(provider))) {
1039:                         engine.registerService(service);
1040:                 }
1041:                 final Set<IService> services = engine.getServices(types);
1042:                 assertEquals(2, services.size());
1043:
1044:                 final Set<Method> methods = new LinkedHashSet<Method>();
1045:                 for (IService service : services) {
1046:                         methods.add(((JavaMethodService)service).getMethod());
1047:                 }
1048:                 assertTrue(methods.contains(ExtendedTestServices1.class.getMethod("service1", EClassifier.class)));
1049:                 assertTrue(methods.contains(ExtendedTestServices1.class.getMethod("service2", EClassifier.class)));
1050:         }
1051:
1052:         @Test
1053:         public void lookupEmpty() {
1054:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1055:                 final ITestLookupEngine engine = instanciate(provider);
1056:
1057:                 assertEquals(null, engine.lookup("service", new IType[] {}));
1058:         }
1059:
1060:         @Test
1061:         public void lookupLowerPriorityLowerType() {
1062:                 final ITestLookupEngine engine = instanciate(null);
1063:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1064:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1065:                                 new TestServiceProvider(0, EClass.class, 1, EClassifier.class))) {
1066:                         registrationResult.merge(engine.registerService(service));
1067:                 }
1068:
1069:                 assertEquals(0, registrationResult.getDuplicated().size());
1070:                 assertEquals(0, registrationResult.getIsMaskedBy().size());
1071:                 assertEquals(1, registrationResult.getMasked().size());
1072:                 Entry<IService, List<IService>> entry = registrationResult.getMasked().entrySet().iterator().next();
1073:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1074:                 assertEquals(1, entry.getValue().size());
1075:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1076:                 assertEquals(2, registrationResult.getRegistered().size());
1077:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1078:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1079:
1080:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1081:                                 .getQueryEnvironment(), EClass.class) });
1082:
1083:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1084:
1085:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1086:                                 .getQueryEnvironment(), EClassifier.class) });
1087:
1088:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1089:         }
1090:
1091:         @Test
1092:         public void lookupLowerPriorityEqualType() {
1093:                 final ITestLookupEngine engine = instanciate(null);
1094:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1095:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1096:                                 new TestServiceProvider(0, EClassifier.class, 1, EClassifier.class))) {
1097:                         registrationResult.merge(engine.registerService(service));
1098:                 }
1099:
1100:                 assertEquals(0, registrationResult.getDuplicated().size());
1101:                 assertEquals(0, registrationResult.getIsMaskedBy().size());
1102:                 assertEquals(1, registrationResult.getMasked().size());
1103:                 Entry<IService, List<IService>> entry = registrationResult.getMasked().entrySet().iterator().next();
1104:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1105:                 assertEquals(1, entry.getValue().size());
1106:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1107:                 assertEquals(2, registrationResult.getRegistered().size());
1108:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1109:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1110:
1111:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1112:                                 .getQueryEnvironment(), EClass.class) });
1113:
1114:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1115:
1116:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1117:                                 .getQueryEnvironment(), EClassifier.class) });
1118:
1119:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1120:         }
1121:
1122:         @Test
1123:         public void lookupLowerPriorityGreaterType() {
1124:                 final ITestLookupEngine engine = instanciate(null);
1125:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1126:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1127:                                 new TestServiceProvider(0, EClassifier.class, 1, EClass.class))) {
1128:                         registrationResult.merge(engine.registerService(service));
1129:                 }
1130:
1131:                 assertEquals(0, registrationResult.getDuplicated().size());
1132:                 assertEquals(0, registrationResult.getIsMaskedBy().size());
1133:                 assertEquals(1, registrationResult.getMasked().size());
1134:                 Entry<IService, List<IService>> entry = registrationResult.getMasked().entrySet().iterator().next();
1135:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1136:                 assertEquals(1, entry.getValue().size());
1137:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1138:                 assertEquals(2, registrationResult.getRegistered().size());
1139:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1140:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1141:
1142:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1143:                                 .getQueryEnvironment(), EClass.class) });
1144:
1145:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1146:
1147:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1148:                                 .getQueryEnvironment(), EClassifier.class) });
1149:
1150:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1151:         }
1152:
1153:         @Test
1154:         public void lookupEqualPriorityLowerType() {
1155:                 final ITestLookupEngine engine = instanciate(null);
1156:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1157:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1158:                                 new TestServiceProvider(0, EClass.class, 0, EClassifier.class))) {
1159:                         registrationResult.merge(engine.registerService(service));
1160:                 }
1161:
1162:                 assertEquals(0, registrationResult.getDuplicated().size());
1163:                 assertEquals(1, registrationResult.getIsMaskedBy().size());
1164:                 Entry<IService, List<IService>> entry = registrationResult.getIsMaskedBy().entrySet().iterator()
1165:                                 .next();
1166:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1167:                 assertEquals(1, entry.getValue().size());
1168:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1169:                 assertEquals(0, registrationResult.getMasked().size());
1170:                 assertEquals(2, registrationResult.getRegistered().size());
1171:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1172:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1173:
1174:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1175:                                 .getQueryEnvironment(), EClass.class) });
1176:
1177:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1178:
1179:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1180:                                 .getQueryEnvironment(), EClassifier.class) });
1181:
1182:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1183:         }
1184:
1185:         @Test
1186:         public void lookupEqualPriorityEqualType() {
1187:                 final ITestLookupEngine engine = instanciate(null);
1188:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1189:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1190:                                 new TestServiceProvider(0, EClassifier.class, 0, EClassifier.class))) {
1191:                         registrationResult.merge(engine.registerService(service));
1192:                 }
1193:
1194:                 assertEquals(1, registrationResult.getDuplicated().size());
1195:                 Entry<IService, List<IService>> entry = registrationResult.getDuplicated().entrySet().iterator()
1196:                                 .next();
1197:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1198:                 assertEquals(1, entry.getValue().size());
1199:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1200:                 assertEquals(0, registrationResult.getIsMaskedBy().size());
1201:                 assertEquals(0, registrationResult.getMasked().size());
1202:                 assertEquals(2, registrationResult.getRegistered().size());
1203:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1204:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1205:
1206:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1207:                                 .getQueryEnvironment(), EClass.class) });
1208:
1209:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1210:
1211:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1212:                                 .getQueryEnvironment(), EClassifier.class) });
1213:
1214:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1215:         }
1216:
1217:         @Test
1218:         public void lookupEqualPriorityGreaterType() {
1219:                 final ITestLookupEngine engine = instanciate(null);
1220:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1221:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1222:                                 new TestServiceProvider(0, EClassifier.class, 0, EClass.class))) {
1223:                         registrationResult.merge(engine.registerService(service));
1224:                 }
1225:
1226:                 assertEquals(0, registrationResult.getDuplicated().size());
1227:                 assertEquals(0, registrationResult.getIsMaskedBy().size());
1228:                 assertEquals(1, registrationResult.getMasked().size());
1229:                 Entry<IService, List<IService>> entry = registrationResult.getMasked().entrySet().iterator().next();
1230:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1231:                 assertEquals(1, entry.getValue().size());
1232:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1233:
1234:                 assertEquals(2, registrationResult.getRegistered().size());
1235:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1236:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1237:
1238:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1239:                                 .getQueryEnvironment(), EClass.class) });
1240:
1241:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1242:
1243:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1244:                                 .getQueryEnvironment(), EClassifier.class) });
1245:
1246:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1247:         }
1248:
1249:         @Test
1250:         public void lookupGreaterPriorityLowerType() {
1251:                 final ITestLookupEngine engine = instanciate(null);
1252:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1253:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1254:                                 new TestServiceProvider(1, EClass.class, 0, EClassifier.class))) {
1255:                         registrationResult.merge(engine.registerService(service));
1256:                 }
1257:
1258:                 assertEquals(0, registrationResult.getDuplicated().size());
1259:                 assertEquals(1, registrationResult.getIsMaskedBy().size());
1260:                 Entry<IService, List<IService>> entry = registrationResult.getIsMaskedBy().entrySet().iterator()
1261:                                 .next();
1262:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1263:                 assertEquals(1, entry.getValue().size());
1264:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1265:                 assertEquals(0, registrationResult.getMasked().size());
1266:                 assertEquals(2, registrationResult.getRegistered().size());
1267:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1268:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1269:
1270:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1271:                                 .getQueryEnvironment(), EClass.class) });
1272:
1273:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1274:
1275:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1276:                                 .getQueryEnvironment(), EClassifier.class) });
1277:
1278:                 assertEquals(TestServiceProvider.Service2.class, service.getClass());
1279:         }
1280:
1281:         @Test
1282:         public void lookupGreaterPriorityEqualType() {
1283:                 final ITestLookupEngine engine = instanciate(null);
1284:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1285:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1286:                                 new TestServiceProvider(1, EClassifier.class, 0, EClassifier.class))) {
1287:                         registrationResult.merge(engine.registerService(service));
1288:                 }
1289:
1290:                 assertEquals(0, registrationResult.getDuplicated().size());
1291:                 assertEquals(1, registrationResult.getIsMaskedBy().size());
1292:                 Entry<IService, List<IService>> entry = registrationResult.getIsMaskedBy().entrySet().iterator()
1293:                                 .next();
1294:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1295:                 assertEquals(1, entry.getValue().size());
1296:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1297:                 assertEquals(0, registrationResult.getMasked().size());
1298:                 assertEquals(2, registrationResult.getRegistered().size());
1299:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1300:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1301:
1302:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1303:                                 .getQueryEnvironment(), EClass.class) });
1304:
1305:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1306:
1307:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1308:                                 .getQueryEnvironment(), EClassifier.class) });
1309:
1310:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1311:         }
1312:
1313:         @Test
1314:         public void lookupGreaterPriorityGreaterType() {
1315:                 final ITestLookupEngine engine = instanciate(null);
1316:                 final ServiceRegistrationResult registrationResult = new ServiceRegistrationResult();
1317:                 for (IService service : ServiceUtils.getServices(engine.getQueryEnvironment(),
1318:                                 new TestServiceProvider(1, EClassifier.class, 0, EClass.class))) {
1319:                         registrationResult.merge(engine.registerService(service));
1320:                 }
1321:
1322:                 assertEquals(0, registrationResult.getDuplicated().size());
1323:                 assertEquals(1, registrationResult.getIsMaskedBy().size());
1324:                 Entry<IService, List<IService>> entry = registrationResult.getIsMaskedBy().entrySet().iterator()
1325:                                 .next();
1326:                 assertEquals(TestServiceProvider.Service2.class, entry.getKey().getClass());
1327:                 assertEquals(1, entry.getValue().size());
1328:                 assertEquals(TestServiceProvider.Service1.class, entry.getValue().get(0).getClass());
1329:                 assertEquals(0, registrationResult.getMasked().size());
1330:                 assertEquals(2, registrationResult.getRegistered().size());
1331:                 assertEquals(TestServiceProvider.Service1.class, registrationResult.getRegistered().get(0).getClass());
1332:                 assertEquals(TestServiceProvider.Service2.class, registrationResult.getRegistered().get(1).getClass());
1333:
1334:                 IService service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1335:                                 .getQueryEnvironment(), EClass.class) });
1336:
1337:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1338:
1339:                 service = engine.lookup(TestServiceProvider.SERVICE_NAME, new IType[] {new ClassType(engine
1340:                                 .getQueryEnvironment(), EClassifier.class) });
1341:
1342:                 assertEquals(TestServiceProvider.Service1.class, service.getClass());
1343:         }
1344:
1345:         @Test
1346:         public void lookupPerf() {
1347:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1348:                 final ITestLookupEngine engine = instanciate(provider);
1349:                 final Map<String, IType[]> services = new LinkedHashMap<String, IType[]>();
1350:
1351:                 registerServices(engine);
1352:                 for (IService service : engine.getRegisteredServices()) {
1353:                         services.put(service.getName(), service.getParameterTypes(engine.getQueryEnvironment()).toArray(
1354:                                         new IType[service.getNumberOfParameters()]));
1355:                 }
1356:
1357:                 for (int i = 0; i < 10000; i++) {
1358:                         for (Entry<String, IType[]> entry : services.entrySet()) {
1359:                                 assertNotNull(engine.lookup(entry.getKey(), entry.getValue()));
1360:                         }
1361:                 }
1362:         }
1363:
1364:         @Test
1365:         public void lookup_492250_twoParameters() {
1366:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1367:                 final ITestLookupEngine engine = instanciate(provider);
1368:                 registerServices(engine);
1369:
1370:                 IService service = engine.lookup("eAllContents", new IType[] {
1371:                                 new ClassType(engine.getQueryEnvironment(), EClass.class),
1372:                                 new ClassType(engine.getQueryEnvironment(), EClass.class) });
1373:
1374:                 assertNotNull(service);
1375:         }
1376:
1377:         @Test
1378:         public void lookup_492250_oneParameter() {
1379:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1380:                 final ITestLookupEngine engine = instanciate(provider);
1381:                 registerServices(engine);
1382:
1383:                 IService service = engine.lookup("eAllContents", new IType[] {new ClassType(engine
1384:                                 .getQueryEnvironment(), EClass.class) });
1385:
1386:                 assertNotNull(service);
1387:         }
1388:
1389:         @Test
1390:         public void lookup_492250_twoThenOneParameters() {
1391:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1392:                 final ITestLookupEngine engine = instanciate(provider);
1393:                 registerServices(engine);
1394:
1395:                 IService service = engine.lookup("eAllContents", new IType[] {
1396:                                 new ClassType(engine.getQueryEnvironment(), EClass.class),
1397:                                 new ClassType(engine.getQueryEnvironment(), EClass.class) });
1398:
1399:                 assertNotNull(service);
1400:
1401:                 service = engine.lookup("eAllContents", new IType[] {new ClassType(engine.getQueryEnvironment(),
1402:                                 EClass.class) });
1403:
1404:                 assertNotNull(service);
1405:         }
1406:
1407:         @Test
1408:         public void lookup_492250_oneThenTwoParameters() {
1409:                 final CrossReferenceProvider provider = new TestCrossReferenceProvider();
1410:                 final ITestLookupEngine engine = instanciate(provider);
1411:                 registerServices(engine);
1412:
1413:                 IService service = engine.lookup("eAllContents", new IType[] {new EClassifierType(engine
1414:                                 .getQueryEnvironment(), EcorePackage.eINSTANCE.getEClass()) });
1415:
1416:                 assertNotNull(service);
1417:
1418:                 service = engine.lookup("eAllContents", new IType[] {
1419:                                 new ClassType(engine.getQueryEnvironment(), EClass.class),
1420:                                 new ClassType(engine.getQueryEnvironment(), EClass.class) });
1421:
1422:                 assertNotNull(service);
1423:         }
1424:
1425:         private void registerServices(ITestLookupEngine engine) {
1426:                 final Set<IService> services = new LinkedHashSet<IService>();
1427:
1428:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), new AnyServices(engine
1429:                                 .getQueryEnvironment())));
1430:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), new EObjectServices(engine
1431:                                 .getQueryEnvironment(), null, null)));
1432:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), new XPathServices(engine
1433:                                 .getQueryEnvironment())));
1434:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), ComparableServices.class));
1435:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), NumberServices.class));
1436:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), StringServices.class));
1437:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), BooleanServices.class));
1438:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), CollectionServices.class));
1439:                 services.addAll(ServiceUtils.getServices(engine.getQueryEnvironment(), ResourceServices.class));
1440:
1441:                 for (IService service : services) {
1442:                         engine.registerService(service);
1443:                 }
1444:         }
1445:
1446: }