1   package us.blanshard.stubout.tests;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import junit.framework.TestCase;
7   import junit.framework.TestSuite;
8   import us.blanshard.stubout.Stubber;
9   import us.blanshard.stubout.StubbingTestCase;
10  import us.blanshard.stubout.tests.model.ABadClass;
11  import us.blanshard.stubout.tests.model.ModelClass;
12  import us.blanshard.stubout.tests.model.ModelClassReplacement;
13  
14  public class TestClassReplacement extends TestCase
15  {
16      public static TestSuite suite() {
17          return new TestSuite( new Class[]{TestBadClass.class, TestModelClass.class, TestExceptions.class,
18                  TestLogging.class} );
19      }
20  
21      public static class TestBadClass extends StubbingTestCase {
22          public static final String SOME_VALUE = "Some replacement value";
23  
24          public void beforeReloading() {
25              Stubber.replaceClass("us.blanshard.stubout.tests.model.ABadClass", MyBadClassReplacement.class);
26              Stubber.replaceClass(ModelClass.class, ModelClassReplacement.class);
27          }
28  
29          public static class MyBadClassReplacement {
30              private static final MyBadClassReplacement instance = new MyBadClassReplacement();
31              private MyBadClassReplacement() {}
32              public static MyBadClassReplacement getInstance() {
33                  return instance;
34              }
35              public String getSomeValue() {
36                  return SOME_VALUE;
37              }
38          }
39  
40          public void testBadClass() {
41              assertEquals( SOME_VALUE, ABadClass.getInstance().getSomeValue() );
42          }
43      }
44  
45      public static class TestModelClass extends StubbingTestCase {
46          public void beforeReloading() {
47              Stubber.replaceClass(ModelClass.class, ModelClassReplacement.class);
48          }
49  
50          public void testModelClass() {
51              assertEquals( "ModelClassReplacement", new ModelClass().toString() );
52          }
53      }    
54  
55      public static class TestExceptions extends StubbingTestCase {
56          public void testNullArgs() {
57              try {
58                  Stubber.replaceClass(null, "xyzzy");
59                  fail();
60              } catch (IllegalArgumentException e) {
61              }
62              try {
63                  Stubber.replaceClass("xyzzy", (String)null);
64                  fail();
65              } catch (IllegalArgumentException e) {
66              }
67              try {
68                  Stubber.exclude(null);
69                  fail();
70              } catch (IllegalArgumentException e) {
71              }
72          }
73  
74          public void testReplacingNestedClass() {
75              try {
76                  Stubber.replaceClass(TestBadClass.class, getClass());
77                  fail();
78              } catch (IllegalArgumentException e) {
79              }
80          }
81  
82          public void beforeReloading() {
83              try {
84                  Stubber.replaceClass(Stubber.class, getClass());
85                  fail();
86              } catch (IllegalArgumentException e) {
87              }
88              Stubber.excludePackage("xyzzy");
89              try {
90                  Stubber.replaceClass("xyzzy.asdf", "abc.xyz");
91                  fail();
92              } catch (IllegalArgumentException e) {
93              }
94              Stubber.replaceClass("asdf.xyzzy", "abc.xyz");
95              try {
96                  Stubber.excludePackage("asdf");
97                  fail();
98              } catch (IllegalArgumentException e) {
99              }
100         }
101 
102         public void testReplacingFromWithinTest() {
103             try {
104                 Stubber.replaceClass(ModelClass.class, ModelClassReplacement.class);
105                 fail();
106             } catch (IllegalStateException e) {
107             }
108         }
109 
110         public void testExcludingFromWithinTest() {
111             try {
112                 Stubber.excludePackage("xyzzy");
113                 fail();
114             } catch (IllegalStateException e) {
115             }
116         }
117     }
118     
119     /**
120      * Tests Apache commons logging, which requires the Thread context class
121      * loader to be the stubbing class loader.
122      */
123     public static class TestLogging extends StubbingTestCase {
124         public void beforeReloading() {
125             Stubber.excludePackage("sun");
126         }
127         public void test() {
128             // Fails on 16th time when run against Sun's VM, unless "sun"
129             // package is excluded from stubbing.
130             for ( int i=0; i < 16; ++i )
131                 LogFactory.getLog("test"+i);
132         }
133     }
134 }