• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.testing.mocking;
17 
18 import junit.framework.TestCase;
19 
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Type;
22 
23 
24 /**
25  * Tests for mocked objects with non default constructors.
26  *
27  * @author swoodward@google.com (Stephen Woodward)
28  */
29 public class ConstructorCreationTests extends TestCase {
30   public static class Foo {
31     private int value;
Foo(int value)32     Foo(int value) { this.value = value; }
get()33     int get() { return value; }
34   }
35 
36   public static class Bar {
37     private double value;
Bar(double value)38     Bar(double value) { this.value = value; }
get()39     double get() { return value; }
40   }
41 
42   public static class TestClass {
43     public int v1;
44     public double v2;
45     public boolean usedFloatConstructor;
46 
TestClass(Foo foo)47     public TestClass(Foo foo) {
48       this(foo.get());
49     }
50 
TestClass(Foo foo, Bar bar)51     public TestClass(Foo foo, Bar bar) {
52       this(foo.get(), bar.get());
53     }
54 
TestClass(int v1)55     public TestClass(int v1) {
56       this(v1, 0);
57     }
58 
TestClass(int v1, float v2)59     public TestClass(int v1, float v2) {
60       this.v1 = v1;
61       this.v2 = v2;
62       usedFloatConstructor = true;
63     }
64 
TestClass(int v1, double v2)65     public TestClass(int v1, double v2) {
66       this.v1 = v1;
67       this.v2 = (int) v2;
68       usedFloatConstructor = false;
69     }
70   }
71 
hasConstructor(Object... args)72   private void hasConstructor(Object... args) {
73     Constructor<TestClass> constructor =
74         AndroidMock.getConstructorFor(TestClass.class, args);
75     assertNotNull(constructor);
76   }
77 
doesNotHaveConstructor(Object... args)78   private void doesNotHaveConstructor(Object... args) {
79     try {
80       Constructor<TestClass> constructor =
81           AndroidMock.getConstructorFor(TestClass.class, args);
82       fail("A constructor was found: " + constructor);
83     } catch (IllegalArgumentException e) {
84       // expected
85     }
86   }
87 
testConstructors()88   public void testConstructors() {
89     hasConstructor(new Foo(1));
90     doesNotHaveConstructor(new Bar(2));
91     hasConstructor(new Foo(1), new Bar(2));
92     hasConstructor(1);
93     hasConstructor(1, 2);
94     doesNotHaveConstructor(new Foo(1), 2);
95     hasConstructor(1, new Integer("2"));
96     hasConstructor(1, 2.0);
97     hasConstructor(1, 2.0f);
98   }
99 
checkConstructor(Object[] args, Type[] expectedTypes)100   private void checkConstructor(Object[] args, Type[] expectedTypes) {
101     Constructor<TestClass> constructor =
102         AndroidMock.getConstructorFor(TestClass.class, args);
103     assertNotNull(constructor);
104     Type[] types = constructor.getGenericParameterTypes();
105     assertEquals(expectedTypes.length, types.length);
106     for (int i = 0; i < expectedTypes.length; ++i) {
107       assertEquals(expectedTypes[i], types[i]);
108     }
109   }
110 
testCorrectConstructor()111   public void testCorrectConstructor() {
112     checkConstructor(
113             new Object[]{new Foo(1)},
114             new Type[]{Foo.class});
115     checkConstructor(
116             new Object[]{new Foo(1), new Bar(2)},
117             new Type[]{Foo.class, Bar.class});
118     checkConstructor(
119             new Object[]{1},
120             new Type[]{Integer.TYPE});
121     checkConstructor(
122             new Object[]{1, new Float("2")},
123             new Type[]{Integer.TYPE, Float.TYPE});
124     checkConstructor(
125             new Object[]{1, 2.0},
126             new Type[]{Integer.TYPE, Double.TYPE});
127     checkConstructor(
128             new Object[]{1, 2.0f},
129             new Type[]{Integer.TYPE, Float.TYPE});
130   }
131 }