PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jmeter-2.5.1/test/src/org/apache/jmeter/engine/util/PackageTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 218 lines | 156 code | 28 blank | 34 comment | 0 complexity | 96f29ba776c3d618d866bfa31a77c295 MD5 | raw file
  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. * Created on Jul 25, 2003
  20. */
  21. package org.apache.jmeter.engine.util;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.apache.jmeter.junit.JMeterTestCase;
  25. import org.apache.jmeter.samplers.SampleResult;
  26. import org.apache.jmeter.testelement.property.JMeterProperty;
  27. import org.apache.jmeter.testelement.property.StringProperty;
  28. import org.apache.jmeter.threads.JMeterContext;
  29. import org.apache.jmeter.threads.JMeterContextService;
  30. import org.apache.jmeter.threads.JMeterVariables;
  31. /*
  32. * To run this test stand-alone, ensure that ApacheJMeter_functions.jar is on the classpath,
  33. * as it is needed to resolve the functions.
  34. */
  35. public class PackageTest extends JMeterTestCase {
  36. private Map<String, String> variables;
  37. private SampleResult result;
  38. private ReplaceStringWithFunctions transformer;
  39. public PackageTest(String arg0) {
  40. super(arg0);
  41. }
  42. private JMeterContext jmctx = null;
  43. @Override
  44. public void setUp() {
  45. jmctx = JMeterContextService.getContext();
  46. variables = new HashMap<String, String>();
  47. variables.put("my_regex", ".*");
  48. variables.put("server", "jakarta.apache.org");
  49. result = new SampleResult();
  50. result.setResponseData("<html>hello world</html> costs: $3.47,$5.67", null);
  51. transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
  52. jmctx.setVariables(new JMeterVariables());
  53. jmctx.setSamplingStarted(true);
  54. jmctx.setPreviousResult(result);
  55. jmctx.getVariables().put("server", "jakarta.apache.org");
  56. jmctx.getVariables().put("my_regex", ".*");
  57. }
  58. public void testFunctionParse1() throws Exception {
  59. StringProperty prop = new StringProperty("date", "${__javaScript((new Date().getDate() / 100).toString()."
  60. + "substr(${__javaScript(1+1,d\\,ay)}\\,2),heute)}");
  61. JMeterProperty newProp = transformer.transformValue(prop);
  62. newProp.setRunningVersion(true);
  63. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  64. newProp.recoverRunningVersion(null);
  65. assertTrue(Integer.parseInt(newProp.getStringValue()) > -1);
  66. assertEquals("2", jmctx.getVariables().getObject("d,ay"));
  67. }
  68. public void testParseExample1() throws Exception {
  69. StringProperty prop = new StringProperty("html", "${__regexFunction(<html>(.*)</html>,$1$)}");
  70. JMeterProperty newProp = transformer.transformValue(prop);
  71. newProp.setRunningVersion(true);
  72. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  73. assertEquals("hello world", newProp.getStringValue());
  74. }
  75. public void testParseExample2() throws Exception {
  76. StringProperty prop = new StringProperty("html", "It should say:\\${${__regexFunction(<html>(.*)</html>,$1$)}}");
  77. JMeterProperty newProp = transformer.transformValue(prop);
  78. newProp.setRunningVersion(true);
  79. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  80. assertEquals("It should say:${hello world}", newProp.getStringValue());
  81. }
  82. public void testParseExample3() throws Exception {
  83. StringProperty prop = new StringProperty("html", "${__regexFunction(<html>(.*)</html>,$1$)}"
  84. + "${__regexFunction(<html>(.*o)(.*o)(.*)</html>," + "$1$$3$)}");
  85. JMeterProperty newProp = transformer.transformValue(prop);
  86. newProp.setRunningVersion(true);
  87. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  88. assertEquals("hello worldhellorld", newProp.getStringValue());
  89. }
  90. public void testParseExample4() throws Exception {
  91. StringProperty prop = new StringProperty("html", "${non-existing function}");
  92. JMeterProperty newProp = transformer.transformValue(prop);
  93. newProp.setRunningVersion(true);
  94. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  95. assertEquals("${non-existing function}", newProp.getStringValue());
  96. }
  97. public void testParseExample6() throws Exception {
  98. StringProperty prop = new StringProperty("html", "${server}");
  99. JMeterProperty newProp = transformer.transformValue(prop);
  100. newProp.setRunningVersion(true);
  101. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  102. assertEquals("jakarta.apache.org", newProp.getStringValue());
  103. }
  104. public void testParseExample5() throws Exception {
  105. StringProperty prop = new StringProperty("html", "");
  106. JMeterProperty newProp = transformer.transformValue(prop);
  107. newProp.setRunningVersion(true);
  108. assertEquals("org.apache.jmeter.testelement.property.StringProperty", newProp.getClass().getName());
  109. assertEquals("", newProp.getStringValue());
  110. }
  111. public void testParseExample7() throws Exception {
  112. StringProperty prop = new StringProperty("html", "${__regexFunction(\\<([a-z]*)\\>,$1$)}");
  113. JMeterProperty newProp = transformer.transformValue(prop);
  114. newProp.setRunningVersion(true);
  115. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  116. assertEquals("html", newProp.getStringValue());
  117. }
  118. public void testParseExample8() throws Exception {
  119. StringProperty prop = new StringProperty("html", "${__regexFunction((\\\\$\\d+\\.\\d+),$1$)}");
  120. JMeterProperty newProp = transformer.transformValue(prop);
  121. newProp.setRunningVersion(true);
  122. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  123. assertEquals("$3.47", newProp.getStringValue());
  124. }
  125. public void testParseExample9() throws Exception {
  126. StringProperty prop = new StringProperty("html", "${__regexFunction(([$]\\d+\\.\\d+),$1$)}");
  127. JMeterProperty newProp = transformer.transformValue(prop);
  128. newProp.setRunningVersion(true);
  129. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  130. assertEquals("$3.47", newProp.getStringValue());
  131. }
  132. public void testParseExample10() throws Exception {
  133. StringProperty prop = new StringProperty("html", "${__regexFunction(\\ "
  134. + "(\\\\\\$\\d+\\.\\d+\\,\\\\$\\d+\\.\\d+),$1$)}");
  135. JMeterProperty newProp = transformer.transformValue(prop);
  136. newProp.setRunningVersion(true);
  137. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  138. assertEquals("$3.47,$5.67", newProp.getStringValue());
  139. }
  140. // Escaped dollar commma and backslash with no variable reference
  141. public void testParseExample11() throws Exception {
  142. StringProperty prop = new StringProperty("html", "\\$a \\, \\\\ \\x \\ jakarta.apache.org");
  143. JMeterProperty newProp = transformer.transformValue(prop);
  144. newProp.setRunningVersion(true);
  145. assertEquals("org.apache.jmeter.testelement.property.StringProperty", newProp.getClass().getName());
  146. assertEquals("\\$a \\, \\\\ \\x \\ jakarta.apache.org", newProp.getStringValue());
  147. }
  148. // N.B. See Bug 46831 which wanted to changed the behaviour of \$
  149. // It's too late now, as this would invalidate some existing test plans,
  150. // so document the current behaviour with some more tests.
  151. // Escaped dollar commma and backslash with variable reference
  152. public void testParseExample12() throws Exception {
  153. StringProperty prop = new StringProperty("html", "\\$a \\, \\\\ \\x \\ ${server} \\$b \\, \\\\ cd");
  154. JMeterProperty newProp = transformer.transformValue(prop);
  155. newProp.setRunningVersion(true);
  156. // N.B. Backslashes are removed before dollar, comma and backslash
  157. assertEquals("$a , \\ \\x \\ jakarta.apache.org $b , \\ cd", newProp.getStringValue());
  158. }
  159. // Escaped dollar commma and backslash with missing variable reference
  160. public void testParseExample13() throws Exception {
  161. StringProperty prop = new StringProperty("html", "\\$a \\, \\\\ \\x \\ ${missing} \\$b \\, \\\\ cd");
  162. JMeterProperty newProp = transformer.transformValue(prop);
  163. newProp.setRunningVersion(true);
  164. // N.B. Backslashes are removed before dollar, comma and backslash
  165. assertEquals("$a , \\ \\x \\ ${missing} $b , \\ cd", newProp.getStringValue());
  166. }
  167. // Escaped dollar commma and backslash with missing function reference
  168. public void testParseExample14() throws Exception {
  169. StringProperty prop = new StringProperty("html", "\\$a \\, \\\\ \\x \\ ${__missing(a)} \\$b \\, \\\\ cd");
  170. JMeterProperty newProp = transformer.transformValue(prop);
  171. newProp.setRunningVersion(true);
  172. // N.B. Backslashes are removed before dollar, comma and backslash
  173. assertEquals("$a , \\ \\x \\ ${__missing(a)} $b , \\ cd", newProp.getStringValue());
  174. }
  175. public void testNestedExample1() throws Exception {
  176. StringProperty prop = new StringProperty("html", "${__regexFunction(<html>(${my_regex})</html>,"
  177. + "$1$)}${__regexFunction(<html>(.*o)(.*o)(.*)" + "</html>,$1$$3$)}");
  178. JMeterProperty newProp = transformer.transformValue(prop);
  179. newProp.setRunningVersion(true);
  180. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  181. assertEquals("hello worldhellorld", newProp.getStringValue());
  182. }
  183. public void testNestedExample2() throws Exception {
  184. StringProperty prop = new StringProperty("html", "${__regexFunction(<html>(${my_regex})</html>,$1$)}");
  185. JMeterProperty newProp = transformer.transformValue(prop);
  186. newProp.setRunningVersion(true);
  187. assertEquals("org.apache.jmeter.testelement.property.FunctionProperty", newProp.getClass().getName());
  188. assertEquals("hello world", newProp.getStringValue());
  189. }
  190. }