KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > vaj > SourceVerifier


1 package com.mockobjects.vaj;
2 import java.util.*;
3 import com.mockobjects.*;
4 import junit.framework.Assert;/**
5  * Test Object to verify method source fragments
6  */

7 public class SourceVerifier implements Verifiable {
8     private ExpectationSet myClassMethods = new ExpectationSet("Expected methods to save");
9     private HashMap myClassAndMethods = new HashMap();
10
11
12
13     public SourceVerifier() {
14         super();
15     }
16
17
18
19     private void addClassAndMethod(String JavaDoc className, String JavaDoc methodName) {
20         getClassMethods(className).add(methodName);
21     }
22
23
24
25     public void addExpectedClass(String JavaDoc className) {
26         addExpectedMethod(className, " class " + className);
27     }
28
29
30
31     private void addExpectedMethod(String JavaDoc className, String JavaDoc methodName) {
32         addClassAndMethod(className, methodName);
33         myClassMethods.addExpected(makeUniqueName(className, methodName));
34     }
35
36
37
38     public void addExpectedMethods(String JavaDoc className, String JavaDoc[] methodNames) {
39         for (int i = 0; i < methodNames.length; i++) {
40             addExpectedMethod(className, methodNames[i]);
41         }
42     }
43
44
45
46     private Set getClassMethods(String JavaDoc className) {
47         Set methodSet = (Set)myClassAndMethods.get(className);
48         if (null == methodSet) {
49             methodSet = new HashSet();
50             myClassAndMethods.put(className, methodSet);
51         }
52         return methodSet;
53     }
54
55
56
57     private String JavaDoc makeUniqueName(String JavaDoc className, String JavaDoc methodName) {
58         return className + "/" + methodName;
59     }
60
61
62
63     public void setSource(String JavaDoc sourceClass, String JavaDoc source) {
64         Iterator methodNames = getClassMethods(sourceClass).iterator();
65
66         while (methodNames.hasNext()) {
67             String JavaDoc methodName = (String JavaDoc)methodNames.next();
68             if (source.indexOf(methodName) != -1) {
69                 myClassMethods.addActual(makeUniqueName(sourceClass, methodName));
70                 return;
71             }
72         }
73
74         Assert.fail("Did not find method matching source:\n " + source);
75     }
76
77
78
79     public void verify() {
80         myClassMethods.verify();
81     }
82 }
Popular Tags