Search in sources :

Example 1 with UnqualifiedImpl

use of org.glassfish.hk2.utilities.UnqualifiedImpl in project glassfish-hk2 by eclipse-ee4j.

the class DefaultUnqualifiedTest method testIterableProviderReturnsAllEvenIfUnqualified.

/**
 * Tests that injection of IterableProvider does NOT have the
 * default Unqualified applied to it
 */
// @org.junit.Ignore
@Test
public void testIterableProviderReturnsAllEvenIfUnqualified() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(BoardGameImpl.class, DollImpl.class, TrainImpl.class, ChildsRoom.class);
    locator.setDefaultUnqualified(new UnqualifiedImpl());
    ChildsRoom childsRoom = locator.getService(ChildsRoom.class);
    IterableProvider<Toy> allToys = childsRoom.getAllToys();
    Assert.assertEquals(3, allToys.getSize());
    int lcv = 0;
    for (Toy toy : allToys) {
        switch(lcv) {
            case 0:
                Assert.assertEquals(BoardGameImpl.class, toy.getClass());
                break;
            case 1:
                Assert.assertEquals(TrainImpl.class, toy.getClass());
                break;
            case 2:
                Assert.assertEquals(DollImpl.class, toy.getClass());
                break;
            default:
                Assert.fail("Should not get here");
        }
        lcv++;
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) UnqualifiedImpl(org.glassfish.hk2.utilities.UnqualifiedImpl) Test(org.junit.Test)

Example 2 with UnqualifiedImpl

use of org.glassfish.hk2.utilities.UnqualifiedImpl in project glassfish-hk2 by eclipse-ee4j.

the class DefaultUnqualifiedTest method testGetsUnqualifiedService.

/**
 * Tests that when using a system default unqualified
 * that the service gets the proper non-qualified
 * service.  Note that DollImpl has a low priority, and
 * hence would NOT be picked for the doll injection
 * point if the DefaultUnqualified were not set
 */
// @org.junit.Ignore
@Test
public void testGetsUnqualifiedService() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(BoardGameImpl.class, DollImpl.class, TrainImpl.class, ChildsRoom.class);
    locator.setDefaultUnqualified(new UnqualifiedImpl());
    ChildsRoom childsRoom = locator.getService(ChildsRoom.class);
    Assert.assertEquals(BoardGameImpl.class, childsRoom.getBoardGame().getClass());
    Assert.assertEquals(TrainImpl.class, childsRoom.getTrain().getClass());
    // The real test:
    Assert.assertEquals(DollImpl.class, childsRoom.getDoll().getClass());
    Assert.assertEquals(DollImpl.class, childsRoom.getDollProvider().get().getClass());
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) UnqualifiedImpl(org.glassfish.hk2.utilities.UnqualifiedImpl) Test(org.junit.Test)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 UnqualifiedImpl (org.glassfish.hk2.utilities.UnqualifiedImpl)2 Test (org.junit.Test)2