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.extractor;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.jmeter.samplers.SampleResult;
024: import org.apache.jmeter.threads.JMeterContext;
025: import org.apache.jmeter.threads.JMeterContextService;
026: import org.apache.jmeter.threads.JMeterVariables;
027:
028: public class TestXPathExtractor extends TestCase {
029: XPathExtractor extractor;
030:
031: SampleResult result;
032:
033: JMeterVariables vars;
034:
035: public TestXPathExtractor(String name) {
036: super (name);
037: }
038:
039: private JMeterContext jmctx = null;
040:
041: private final static String VAL_NAME = "value";
042: private final static String VAL_NAME_NR = "value_matchNr";
043:
044: public void setUp() {
045: jmctx = JMeterContextService.getContext();
046: extractor = new XPathExtractor();
047: extractor.setThreadContext(jmctx);// This would be done by the run command
048: extractor.setRefName(VAL_NAME);
049: extractor.setDefaultValue("Default");
050: result = new SampleResult();
051: String data = "<book><preface title='Intro'>zero</preface><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
052: result.setResponseData(data.getBytes());
053: vars = new JMeterVariables();
054: jmctx.setVariables(vars);
055: jmctx.setPreviousResult(result);
056: }
057:
058: public void testAttributeExtraction() throws Exception {
059: extractor.setXPathQuery("/book/preface/@title");
060: extractor.process();
061: assertEquals("Intro", vars.get(VAL_NAME));
062: assertEquals("1", vars.get(VAL_NAME_NR));
063: assertEquals("Intro", vars.get(VAL_NAME + "_1"));
064: assertNull(vars.get(VAL_NAME + "_2"));
065:
066: extractor.setXPathQuery("/book/preface[@title]");
067: extractor.process();
068: assertEquals("zero", vars.get(VAL_NAME));
069: assertEquals("1", vars.get(VAL_NAME_NR));
070: assertEquals("zero", vars.get(VAL_NAME + "_1"));
071: assertNull(vars.get(VAL_NAME + "_2"));
072:
073: extractor.setXPathQuery("/book/preface[@title='Intro']");
074: extractor.process();
075: assertEquals("zero", vars.get(VAL_NAME));
076: assertEquals("1", vars.get(VAL_NAME_NR));
077: assertEquals("zero", vars.get(VAL_NAME + "_1"));
078: assertNull(vars.get(VAL_NAME + "_2"));
079:
080: extractor.setXPathQuery("/book/preface[@title='xyz']");
081: extractor.process();
082: assertEquals("Default", vars.get(VAL_NAME));
083: assertEquals("0", vars.get(VAL_NAME_NR));
084: assertNull(vars.get(VAL_NAME + "_1"));
085: }
086:
087: public void testVariableExtraction() throws Exception {
088: extractor.setXPathQuery("/book/preface");
089: extractor.process();
090: assertEquals("zero", vars.get(VAL_NAME));
091: assertEquals("1", vars.get(VAL_NAME_NR));
092: assertEquals("zero", vars.get(VAL_NAME + "_1"));
093: assertNull(vars.get(VAL_NAME + "_2"));
094:
095: extractor.setXPathQuery("/book/page");
096: extractor.process();
097: assertEquals("one", vars.get(VAL_NAME));
098: assertEquals("2", vars.get(VAL_NAME_NR));
099: assertEquals("one", vars.get(VAL_NAME + "_1"));
100: assertEquals("two", vars.get(VAL_NAME + "_2"));
101: assertNull(vars.get(VAL_NAME + "_3"));
102:
103: extractor.setXPathQuery("/book/page[2]");
104: extractor.process();
105: assertEquals("two", vars.get(VAL_NAME));
106: assertEquals("1", vars.get(VAL_NAME_NR));
107: assertEquals("two", vars.get(VAL_NAME + "_1"));
108: assertNull(vars.get(VAL_NAME + "_2"));
109: assertNull(vars.get(VAL_NAME + "_3"));
110:
111: extractor.setXPathQuery("/book/index");
112: extractor.process();
113: assertEquals("Default", vars.get(VAL_NAME));
114: assertEquals("0", vars.get(VAL_NAME_NR));
115: assertNull(vars.get(VAL_NAME + "_1"));
116:
117: // Has child, but child is empty
118: extractor.setXPathQuery("/book/a");
119: extractor.process();
120: assertEquals("Default", vars.get(VAL_NAME));
121: assertEquals("1", vars.get(VAL_NAME_NR));
122: assertNull(vars.get(VAL_NAME + "_1"));
123:
124: // Has no child
125: extractor.setXPathQuery("/book/empty");
126: extractor.process();
127: assertEquals("Default", vars.get(VAL_NAME));
128: assertEquals("1", vars.get(VAL_NAME_NR));
129: assertNull(vars.get(VAL_NAME + "_1"));
130: }
131:
132: public void testInvalidXpath() throws Exception {
133: extractor.setXPathQuery("<");
134: extractor.process();
135: assertEquals("Default", vars.get(VAL_NAME));
136: assertEquals("0", vars.get(VAL_NAME_NR));
137: }
138:
139: public void testInvalidDocument() throws Exception {
140: result.setResponseData("<z>".getBytes());
141: extractor.setXPathQuery("<");
142: extractor.process();
143: assertEquals("Default", vars.get(VAL_NAME));
144: assertEquals("0", vars.get(VAL_NAME_NR));
145: }
146: }
|