1 package com.google.inject.spi;
2 
3 import com.google.inject.AbstractModule;
4 import com.google.inject.Binder;
5 import com.google.inject.Module;
6 
7 import junit.framework.TestCase;
8 
9 /**
10  * Tests for {@link ModuleSource}.
11  */
12 public class ModuleSourceTest extends TestCase {
13 
14   private static final StackTraceElement BINDER_INSTALL =
15       new StackTraceElement("com.google.inject.spi.Elements$RecordingBinder", "install",
16           "Unknown Source", 235 /* line number*/);
17 
testOneModule()18   public void testOneModule() {
19     ModuleSource moduleSource = createWithSizeOne();
20     checkSizeOne(moduleSource);
21   }
22 
testTwoModules()23   public void testTwoModules() {
24     ModuleSource moduleSource = createWithSizeTwo();
25     checkSizeTwo(moduleSource);
26     moduleSource = moduleSource.getParent();
27     checkSizeOne(moduleSource);
28   }
29 
testThreeModules()30   public void testThreeModules() {
31     ModuleSource moduleSource = createWithSizeThree();
32     checkSizeThree(moduleSource);
33     moduleSource = moduleSource.getParent();
34     checkSizeTwo(moduleSource);
35     moduleSource = moduleSource.getParent();
36     checkSizeOne(moduleSource);
37   }
38 
checkSizeOne(ModuleSource moduleSource)39   private void checkSizeOne(ModuleSource moduleSource) {
40     assertEquals(1, moduleSource.size());
41     assertEquals(1, moduleSource.getStackTraceSize());
42     // Check call stack
43     StackTraceElement[] callStack = moduleSource.getStackTrace();
44     assertEquals(BINDER_INSTALL, callStack[0]);
45   }
46 
checkSizeTwo(ModuleSource moduleSource)47   private void checkSizeTwo(ModuleSource moduleSource) {
48     assertEquals(2, moduleSource.size());
49     assertEquals(3, moduleSource.getStackTraceSize());
50     // Check call stack
51     StackTraceElement[] callStack = moduleSource.getStackTrace();
52     assertEquals(BINDER_INSTALL, callStack[0]);
53     assertEquals(
54         new StackTraceElement(
55             "com.google.inject.spi.moduleSourceTest$A", "configure", "Unknown Source", 100),
56         callStack[1]);
57     assertEquals(BINDER_INSTALL, callStack[2]);
58   }
59 
checkSizeThree(ModuleSource moduleSource)60   private void checkSizeThree(ModuleSource moduleSource) {
61     assertEquals(3, moduleSource.size());
62     assertEquals(7, moduleSource.getStackTraceSize());
63     // Check call stack
64     StackTraceElement[] callStack = moduleSource.getStackTrace();
65     assertEquals(BINDER_INSTALL, callStack[0]);
66     assertEquals(new StackTraceElement("class1", "method1", "Unknown Source", 1), callStack[1]);
67     assertEquals(new StackTraceElement("class2", "method2", "Unknown Source", 2), callStack[2]);
68     assertEquals(
69         new StackTraceElement(
70             "com.google.inject.spi.moduleSourceTest$B", "configure", "Unknown Source", 200),
71             callStack[3]);
72     assertEquals(BINDER_INSTALL, callStack[4]);
73 
74     assertEquals(
75         new StackTraceElement(
76             "com.google.inject.spi.moduleSourceTest$A", "configure", "Unknown Source", 100),
77         callStack[5]);
78     assertEquals(BINDER_INSTALL, callStack[6]);
79   }
80 
createWithSizeOne()81   private ModuleSource createWithSizeOne() {
82     StackTraceElement[] partialCallStack = new StackTraceElement[1];
83     partialCallStack[0] = BINDER_INSTALL;
84     return new ModuleSource(new A(), partialCallStack);
85   }
86 
createWithSizeTwo()87   private ModuleSource createWithSizeTwo() {
88     ModuleSource moduleSource = createWithSizeOne();
89     StackTraceElement[] partialCallStack = new StackTraceElement[2];
90     partialCallStack[0] = BINDER_INSTALL;
91     partialCallStack[1] = new StackTraceElement(
92         "com.google.inject.spi.moduleSourceTest$A", "configure", "moduleSourceTest.java", 100);
93     return moduleSource.createChild(new B(), partialCallStack);
94   }
95 
createWithSizeThree()96   private ModuleSource createWithSizeThree() {
97     ModuleSource moduleSource = createWithSizeTwo();
98     StackTraceElement[] partialCallStack = new StackTraceElement[4];
99     partialCallStack[0] = BINDER_INSTALL;
100     partialCallStack[1] = new StackTraceElement("class1", "method1", "Class1.java", 1);
101     partialCallStack[2] = new StackTraceElement("class2", "method2", "Class2.java", 2);
102     partialCallStack[3] = new StackTraceElement(
103         "com.google.inject.spi.moduleSourceTest$B", "configure", "moduleSourceTest.java", 200);
104     return moduleSource.createChild(new C(), partialCallStack);
105   }
106 
107   private static class A extends AbstractModule {
108     @Override
configure()109     public void configure() {
110       install(new B());
111     }
112   }
113 
114   private static class B implements Module {
115     @Override
configure(Binder binder)116     public void configure(Binder binder) {
117       binder.install(new C());
118     }
119   }
120 
121   private static class C extends AbstractModule {
122     @Override
configure()123     public void configure() {}
124   }
125 }
126