summaryrefslogtreecommitdiff
path: root/tests/com/google/android/testing/mocking/ClassTypeTests.java
blob: a4d60c2289005fb574ff3c7884bd9da1dc5fd511 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * 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 javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Various tests that verify that different types of Classes are handled
 * correctly.
 * 
 * @author swoodward@google.com (Stephen Woodward)
 */
public class ClassTypeTests extends TestCase {
  private AndroidMockGenerator androidMockGenerator = new AndroidMockGenerator();

  private AndroidMockGenerator getAndroidMockGenerator() {
    return androidMockGenerator;
  }

  private void assertAllMethodNames(List<String> expectedNames,
      Map<String, List<String>> expectedMethods, List<GeneratedClassFile> classes)
      throws IOException {
    for (GeneratedClassFile clazz : classes) {
      assertTrue(expectedNames.contains(clazz.getClassName()));
      assertUnorderedContentsSame(expectedMethods.get(clazz.getClassName()), getMethodNames(clazz));
    }
  }

  private <T> void assertUnorderedContentsSame(Iterable<T> expected, Iterable<T> actual) {
    List<T> missingItems = new ArrayList<T>();
    List<T> extraItems = new ArrayList<T>();
    for (T item : expected) {
      missingItems.add(item);
    }
    for (T item : actual) {
      missingItems.remove(item);
      extraItems.add(item);
    }
    for (T item : expected) {
      extraItems.remove(item);
    }
    if (missingItems.size() + extraItems.size() != 0) {
      String errorMessage =
          "Contents were different. Missing: " + Arrays.toString(missingItems.toArray())
              + " Extra: " + Arrays.toString(extraItems.toArray());
      fail(errorMessage);
    }
  }

  private List<String> getExpectedNames(Class<?> clazz) {
    return new ArrayList<String>(Arrays.asList(new String[] {
        "genmocks." + clazz.getCanonicalName() + "DelegateInterface",
        "genmocks." + clazz.getCanonicalName() + "DelegateSubclass"}));
  }

  private Iterable<String> getMethodNames(GeneratedClassFile clazz) throws IOException {
    ByteArrayInputStream classInputStream = new ByteArrayInputStream(clazz.getContents());
    CtClass ctClass;
    try {
      ctClass = ClassPool.getDefault().getCtClass(clazz.getClassName());
      if (ctClass.isFrozen()) {
        ctClass.defrost();
      }
    } catch (NotFoundException e) {
      // That's ok, we're just defrosting any classes that affect us that were created
      // by other tests.  NotFoundException implies the class is not frozen.
    }
    ctClass = ClassPool.getDefault().makeClass(classInputStream);
    return getMethodNames(ctClass.getDeclaredMethods());
  }

  private List<String> getMethodNames(CtMethod[] methods) {
    List<String> methodNames = new ArrayList<String>();
    for (CtMethod method : methods) {
      methodNames.add(method.getName());
    }
    return methodNames;
  }

  private List<String> getMethodNames(Method[] methods, String[] exclusions) {
    List<String> methodNames = new ArrayList<String>();
    for (Method method : methods) {
      if (!Arrays.asList(exclusions).contains(method.getName())) {
        methodNames.add(method.getName());
      }
    }
    return methodNames;
  }

  private Map<String, List<String>> getExpectedMethodsMap(List<String> expectedNames,
      Class<?> clazz) {
    return getExpectedMethodsMap(expectedNames, clazz, new String[0]);
  }

  private Map<String, List<String>> getExpectedMethodsMap(List<String> expectedNames,
      Class<?> clazz, String[] exclusions) {
    Map<String, List<String>> expectedMethods = new HashMap<String, List<String>>();
    expectedMethods.put(expectedNames.get(0), new ArrayList<String>(Arrays.asList(new String[] {
        "finalize", "clone"})));
    expectedMethods.put(expectedNames.get(1), new ArrayList<String>(Arrays.asList(new String[] {
        "finalize", "clone", "setDelegate___AndroidMock", "getDelegate___AndroidMock"})));
    expectedMethods.get(expectedNames.get(0)).addAll(
        getMethodNames(clazz.getDeclaredMethods(), exclusions));
    expectedMethods.get(expectedNames.get(1)).addAll(
        getMethodNames(clazz.getDeclaredMethods(), exclusions));
    return expectedMethods;
  }

  public void testClassIsDuplicate() throws ClassNotFoundException, IOException,
      CannotCompileException {
    List<GeneratedClassFile> classList =
        getAndroidMockGenerator().createMocksForClass(Object.class);
    List<GeneratedClassFile> secondClassList =
        getAndroidMockGenerator().createMocksForClass(Object.class);
    assertEquals(classList, secondClassList);
  }

  public void testClassHasDelegateMethods() throws ClassNotFoundException, IOException,
      CannotCompileException {
    List<String> expectedNames = getExpectedNames(ClassHasDelegateMethods.class);
    Map<String, List<String>> expectedMethods =
        getExpectedMethodsMap(expectedNames, ClassHasDelegateMethods.class,
            new String[] {"getDelegate___AndroidMock"});
    // This use case doesn't fit our util in any nice way, so just tweak it.
    expectedMethods.get(
        "genmocks.com.google.android.testing.mocking.ClassHasDelegateMethodsDelegateInterface")
        .add("getDelegate___AndroidMock");

    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasDelegateMethods.class);
    assertEquals(2, classes.size());
    assertAllMethodNames(expectedNames, expectedMethods, classes);
  }

  public void testClassHasFinalMethods() throws ClassNotFoundException, IOException,
      CannotCompileException {
    List<String> expectedNames = getExpectedNames(ClassHasFinalMethods.class);
    Map<String, List<String>> expectedMethods =
        getExpectedMethodsMap(expectedNames, ClassHasFinalMethods.class, new String[] {"foo",
            "foobar"});

    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasFinalMethods.class);
    assertEquals(2, classes.size());
    assertAllMethodNames(expectedNames, expectedMethods, classes);
  }

  public void testClassHasNoDefaultConstructor() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasNoDefaultConstructor.class);
    assertEquals(2, classes.size());
  }

  public void testClassHasNoPublicConstructors() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasNoPublicConstructors.class);
    assertEquals(0, classes.size());
  }

  public void testClassHasOverloadedMethods() throws ClassNotFoundException, IOException,
      CannotCompileException {
    List<String> expectedNames = getExpectedNames(ClassHasOverloadedMethods.class);
    Map<String, List<String>> expectedMethods =
        getExpectedMethodsMap(expectedNames, ClassHasOverloadedMethods.class);

    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasOverloadedMethods.class);
    assertEquals(2, classes.size());
    assertAllMethodNames(expectedNames, expectedMethods, classes);
  }

  public void testClassHasStaticMethods() throws ClassNotFoundException, IOException,
      CannotCompileException {
    List<String> expectedNames = getExpectedNames(ClassHasStaticMethods.class);
    Map<String, List<String>> expectedMethods =
        getExpectedMethodsMap(expectedNames, ClassHasStaticMethods.class,
            new String[] {"staticFoo"});

    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes =
        mockGenerator.createMocksForClass(ClassHasStaticMethods.class);
    assertEquals(2, classes.size());
    assertAllMethodNames(expectedNames, expectedMethods, classes);
  }

  public void testClassIsAnnotation() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(ClassIsAnnotation.class);
    assertEquals(0, classes.size());
  }

  public void testClassIsEnum() throws ClassNotFoundException, IOException, CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(ClassIsEnum.class);
    assertEquals(0, classes.size());
  }

  public void testClassIsFinal() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(ClassIsFinal.class);
    assertEquals(0, classes.size());
  }

  public void testClassIsInterface() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(ClassIsInterface.class);
    assertEquals(0, classes.size());
  }

  public void testClassIsArray() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(Object[].class);
    assertEquals(0, classes.size());
  }

  public void testClassIsNormal() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(Object.class);
    assertEquals(2, classes.size());
  }

  public void testClassIsPrimitive() throws ClassNotFoundException, IOException,
      CannotCompileException {
    AndroidMockGenerator mockGenerator = getAndroidMockGenerator();
    List<GeneratedClassFile> classes = mockGenerator.createMocksForClass(Integer.TYPE);
    assertEquals(0, classes.size());
  }
}