1 20 21 package net.innig.macker.recording; 22 23 import net.innig.macker.event.*; 24 import net.innig.macker.rule.Rule; 25 import net.innig.macker.rule.RuleSet; 26 27 import java.io.PrintWriter ; 28 import java.util.*; 29 30 import org.jdom.Element; 31 32 import net.innig.collect.CollectionDiff; 33 34 public class ForEachRecording 35 extends EventRecording 36 { 37 public ForEachRecording(EventRecording parent) 38 { 39 super(parent); 40 iterations = new TreeMap(); 41 } 42 43 public EventRecording record(MackerEvent event) 44 { 45 if(!(event instanceof ForEachEvent)) 46 return getParent().record(event); 47 48 if(event instanceof ForEachStarted) 49 var = ((ForEachEvent) event).getForEach().getVariableName(); 50 51 if(event instanceof ForEachIterationStarted) 52 { 53 String value = ((ForEachIterationStarted) event).getVariableValue(); 54 RuleSetRecording ruleSetRec = new RuleSetRecording(this); 55 iterations.put(value, ruleSetRec); 56 return ruleSetRec; 57 } 58 59 if(event instanceof ForEachFinished) 60 return getParent(); 61 62 return this; 63 } 64 65 public void read(Element elem) 66 { 67 var = elem.getAttributeValue("var"); 68 for(Iterator childIter = elem.getChildren("iteration").iterator(); childIter.hasNext(); ) 69 { 70 Element iterElem = (Element) childIter.next(); 71 72 String varValue = iterElem.getAttributeValue("value"); 73 EventRecording iter = new RuleSetRecording(this); 74 iter.read(iterElem); 75 iterations.put(varValue, iter); 76 } 77 } 78 79 public boolean compare(EventRecording actual, PrintWriter out) 80 { 81 if(!super.compare(actual, out)) 82 return false; 83 84 boolean match = true; 85 86 ForEachRecording actualForEach = (ForEachRecording) actual; 87 if(!var.equals(actualForEach.var)) 88 { 89 out.println("Expected " + this + ", but got " + actual); 90 match = false; 91 } 92 93 CollectionDiff diff = new CollectionDiff(iterations.keySet(), actualForEach.iterations.keySet()); 94 if(!diff.getRemoved().isEmpty()) 95 out.println(this + ": missing iterations: " + diff.getRemoved()); 96 if(!diff.getAdded().isEmpty()) 97 out.println(this + ": unexpected iterations: " + diff.getAdded()); 98 match = match && diff.getRemoved().isEmpty() && diff.getAdded().isEmpty(); 99 100 for(Iterator i = diff.getSame().iterator(); i.hasNext(); ) 101 { 102 String varValue = (String ) i.next(); 103 RuleSetRecording iterExpected = (RuleSetRecording) iterations.get(varValue); 105 RuleSetRecording iterActual = (RuleSetRecording) actualForEach.iterations.get(varValue); 106 match = iterExpected.compare(iterActual, out) && match; 107 } 108 return match; 109 } 110 111 public String toString() 112 { return "[foreach:" + var + "]"; } 113 114 public void dump(PrintWriter out, int indent) 115 { 116 super.dump(out, indent); 117 for(Iterator entryIter = iterations.entrySet().iterator(); entryIter.hasNext(); ) 118 { 119 Map.Entry entry = (Map.Entry) entryIter.next(); 120 for(int n = -3; n < indent; n++) 121 out.print(' '); 122 out.println("[iteration:" + entry.getKey() + "]"); 123 ((EventRecording) entry.getValue()).dump(out, indent+6); 124 } 125 } 126 127 private String var; 128 private Map iterations; 129 } 130 131 132 133 | Popular Tags |