/test/kilim/test/Base.java

http://github.com/kilim/kilim · Java · 101 lines · 75 code · 11 blank · 15 comment · 15 complexity · 0dd5e2c9c39394e0fbec188b5e95c92b MD5 · raw file

  1. /* Copyright (c) 2006, Sriram Srinivasan
  2. *
  3. * You may distribute this software under the terms of the license
  4. * specified in the file "License"
  5. */
  6. package kilim.test;
  7. import java.util.ArrayList;
  8. import junit.framework.TestCase;
  9. import kilim.analysis.BasicBlock;
  10. import kilim.analysis.ClassFlow;
  11. import kilim.analysis.KilimContext;
  12. import kilim.analysis.MethodFlow;
  13. import kilim.mirrors.Detector;
  14. import org.objectweb.asm.tree.AbstractInsnNode;
  15. import org.objectweb.asm.tree.MethodInsnNode;
  16. public class Base extends TestCase {
  17. private static ArrayList<MethodFlow> stflows;
  18. private static String lastClassName = null;
  19. KilimContext context = KilimContext.DEFAULT;
  20. protected void cache(String className) throws Exception {
  21. if (lastClassName != className) {
  22. ClassFlow cf = new ClassFlow(
  23. context,
  24. ClassLoader.getSystemResourceAsStream(className.replace('.', '/')+".class"));
  25. stflows = cf.analyze(/* forceAnalysis = */true);
  26. lastClassName = className;
  27. }
  28. }
  29. protected MethodFlow getFlow(String methodName) {
  30. for (int i = 0; i < stflows.size(); i++) {
  31. MethodFlow flow = stflows.get(i);
  32. if (flow.name.equals(methodName)) {
  33. return flow;
  34. }
  35. }
  36. fail("No method called " + methodName);
  37. return null;
  38. }
  39. /**
  40. * Returns the first basic block in the flow that has a method invocation of
  41. * <methodName>
  42. */
  43. protected BasicBlock getBBForMethod(MethodFlow flow, String methodName) {
  44. for (BasicBlock bb : flow.getBasicBlocks()) {
  45. AbstractInsnNode ainode = bb.getInstruction(bb.startPos);
  46. if (ainode instanceof MethodInsnNode
  47. && ((MethodInsnNode) ainode).name.equals(methodName)) {
  48. return bb;
  49. }
  50. }
  51. fail("No method invocation found for " + methodName);
  52. return null;
  53. }
  54. protected ArrayList<MethodFlow> getFlows() {
  55. return stflows;
  56. }
  57. protected void checkCov(String methodName) {
  58. MethodFlow flow = getFlow(methodName);
  59. if (flow == null)
  60. return;
  61. ArrayList<BasicBlock> bbs = flow.getBasicBlocks();
  62. // Verify that all instructions are covered and that the only ones that
  63. // aren't are labelnodes. Also verify that there are no overlaps.
  64. int size = flow.instructions.size();
  65. boolean coverage[] = new boolean[size];
  66. for (int i = 0; i < size; i++) {
  67. coverage[i] = false;
  68. }
  69. for (BasicBlock bb : bbs) {
  70. /*
  71. * if (bb.startFrame == null) { fail("BB doesn't have a starting
  72. * frame"); return; }
  73. */
  74. int end = bb.endPos;
  75. for (int i = bb.startPos; i <= end; i++) {
  76. if (coverage[i]) {
  77. fail("BasicBlock overlap");
  78. return;
  79. }
  80. coverage[i] = true;
  81. }
  82. }
  83. for (int i = 0; i < size; i++) {
  84. if (!coverage[i]) {
  85. fail("Instruction " + i + " not covered");
  86. return;
  87. }
  88. }
  89. }
  90. }