PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/TestabilityVisitorTest.java

http://testability-explorer.googlecode.com/
Java | 238 lines | 189 code | 34 blank | 15 comment | 3 complexity | dca66375066de8067c3c502da8a73ae4 MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric;
 17
 18
 19import static java.util.Collections.EMPTY_LIST;
 20
 21import java.util.ArrayList;
 22import java.util.Arrays;
 23import java.util.List;
 24
 25import junit.framework.TestCase;
 26
 27import com.google.test.metric.TestabilityVisitor.CostRecordingFrame;
 28import com.google.test.metric.TestabilityVisitor.ParentFrame;
 29
 30public class TestabilityVisitorTest extends TestCase {
 31
 32  final List<Integer> cost1 = Arrays.asList(0);
 33  Variable instance = new Variable("instance", null, false, false);
 34  Variable finalInstance = new Variable("instance", null, true, false);
 35  FieldInfo field = new FieldInfo(null, "field", null, false, false, false);
 36  FieldInfo finalField = new FieldInfo(null, "field", null, true, false, false);
 37  FieldInfo finalStaticField = new FieldInfo(null, "field", null, true, true, false);
 38  LocalField localField = new LocalField(instance, field);
 39  LocalField localFinalField = new LocalField(instance, finalField);
 40  LocalField localStaticFinalField = new LocalField(null, finalStaticField);
 41  Variable dst = new Variable("dst", null, false, false);
 42  Variable dstField = new FieldInfo(null, "dstField", null, false, false, false);
 43  @SuppressWarnings("unchecked")
 44  ClassInfo classInfo = new ClassInfo("c.g.t.A", false, null, EMPTY_LIST, null);
 45  MethodInfo method =
 46      new MethodInfo(classInfo, "void method()", 0, null, null, null, null, null, false, false, cost1);
 47
 48  private final JavaClassRepository repo = new JavaClassRepository();
 49  private final VariableState globalVariables = new VariableState();
 50
 51  TestabilityVisitor visitor =
 52    new TestabilityVisitor(repo, globalVariables, null, new RegExpWhiteList());
 53  TestabilityVisitor.CostRecordingFrame frame = visitor.createFrame(method, 1);
 54  ParentFrame parentFrame = frame.getParentFrame();
 55
 56  private String method(String string, Class<?> clazz) {
 57    return "void execute("+clazz.getName()+")";
 58  }
 59
 60  public void testIsInjectable() throws Exception {
 61    Variable var = new Variable("", JavaType.fromJava("X"), false, false);
 62    assertFalse(globalVariables.isInjectable(var));
 63    globalVariables.setInjectable(var);
 64    assertTrue(globalVariables.isInjectable(var));
 65  }
 66
 67  public void testNoop() throws Exception {
 68    frame.assignParameter(1, dst, parentFrame, instance);
 69    assertFalse(globalVariables.isGlobal(dst));
 70    assertFalse(globalVariables.isInjectable(dst));
 71    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
 72  }
 73
 74  public void testInjectability() throws Exception {
 75    globalVariables.setInjectable(instance);
 76    frame.assignParameter(1, dst, parentFrame, instance);
 77    assertFalse(globalVariables.isGlobal(dst));
 78    assertTrue(frame.getVariableState().isInjectable(dst));
 79    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
 80  }
 81
 82  public void testFieldReadNoop() throws Exception {
 83    frame.assignParameter(1, dst, parentFrame, localField);
 84    assertFalse(globalVariables.isGlobal(dst));
 85    assertFalse(globalVariables.isInjectable(dst));
 86    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
 87  }
 88
 89  public void testFieldReadInjectableInstance() throws Exception {
 90    globalVariables.setInjectable(instance);
 91    frame.assignParameter(1, dst, parentFrame, localField);
 92    assertFalse(globalVariables.isGlobal(dst));
 93    assertFalse(globalVariables.isInjectable(dst));
 94    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
 95  }
 96
 97  public void testFieldReadInjectableField() throws Exception {
 98    globalVariables.setInjectable(field);
 99    frame.assignParameter(1, dst, parentFrame, localField);
100    assertFalse(globalVariables.isGlobal(dst));
101    assertTrue(frame.getVariableState().isInjectable(dst));
102    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
103  }
104
105  public void testFieldReadGlobalInstance() throws Exception {
106    globalVariables.setGlobal(instance);
107    frame.assignParameter(1, dstField, parentFrame, localField);
108    assertTrue(globalVariables.isGlobal(dstField));
109    assertFalse(globalVariables.isInjectable(dstField));
110    assertEquals(1, frame.getMethodCost().getTotalCost().getGlobalCost());
111  }
112
113  public void testFinalFieldReadGlobalInstance() throws Exception {
114    globalVariables.setGlobal(instance);
115    frame.assignParameter(1, dstField, parentFrame, localFinalField);
116    assertTrue(globalVariables.isGlobal(dstField));
117    assertFalse(globalVariables.isInjectable(dstField));
118    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
119  }
120
121  public void testReadFinalStaticField() throws Exception {
122    frame.assignParameter(1, dstField, parentFrame, localStaticFinalField);
123    assertTrue(globalVariables.isGlobal(dstField));
124    assertFalse(globalVariables.isInjectable(dstField));
125    assertEquals(0, frame.getMethodCost().getTotalCost().getGlobalCost());
126  }
127
128  private static class LoDExample {
129
130    Object conforming;
131    Object violator;
132    Object transitiveViolator;
133
134    public void assign(Object in) {
135      conforming = in;
136      violator = in.getClass();
137      transitiveViolator = violator;
138    }
139  }
140
141  public void testLoDExample() throws Exception {
142    ClassInfo clazz = repo.getClass(LoDExample.class.getCanonicalName());
143    MethodInfo methodInfo = clazz.getMethod("void assign(java.lang.Object)");
144    CostRecordingFrame frame = visitor.createFrame(methodInfo, 1);
145    frame.applyMethodOperations();
146    assertEquals(0, frame.getLoDCount(clazz.getField("conforming")));
147    assertEquals(1, frame.getLoDCount(clazz.getField("violator")));
148    assertEquals(1, frame.getLoDCount(clazz.getField("transitiveViolator")));
149  }
150
151  private static class LoDMultipleSameInvocations {
152    Obj plus2;
153    public void execute(Obj plus0) {
154      Obj plus1 = plus0.getValueA();
155      plus2 = plus1.getValueA();
156      plus2 = plus1;
157    }
158  }
159
160  public void testLoDMultipleSameInvocations() throws Exception {
161    ClassInfo clazz = repo.getClass(LoDMultipleSameInvocations.class.getCanonicalName());
162    MethodInfo methodInfo = clazz.getMethod(method("execute", Obj.class));
163    CostRecordingFrame frame = visitor.createFrame(methodInfo, 1);
164    frame.applyMethodOperations();
165    assertEquals(2, frame.getLoDCount(clazz.getField("plus2")));
166  }
167
168  private static class LoDMultipleDifferentInvocations {
169    Obj plus2;
170    public void execute(Obj plus0) {
171      Obj plus1 = plus0.getValueA();
172      plus2 = plus1.getValueB();
173      plus2 = plus1;
174    }
175  }
176
177  public void testLoDMultipleDifferentInvocations() throws Exception {
178    ClassInfo clazz = repo.getClass(LoDMultipleDifferentInvocations.class.getCanonicalName());
179    MethodInfo methodInfo = clazz.getMethod(method("execute", Obj.class));
180    CostRecordingFrame frame = visitor.createFrame(methodInfo, 1);
181    frame.applyMethodOperations();
182    assertEquals(2, frame.getLoDCount(clazz.getField("plus2")));
183    MethodCost methodCost = frame.getMethodCost();
184    List<LoDViolation> costSources = filterLoD(methodCost.getViolationCosts());
185    assertEquals(1, costSources.size());
186    LoDViolation violation = costSources.get(0);
187    assertTrue(violation.getDescription().contains("getValueB()"));
188    assertTrue(violation.getDescription().contains("[distance=2]"));
189  }
190
191  private List<LoDViolation> filterLoD(List<? extends ViolationCost> violations) {
192    List<LoDViolation> lods = new ArrayList<LoDViolation>();
193    for (ViolationCost violation : violations) {
194      if (violation instanceof LoDViolation) {
195        lods.add((LoDViolation) violation);
196      }
197    }
198    return lods;
199  }
200
201  private static class MultipleInjectability {
202    private Obj injectable1;
203    private Obj injectable2;
204    public void execute(Obj injectable) {
205      injectable1 = injectable.getValueA();
206      injectable2 = injectable.getValueA();
207    }
208    public boolean useFields() {
209      return injectable1 == injectable2;
210    }
211  }
212
213  public void testMultipleInjectability() throws Exception {
214    ClassInfo clazz = repo.getClass(MultipleInjectability.class.getCanonicalName());
215    MethodInfo methodInfo = clazz.getMethod(method("execute", Obj.class));
216    globalVariables.setInjectable(methodInfo.getParameters().get(0));
217    visitor.createFrame(methodInfo, 1).applyMethodOperations();
218    assertTrue(frame.getVariableState().isInjectable(clazz.getField("injectable1")));
219    assertTrue(frame.getVariableState().isInjectable(clazz.getField("injectable2")));
220  }
221
222  private static class LoDStaticCall {
223    Obj plus1;
224    public void execute() {
225      plus1 = Obj.getStaticValueA();
226    }
227  }
228
229  public void testLoDStaticCall() throws Exception {
230    ClassInfo clazz = repo.getClass(LoDStaticCall.class.getCanonicalName());
231    MethodInfo methodInfo = clazz.getMethod("void execute()");
232    CostRecordingFrame frame = visitor.createFrame(methodInfo, 1);
233    frame.applyMethodOperations();
234    assertEquals(1, frame.getLoDCount(clazz.getField("plus1")));
235  }
236
237}
238