summaryrefslogtreecommitdiffstats
path: root/tests/com/google/android/testing/mocking/ConstructorCreationTests.java
blob: 152918d54dcea4d06ce782c474c6c666b1292a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 *  Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.testing.mocking;

import junit.framework.TestCase;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;


/**
 * Tests for mocked objects with non default constructors.
 * 
 * @author swoodward@google.com (Stephen Woodward)
 */
public class ConstructorCreationTests extends TestCase {
  public static class Foo {
    private int value;
    Foo(int value) { this.value = value; }
    int get() { return value; }
  }

  public static class Bar {
    private double value;
    Bar(double value) { this.value = value; }
    double get() { return value; }
  }

  public static class TestClass {
    public int v1;
    public double v2;
    public boolean usedFloatConstructor;
    
    public TestClass(Foo foo) {
      this(foo.get());
    }

    public TestClass(Foo foo, Bar bar) {
      this(foo.get(), bar.get());
    }

    public TestClass(int v1) {
      this(v1, 0);
    }

    public TestClass(int v1, float v2) {
      this.v1 = v1;
      this.v2 = v2;
      usedFloatConstructor = true;
    }

    public TestClass(int v1, double v2) {
      this.v1 = v1;
      this.v2 = (int) v2;
      usedFloatConstructor = false;
    }
  }

  private void hasConstructor(Object... args) {
    Constructor<TestClass> constructor =
        AndroidMock.getConstructorFor(TestClass.class, args);
    assertNotNull(constructor);
  }

  private void doesNotHaveConstructor(Object... args) {
    try {
      Constructor<TestClass> constructor =
          AndroidMock.getConstructorFor(TestClass.class, args);
      fail("A constructor was found: " + constructor);
    } catch (IllegalArgumentException e) {
      // expected
    }
  }

  public void testConstructors() {
    hasConstructor(new Foo(1));
    doesNotHaveConstructor(new Bar(2));
    hasConstructor(new Foo(1), new Bar(2));
    hasConstructor(1);
    hasConstructor(1, 2);
    doesNotHaveConstructor(new Foo(1), 2);
    hasConstructor(1, new Integer("2"));
    hasConstructor(1, 2.0);
    hasConstructor(1, 2.0f);
  }

  private void checkConstructor(Object[] args, Type[] expectedTypes) {
    Constructor<TestClass> constructor =
        AndroidMock.getConstructorFor(TestClass.class, args);
    assertNotNull(constructor);
    Type[] types = constructor.getGenericParameterTypes();
    assertEquals(expectedTypes.length, types.length);
    for (int i = 0; i < expectedTypes.length; ++i) {
      assertEquals(expectedTypes[i], types[i]);
    }
  }

  public void testCorrectConstructor() {
    checkConstructor(
            new Object[]{new Foo(1)},
            new Type[]{Foo.class});
    checkConstructor(
            new Object[]{new Foo(1), new Bar(2)},
            new Type[]{Foo.class, Bar.class});
    checkConstructor(
            new Object[]{1},
            new Type[]{Integer.TYPE});
    checkConstructor(
            new Object[]{1, new Float("2")},
            new Type[]{Integer.TYPE, Float.TYPE});
    checkConstructor(
            new Object[]{1, 2.0},
            new Type[]{Integer.TYPE, Double.TYPE});
    checkConstructor(
            new Object[]{1, 2.0f},
            new Type[]{Integer.TYPE, Float.TYPE});
  }
}