View Javadoc

1   package com.atlassian.plugin.hostcontainer;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import junit.framework.TestCase;
8   
9   public class TestSimpleConstructorModuleFactory extends TestCase
10  {
11      public void testCreateModule()
12      {
13          final Map<Class<?>, Object> context = new HashMap<Class<?>, Object>()
14          {
15              {
16                  put(String.class, "bob");
17              }
18          };
19  
20          final SimpleConstructorHostContainer factory = new SimpleConstructorHostContainer(context);
21          final Base world = factory.create(OneArg.class);
22          assertEquals("bob", world.getName());
23      }
24  
25      public void testCreateModuleFindBiggest()
26      {
27          final Map<Class<?>, Object> context = new HashMap<Class<?>, Object>()
28          {
29              {
30                  put(String.class, "bob");
31                  put(Integer.class, 10);
32              }
33          };
34  
35          final SimpleConstructorHostContainer factory = new SimpleConstructorHostContainer(context);
36          final Base world = factory.create(TwoArg.class);
37          assertEquals("bob 10", world.getName());
38      }
39  
40      public void testCreateModuleFindSmaller()
41      {
42          final Map<Class<?>, Object> context = new HashMap<Class<?>, Object>()
43          {
44              {
45                  put(String.class, "bob");
46              }
47          };
48  
49          final SimpleConstructorHostContainer factory = new SimpleConstructorHostContainer(context);
50          final Base world = factory.create(TwoArg.class);
51          assertEquals("bob", world.getName());
52      }
53  
54      public void testCreateModuleNoMatch()
55      {
56          final SimpleConstructorHostContainer factory = new SimpleConstructorHostContainer(Collections.<Class<?>, Object> emptyMap());
57          try
58          {
59              factory.create(OneArg.class);
60              fail("Should have thrown exception");
61          }
62          catch (final IllegalArgumentException ex)
63          {
64              // good, good
65          }
66      }
67  
68      public abstract static class Base
69      {
70          private final String name;
71  
72          public Base(final String name)
73          {
74              this.name = name;
75          }
76  
77          public String getName()
78          {
79              return name;
80          }
81      }
82  
83      public static class OneArg extends Base
84      {
85          public OneArg(final String name)
86          {
87              super(name);
88          }
89      }
90  
91      public static class TwoArg extends Base
92      {
93          public TwoArg(final String name)
94          {
95              super(name);
96          }
97  
98          public TwoArg(final String name, final Integer age)
99          {
100             super(name + " " + age);
101         }
102     }
103 
104 }