Search in sources :

Example 11 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class ServicesInstance method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("Services{");
    String sep = " ";
    for (ServiceReference serviceReference : serviceReferences) {
        sb.append(sep).append(serviceReference.identity()).append("(active=").append(serviceReference.isActive()).append(")");
        sep = ", ";
    }
    return sb.append(" }").toString();
}
Also used : ServiceReference(org.qi4j.api.service.ServiceReference)

Example 12 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class JDKMixinTest method testMixinExtendsJDK.

@Test
public void testMixinExtendsJDK() {
    List<ServiceReference<JSONSerializableMap>> services = toList(filter(EXTENDS_IDENTITY_SPEC, module.findServices(JSONSerializableMap.class)));
    assertThat(services.size(), equalTo(1));
    assertThat(services.get(0).identity(), equalTo(EXTENDS_IDENTITY));
    JSONSerializableMap extending = services.get(0).get();
    // Concern trigger #1 (put)
    extending.put("foo", "bar");
    // Concern trigger #2, #3 and #4 (toJSON, size, entrySet)
    JSONObject json = extending.toJSON();
    assertThat(json.length(), equalTo(1));
    assertThat(json.optString("foo"), equalTo("bar"));
    assertThat(CONCERN_RECORDS.size(), equalTo(4));
}
Also used : JSONObject(org.json.JSONObject) ServiceReference(org.qi4j.api.service.ServiceReference) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 13 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class JDKMixinTest method testComposeJDKMixin.

@Test
public void testComposeJDKMixin() {
    List<ServiceReference<JSONSerializableMap>> services = toList(filter(COMPOSE_IDENTITY_SPEC, module.findServices(JSONSerializableMap.class)));
    assertThat(services.size(), equalTo(1));
    assertThat(services.get(0).identity(), equalTo(COMPOSE_IDENTITY));
    JSONSerializableMap composing = services.get(0).get();
    // Concern trigger #1 (put)
    composing.put("foo", "bar");
    // Concern trigger #2, #3 and #4 (toJSON, size, entrySet)
    JSONObject json = composing.toJSON();
    assertThat(json.length(), equalTo(1));
    assertThat(json.optString("foo"), equalTo("bar"));
    assertThat(CONCERN_RECORDS.size(), equalTo(4));
}
Also used : JSONObject(org.json.JSONObject) ServiceReference(org.qi4j.api.service.ServiceReference) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 14 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class PassivationTest method givenSuccessPassivationWhenPassivatingExpectNoExceptions.

@Test
public void givenSuccessPassivationWhenPassivatingExpectNoExceptions() throws Throwable {
    SingletonAssembler assembly = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
            module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
        }
    };
    Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices(DataAccess.class);
    for (ServiceReference<DataAccess> service : iterable) {
        assertTrue("Service should not be Active before accessed", !service.isActive());
        assertTrue(service.get().data().activated);
        assertTrue("Service should be Active after access.", service.isActive());
    }
    assembly.application().passivate();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Example 15 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class TypeToCompositeLookupTest method servicesPluralDeclaration.

@Test
public void servicesPluralDeclaration() throws ActivationException, AssemblyException {
    Module module = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.services(SomeOtherFoo.class, BasicFoo.class);
        }
    }.module();
    assertEquals(1, Iterables.count(module.findServices(SomeOtherFoo.class)));
    assertEquals(2, Iterables.count(module.findServices(BasicFoo.class)));
    assertEquals(2, Iterables.count(module.findServices(Foo.class)));
    assertEquals(CATHEDRAL, module.findService(SomeOtherFoo.class).get().bar());
    // Exact type match first even if it is assembled _after_ an assignable, the assignable comes after
    Iterator<ServiceReference<BasicFoo>> basicFoos = module.findServices(BasicFoo.class).iterator();
    assertEquals(BAZAR, basicFoos.next().get().bar());
    assertEquals(CATHEDRAL, basicFoos.next().get().bar());
    assertFalse(basicFoos.hasNext());
    // No exact type match, all assembled are assignable, follows assembly Type order
    Iterator<ServiceReference<Foo>> foos = module.findServices(Foo.class).iterator();
    assertEquals(CATHEDRAL, foos.next().get().bar());
    assertEquals(BAZAR, foos.next().get().bar());
    assertFalse(foos.hasNext());
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceReference (org.qi4j.api.service.ServiceReference)15 Test (org.junit.Test)7 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)4 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)4 PassivationException (org.qi4j.api.activation.PassivationException)3 Module (org.qi4j.api.structure.Module)3 ArrayList (java.util.ArrayList)2 JSONObject (org.json.JSONObject)2 ActivationEvent (org.qi4j.api.activation.ActivationEvent)2 ActivationException (org.qi4j.api.activation.ActivationException)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 LinkedHashSet (java.util.LinkedHashSet)1 ServletException (javax.servlet.ServletException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Source (javax.xml.transform.Source)1