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

http://testability-explorer.googlecode.com/ · Java · 55 lines · 33 code · 7 blank · 15 comment · 13 complexity · 02f02902ed265e8df19ef3809013a0e6 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. */
  16. package com.google.test.metric;
  17. import junit.framework.Assert;
  18. public class InjectabilityVerifier {
  19. private String errors = "";
  20. public void assertInjectable(ClassInfo classInfo, VariableState globalVariables) {
  21. for (Variable field : classInfo.getFields()) {
  22. verify(field, globalVariables);
  23. }
  24. for (MethodInfo method : classInfo.getMethods()) {
  25. for (Variable param : method.getParameters()) {
  26. verify(param, globalVariables);
  27. }
  28. for (Variable param : method.getLocalVariables()) {
  29. verify(param, globalVariables);
  30. }
  31. }
  32. Assert.assertTrue(errors, errors.length() == 0);
  33. }
  34. public void verify(Variable variable, VariableState globalVariables) {
  35. if (variable.getName().equals("this")) {
  36. } else if (variable.getName().endsWith("_I")) {
  37. if (!globalVariables.isInjectable(variable)) {
  38. errors += "\n" + variable + " should be injectable";
  39. }
  40. } else if (variable.getName().endsWith("_NI")) {
  41. if (globalVariables.isInjectable(variable)) {
  42. errors += "\n" + variable + " should be non injectable";
  43. }
  44. } else {
  45. errors += "\n" + variable + " should end with '_I' or '_NI'.";
  46. }
  47. }
  48. }