Search in sources :

Example 1 with InjectedElement

use of com.mvp4g.rebind.config.element.InjectedElement in project mvp4g by mvp4g.

the class AbstractMvp4gAnnotationsWithServiceLoaderTest method testServicesWithName.

@Test
public void testServicesWithName() throws Mvp4gAnnotationException {
    List<JClassType> annotedClasses = new ArrayList<JClassType>();
    JClassType type = oracle.addClass(getServiceWithName());
    annotedClasses.add(type);
    loader.load(annotedClasses, configuration);
    Set<Mvp4gWithServicesElement> elements = getSet();
    List<InjectedElement> injectedServices = elements.iterator().next().getInjectedServices();
    InjectedElement injectedService = injectedServices.get(0);
    assertEquals(injectedServices.size(), 1);
    assertEquals(injectedService.getSetterName(), "setSthg");
    assertEquals(injectedService.getElementName(), "name");
    assertEquals(configuration.getServices().size(), 0);
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) ArrayList(java.util.ArrayList) Mvp4gWithServicesElement(com.mvp4g.rebind.config.element.Mvp4gWithServicesElement) Test(org.junit.Test)

Example 2 with InjectedElement

use of com.mvp4g.rebind.config.element.InjectedElement in project mvp4g by mvp4g.

the class AbstractMvp4gAnnotationsWithServiceLoaderTest method testServices.

@Test
public void testServices() throws Mvp4gAnnotationException {
    List<JClassType> annotedClasses = new ArrayList<JClassType>();
    JClassType type = oracle.addClass(getService());
    annotedClasses.add(type);
    loader.load(annotedClasses, configuration);
    Set<Mvp4gWithServicesElement> elements = getSet();
    String serviceClass = SimpleService.class.getCanonicalName();
    String serviceName = serviceClass.replace('.', '_');
    List<InjectedElement> injectedServices = elements.iterator().next().getInjectedServices();
    InjectedElement injectedService = injectedServices.get(0);
    assertEquals(injectedServices.size(), 1);
    assertEquals(injectedService.getSetterName(), "setSthg");
    assertEquals(injectedService.getElementName(), serviceName + "Async");
    Set<ServiceElement> services = configuration.getServices();
    assertEquals(services.size(), 1);
    ServiceElement service = services.iterator().next();
    assertEquals(service.getName(), serviceName + "Async");
    assertEquals(service.getClassName(), serviceClass);
    annotedClasses.clear();
    type = oracle.addClass(getSameService());
    annotedClasses.add(type);
    loader.load(annotedClasses, configuration);
    assertEquals(services.size(), 1);
    ServiceElement service2 = services.iterator().next();
    assertSame(service, service2);
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) ArrayList(java.util.ArrayList) Mvp4gWithServicesElement(com.mvp4g.rebind.config.element.Mvp4gWithServicesElement) ServiceElement(com.mvp4g.rebind.config.element.ServiceElement) Test(org.junit.Test)

Example 3 with InjectedElement

use of com.mvp4g.rebind.config.element.InjectedElement in project mvp4g by mvp4g.

the class Mvp4gConfigurationTest method testInjectedServiceValidationFailsForPresenter.

@Test(expected = UnknownConfigurationElementException.class)
public void testInjectedServiceValidationFailsForPresenter() throws UnknownConfigurationElementException, InvalidTypeException, InvalidClassException, NotFoundClassException {
    events.add(newEvent("badService"));
    views.add(newView(SimpleView02.class, "badService"));
    presenters.add(newPresenter(SimplePresenter01.class, "badService"));
    historyConverters.add(newHistoryConverter(SimpleHistoryConverter01.class, "badService"));
    PresenterElement presenter = newPresenter(SimplePresenter02.class, "testPresenter");
    presenter.getInjectedServices().add(new InjectedElement("badService", "setBadService"));
    presenters.add(presenter);
    configuration.validateServices();
}
Also used : SimpleView02(com.mvp4g.rebind.test_tools.annotation.views.SimpleView02) InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) SimpleHistoryConverter01(com.mvp4g.rebind.test_tools.annotation.history_converters.SimpleHistoryConverter01) PresenterElement(com.mvp4g.rebind.config.element.PresenterElement) SimplePresenter01(com.mvp4g.rebind.test_tools.annotation.presenters.SimplePresenter01) Test(org.junit.Test)

Example 4 with InjectedElement

use of com.mvp4g.rebind.config.element.InjectedElement in project mvp4g by mvp4g.

the class Mvp4gConfigurationTest method testInjectedServiceRemove.

@Test
public void testInjectedServiceRemove() throws UnknownConfigurationElementException {
    ServiceElement service1 = newService("service1");
    ServiceElement service2 = newService("service2");
    ServiceElement service3 = newService("service3");
    ServiceElement service4 = newService("service4");
    services.add(service1);
    services.add(service2);
    services.add(service3);
    services.add(service4);
    PresenterElement presenter = newPresenter(SimplePresenter01.class, "testPresenter");
    presenter.getInjectedServices().add(new InjectedElement("service1", "setTestService"));
    presenters.add(presenter);
    HistoryConverterElement historyConverter = newHistoryConverter(SimpleHistoryConverter01.class, "testHistoryConverter");
    historyConverter.getInjectedServices().add(new InjectedElement("service2", "setTestService"));
    historyConverters.add(historyConverter);
    EventHandlerElement eventHandlerElement = newEventHandler(SimpleEventHandler01.class, "testEventHandler");
    eventHandlerElement.getInjectedServices().add(new InjectedElement("service3", "setTestService"));
    eventHandlers.add(eventHandlerElement);
    assertEquals(services.size(), 4);
    assertTrue(services.contains(service1));
    assertTrue(services.contains(service2));
    assertTrue(services.contains(service3));
    assertTrue(services.contains(service4));
    configuration.validateServices();
    assertEquals(services.size(), 3);
    assertTrue(services.contains(service1));
    assertTrue(services.contains(service2));
    assertTrue(services.contains(service3));
    assertFalse(services.contains(service4));
}
Also used : HistoryConverterElement(com.mvp4g.rebind.config.element.HistoryConverterElement) InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) EventHandlerElement(com.mvp4g.rebind.config.element.EventHandlerElement) PresenterElement(com.mvp4g.rebind.config.element.PresenterElement) ServiceElement(com.mvp4g.rebind.config.element.ServiceElement) Test(org.junit.Test)

Example 5 with InjectedElement

use of com.mvp4g.rebind.config.element.InjectedElement in project mvp4g by mvp4g.

the class Mvp4gAnnotationsWithServiceLoader method loadElement.

/*
   * (non-Javadoc)
   *
   * @see com.mvp4g.rebind.config.loader.annotation.Mvp4gAnnotationsLoader#loadElement
   * (com.google.gwt .core.ext.typeinfo.JClassType, java.lang.annotation.Annotation,
   * com.mvp4g.rebind.config.Mvp4gConfiguration)
   */
@Override
protected void loadElement(JClassType c, T annotation, Mvp4gConfiguration configuration) throws Mvp4gAnnotationException {
    Mvp4gWithServicesElement element = loadElementWithServices(c, annotation, configuration);
    // check if any services need to be injected
    JParameter[] params = null;
    String className = null;
    String serviceName = null;
    InjectService serviceAnnotation = null;
    for (JMethod m : c.getOverridableMethods()) {
        serviceAnnotation = m.getAnnotation(InjectService.class);
        if (serviceAnnotation != null) {
            if (!m.isPublic()) {
                String err = "Only public setter method can be used to inject a service.";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), m.getName(), err);
            }
            params = m.getParameters();
            if (params.length != 1) {
                String err = "Only setter method with one parameter can be used to inject a service";
                throw new Mvp4gAnnotationException(c.getQualifiedSourceName(), m.getName(), err);
            }
            serviceName = serviceAnnotation.serviceName();
            if ((serviceName == null) || (serviceName.length() == 0)) {
                className = params[0].getType().getQualifiedSourceName();
                serviceName = getServiceName(configuration, className);
            }
            element.getInjectedServices().add(new InjectedElement(serviceName, m.getName()));
        }
    }
}
Also used : InjectedElement(com.mvp4g.rebind.config.element.InjectedElement) JParameter(com.google.gwt.core.ext.typeinfo.JParameter) Mvp4gAnnotationException(com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException) Mvp4gWithServicesElement(com.mvp4g.rebind.config.element.Mvp4gWithServicesElement) JMethod(com.google.gwt.core.ext.typeinfo.JMethod) InjectService(com.mvp4g.client.annotation.InjectService)

Aggregations

InjectedElement (com.mvp4g.rebind.config.element.InjectedElement)7 Test (org.junit.Test)6 HistoryConverterElement (com.mvp4g.rebind.config.element.HistoryConverterElement)3 Mvp4gWithServicesElement (com.mvp4g.rebind.config.element.Mvp4gWithServicesElement)3 PresenterElement (com.mvp4g.rebind.config.element.PresenterElement)3 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)2 ServiceElement (com.mvp4g.rebind.config.element.ServiceElement)2 SimpleHistoryConverter01 (com.mvp4g.rebind.test_tools.annotation.history_converters.SimpleHistoryConverter01)2 SimplePresenter01 (com.mvp4g.rebind.test_tools.annotation.presenters.SimplePresenter01)2 SimpleView02 (com.mvp4g.rebind.test_tools.annotation.views.SimpleView02)2 ArrayList (java.util.ArrayList)2 JMethod (com.google.gwt.core.ext.typeinfo.JMethod)1 JParameter (com.google.gwt.core.ext.typeinfo.JParameter)1 InjectService (com.mvp4g.client.annotation.InjectService)1 EventHandlerElement (com.mvp4g.rebind.config.element.EventHandlerElement)1 Mvp4gAnnotationException (com.mvp4g.rebind.exception.loader.Mvp4gAnnotationException)1