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.functions;
020:
021: import java.util.Collection;
022: import java.util.LinkedList;
023: import java.util.Locale;
024:
025: import org.apache.jmeter.engine.util.CompoundVariable;
026: import org.apache.jmeter.junit.JMeterTestCase;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.threads.JMeterContext;
029: import org.apache.jmeter.threads.JMeterContextService;
030: import org.apache.jmeter.threads.JMeterVariables;
031:
032: public class TestTimeFunction extends JMeterTestCase {
033: private Function variable;
034:
035: private SampleResult result;
036:
037: private Collection params;
038:
039: private JMeterVariables vars;
040:
041: private JMeterContext jmctx = null;
042:
043: private String value;
044:
045: public TestTimeFunction(String name) {
046: super (name);
047: }
048:
049: public void setUp() {
050: jmctx = JMeterContextService.getContext();
051: vars = new JMeterVariables();
052: jmctx.setVariables(vars);
053: jmctx.setPreviousResult(result);
054: params = new LinkedList();
055: result = new SampleResult();
056: variable = new TimeFunction();
057: }
058:
059: public void testDefault() throws Exception {
060: variable.setParameters(params);
061: long before = System.currentTimeMillis();
062: value = variable.execute(result, null);
063: long now = Long.parseLong(value);
064: long after = System.currentTimeMillis();
065: assertTrue(now >= before && now <= after);
066: }
067:
068: public void testDefault1() throws Exception {
069: params.add(new CompoundVariable());
070: variable.setParameters(params);
071: long before = System.currentTimeMillis();
072: value = variable.execute(result, null);
073: long now = Long.parseLong(value);
074: long after = System.currentTimeMillis();
075: assertTrue(now >= before && now <= after);
076: }
077:
078: public void testDefault2() throws Exception {
079: params.add(new CompoundVariable());
080: params.add(new CompoundVariable());
081: variable.setParameters(params);
082: long before = System.currentTimeMillis();
083: value = variable.execute(result, null);
084: long now = Long.parseLong(value);
085: long after = System.currentTimeMillis();
086: assertTrue(now >= before && now <= after);
087: }
088:
089: public void testDefaultNone() throws Exception {
090: long before = System.currentTimeMillis();
091: value = variable.execute(result, null);
092: long now = Long.parseLong(value);
093: long after = System.currentTimeMillis();
094: assertTrue(now >= before && now <= after);
095: }
096:
097: public void testTooMany() throws Exception {
098: params.add(new CompoundVariable("YMD"));
099: params.add(new CompoundVariable("NAME"));
100: params.add(new CompoundVariable("YMD"));
101: try {
102: variable.setParameters(params);
103: fail("Should have raised InvalidVariableException");
104: } catch (InvalidVariableException ignored) {
105: }
106: }
107:
108: public void testYMD() throws Exception {
109: params.add(new CompoundVariable("YMD"));
110: params.add(new CompoundVariable("NAME"));
111: variable.setParameters(params);
112: value = variable.execute(result, null);
113: assertEquals(8, value.length());
114: assertEquals(value, vars.get("NAME"));
115: }
116:
117: public void testYMDnoV() throws Exception {
118: params.add(new CompoundVariable("YMD"));
119: variable.setParameters(params);
120: value = variable.execute(result, null);
121: assertEquals(8, value.length());
122: assertNull(vars.get("NAME"));
123: }
124:
125: public void testHMS() throws Exception {
126: params.add(new CompoundVariable("HMS"));
127: variable.setParameters(params);
128: value = variable.execute(result, null);
129: assertEquals(6, value.length());
130: }
131:
132: public void testYMDHMS() throws Exception {
133: params.add(new CompoundVariable("YMDHMS"));
134: variable.setParameters(params);
135: value = variable.execute(result, null);
136: assertEquals(15, value.length());
137: }
138:
139: public void testUSER1() throws Exception {
140: params.add(new CompoundVariable("USER1"));
141: variable.setParameters(params);
142: value = variable.execute(result, null);
143: assertEquals(0, value.length());
144: }
145:
146: public void testUSER2() throws Exception {
147: params.add(new CompoundVariable("USER2"));
148: variable.setParameters(params);
149: value = variable.execute(result, null);
150: assertEquals(0, value.length());
151: }
152:
153: public void testFixed() throws Exception {
154: params.add(new CompoundVariable("'Fixed text'"));
155: variable.setParameters(params);
156: value = variable.execute(result, null);
157: assertEquals("Fixed text", value);
158: }
159:
160: public void testMixed() throws Exception {
161: params.add(new CompoundVariable("G"));
162: variable.setParameters(params);
163: Locale locale = Locale.getDefault();
164: Locale.setDefault(Locale.ENGLISH);
165: value = variable.execute(result, null);
166: Locale.setDefault(locale);
167: assertEquals("AD", value);
168: }
169: }
|