• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import junit.framework.TestCase;
7 
8 /** Tests for {@link ModuleSource}. */
9 public class ModuleSourceTest extends TestCase {
10 
11   private static final StackTraceElement BINDER_INSTALL =
12       new StackTraceElement(
13           "com.google.inject.spi.Elements$RecordingBinder",
14           "install",
15           "Unknown Source",
16           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] =
92         new StackTraceElement(
93             "com.google.inject.spi.moduleSourceTest$A", "configure", "moduleSourceTest.java", 100);
94     return moduleSource.createChild(new B(), partialCallStack);
95   }
96 
createWithSizeThree()97   private ModuleSource createWithSizeThree() {
98     ModuleSource moduleSource = createWithSizeTwo();
99     StackTraceElement[] partialCallStack = new StackTraceElement[4];
100     partialCallStack[0] = BINDER_INSTALL;
101     partialCallStack[1] = new StackTraceElement("class1", "method1", "Class1.java", 1);
102     partialCallStack[2] = new StackTraceElement("class2", "method2", "Class2.java", 2);
103     partialCallStack[3] =
104         new StackTraceElement(
105             "com.google.inject.spi.moduleSourceTest$B", "configure", "moduleSourceTest.java", 200);
106     return moduleSource.createChild(new C(), partialCallStack);
107   }
108 
109   private static class A extends AbstractModule {
110     @Override
configure()111     public void configure() {
112       install(new B());
113     }
114   }
115 
116   private static class B implements Module {
117     @Override
configure(Binder binder)118     public void configure(Binder binder) {
119       binder.install(new C());
120     }
121   }
122 
123   private static class C extends AbstractModule {
124   }
125 }
126