001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.visualizers;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023:
024: import javax.swing.Box;
025: import javax.swing.JLabel;
026: import javax.swing.JScrollPane;
027: import javax.swing.JTextArea;
028: import javax.swing.border.Border;
029: import javax.swing.border.EmptyBorder;
030:
031: import org.apache.jmeter.assertions.AssertionResult;
032: import org.apache.jmeter.samplers.Clearable;
033: import org.apache.jmeter.samplers.SampleResult;
034: import org.apache.jmeter.util.JMeterUtils;
035: import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
036:
037: public class AssertionVisualizer extends AbstractVisualizer implements
038: Clearable {
039:
040: private JTextArea textArea;
041:
042: public AssertionVisualizer() {
043: init();
044: setName(getStaticLabel());
045: }
046:
047: public String getLabelResource() {
048: return "assertion_visualizer_title"; // $NON-NLS-1$
049: }
050:
051: public void add(SampleResult sample) {
052: StringBuffer sb = new StringBuffer(100);
053: sb.append(sample.getSampleLabel());
054: sb.append(getAssertionResult(sample));
055: sb.append("\n"); // $NON-NLS-1$
056: synchronized (textArea) {
057: textArea.append(sb.toString());
058: }
059: }
060:
061: public void clearData() {
062: textArea.setText(""); // $NON-NLS-1$
063: }
064:
065: private String getAssertionResult(SampleResult res) {
066: if (res != null) {
067: StringBuffer display = new StringBuffer();
068: AssertionResult assertionResults[] = res
069: .getAssertionResults();
070: for (int i = 0; i < assertionResults.length; i++) {
071: AssertionResult item = assertionResults[i];
072:
073: if (item.isFailure() || item.isError()) {
074: display.append("\n\t"); // $NON-NLS-1$
075: display.append(item.getName() != null ? item
076: .getName()
077: + " : " : "");// $NON-NLS-1$
078: display.append(item.getFailureMessage());
079: }
080: }
081: return display.toString();
082: }
083: return "";
084: }
085:
086: private void init() {
087: this .setLayout(new BorderLayout());
088:
089: // MAIN PANEL
090: Border margin = new EmptyBorder(10, 10, 5, 10);
091:
092: this .setBorder(margin);
093:
094: // NAME
095: this .add(makeTitlePanel(), BorderLayout.NORTH);
096:
097: // TEXTAREA LABEL
098: JLabel textAreaLabel = new JLabel(JMeterUtils
099: .getResString("assertion_textarea_label")); // $NON-NLS-1$
100: Box mainPanel = Box.createVerticalBox();
101: mainPanel.add(textAreaLabel);
102:
103: // TEXTAREA
104: textArea = new JTextArea();
105: textArea.setEditable(false);
106: textArea.setLineWrap(false);
107: JScrollPane areaScrollPane = new JScrollPane(textArea);
108:
109: areaScrollPane
110: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
111: areaScrollPane
112: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
113:
114: areaScrollPane.setPreferredSize(new Dimension(mainPanel
115: .getWidth(), mainPanel.getHeight()));
116: mainPanel.add(areaScrollPane);
117: this.add(mainPanel, BorderLayout.CENTER);
118: }
119: }
|