Package se.jbee.inject.service

Source Code of se.jbee.inject.service.TestServiceMethodBinds$ServiceBindsModule

package se.jbee.inject.service;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static se.jbee.inject.Dependency.dependency;
import static se.jbee.inject.Type.raw;

import org.junit.Test;

import se.jbee.inject.Dependency;
import se.jbee.inject.Injector;
import se.jbee.inject.bootstrap.Bootstrap;

public class TestServiceMethodBinds {

  private static class ServiceBindsModule
      extends ServiceModule {

    @Override
    protected void declare() {
      bindServiceMethodsIn( MyService.class );
      bindServiceMethodsIn( MyOtherService.class );
    }

  }

  static class MyService {

    public Integer negate( Number value ) {
      return -value.intValue();
    }
  }

  static class MyOtherService {

    public int mul2( int value, ServiceMethod<Float, Integer> service ) {
      return value * 2 + service.invoke( 2.8f );
    }

    public int round( float value ) {
      return Math.round( value );
    }
  }

  @SuppressWarnings ( "unchecked" )
  @Test
  public void test() {
    Injector injector = Bootstrap.injector( ServiceBindsModule.class );
    @SuppressWarnings ( "rawtypes" )
    Dependency<ServiceMethod> dependency = dependency( raw( ServiceMethod.class ).parametized(
        Integer.class, Integer.class ) );
    ServiceMethod<Integer, Integer> mul2 = injector.resolve( dependency );
    assertNotNull( mul2 );
    assertThat( mul2.invoke( 3 ), is( 9 ) );
    @SuppressWarnings ( "rawtypes" )
    Dependency<ServiceMethod> dependency2 = dependency( raw( ServiceMethod.class ).parametized(
        Number.class, Integer.class ) );
    ServiceMethod<Number, Integer> negate = injector.resolve( dependency2 );
    assertNotNull( mul2 );
    assertThat( negate.invoke( 3 ), is( -3 ) );
    assertThat( mul2.invoke( 4 ), is( 11 ) );
  }
}
TOP

Related Classes of se.jbee.inject.service.TestServiceMethodBinds$ServiceBindsModule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.