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