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: /*
020: * Package to test functions
021: *
022: * Functions are created and parameters set up in one thread.
023: *
024: * They are then tested in another thread, or two threads running in parallel
025: *
026: */
027: package org.apache.jmeter.functions;
028:
029: import java.io.FileNotFoundException;
030: import java.util.Collection;
031: import java.util.LinkedList;
032:
033: import junit.extensions.ActiveTestSuite;
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import org.apache.jmeter.engine.util.CompoundVariable;
038: import org.apache.jmeter.junit.JMeterTestCase;
039: import org.apache.jmeter.threads.JMeterContext;
040: import org.apache.jmeter.threads.JMeterContextService;
041: import org.apache.jmeter.threads.JMeterVariables;
042: import org.apache.jmeter.util.JMeterUtils;
043: import org.apache.jorphan.logging.LoggingManager;
044: import org.apache.jorphan.util.JMeterStopThreadException;
045: import org.apache.log.Logger;
046:
047: /**
048: * Test cases for Functions
049: */
050: public class PackageTest extends JMeterTestCase {
051:
052: private static final Logger log = LoggingManager
053: .getLoggerForClass();
054:
055: static {
056: // LoggingManager.setPriority("DEBUG","jmeter");
057: // LoggingManager.setTarget(new java.io.PrintWriter(System.out));
058: }
059:
060: public PackageTest(String arg0) {
061: super (arg0);
062: }
063:
064: // Create the CSVRead function and set its parameters.
065: private static CSVRead setParams(String p1, String p2)
066: throws Exception {
067: CSVRead cr = new CSVRead();
068: Collection parms = new LinkedList();
069: if (p1 != null)
070: parms.add(new CompoundVariable(p1));
071: if (p2 != null)
072: parms.add(new CompoundVariable(p2));
073: cr.setParameters(parms);
074: return cr;
075: }
076:
077: // Create the StringFromFile function and set its parameters.
078: private static StringFromFile SFFParams(String p1, String p2,
079: String p3, String p4) throws Exception {
080: StringFromFile sff = new StringFromFile();
081: Collection parms = new LinkedList();
082: if (p1 != null)
083: parms.add(new CompoundVariable(p1));
084: if (p2 != null)
085: parms.add(new CompoundVariable(p2));
086: if (p3 != null)
087: parms.add(new CompoundVariable(p3));
088: if (p4 != null)
089: parms.add(new CompoundVariable(p4));
090: sff.setParameters(parms);
091: return sff;
092: }
093:
094: // Create the SplitFile function and set its parameters.
095: private static SplitFunction SplitParams(String p1, String p2,
096: String p3) throws Exception {
097: SplitFunction split = new SplitFunction();
098: Collection parms = new LinkedList();
099: parms.add(new CompoundVariable(p1));
100: if (p2 != null)
101: parms.add(new CompoundVariable(p2));
102: if (p3 != null)
103: parms.add(new CompoundVariable(p3));
104: split.setParameters(parms);
105: return split;
106: }
107:
108: // Create the BeanShell function and set its parameters.
109: private static BeanShell BSHFParams(String p1, String p2, String p3)
110: throws Exception {
111: BeanShell bsh = new BeanShell();
112: bsh.setParameters(MakeParams(p1, p2, p3));
113: return bsh;
114: }
115:
116: private static Collection MakeParams(String p1, String p2, String p3) {
117: Collection parms = new LinkedList();
118: if (p1 != null)
119: parms.add(new CompoundVariable(p1));
120: if (p2 != null)
121: parms.add(new CompoundVariable(p2));
122: if (p3 != null)
123: parms.add(new CompoundVariable(p3));
124: return parms;
125: }
126:
127: public static Test suite() throws Exception {
128: TestSuite allsuites = new TestSuite("Function PackageTest");
129:
130: TestSuite bsh = new TestSuite("BeanShell");
131: bsh.addTest(new PackageTest("BSH1"));
132: allsuites.addTest(bsh);
133:
134: TestSuite suite = new TestSuite("SingleThreaded");
135: suite.addTest(new PackageTest("CSVParams"));
136: suite.addTest(new PackageTest("CSVNoFile"));
137: suite.addTest(new PackageTest("CSVSetup"));
138: suite.addTest(new PackageTest("CSVRun"));
139:
140: suite.addTest(new PackageTest("CSValias"));
141: suite.addTest(new PackageTest("CSVBlankLine"));
142: allsuites.addTest(suite);
143:
144: // Reset files
145: suite.addTest(new PackageTest("CSVSetup"));
146: TestSuite par = new ActiveTestSuite("Parallel");
147: par.addTest(new PackageTest("CSVThread1"));
148: par.addTest(new PackageTest("CSVThread2"));
149: allsuites.addTest(par);
150:
151: TestSuite sff = new TestSuite("StringFromFile");
152: sff.addTest(new PackageTest("SFFTest1"));
153: sff.addTest(new PackageTest("SFFTest2"));
154: sff.addTest(new PackageTest("SFFTest3"));
155: sff.addTest(new PackageTest("SFFTest4"));
156: sff.addTest(new PackageTest("SFFTest5"));
157: allsuites.addTest(sff);
158:
159: TestSuite split = new TestSuite("SplitFunction");
160: split.addTest(new PackageTest("splitTest1"));
161: allsuites.addTest(split);
162:
163: TestSuite xpath = new TestSuite("XPath");
164: xpath.addTest(new PackageTest("XPathtestColumns"));
165: xpath.addTest(new PackageTest("XPathtestDefault"));
166: xpath.addTest(new PackageTest("XPathtestNull"));
167: xpath.addTest(new PackageTest("XPathtestrowNum"));
168: xpath.addTest(new PackageTest("XPathEmpty"));
169: xpath.addTest(new PackageTest("XPathFile1"));
170: xpath.addTest(new PackageTest("XPathFile2"));
171: xpath.addTest(new PackageTest("XPathNoFile"));
172:
173: allsuites.addTest(xpath);
174:
175: TestSuite random = new TestSuite("Random");
176: random.addTest(new PackageTest("randomTest1"));
177: allsuites.addTest(random);
178:
179: allsuites.addTest(new PackageTest("XPathSetup1"));
180: TestSuite par2 = new ActiveTestSuite("ParallelXPath1");
181: par2.addTest(new PackageTest("XPathThread1"));
182: par2.addTest(new PackageTest("XPathThread2"));
183: allsuites.addTest(par2);
184:
185: allsuites.addTest(new PackageTest("XPathSetup2"));
186: TestSuite par3 = new ActiveTestSuite("ParallelXPath2");
187: par3.addTest(new PackageTest("XPathThread1"));
188: par3.addTest(new PackageTest("XPathThread2"));
189: allsuites.addTest(par3);
190:
191: TestSuite variable = new TestSuite("Variable");
192: variable.addTest(new PackageTest("variableTest1"));
193: allsuites.addTest(variable);
194:
195: TestSuite eval = new TestSuite("Eval");
196: eval.addTest(new PackageTest("evalTest1"));
197: eval.addTest(new PackageTest("evalTest2"));
198: allsuites.addTest(eval);
199:
200: return allsuites;
201: }
202:
203: private JMeterContext jmctx = null;
204:
205: private JMeterVariables vars = null;
206:
207: public void setUp() {
208: jmctx = JMeterContextService.getContext();
209: jmctx.setVariables(new JMeterVariables());
210: vars = jmctx.getVariables();
211: }
212:
213: public void BSH1() throws Exception {
214: String fn = "testfiles/BeanShellTest.bsh";
215: BeanShell bsh;
216: try {
217: bsh = BSHFParams(null, null, null);
218: fail("Expected InvalidVariableException");
219: } catch (InvalidVariableException e) {
220: }
221:
222: try {
223: bsh = BSHFParams("", "", "");
224: fail("Expected InvalidVariableException");
225: } catch (InvalidVariableException e) {
226: }
227:
228: try {
229: bsh = BSHFParams("", "", null);
230: assertEquals("", bsh.execute());
231: } catch (InvalidVariableException e) {
232: fail("BeanShell not present");
233: }
234:
235: bsh = BSHFParams("1", null, null);
236: assertEquals("1", bsh.execute());
237:
238: bsh = BSHFParams("1+1", "VAR", null);
239: assertEquals("2", bsh.execute());
240: assertEquals("2", vars.get("VAR"));
241:
242: // Check some initial variables
243: bsh = BSHFParams("return threadName", null, null);
244: assertEquals(Thread.currentThread().getName(), bsh.execute());
245: bsh = BSHFParams("return log.getClass().getName()", null, null);
246: assertEquals(log.getClass().getName(), bsh.execute());
247:
248: // Check source works
249: bsh = BSHFParams("source (\"testfiles/BeanShellTest.bsh\")",
250: null, null);
251: assertEquals("9876", bsh.execute());
252:
253: // Check persistence
254: bsh = BSHFParams("${SCR1}", null, null);
255:
256: vars.put("SCR1", "var1=11");
257: assertEquals("11", bsh.execute());
258:
259: vars.put("SCR1", "var2=22");
260: assertEquals("22", bsh.execute());
261:
262: vars.put("SCR1", "x=var1");
263: assertEquals("11", bsh.execute());
264:
265: vars.put("SCR1", "++x");
266: assertEquals("12", bsh.execute());
267:
268: vars.put("VAR1", "test");
269: vars.put("SCR1", "vars.get(\"VAR1\")");
270: assertEquals("test", bsh.execute());
271:
272: // Check init file functioning
273: JMeterUtils.getJMeterProperties().setProperty(
274: BeanShell.INIT_FILE, fn);
275: bsh = BSHFParams("${SCR2}", null, null);
276: vars.put("SCR2", "getprop(\"" + BeanShell.INIT_FILE + "\")");
277: assertEquals(fn, bsh.execute());// Check that bsh has read the file
278: vars.put("SCR2", "getprop(\"avavaav\",\"default\")");
279: assertEquals("default", bsh.execute());
280: vars.put("SCR2", "++i");
281: assertEquals("1", bsh.execute());
282: vars.put("SCR2", "++i");
283: assertEquals("2", bsh.execute());
284:
285: }
286:
287: public void splitTest1() throws Exception {
288: SplitFunction split = null;
289: String src = "";
290:
291: try {
292: split = SplitParams("a,b,c", null, null);
293: fail("Expected InvalidVariableException (wrong number of parameters)");
294: } catch (InvalidVariableException e) {
295: // OK
296: }
297: src = "a,b,c";
298: split = SplitParams(src, "VAR1", null);
299: assertEquals(src, split.execute());
300: assertEquals(src, vars.get("VAR1"));
301: assertEquals("3", vars.get("VAR1_n"));
302: assertEquals("a", vars.get("VAR1_1"));
303: assertEquals("b", vars.get("VAR1_2"));
304: assertEquals("c", vars.get("VAR1_3"));
305: assertNull(vars.get("VAR1_4"));
306:
307: split = SplitParams(src, "VAR2", ",");
308: assertEquals(src, split.execute());
309: assertEquals(src, vars.get("VAR2"));
310: assertEquals("3", vars.get("VAR2_n"));
311: assertEquals("a", vars.get("VAR2_1"));
312: assertEquals("b", vars.get("VAR2_2"));
313: assertEquals("c", vars.get("VAR2_3"));
314: assertNull(vars.get("VAR2_4"));
315:
316: src = "a|b|c";
317: split = SplitParams(src, "VAR3", "|");
318: assertEquals(src, split.execute());
319: assertEquals(src, vars.get("VAR3"));
320: assertEquals("3", vars.get("VAR3_n"));
321: assertEquals("a", vars.get("VAR3_1"));
322: assertEquals("b", vars.get("VAR3_2"));
323: assertEquals("c", vars.get("VAR3_3"));
324: assertNull(vars.get("VAR3_4"));
325:
326: src = "a|b||";
327: split = SplitParams(src, "VAR4", "|");
328: assertEquals(src, split.execute());
329: assertEquals(src, vars.get("VAR4"));
330: assertEquals("4", vars.get("VAR4_n"));
331: assertEquals("a", vars.get("VAR4_1"));
332: assertEquals("b", vars.get("VAR4_2"));
333: assertEquals("?", vars.get("VAR4_3"));
334: assertNull(vars.get("VAR4_5"));
335:
336: src = "a,,c";
337: vars.put("VAR", src);
338: split = SplitParams("${VAR}", "VAR", null);
339: assertEquals(src, split.execute());
340: assertEquals("3", vars.get("VAR_n"));
341: assertEquals("a", vars.get("VAR_1"));
342: assertEquals("?", vars.get("VAR_2"));
343: assertEquals("c", vars.get("VAR_3"));
344: assertNull(vars.get("VAR_4"));
345:
346: src = "a,b";
347: vars.put("VAR", src);
348: split = SplitParams("${VAR}", "VAR", null);
349: assertEquals(src, split.execute());
350: assertEquals("2", vars.get("VAR_n"));
351: assertEquals("a", vars.get("VAR_1"));
352: assertEquals("b", vars.get("VAR_2"));
353: assertNull(vars.get("VAR_3"));
354:
355: src = "a,,c,";
356: vars.put("VAR", src);
357: split = SplitParams("${VAR}", "VAR5", null);
358: assertEquals(src, split.execute());
359: assertEquals("4", vars.get("VAR5_n"));
360: assertEquals("a", vars.get("VAR5_1"));
361: assertEquals("?", vars.get("VAR5_2"));
362: assertEquals("c", vars.get("VAR5_3"));
363: assertEquals("?", vars.get("VAR5_4"));
364: assertNull(vars.get("VAR5_5"));
365:
366: }
367:
368: public void SFFTest1() throws Exception {
369: StringFromFile sff1 = SFFParams("testfiles/SFFTest#'.'txt", "",
370: "1", "3");
371: assertEquals("uno", sff1.execute());
372: assertEquals("dos", sff1.execute());
373: assertEquals("tres", sff1.execute());
374: assertEquals("cuatro", sff1.execute());
375: assertEquals("cinco", sff1.execute());
376: assertEquals("one", sff1.execute());
377: assertEquals("two", sff1.execute());
378: sff1.execute();
379: sff1.execute();
380: assertEquals("five", sff1.execute());
381: assertEquals("eins", sff1.execute());
382: sff1.execute();
383: sff1.execute();
384: sff1.execute();
385: assertEquals("fuenf", sff1.execute());
386: try {
387: sff1.execute();
388: fail("Should have thrown JMeterStopThreadException");
389: } catch (JMeterStopThreadException e) {
390: // expected
391: }
392: }
393:
394: public void SFFTest2() throws Exception {
395: StringFromFile sff = SFFParams("testfiles/SFFTest1.txt", "",
396: null, null);
397: assertEquals("uno", sff.execute());
398: assertEquals("dos", sff.execute());
399: assertEquals("tres", sff.execute());
400: assertEquals("cuatro", sff.execute());
401: assertEquals("cinco", sff.execute());
402: assertEquals("uno", sff.execute()); // Restarts
403: assertEquals("dos", sff.execute());
404: assertEquals("tres", sff.execute());
405: assertEquals("cuatro", sff.execute());
406: assertEquals("cinco", sff.execute());
407: }
408:
409: public void SFFTest3() throws Exception {
410: StringFromFile sff = SFFParams("testfiles/SFFTest1.txt", "",
411: "", "");
412: assertEquals("uno", sff.execute());
413: assertEquals("dos", sff.execute());
414: assertEquals("tres", sff.execute());
415: assertEquals("cuatro", sff.execute());
416: assertEquals("cinco", sff.execute());
417: assertEquals("uno", sff.execute()); // Restarts
418: assertEquals("dos", sff.execute());
419: assertEquals("tres", sff.execute());
420: assertEquals("cuatro", sff.execute());
421: assertEquals("cinco", sff.execute());
422: }
423:
424: public void SFFTest4() throws Exception {
425: StringFromFile sff = SFFParams("xxtestfiles/SFFTest1.txt", "",
426: "", "");
427: assertEquals(StringFromFile.ERR_IND, sff.execute());
428: assertEquals(StringFromFile.ERR_IND, sff.execute());
429: }
430:
431: // Test that only loops twice
432: public void SFFTest5() throws Exception {
433: StringFromFile sff = SFFParams("testfiles/SFFTest1.txt", "",
434: "", "2");
435: assertEquals("uno", sff.execute());
436: assertEquals("dos", sff.execute());
437: assertEquals("tres", sff.execute());
438: assertEquals("cuatro", sff.execute());
439: assertEquals("cinco", sff.execute());
440: assertEquals("uno", sff.execute());
441: assertEquals("dos", sff.execute());
442: assertEquals("tres", sff.execute());
443: assertEquals("cuatro", sff.execute());
444: assertEquals("cinco", sff.execute());
445: try {
446: sff.execute();
447: fail("Should have thrown JMeterStopThreadException");
448: } catch (JMeterStopThreadException e) {
449: // expected
450: }
451: }
452:
453: // Function objects to be tested
454: private static CSVRead cr1, cr2, cr3, cr4, cr5, cr6;
455:
456: // Helper class used to implement co-routine between two threads
457: private static class Baton {
458: void pass() {
459: done();
460: try {
461: // System.out.println(">wait:"+Thread.currentThread().getName());
462: wait(1000);
463: } catch (InterruptedException e) {
464: System.out.println(e);
465: }
466: // System.out.println("<wait:"+Thread.currentThread().getName());
467:
468: }
469:
470: void done() {
471: // System.out.println(">done:"+Thread.currentThread().getName());
472: notifyAll();
473: }
474:
475: }
476:
477: private static Baton baton = new Baton();
478:
479: public void CSVThread1() throws Exception {
480: Thread.currentThread().setName("One");
481: synchronized (baton) {
482:
483: assertEquals("b1", cr1.execute(null, null));
484:
485: assertEquals("", cr4.execute(null, null));
486:
487: assertEquals("b2", cr1.execute(null, null));
488:
489: baton.pass();
490:
491: assertEquals("", cr4.execute(null, null));
492:
493: assertEquals("b4", cr1.execute(null, null));
494:
495: assertEquals("", cr4.execute(null, null));
496:
497: baton.pass();
498:
499: assertEquals("b3", cr1.execute(null, null));
500:
501: assertEquals("", cr4.execute(null, null));
502:
503: baton.done();
504: }
505: }
506:
507: public void CSVThread2() throws Exception {
508: Thread.currentThread().setName("Two");
509: Thread.sleep(500);// Allow other thread to start
510: synchronized (baton) {
511:
512: assertEquals("b3", cr1.execute(null, null));
513:
514: assertEquals("", cr4.execute(null, null));
515:
516: baton.pass();
517:
518: assertEquals("b1", cr1.execute(null, null));
519:
520: assertEquals("", cr4.execute(null, null));
521:
522: assertEquals("b2", cr1.execute(null, null));
523:
524: baton.pass();
525:
526: assertEquals("", cr4.execute(null, null));
527:
528: assertEquals("b4", cr1.execute(null, null));
529:
530: baton.done();
531: }
532: }
533:
534: public void CSVRun() throws Exception {
535: assertEquals("b1", cr1.execute(null, null));
536: assertEquals("c1", cr2.execute(null, null));
537: assertEquals("d1", cr3.execute(null, null));
538:
539: assertEquals("", cr4.execute(null, null));
540: assertEquals("b2", cr1.execute(null, null));
541: assertEquals("c2", cr2.execute(null, null));
542: assertEquals("d2", cr3.execute(null, null));
543:
544: assertEquals("", cr4.execute(null, null));
545: assertEquals("b3", cr1.execute(null, null));
546: assertEquals("c3", cr2.execute(null, null));
547: assertEquals("d3", cr3.execute(null, null));
548:
549: assertEquals("", cr4.execute(null, null));
550: assertEquals("b4", cr1.execute(null, null));
551: assertEquals("c4", cr2.execute(null, null));
552: assertEquals("d4", cr3.execute(null, null));
553:
554: assertEquals("", cr4.execute(null, null));
555: assertEquals("b1", cr1.execute(null, null));
556: assertEquals("c1", cr2.execute(null, null));
557: assertEquals("d1", cr3.execute(null, null));
558:
559: assertEquals("a1", cr5.execute(null, null));
560: assertEquals("", cr6.execute(null, null));
561: assertEquals("a2", cr5.execute(null, null));
562:
563: }
564:
565: public void CSVParams() throws Exception {
566: try {
567: setParams(null, null);
568: fail("Should have failed");
569: } catch (InvalidVariableException e) {
570: }
571: try {
572: setParams(null, "");
573: fail("Should have failed");
574: } catch (InvalidVariableException e) {
575: }
576: try {
577: setParams("", null);
578: fail("Should have failed");
579: } catch (InvalidVariableException e) {
580: }
581: }
582:
583: public void CSVSetup() throws Exception {
584: cr1 = setParams("testfiles/test.csv", "1");
585: cr2 = setParams("testfiles/test.csv", "2");
586: cr3 = setParams("testfiles/test.csv", "3");
587: cr4 = setParams("testfiles/test.csv", "next");
588: cr5 = setParams("", "0");
589: cr6 = setParams("", "next");
590: }
591:
592: public void CSValias() throws Exception {
593: cr1 = setParams("testfiles/test.csv", "*A");
594: cr2 = setParams("*A", "1");
595: cr3 = setParams("*A", "next");
596:
597: cr4 = setParams("testfiles/test.csv", "*B");
598: cr5 = setParams("*B", "2");
599: cr6 = setParams("*B", "next");
600:
601: String s;
602:
603: s = cr1.execute(null, null); // open as *A
604: assertEquals("", s);
605: s = cr2.execute(null, null); // col 1, line 1, *A
606: assertEquals("b1", s);
607:
608: s = cr4.execute(null, null);// open as *B
609: assertEquals("", s);
610: s = cr5.execute(null, null);// col2 line 1
611: assertEquals("c1", s);
612:
613: s = cr3.execute(null, null);// *A next
614: assertEquals("", s);
615: s = cr2.execute(null, null);// col 1, line 2, *A
616: assertEquals("b2", s);
617:
618: s = cr5.execute(null, null);// col2, line 1, *B
619: assertEquals("c1", s);
620:
621: s = cr6.execute(null, null);// *B next
622: assertEquals("", s);
623:
624: s = cr5.execute(null, null);// col2, line 2, *B
625: assertEquals("c2", s);
626:
627: }
628:
629: public void CSVNoFile() throws Exception {
630: String s;
631:
632: cr1 = setParams("xtestfiles/test.csv", "1");
633: log.info("Expecting file not found");
634: s = cr1.execute(null, null);
635: assertEquals("", s);
636:
637: cr2 = setParams("xtestfiles/test.csv", "next");
638: log.info("Expecting no entry for file");
639: s = cr2.execute(null, null);
640: assertEquals("", s);
641:
642: cr3 = setParams("xtestfiles/test.csv", "*ABC");
643: log.info("Expecting file not found");
644: s = cr3.execute(null, null);
645: assertEquals("", s);
646:
647: cr4 = setParams("*ABC", "1");
648: log.info("Expecting cannot open file");
649: s = cr4.execute(null, null);
650: assertEquals("", s);
651: }
652:
653: // Check blank lines are treated as EOF
654: public void CSVBlankLine() throws Exception {
655: CSVRead csv1 = setParams("testfiles/testblank.csv", "1");
656: CSVRead csv2 = setParams("testfiles/testblank.csv", "next");
657:
658: String s;
659:
660: for (int i = 1; i <= 2; i++) {
661: s = csv1.execute(null, null);
662: assertEquals("b1", s);
663:
664: s = csv2.execute(null, null);
665: assertEquals("", s);
666:
667: s = csv1.execute(null, null);
668: assertEquals("b2", s);
669:
670: s = csv2.execute(null, null);
671: assertEquals("", s);
672: }
673:
674: }
675:
676: // XPathFileContainer tests
677:
678: public void XPathtestNull() throws Exception {
679: try {
680: new XPathFileContainer("nosuch.xml", "/");
681: fail("Should not find the file");
682: } catch (FileNotFoundException e) {
683: }
684: }
685:
686: public void XPathtestrowNum() throws Exception {
687: XPathFileContainer f = new XPathFileContainer("../build.xml",
688: "/project/target/@name");
689: assertNotNull(f);
690: // assertEquals("Expected 4 lines",4,f.size());
691:
692: int myRow = f.nextRow();
693: assertEquals(0, myRow);
694: assertEquals(1, f.getNextRow());
695:
696: myRow = f.nextRow();
697: assertEquals(1, myRow);
698: assertEquals(2, f.getNextRow());
699:
700: myRow = f.nextRow();
701: assertEquals(2, myRow);
702: assertEquals(3, f.getNextRow());
703:
704: // myRow = f.nextRow();
705: // assertEquals(3,myRow);
706: // assertEquals(0,f.getNextRow());
707:
708: // myRow = f.nextRow();
709: // assertEquals(0,myRow);
710: // assertEquals(1,f.getNextRow());
711:
712: }
713:
714: public void XPathtestColumns() throws Exception {
715: XPathFileContainer f = new XPathFileContainer("../build.xml",
716: "/project/target/@name");
717: assertNotNull(f);
718: assertTrue("Not empty", f.size() > 0);
719: int last = 0;
720: for (int i = 0; i < f.size(); i++) {
721: last = f.nextRow();
722: log.debug("found [" + i + "]" + f.getXPathString(last));
723: }
724: assertEquals(last + 1, f.size());
725:
726: }
727:
728: public void XPathtestDefault() throws Exception {
729: XPathFileContainer f = new XPathFileContainer("../build.xml",
730: "/project/@default");
731: assertNotNull(f);
732: assertTrue("Not empty", f.size() > 0);
733: assertEquals("all", f.getXPathString(0));
734:
735: }
736:
737: public void XPathEmpty() throws Exception {
738: XPath xp = setupXPath("", "");
739: String val = xp.execute();
740: assertEquals("", val);
741: val = xp.execute();
742: assertEquals("", val);
743: val = xp.execute();
744: assertEquals("", val);
745: }
746:
747: public void XPathNoFile() throws Exception {
748: XPath xp = setupXPath("no-such-file", "");
749: String val = xp.execute();
750: assertEquals("", val); // TODO - should check that error has been logged...
751: }
752:
753: public void XPathFile1() throws Exception {
754: XPath xp = setupXPath("testfiles/XPathTest.xml",
755: "//user/@username");
756: assertEquals("u1", xp.execute());
757: assertEquals("u2", xp.execute());
758: assertEquals("u3", xp.execute());
759: assertEquals("u4", xp.execute());
760: assertEquals("u5", xp.execute());
761: assertEquals("u1", xp.execute());
762: }
763:
764: public void XPathFile2() throws Exception {
765: XPath xp1 = setupXPath("testfiles/XPathTest.xml",
766: "//user/@username");
767: XPath xp1a = setupXPath("testfiles/XPathTest.xml",
768: "//user/@username");
769: XPath xp2 = setupXPath("testfiles/XPathTest.xml",
770: "//user/@password");
771: XPath xp2a = setupXPath("testfiles/XPathTest.xml",
772: "//user/@password");
773: assertEquals("u1", xp1.execute());
774: assertEquals("p1", xp2.execute());
775: assertEquals("p2", xp2.execute());
776: assertEquals("u2", xp1a.execute());
777: assertEquals("u3", xp1.execute());
778: assertEquals("u4", xp1.execute());
779: assertEquals("p3", xp2a.execute());
780:
781: }
782:
783: private static XPath sxp1, sxp2;
784:
785: // Use same XPath for both threads
786: public void XPathSetup1() throws Exception {
787: sxp1 = setupXPath("testfiles/XPathTest.xml", "//user/@username");
788: sxp2 = sxp1;
789: }
790:
791: // Use different XPath for both threads
792: public void XPathSetup2() throws Exception {
793: sxp1 = setupXPath("testfiles/XPathTest.xml", "//user/@username");
794: sxp2 = setupXPath("testfiles/XPathTest.xml", "//user/@username");
795: }
796:
797: public void XPathThread1() throws Exception {
798: Thread.currentThread().setName("XPathOne");
799: synchronized (baton) {
800: assertEquals("u1", sxp1.execute());
801: assertEquals("u2", sxp1.execute());
802: baton.pass();
803: assertEquals("u5", sxp1.execute());
804: baton.pass();
805: assertEquals("u2", sxp1.execute());
806: baton.done();
807: }
808: }
809:
810: public void XPathThread2() throws Exception {
811: Thread.currentThread().setName("XPathTwo");
812: Thread.sleep(500);
813: synchronized (baton) {
814: assertEquals("u3", sxp2.execute());
815: assertEquals("u4", sxp2.execute());
816: baton.pass();
817: assertEquals("u1", sxp2.execute());
818: baton.pass();
819: assertEquals("u3", sxp2.execute());
820: baton.done();
821: }
822: }
823:
824: private XPath setupXPath(String file, String expr) throws Exception {
825: Collection parms = new LinkedList();
826: parms.add(new CompoundVariable(file));
827: parms.add(new CompoundVariable(expr));
828: XPath xp = new XPath();
829: xp.setParameters(parms);
830: return xp;
831: }
832:
833: public void randomTest1() throws Exception {
834: Random r = new Random();
835: Collection parms = MakeParams("0", "10000000000", "VAR");
836: r.setParameters(parms);
837: //String s =
838: r.execute(null, null);
839: }
840:
841: public void variableTest1() throws Exception {
842: Variable r = new Variable();
843: vars.put("A_1", "a1");
844: vars.put("A_2", "a2");
845: vars.put("one", "1");
846: vars.put("two", "2");
847: vars.put("V", "A");
848: Collection parms;
849: String s;
850:
851: parms = MakeParams("V", null, null);
852: r.setParameters(parms);
853: s = r.execute(null, null);
854: assertEquals("A", s);
855:
856: parms = MakeParams("X", null, null);
857: r.setParameters(parms);
858: s = r.execute(null, null);
859: assertEquals("X", s);
860:
861: parms = MakeParams("A${X}", null, null);
862: r.setParameters(parms);
863: s = r.execute(null, null);
864: assertEquals("A${X}", s);
865:
866: parms = MakeParams("A_1", null, null);
867: r.setParameters(parms);
868: s = r.execute(null, null);
869: assertEquals("a1", s);
870:
871: parms = MakeParams("A_2", null, null);
872: r.setParameters(parms);
873: s = r.execute(null, null);
874: assertEquals("a2", s);
875:
876: parms = MakeParams("A_${two}", null, null);
877: r.setParameters(parms);
878: s = r.execute(null, null);
879: assertEquals("a2", s);
880:
881: parms = MakeParams("${V}_${one}", null, null);
882: r.setParameters(parms);
883: s = r.execute(null, null);
884: assertEquals("a1", s);
885: }
886:
887: public void evalTest1() throws Exception {
888: EvalFunction eval = new EvalFunction();
889: vars.put("query", "select ${column} from ${table}");
890: vars.put("column", "name");
891: vars.put("table", "customers");
892: Collection parms;
893: String s;
894:
895: parms = MakeParams("${query}", null, null);
896: eval.setParameters(parms);
897: s = eval.execute(null, null);
898: assertEquals("select name from customers", s);
899:
900: }
901:
902: public void evalTest2() throws Exception {
903: EvalVarFunction evalVar = new EvalVarFunction();
904: vars.put("query", "select ${column} from ${table}");
905: vars.put("column", "name");
906: vars.put("table", "customers");
907: Collection parms;
908: String s;
909:
910: parms = MakeParams("query", null, null);
911: evalVar.setParameters(parms);
912: s = evalVar.execute(null, null);
913: assertEquals("select name from customers", s);
914: }
915: }
|