示例#1
0
  @Test
  public void registerInstance() throws Exception {
    Bar b = new Bar();
    c.RegisterInstance(b);
    Bar r1 = c.Resolve(Bar.class);
    Bar r2 = c.Resolve(Bar.class);
    assertSame(r1, r2);
    assertSame(b, r1);

    c.RegisterType(Bar.class, false);
    Bar r3 = c.Resolve(Bar.class);
    assertNotSame(r1, r3);
  }
示例#2
0
  @Test
  public void siblingSingleton() throws Exception {
    c.RegisterType(BarChild.class, BarChild.class, true);
    c.RegisterType(AbstractBar.class, BarChild2.class, false);

    BarChild bc1 = c.Resolve(BarChild.class);
    BarChild bc2 = c.Resolve(BarChild.class);
    assertSame(bc1, bc2);

    AbstractBar sibling = c.Resolve(AbstractBar.class);
    AbstractBar sibling2 = c.Resolve(AbstractBar.class);
    assertNotSame(sibling, sibling2);
  }
示例#3
0
  @Test
  public void fromRegisteredInterface() {
    try {
      c.RegisterType(IFoo.class, Foo.class, false);
      IFoo f = c.Resolve(IFoo.class);
      c.RegisterType(IFoo.class, Foo2.class, false);
      IFoo f2 = c.Resolve(IFoo.class);

      assertTrue(f instanceof Foo);
      assertTrue(f2 instanceof Foo2);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }
示例#4
0
 @Test
 public void PublicPrivateDependencyField() throws Exception {
   PDP p = c.Resolve(PDP.class);
   assertNotNull(p.dpub);
   assertEquals(p.isDprivNull(), true);
   assertNull(p.pub);
 }
示例#5
0
 @Test
 public void TwoAnnotatedConstructors() throws Exception {
   expexc.expect(InstantiationException.class);
   expexc.expectMessage("more");
   expexc.expectMessage("DependencyConstructor");
   TwoAnnotated ta = c.Resolve(TwoAnnotated.class);
 }
示例#6
0
  // /*
  @Test
  public void singletonsSame() {
    try {
      c.RegisterType(Dummy.class, true);

      Dummy d1 = c.Resolve(Dummy.class);
      Dummy d2 = c.Resolve(Dummy.class);
      assertSame(d1, d2);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }
示例#7
0
 @Test
 public void fromRegisteredAbstractClass() {
   try {
     c.RegisterType(AbstractBar.class, Bar.class, false);
     AbstractBar b = c.Resolve(AbstractBar.class);
     assertTrue(b instanceof Bar);
     b.doBar();
     Bar bb = (Bar) b;
     assertTrue(bb.isDone());
   } catch (Exception ex) {
     System.out.println(ex.getMessage());
     fail();
   }
 }
示例#8
0
 @Test
 public void genericPair() {
   try {
     c.RegisterType(Pair.class, false);
     Pair<String, Integer> p1 = c.Resolve(Pair.class);
     p1.setA("asdf");
     p1.setB(5);
     assertEquals("asdf", p1.a);
     assertEquals(5, (long) p1.b);
   } catch (Exception ex) {
     System.out.println(ex.getMessage());
     fail();
   }
 }
示例#9
0
  @Test
  public void TBsInTreeWithoutCycle() throws Exception {
    T t = c.Resolve(T.class);

    assertNotNull(t.tb);
    assertNotNull(t.tb.td);
    assertNotNull(t.ta);
    assertNotNull(t.ta.tb);
    assertNotNull(t.ta.tb.td);
    assertNotNull(t.ta.tc);
    assertNotNull(t.ta.tc.tb);
    assertNotNull(t.ta.tc.tb.td);

    assertNotSame(t.tb, t.ta.tb);
    assertNotSame(t.ta.tc.tb, t.ta.tb);
  }
示例#10
0
 @Test
 public void fromUnregisteredInterface() throws Exception {
   expexc.expect(InstantiationException.class);
   IFoo f = c.Resolve(IFoo.class);
 }
示例#11
0
 @Test
 public void simpleInjectionAB() throws Exception {
   A a = c.Resolve(A.class);
   assertNotNull(a.b);
 }
示例#12
0
 @Test
 public void fromUnregisteredResolvableType() throws Exception {
   Dummy d = c.Resolve(Dummy.class);
   assertNotNull(d);
 }
示例#13
0
/** @author ja */
public class IoCTests {

  private SimpleContainer c;
  @Rule public ExpectedException expexc = ExpectedException.none();

  public IoCTests() {}

  @BeforeClass
  public static void setUpClass() {}

  @AfterClass
  public static void tearDownClass() {}

  @Before
  public void setUp() {
    c = new SimpleContainer();
    // expexc = ExpectedException.none();
  }

  @After
  public void tearDown() {}
  // /*
  @Test
  public void singletonsSame() {
    try {
      c.RegisterType(Dummy.class, true);

      Dummy d1 = c.Resolve(Dummy.class);
      Dummy d2 = c.Resolve(Dummy.class);
      assertSame(d1, d2);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }

  @Test
  public void genericPair() {
    try {
      c.RegisterType(Pair.class, false);
      Pair<String, Integer> p1 = c.Resolve(Pair.class);
      p1.setA("asdf");
      p1.setB(5);
      assertEquals("asdf", p1.a);
      assertEquals(5, (long) p1.b);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }

  @Test
  public void fromUnregisteredResolvableType() throws Exception {
    Dummy d = c.Resolve(Dummy.class);
    assertNotNull(d);
  }

  @Test
  public void fromRegisteredInterface() {
    try {
      c.RegisterType(IFoo.class, Foo.class, false);
      IFoo f = c.Resolve(IFoo.class);
      c.RegisterType(IFoo.class, Foo2.class, false);
      IFoo f2 = c.Resolve(IFoo.class);

      assertTrue(f instanceof Foo);
      assertTrue(f2 instanceof Foo2);
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }

  @Test
  public void fromUnregisteredInterface() throws Exception {
    expexc.expect(InstantiationException.class);
    IFoo f = c.Resolve(IFoo.class);
  }

  @Test
  public void fromRegisteredAbstractClass() {
    try {
      c.RegisterType(AbstractBar.class, Bar.class, false);
      AbstractBar b = c.Resolve(AbstractBar.class);
      assertTrue(b instanceof Bar);
      b.doBar();
      Bar bb = (Bar) b;
      assertTrue(bb.isDone());
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      fail();
    }
  }

  @Test
  public void fromUnregisteredAbstractClass() throws Exception {
    expexc.expect(InstantiationException.class);
    AbstractBar b = c.Resolve(AbstractBar.class);
  }

  @Test
  public void supertypeSingleton() throws Exception {
    expexc.expect(RuntimeException.class);
    c.RegisterType(AbstractBar.class, Bar.class, true);
    c.RegisterType(BarChild.class, BarChild.class, false);
  }

  @Test
  public void siblingSingleton() throws Exception {
    c.RegisterType(BarChild.class, BarChild.class, true);
    c.RegisterType(AbstractBar.class, BarChild2.class, false);

    BarChild bc1 = c.Resolve(BarChild.class);
    BarChild bc2 = c.Resolve(BarChild.class);
    assertSame(bc1, bc2);

    AbstractBar sibling = c.Resolve(AbstractBar.class);
    AbstractBar sibling2 = c.Resolve(AbstractBar.class);
    assertNotSame(sibling, sibling2);
  }

  @Test
  public void registerInstance() throws Exception {
    Bar b = new Bar();
    c.RegisterInstance(b);
    Bar r1 = c.Resolve(Bar.class);
    Bar r2 = c.Resolve(Bar.class);
    assertSame(r1, r2);
    assertSame(b, r1);

    c.RegisterType(Bar.class, false);
    Bar r3 = c.Resolve(Bar.class);
    assertNotSame(r1, r3);
  }

  @Test
  public void simpleInjectionAB() throws Exception {
    A a = c.Resolve(A.class);
    assertNotNull(a.b);
  }
  // */
  @Test
  public void unregisteredStringConstructor() throws Exception {
    expexc.expect(InstantiationException.class);
    X x = c.Resolve(X.class);
  }
示例#14
0
 @Test
 public void SetterInjection() throws Exception {
   SI s = c.Resolve(SI.class);
   assertNotNull(s.AGetter());
 }
示例#15
0
 @Test
 public void registeredStringConstructor() throws Exception {
   c.RegisterInstance("ala ma kota");
   X x = c.Resolve(X.class);
   assertEquals("ala ma kota", x.s);
 }
示例#16
0
 @Test
 public void DifferentLengthsConstructors() throws Exception {
   DLC d = c.Resolve(DLC.class);
   assertEquals(d.wasLongestChosen(), true);
 }
示例#17
0
 @Test
 public void OneAnnotatedConstructor() throws Exception {
   OneAnnotated oa = c.Resolve(OneAnnotated.class);
   assertEquals(oa.wasAnnotatedChosen(), true);
 }
示例#18
0
 @Test
 public void fromUnregisteredAbstractClass() throws Exception {
   expexc.expect(InstantiationException.class);
   AbstractBar b = c.Resolve(AbstractBar.class);
 }
示例#19
0
 @Test
 public void TrivialCycle() throws Exception {
   expexc.expect(InstantiationException.class);
   expexc.expectMessage("Infinite");
   Cyclic cy = c.Resolve(Cyclic.class);
 }
示例#20
0
 @Test
 public void UBsInTreeWithCycle() throws Exception {
   expexc.expect(InstantiationException.class);
   expexc.expectMessage("Infinite");
   U u = c.Resolve(U.class);
 }