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.sampler;
020:
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.Comparator;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Properties;
027: import java.util.Set;
028:
029: import org.apache.jmeter.samplers.AbstractSampler;
030: import org.apache.jmeter.samplers.Entry;
031: import org.apache.jmeter.samplers.SampleResult;
032: import org.apache.jmeter.testbeans.TestBean;
033: import org.apache.jmeter.threads.JMeterContextService;
034: import org.apache.jmeter.threads.JMeterVariables;
035: import org.apache.jmeter.util.JMeterUtils;
036:
037: /**
038: * This TestBean is just an example about how to write testbeans. The intent is
039: * to demonstrate usage of the TestBean features to podential TestBean
040: * developers. Note that only the class's introspector view matters: the methods
041: * do nothing -- nothing useful, in any case.
042: */
043: public class DebugSampler extends AbstractSampler implements TestBean {
044:
045: private boolean displayJMeterVariables;
046:
047: private boolean displayJMeterProperties;
048:
049: private boolean displaySystemProperties;
050:
051: public SampleResult sample(Entry e) {
052: SampleResult res = new SampleResult();
053: res.setSampleLabel(getName());
054: res.sampleStart();
055: StringBuffer sb = new StringBuffer(100);
056: StringBuffer rd = new StringBuffer(20); // for request Data
057: if (isDisplayJMeterVariables()) {
058: rd.append("JMeterVariables\n");
059: sb.append("JMeterVariables:\n");
060: JMeterVariables vars = JMeterContextService.getContext()
061: .getVariables();
062: Iterator i = vars.getIterator();
063: while (i.hasNext()) {
064: Map.Entry me = (Map.Entry) i.next();
065: if (String.class.equals(me.getValue().getClass())) {
066: sb.append(me.toString()).append("\n");
067: }
068: }
069: sb.append("\n");
070: }
071:
072: if (isDisplayJMeterProperties()) {
073: rd.append("JMeterProperties\n");
074: sb.append("JMeterProperties:\n");
075: formatProperties(sb, JMeterUtils.getJMeterProperties());
076: sb.append("\n");
077: }
078:
079: if (isDisplaySystemProperties()) {
080: rd.append("SystemProperties\n");
081: sb.append("SystemProperties:\n");
082: formatProperties(sb, System.getProperties());
083: sb.append("\n");
084: }
085:
086: res.setResponseData(sb.toString().getBytes());
087: res.setDataType(SampleResult.TEXT);
088: res.setSamplerData(rd.toString());
089: res.setSuccessful(true);
090: res.sampleEnd();
091: return res;
092: }
093:
094: private void formatProperties(StringBuffer sb, Properties p) {
095: Set s = p.entrySet();
096: ArrayList al = new ArrayList(s);
097: Collections.sort(al, new Comparator() {
098: public int compare(Object o1, Object o2) {
099: String m1, m2;
100: m1 = (String) ((Map.Entry) o1).getKey();
101: m2 = (String) ((Map.Entry) o2).getKey();
102: return m1.compareTo(m2);
103: }
104: });
105: Iterator i = al.iterator();
106: while (i.hasNext()) {
107: Map.Entry me = (Map.Entry) i.next();
108: sb.append(me.getKey());
109: sb.append("=");
110: sb.append(me.getValue());
111: sb.append("\n");
112: }
113: }
114:
115: public boolean isDisplayJMeterVariables() {
116: return displayJMeterVariables;
117: }
118:
119: public void setDisplayJMeterVariables(boolean displayJMeterVariables) {
120: this .displayJMeterVariables = displayJMeterVariables;
121: }
122:
123: public boolean isDisplayJMeterProperties() {
124: return displayJMeterProperties;
125: }
126:
127: public void setDisplayJMeterProperties(
128: boolean displayJMeterPropterties) {
129: this .displayJMeterProperties = displayJMeterPropterties;
130: }
131:
132: public boolean isDisplaySystemProperties() {
133: return displaySystemProperties;
134: }
135:
136: public void setDisplaySystemProperties(
137: boolean displaySystemProperties) {
138: this.displaySystemProperties = displaySystemProperties;
139: }
140: }
|