|
1 |
| package com.agical.rmock.core.describe.impl; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.PrintWriter; |
|
5 |
| import java.io.StringWriter; |
|
6 |
| import java.util.ArrayList; |
|
7 |
| import java.util.LinkedList; |
|
8 |
| import java.util.List; |
|
9 |
| |
|
10 |
| import com.agical.rmock.core.Expectation; |
|
11 |
| import com.agical.rmock.core.ExpectationVisitor; |
|
12 |
| import com.agical.rmock.core.Section; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| public class DocumentExpectationsVisitor implements ExpectationVisitor { |
|
21 |
| private StringWriter message = new StringWriter(); |
|
22 |
| private ExpectationDescriberImpl expectationDescriber = new ExpectationDescriberImpl(message); |
|
23 |
| private SectionDescriberImpl sectionDescriberImpl; |
|
24 |
| private List unsatisfiedExpectations = new ArrayList(); |
|
25 |
| private LinkedList sectionStack = new LinkedList(); |
|
26 |
| private final String indentString; |
|
27 |
| |
|
28 |
14
| public DocumentExpectationsVisitor(String indentString) {
|
|
29 |
14
| sectionDescriberImpl = new SectionDescriberImpl(message, indentString );
|
|
30 |
14
| this.indentString = indentString;
|
|
31 |
| } |
|
32 |
| |
|
33 |
18
| public boolean visit(Expectation expectation, boolean available) {
|
|
34 |
18
| boolean satisfied = expectation.getMultiplicity().isSatisfied();
|
|
35 |
18
| if( !satisfied ) {
|
|
36 |
14
| unsatisfiedExpectations.add( expectation );
|
|
37 |
| } |
|
38 |
18
| try {
|
|
39 |
18
| message.write(satisfied?" ":"->");
|
|
40 |
18
| if (!available && !satisfied) {
|
|
41 |
1
| message.write(" (N/A)");
|
|
42 |
| } else { |
|
43 |
17
| message.write(" ");
|
|
44 |
| } |
|
45 |
18
| for( int i = 0; i < sectionStack.size(); i++ ) {
|
|
46 |
26
| message.write(indentString);
|
|
47 |
| } |
|
48 |
18
| expectationDescriber.describe(expectation);
|
|
49 |
| |
|
50 |
18
| message.write('\n');
|
|
51 |
| } catch (IOException e) { |
|
52 |
0
| e.printStackTrace(new PrintWriter(message));
|
|
53 |
| } |
|
54 |
18
| return true;
|
|
55 |
| } |
|
56 |
| |
|
57 |
14
| public Expectation[] getMatchedExpectations() {
|
|
58 |
14
| return (Expectation[]) unsatisfiedExpectations.toArray(new Expectation[unsatisfiedExpectations.size()] );
|
|
59 |
| } |
|
60 |
| |
|
61 |
14
| public String getMessage() {
|
|
62 |
14
| return message.toString();
|
|
63 |
| } |
|
64 |
| |
|
65 |
22
| public void enteredSection(Section section) {
|
|
66 |
22
| sectionDescriberImpl.describeSectionEntered( section, sectionStack.size() );
|
|
67 |
22
| sectionStack.addLast( section );
|
|
68 |
| } |
|
69 |
| |
|
70 |
22
| public void exitedSection() {
|
|
71 |
22
| Section section = (Section) sectionStack.removeLast( );
|
|
72 |
22
| sectionDescriberImpl.describeSectionExited( section, sectionStack.size() );
|
|
73 |
| } |
|
74 |
| } |