![JAR search and dependency download from the Maven repository](/logo.png)
org.eclipse.sisu.inject.LazyBeanEntryTest Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2010, 2013 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu.inject;
import java.lang.annotation.Annotation;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.eclipse.sisu.Description;
import org.eclipse.sisu.inject.RankedBindingsTest.Bean;
import org.eclipse.sisu.inject.RankedBindingsTest.BeanImpl;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import com.google.inject.Scopes;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
public class LazyBeanEntryTest
extends TestCase
{
@Description( "This is a test" )
static class DescribedBean
implements Bean
{
}
public void testDetails()
{
final Key key1 = Key.get( Bean.class, Names.named( "1" ) );
final Key key2 = Key.get( Bean.class, Names.named( "2" ) );
final Key key3 = Key.get( Bean.class, Names.named( "3" ) );
final Key key4 = Key.get( Bean.class, Names.named( "4" ) );
final Injector injector = Guice.createInjector( new AbstractModule()
{
@Override
protected void configure()
{
bind( key1 ).to( DescribedBean.class ).in( Scopes.SINGLETON );
binder().withSource( new BeanDescription()
{
public String getDescription()
{
return "Another test";
}
} ).bind( key2 ).toInstance( new BeanImpl() );
binder().withSource( "where?" ).bind( key3 ).to( BeanImpl.class );
bind( key4 ).toProvider( Providers.of( new BeanImpl() ) );
}
} );
final LazyBeanEntry bean1 =
new LazyBeanEntry( key1.getAnnotation(), injector.getBinding( key1 ), 42 );
final LazyBeanEntry bean2 =
new LazyBeanEntry( key2.getAnnotation(), injector.getBinding( key2 ), -24 );
final LazyBeanEntry bean3 =
new LazyBeanEntry( key3.getAnnotation(), injector.getBinding( key3 ), 0 );
final LazyBeanEntry bean4 =
new LazyBeanEntry( key4.getAnnotation(), injector.getBinding( key4 ), -1 );
assertEquals( "This is a test", bean1.getDescription() );
assertTrue( bean1.getSource() instanceof StackTraceElement );
assertEquals( DescribedBean.class, bean1.getImplementationClass() );
assertEquals( 42, bean1.getRank() );
assertEquals( "Another test", bean2.getDescription() );
assertTrue( bean2.getSource() instanceof BeanDescription );
assertEquals( BeanImpl.class, bean2.getImplementationClass() );
assertEquals( -24, bean2.getRank() );
assertNull( bean3.getDescription() );
assertTrue( bean3.getSource() instanceof String );
assertEquals( BeanImpl.class, bean3.getImplementationClass() );
assertEquals( 0, bean3.getRank() );
assertNull( bean4.getDescription() );
assertTrue( bean4.getSource() instanceof StackTraceElement );
assertEquals( null, bean4.getImplementationClass() );
assertEquals( -1, bean4.getRank() );
}
static class CountingProvider
implements Provider
© 2015 - 2025 Weber Informatics LLC | Privacy Policy