View | Details | Raw Unified | Return to bug 47907
Collapse All | Expand All

(-)/src/components/org/apache/jmeter/assertions/CompareAssertion.java (-70 / +90 lines)
Lines 20-28 Link Here
20
20
21
import java.io.Serializable;
21
import java.io.Serializable;
22
import java.util.Collection;
22
import java.util.Collection;
23
import java.util.Iterator;
24
import java.util.LinkedList;
23
import java.util.LinkedList;
25
import java.util.List;
26
24
27
import org.apache.jmeter.engine.event.LoopIterationEvent;
25
import org.apache.jmeter.engine.event.LoopIterationEvent;
28
import org.apache.jmeter.engine.event.LoopIterationListener;
26
import org.apache.jmeter.engine.event.LoopIterationListener;
Lines 38-44 Link Here
38
36
39
    private static final long serialVersionUID = 240L;
37
    private static final long serialVersionUID = 240L;
40
    
38
    
41
	private transient List<SampleResult> responses;
39
    private static final String NL = "\n"; // $NON-NLS-1$
40
    
41
    private static final String NL2 = "\n\n"; // $NON-NLS-1$
42
43
	private transient LinkedList<SampleResult> responses;
42
44
43
	private transient final StringSubstitution emptySub = new StringSubstitution("");
45
	private transient final StringSubstitution emptySub = new StringSubstitution("");
44
46
Lines 53-131 Link Here
53
	}
55
	}
54
56
55
	public AssertionResult getResult(SampleResult response) {
57
	public AssertionResult getResult(SampleResult response) {
56
		responses.add(response);
58
		responses.addLast(response);
57
		if (responses.size() > 1) {
59
		if (responses.size() > 1) {
58
			CompareAssertionResult result = new CompareAssertionResult(getName());
60
			CompareAssertionResult result = new CompareAssertionResult(getName());
59
			compareContent(result);
61
			if (compareTime >= 0) {
60
			compareTime(result);
62
			    compareTime(result);
63
            }
64
			if (compareContent) {
65
                compareContent(result);
66
            }
61
			return result;
67
			return result;
62
		} else
68
		} else {
63
			return new AssertionResult(getName());
69
		    CompareAssertionResult assRes = new CompareAssertionResult(getName());
70
		    assRes.setFailure(false);
71
		    assRes.addToBaseResult("No previous");
72
73
	        SampleResult firstResult = responses.getFirst();
74
	        long prevTime = responses.getFirst().getTime();
75
	        StringBuffer buf = new StringBuffer();
76
	        appendResultDetails(buf, firstResult);
77
	        buf.append("Response Time (ms): ").append(prevTime + NL2);
78
	        buf.append(filterString(firstResult.getResponseDataAsString()));
79
	        assRes.addToSecondaryResult(buf.toString());
80
		    
81
			return assRes;
82
		}
64
	}
83
	}
65
84
66
	private void compareTime(CompareAssertionResult result) {
85
	private void compareTime(CompareAssertionResult result) {
67
		if (compareTime >= 0) {
86
        SampleResult prevResult = responses.getFirst();
68
			Iterator<SampleResult> iter = responses.iterator();
87
        long prevTime = prevResult.getTime();
69
			long prevTime = -1;
88
        StringBuffer buf = new StringBuffer();
70
			SampleResult prevResult = null;
89
        appendResultDetails(buf, prevResult);
71
			boolean success = true;
90
        buf.append("Response Time (ms): ").append(prevTime);
72
			while (iter.hasNext()) {
91
        result.addToBaseResult(buf.toString());
73
				SampleResult sResult = iter.next();
92
        
74
				long currentTime = sResult.getTime();
93
        boolean success = true;
75
				if (prevTime != -1) {
94
        SampleResult sResult = responses.getLast();
76
					success = Math.abs(prevTime - currentTime) <= compareTime;
95
        if (sResult != null) {
77
					prevResult = sResult;
96
            long currentTime = sResult.getTime();
78
				}
97
            success = Math.abs(prevTime - currentTime) <= compareTime;
79
				if (!success) {
98
            if (!success) {
80
					result.setFailure(true);
99
                result.setFailure(true);
81
                    StringBuffer buf = new StringBuffer();
100
                buf = new StringBuffer();
82
                    appendResultDetails(buf, prevResult);
101
                appendResultDetails(buf, sResult);
83
                    buf.append("Response Time: ").append(prevTime);
102
                buf.append("Response Time (ms): ").append(currentTime);
84
					result.addToBaseResult(buf.toString());
103
                result.addToSecondaryResult(buf.toString());
85
					buf = new StringBuffer();
104
                result.setFailureMessage("Responses differ in response time by more than "
86
                    appendResultDetails(buf, sResult);
105
                                + compareTime + " ms");
87
					buf.append("Response Time: ").append(currentTime);
106
            } else {
88
					result.addToSecondaryResult(buf.toString());
107
                buf = new StringBuffer();
89
					result.setFailureMessage("Responses differ in response time by more than "+compareTime+" ms");
108
                buf.append("Compare time less than " + compareTime + " ms" + NL);
90
					break;
109
                buf.append(NL2 + "Response Time (ms): ").append(currentTime);
91
				}
110
                result.addToSecondaryResult(buf.toString());
92
				prevResult = sResult;
111
            }
93
				prevTime = currentTime;
112
        }
94
			}
113
    }
95
		}
96
	}
97
114
98
    private void compareContent(CompareAssertionResult result) {
115
    private void compareContent(CompareAssertionResult result) {
99
		if (compareContent) {
116
        SampleResult prevResult = responses.getFirst();
100
			Iterator<SampleResult> iter = responses.iterator();
117
        String prevContent = filterString(prevResult.getResponseDataAsString());
101
			String prevContent = null;
118
        StringBuffer buf = new StringBuffer();
102
			SampleResult prevResult = null;
119
        // no append if compare time already pass
103
			boolean success = true;
120
        if (compareTime < 0) {
104
			while (iter.hasNext()) {
121
            appendResultDetails(buf, prevResult);
105
				SampleResult sResult = iter.next();
122
        } else {
106
				String currentContent = sResult.getResponseDataAsString();
123
            buf.append(NL2);
107
				currentContent = filterString(currentContent);
124
        }
108
				if (prevContent != null) {
125
        buf.append(prevContent);
109
					success = prevContent.equals(currentContent);
126
        result.addToBaseResult(buf.toString());
110
				}
127
        
111
				if (!success) {
128
        SampleResult sResult = responses.getLast();
112
					result.setFailure(true);
129
        if (sResult != null) {
113
                    StringBuffer buf = new StringBuffer();
130
            String currentContent = sResult.getResponseDataAsString();
114
                    appendResultDetails(buf, prevResult);
131
            currentContent = filterString(currentContent);
115
					buf.append(prevContent);
132
            if (!prevContent.equals(currentContent)) {
116
					result.addToBaseResult(buf.toString());
133
                result.setFailure(true);
117
					buf = new StringBuffer();
134
                buf = new StringBuffer();
118
					appendResultDetails(buf, sResult);
135
                // no append if compare time already pass
119
					buf.append(currentContent);
136
                if (compareTime < 0) {
120
					result.addToSecondaryResult(buf.toString());
137
                    appendResultDetails(buf, sResult);
121
					result.setFailureMessage("Responses differ in content");
138
                } else {
122
					break;
139
                    buf.append(NL2);
123
				}
140
                }
124
				prevResult = sResult;
141
                buf.append(currentContent);
125
				prevContent = currentContent;
142
                result.addToSecondaryResult(buf.toString());
126
			}
143
                result.setFailureMessage("Responses differ in content");
127
		}
144
            } else {
128
	}
145
                result.addToSecondaryResult("Same content");
146
            }
147
        }
148
    }
129
149
130
    private void appendResultDetails(StringBuffer buf, SampleResult result) {
150
    private void appendResultDetails(StringBuffer buf, SampleResult result) {
131
        final String samplerData = result.getSamplerData();
151
        final String samplerData = result.getSamplerData();
Lines 132-138 Link Here
132
        if (samplerData != null){
152
        if (samplerData != null){
133
            buf.append(samplerData.trim());
153
            buf.append(samplerData.trim());
134
        }
154
        }
135
        buf.append("\n");
155
        buf.append(NL);
136
        final String requestHeaders = result.getRequestHeaders();
156
        final String requestHeaders = result.getRequestHeaders();
137
        if (requestHeaders != null){
157
        if (requestHeaders != null){
138
            buf.append(requestHeaders);
158
            buf.append(requestHeaders);
Lines 137-143 Link Here
137
        if (requestHeaders != null){
157
        if (requestHeaders != null){
138
            buf.append(requestHeaders);
158
            buf.append(requestHeaders);
139
        }
159
        }
140
        buf.append("\n\n");        
160
        buf.append(NL2);
141
    }
161
    }
142
162
143
	private String filterString(String content) {
163
	private String filterString(String content) {
(-)/src/components/org/apache/jmeter/visualizers/ComparisonVisualizer.java (-17 / +25 lines)
Lines 40-48 Link Here
40
import org.apache.jmeter.samplers.Clearable;
40
import org.apache.jmeter.samplers.Clearable;
41
import org.apache.jmeter.samplers.SampleResult;
41
import org.apache.jmeter.samplers.SampleResult;
42
import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
42
import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
43
import org.apache.jorphan.logging.LoggingManager;
44
import org.apache.log.Logger;
43
45
44
public class ComparisonVisualizer extends AbstractVisualizer implements Clearable {
46
public class ComparisonVisualizer extends AbstractVisualizer implements Clearable {
45
	private JTree resultsTree;
47
	
48
    private static final long serialVersionUID = 240L;
49
50
    private JTree resultsTree;
46
51
47
	private DefaultTreeModel treeModel;
52
	private DefaultTreeModel treeModel;
48
53
Lines 65-71 Link Here
65
	}
70
	}
66
71
67
	public String getLabelResource() {
72
	public String getLabelResource() {
68
		return "comparison_visualizer_title";
73
		return "comparison_visualizer_title"; // $NON-NLS-1$
69
	}
74
	}
70
75
71
	private void init() {
76
	private void init() {
Lines 91-111 Link Here
91
	}
96
	}
92
97
93
	private JTextPane getBaseTextPane() {
98
	private JTextPane getBaseTextPane() {
94
		base = new JTextPane();
99
        base = new JTextPane();
95
		return base;
100
        base.setEditable(false);
96
	}
101
        base.setBackground(getBackground());
102
        return base;
103
    }
97
104
98
	private JTextPane getSecondaryTextPane() {
105
	private JTextPane getSecondaryTextPane() {
99
		secondary = new JTextPane();
106
        secondary = new JTextPane();
100
		return secondary;
107
        secondary.setEditable(false);
101
	}
108
        return secondary;
109
    }
102
110
103
	private JComponent getTreePanel() {
111
	private JComponent getTreePanel() {
104
		root = new DefaultMutableTreeNode("Root");
112
		root = new DefaultMutableTreeNode("Root"); // $NON-NLS-1$
105
		treeModel = new DefaultTreeModel(root);
113
		treeModel = new DefaultTreeModel(root);
106
		resultsTree = new JTree(treeModel);
114
		resultsTree = new JTree(treeModel);
107
		resultsTree.setCellRenderer(new TreeNodeRenderer());
115
        resultsTree.setCellRenderer(new TreeNodeRenderer());
108
		resultsTree.setCellRenderer(new TreeNodeRenderer());
109
		resultsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
116
		resultsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
110
		resultsTree.addTreeSelectionListener(new Selector());
117
		resultsTree.addTreeSelectionListener(new Selector());
111
		resultsTree.setRootVisible(false);
118
		resultsTree.setRootVisible(false);
Lines 130-143 Link Here
130
				SampleResult sr = (SampleResult) node.getUserObject();
137
				SampleResult sr = (SampleResult) node.getUserObject();
131
				AssertionResult[] results = sr.getAssertionResults();
138
				AssertionResult[] results = sr.getAssertionResults();
132
				CompareAssertionResult result = null;
139
				CompareAssertionResult result = null;
133
				for (AssertionResult r : results) {
140
				for (AssertionResult assRes : results) {
134
					if (r instanceof CompareAssertionResult) {
141
					if (assRes instanceof CompareAssertionResult) {
135
						result = (CompareAssertionResult) r;
142
						result = (CompareAssertionResult) assRes;
136
						break;
143
						break;
137
					}
144
					}
138
				}
145
				}
139
				if (result == null)
146
				if (result == null) {
140
					result = new CompareAssertionResult();
147
					result = new CompareAssertionResult();
148
				}
141
				base.setText(result.getBaseResult());
149
				base.setText(result.getBaseResult());
142
				secondary.setText(result.getSecondaryResult());
150
				secondary.setText(result.getSecondaryResult());
143
			} catch (Exception err) {
151
			} catch (Exception err) {
Lines 155-162 Link Here
155
			// the child to be removed will always be 0 'cos as the nodes are
163
			// the child to be removed will always be 0 'cos as the nodes are
156
			// removed the nth node will become (n-1)th
164
			// removed the nth node will become (n-1)th
157
			treeModel.removeNodeFromParent((DefaultMutableTreeNode) root.getChildAt(0));
165
			treeModel.removeNodeFromParent((DefaultMutableTreeNode) root.getChildAt(0));
158
			base.setText("");
166
			base.setText(""); // $NON-NLS-1$
159
			secondary.setText("");
167
			secondary.setText(""); // $NON-NLS-1$
160
		}
168
		}
161
	}
169
	}
162
170
(-)/src/components/org/apache/jmeter/visualizers/TreeNodeRenderer.java (-5 / +20 lines)
Lines 21-26 Link Here
21
import java.awt.Color;
21
import java.awt.Color;
22
import java.awt.Component;
22
import java.awt.Component;
23
23
24
import javax.swing.ImageIcon;
24
import javax.swing.JTree;
25
import javax.swing.JTree;
25
import javax.swing.tree.DefaultMutableTreeNode;
26
import javax.swing.tree.DefaultMutableTreeNode;
26
import javax.swing.tree.DefaultTreeCellRenderer;
27
import javax.swing.tree.DefaultTreeCellRenderer;
Lines 26-31 Link Here
26
import javax.swing.tree.DefaultTreeCellRenderer;
27
import javax.swing.tree.DefaultTreeCellRenderer;
27
28
28
import org.apache.jmeter.samplers.SampleResult;
29
import org.apache.jmeter.samplers.SampleResult;
30
import org.apache.jmeter.util.JMeterUtils;
29
31
30
/**
32
/**
31
 * Tree cell renderer used by ComparisonVisualizer.
33
 * Tree cell renderer used by ComparisonVisualizer.
Lines 32-37 Link Here
32
 */
34
 */
33
public class TreeNodeRenderer extends DefaultTreeCellRenderer {
35
public class TreeNodeRenderer extends DefaultTreeCellRenderer {
34
36
37
    private static final long serialVersionUID = 240L;
38
39
    // Same ViewResultsTree
40
    private static final ImageIcon imageSuccess = JMeterUtils.getImage(
41
            JMeterUtils.getPropDefault("viewResultsTree.success",  //$NON-NLS-1$
42
                    "icon_success_sml.gif")); //$NON-NLS-1$
43
44
    private static final ImageIcon imageFailure = JMeterUtils.getImage(
45
            JMeterUtils.getPropDefault("viewResultsTree.failure",  //$NON-NLS-1$
46
                    "icon_warning_sml.gif")); //$NON-NLS-1$
47
35
	public TreeNodeRenderer() {
48
	public TreeNodeRenderer() {
36
		super();
49
		super();
37
	}
50
	}
Lines 41-51 Link Here
41
			boolean leaf, int row, boolean focus) {
54
			boolean leaf, int row, boolean focus) {
42
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, focus);
55
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, focus);
43
		Object obj = ((DefaultMutableTreeNode) value).getUserObject();
56
		Object obj = ((DefaultMutableTreeNode) value).getUserObject();
44
		if(obj instanceof SampleResult)
57
		if (obj instanceof SampleResult) {
45
		{
58
            if (!((SampleResult) obj).isSuccessful()) {
46
			if (!((SampleResult) obj).isSuccessful()) {
59
                this.setForeground(Color.red);
47
				this.setForeground(Color.red);
60
                this.setIcon(imageFailure);
48
			}
61
            } else {
62
                this.setIcon(imageSuccess);
63
            }
49
		}
64
		}
50
		return this;
65
		return this;
51
	}
66
	}
(-)/src/core/org/apache/jmeter/assertions/CompareAssertionResult.java (-2 / +2 lines)
Lines 75-81 Link Here
75
			}
75
			}
76
			else
76
			else
77
			{
77
			{
78
				baseResult = baseResult + "\n\n" + r;
78
				baseResult = baseResult + "\n\n" + r; // $NON-NLS-1$
79
			}
79
			}
80
		}
80
		}
81
		
81
		
Lines 87-93 Link Here
87
			}
87
			}
88
			else
88
			else
89
			{
89
			{
90
				secondaryResult = secondaryResult + "\n\n" + r;
90
				secondaryResult = secondaryResult + "\n\n" + r; // $NON-NLS-1$
91
			}
91
			}
92
		}
92
		}
93
	}
93
	}
(-)/xdocs/usermanual/component_reference.xml (-2 / +2 lines)
Lines 3413-3420 Link Here
3413
<properties>
3413
<properties>
3414
    <property name="Name" required="No">Descriptive name for this element that is shown in the tree.</property>
3414
    <property name="Name" required="No">Descriptive name for this element that is shown in the tree.</property>
3415
    <property name="Compare Content" required="Yes">Whether or not to compare the content (response data)</property>
3415
    <property name="Compare Content" required="Yes">Whether or not to compare the content (response data)</property>
3416
    <property name="Compare Time" required="Yes">If the value is >=0, then check if the time difference is no greater than the value. 
3416
    <property name="Compare Time" required="Yes">If the value is >=0, then check if the response time difference is no greater than the value. 
3417
    I.e. if the value is 0, then the times must be exactly equal.</property>
3417
    I.e. if the value is 0, then the response times must be exactly equal.</property>
3418
    <property name="Comparison Filters" required="No">Filters can be used to remove strings from the content comparison.
3418
    <property name="Comparison Filters" required="No">Filters can be used to remove strings from the content comparison.
3419
    For example, if the page has a time-stamp, it might be matched with: "Time: \d\d:\d\d:\d\d" and replaced with a dummy fixed time "Time: HH:MM:SS".
3419
    For example, if the page has a time-stamp, it might be matched with: "Time: \d\d:\d\d:\d\d" and replaced with a dummy fixed time "Time: HH:MM:SS".
3420
    </property>
3420
    </property>

Return to bug 47907