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.control;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.samplers.Sampler;
024: import org.apache.jmeter.testelement.property.JMeterProperty;
025: import org.apache.jmeter.testelement.property.StringProperty;
026: import org.apache.jmeter.threads.JMeterContextService;
027: import org.apache.jmeter.threads.JMeterThread;
028: import org.apache.jmeter.threads.JMeterVariables;
029: import org.apache.jorphan.logging.LoggingManager;
030: import org.apache.log.Logger;
031:
032: // @see TestWhileController for unit tests
033:
034: /**
035: * @version $Revision: 595966 $
036: */
037: public class WhileController extends GenericController implements
038: Serializable {
039: private static Logger log = LoggingManager.getLoggerForClass();
040:
041: private final static String CONDITION = "WhileController.condition"; // $NON-NLS-1$
042:
043: boolean testMode = false; // To make testing easier
044:
045: boolean testModeResult = false; // dummy sample result
046:
047: public WhileController() {
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.apache.jmeter.control.Controller#isDone()
054: */
055: public boolean isDone() {
056: if (getSubControllers().size() == 0) // Nothing left to run
057: {
058: return true;
059: }
060: return false;// Never want to remove the controller from the tree
061: }
062:
063: /*
064: * Evaluate the condition, which can be: blank or LAST = was the last
065: * sampler OK? otherwise, evaluate the condition to see if it is not "false"
066: * If blank, only evaluate at the end of the loop
067: *
068: * Must only be called at start and end of loop
069: *
070: * @param loopEnd - are we at loop end? @return true means OK to continue
071: */
072: private boolean endOfLoop(boolean loopEnd) {
073: String cnd = getCondition();
074: log.debug("Condition string:" + cnd);
075: boolean res;
076: // If blank, only check previous sample when at end of loop
077: if ((loopEnd && cnd.length() == 0)
078: || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$
079: if (testMode) {
080: res = !testModeResult;
081: } else {
082: JMeterVariables threadVars = JMeterContextService
083: .getContext().getVariables();
084: // Use !false rather than true, so that null is treated as true
085: res = "false".equalsIgnoreCase(threadVars
086: .get(JMeterThread.LAST_SAMPLE_OK));// $NON-NLS-1$
087: }
088: } else {
089: // cnd may be blank if next() called us
090: res = "false".equalsIgnoreCase(cnd);// $NON-NLS-1$
091: }
092: log.debug("Condition value: " + res);
093: return res;
094: }
095:
096: /*
097: * (non-Javadoc) Only called at End of Loop
098: *
099: * @see org.apache.jmeter.control.GenericController#nextIsNull()
100: */
101: protected Sampler nextIsNull() throws NextIsNullException {
102: reInitialize();
103: if (!endOfLoop(true)) {
104: return super .next();
105: }
106: setDone(true);
107: return null;
108: }
109:
110: /*
111: * This skips controller entirely if the condition is false
112: *
113: * TODO consider checking for previous sampler failure here - would need to
114: * distinguish this from previous failure *inside* loop
115: *
116: */
117: public Sampler next() {
118: if (current != 0) { // in the middle of the loop
119: return super .next();
120: }
121: // Must be start of loop
122: if (!endOfLoop(false)) {
123: return super .next(); // OK to continue
124: }
125: reInitialize(); // Don't even start the loop
126: return null;
127: }
128:
129: /**
130: * @param string
131: * the condition to save
132: */
133: public void setCondition(String string) {
134: log.debug("setCondition(" + string + ")");
135: setProperty(new StringProperty(CONDITION, string));
136: }
137:
138: /**
139: * @return the condition
140: */
141: public String getCondition() {
142: String cnd;
143: JMeterProperty prop = getProperty(CONDITION);
144: prop.recoverRunningVersion(this );
145: cnd = prop.getStringValue();
146: log.debug("getCondition() => " + cnd);
147: return cnd;
148: }
149: }
|