1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.sampler; 20 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.Comparator; 24 import java.util.Iterator; 25 import java.util.Map; 26 import java.util.Set; 27 28 import org.apache.jmeter.samplers.AbstractSampler; 29 import org.apache.jmeter.samplers.Entry; 30 import org.apache.jmeter.samplers.SampleResult; 31 import org.apache.jmeter.testbeans.TestBean; 32 import org.apache.jmeter.threads.JMeterContextService; 33 import org.apache.jmeter.util.JMeterUtils; 34 35 /** 36 * The Debug Sampler can be used to "sample" JMeter variables, JMeter properties and System Properties. 37 * 38 */ 39 public class DebugSampler extends AbstractSampler implements TestBean { 40 41 private static final long serialVersionUID = 232L; 42 43 private boolean displayJMeterVariables; 44 45 private boolean displayJMeterProperties; 46 47 private boolean displaySystemProperties; 48 49 public SampleResult sample(Entry e) { 50 SampleResult res = new SampleResult(); 51 res.setSampleLabel(getName()); 52 res.sampleStart(); 53 StringBuffer sb = new StringBuffer(100); 54 StringBuffer rd = new StringBuffer(20); // for request Data 55 if (isDisplayJMeterVariables()){ 56 rd.append("JMeterVariables\n"); 57 sb.append("JMeterVariables:\n"); 58 formatSet(sb, JMeterContextService.getContext().getVariables().entrySet()); 59 sb.append("\n"); 60 } 61 62 if (isDisplayJMeterProperties()){ 63 rd.append("JMeterProperties\n"); 64 sb.append("JMeterProperties:\n"); 65 formatSet(sb, JMeterUtils.getJMeterProperties().entrySet()); 66 sb.append("\n"); 67 } 68 69 if (isDisplaySystemProperties()){ 70 rd.append("SystemProperties\n"); 71 sb.append("SystemProperties:\n"); 72 formatSet(sb, System.getProperties().entrySet()); 73 sb.append("\n"); 74 } 75 76 res.setResponseData(sb.toString().getBytes()); 77 res.setDataType(SampleResult.TEXT); 78 res.setSamplerData(rd.toString()); 79 res.setSuccessful(true); 80 res.sampleEnd(); 81 return res; 82 } 83 84 private void formatSet(StringBuffer sb, Set s) { 85 ArrayList al = new ArrayList(s); 86 Collections.sort(al, new Comparator(){ 87 public int compare(Object o1, Object o2) { 88 String m1,m2; 89 m1=(String)((Map.Entry)o1).getKey(); 90 m2=(String)((Map.Entry)o2).getKey(); 91 return m1.compareTo(m2); 92 } 93 }); 94 Iterator i = al.iterator(); 95 while(i.hasNext()){ 96 Map.Entry me = (Map.Entry) i.next(); 97 sb.append(me.getKey()); 98 sb.append("="); 99 sb.append(me.getValue()); 100 sb.append("\n"); 101 } 102 } 103 104 public boolean isDisplayJMeterVariables() { 105 return displayJMeterVariables; 106 } 107 108 public void setDisplayJMeterVariables(boolean displayJMeterVariables) { 109 this.displayJMeterVariables = displayJMeterVariables; 110 } 111 112 public boolean isDisplayJMeterProperties() { 113 return displayJMeterProperties; 114 } 115 116 public void setDisplayJMeterProperties(boolean displayJMeterPropterties) { 117 this.displayJMeterProperties = displayJMeterPropterties; 118 } 119 120 public boolean isDisplaySystemProperties() { 121 return displaySystemProperties; 122 } 123 124 public void setDisplaySystemProperties(boolean displaySystemProperties) { 125 this.displaySystemProperties = displaySystemProperties; 126 } 127 }