KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > PackageTest


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/PackageTest.java,v 1.10.2.7 2005/03/07 00:24:51 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 /*
20  * Package to test functions
21  *
22  * Functions are created and parameters set up in one thread.
23  *
24  * They are then tested in another thread, or two threads running in parallel
25  *
26  */

27 package org.apache.jmeter.functions;
28
29 import java.util.Collection JavaDoc;
30 import java.util.LinkedList JavaDoc;
31
32 import junit.extensions.ActiveTestSuite;
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.apache.jmeter.engine.util.CompoundVariable;
37 import org.apache.jmeter.junit.JMeterTestCase;
38 import org.apache.jmeter.threads.JMeterContext;
39 import org.apache.jmeter.threads.JMeterContextService;
40 import org.apache.jmeter.threads.JMeterVariables;
41 import org.apache.jmeter.util.JMeterUtils;
42 import org.apache.jorphan.logging.LoggingManager;
43 import org.apache.jorphan.util.JMeterStopThreadException;
44 import org.apache.log.Logger;
45
46 /**
47  * @version $Revision: 1.10.2.7 $ $Date: 2005/03/07 00:24:51 $
48  */

49 public class PackageTest extends JMeterTestCase
50 {
51
52     transient private static final Logger log = LoggingManager.getLoggerForClass();
53
54     static{
55         //LoggingManager.setPriority("DEBUG","jmeter");
56
//LoggingManager.setTarget(new java.io.PrintWriter(System.out));
57
}
58
59     public PackageTest(String JavaDoc arg0)
60     {
61         super(arg0);
62     }
63
64
65     // Create the CSVRead function and set its parameters.
66
private static CSVRead setParams(String JavaDoc p1, String JavaDoc p2) throws Exception JavaDoc
67     {
68         CSVRead cr = new CSVRead();
69         Collection JavaDoc parms = new LinkedList JavaDoc();
70         if (p1 != null) parms.add(new CompoundVariable(p1));
71         if (p2 != null) parms.add(new CompoundVariable(p2));
72         cr.setParameters(parms);
73         return cr;
74     }
75
76     // Create the StringFromFile function and set its parameters.
77
private static StringFromFile SFFParams(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3, String JavaDoc p4)
78     throws Exception JavaDoc
79     {
80         StringFromFile sff = new StringFromFile();
81         Collection JavaDoc parms = new LinkedList JavaDoc();
82         if (p1 != null) parms.add(new CompoundVariable(p1));
83         if (p2 != null) parms.add(new CompoundVariable(p2));
84         if (p3 != null) parms.add(new CompoundVariable(p3));
85         if (p4 != null) parms.add(new CompoundVariable(p4));
86         sff.setParameters(parms);
87         return sff;
88     }
89
90     // Create the SplitFile function and set its parameters.
91
private static SplitFunction SplitParams(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3)
92     throws Exception JavaDoc
93     {
94         SplitFunction split = new SplitFunction();
95         Collection JavaDoc parms = new LinkedList JavaDoc();
96         parms.add(new CompoundVariable(p1));
97         if (p2 != null) parms.add(new CompoundVariable(p2));
98         if (p3 != null) parms.add(new CompoundVariable(p3));
99         split.setParameters(parms);
100         return split;
101     }
102
103     // Create the BeanShell function and set its parameters.
104
private static BeanShell BSHFParams(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3)
105     throws Exception JavaDoc
106     {
107         BeanShell bsh = new BeanShell();
108         bsh.setParameters(MakeParams(p1,p2,p3));
109         return bsh;
110     }
111     private static Collection JavaDoc MakeParams(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3)
112     {
113         Collection JavaDoc parms = new LinkedList JavaDoc();
114         if (p1 != null) parms.add(new CompoundVariable(p1));
115         if (p2 != null) parms.add(new CompoundVariable(p2));
116         if (p3 != null) parms.add(new CompoundVariable(p3));
117         return parms;
118     }
119
120     public static Test suite() throws Exception JavaDoc
121     {
122            TestSuite allsuites = new TestSuite();
123            
124            TestSuite bsh = new TestSuite("BeanShell");
125            bsh.addTest( new PackageTest("BSH1"));
126            allsuites.addTest(bsh);
127    
128            TestSuite suite = new TestSuite("SingleThreaded");
129            suite.addTest(new PackageTest("CSVParams"));
130            suite.addTest(new PackageTest("CSVNoFile"));
131            suite.addTest(new PackageTest("CSVSetup"));
132            suite.addTest(new PackageTest("CSVRun"));
133
134            suite.addTest(new PackageTest("CSValias"));
135            suite.addTest(new PackageTest("CSVBlankLine"));
136            allsuites.addTest(suite);
137
138            //Reset files
139
suite.addTest(new PackageTest("CSVSetup"));
140            TestSuite par = new ActiveTestSuite("Parallel");
141            par.addTest(new PackageTest("CSVThread1"));
142            par.addTest(new PackageTest("CSVThread2"));
143            allsuites.addTest(par);
144            
145            TestSuite sff = new TestSuite("StringFromFile");
146            sff.addTest(new PackageTest("SFFTest1"));
147            sff.addTest(new PackageTest("SFFTest2"));
148            sff.addTest(new PackageTest("SFFTest3"));
149            sff.addTest(new PackageTest("SFFTest4"));
150            sff.addTest(new PackageTest("SFFTest5"));
151            allsuites.addTest(sff);
152            
153            TestSuite split = new TestSuite("SplitFunction");
154            split.addTest(new PackageTest("SplitTest1"));
155            allsuites.addTest(split);
156            
157            return allsuites;
158     }
159     
160     private JMeterContext jmctx = null;
161     private JMeterVariables vars = null;
162     public void setUp()
163     {
164         jmctx = JMeterContextService.getContext();
165         jmctx.setVariables(new JMeterVariables());
166         vars = jmctx.getVariables();
167     }
168
169     public void BSH1() throws Exception JavaDoc
170     {
171         String JavaDoc fn = "testfiles/BeanShellTest.bsh";
172         BeanShell bsh;
173         try
174         {
175             bsh = BSHFParams(null,null,null);
176             fail("Expected InvalidVariableException");
177         } catch (InvalidVariableException e) {}
178         
179         try
180         {
181             bsh = BSHFParams("","","");
182             fail("Expected InvalidVariableException");
183         } catch (InvalidVariableException e) {}
184         
185         try {
186             bsh = BSHFParams("","",null);
187             assertEquals("",bsh.execute());
188         } catch (InvalidVariableException e) {
189             fail("BeanShell not present");
190         }
191         
192         bsh = BSHFParams("1",null,null);
193         assertEquals("1",bsh.execute());
194
195         bsh = BSHFParams("1+1","VAR",null);
196         assertEquals("2",bsh.execute());
197         assertEquals("2",vars.get("VAR"));
198
199         // Check some initial variables
200
bsh = BSHFParams("return threadName",null,null);
201         assertEquals(Thread.currentThread().getName(),bsh.execute());
202         bsh = BSHFParams("return log.getClass().getName()",null,null);
203         assertEquals(log.getClass().getName(),bsh.execute());
204
205         // Check source works
206
bsh = BSHFParams("source (\"testfiles/BeanShellTest.bsh\")",null,null);
207         assertEquals("9876",bsh.execute());
208
209         // Check persistence
210
bsh = BSHFParams("${SCR1}",null,null);
211
212         vars.put("SCR1","var1=11");
213         assertEquals("11",bsh.execute());
214
215         vars.put("SCR1","var2=22");
216         assertEquals("22",bsh.execute());
217
218         vars.put("SCR1","x=var1");
219         assertEquals("11",bsh.execute());
220
221         vars.put("SCR1","++x");
222         assertEquals("12",bsh.execute());
223
224         vars.put("VAR1","test");
225         vars.put("SCR1","vars.get(\"VAR1\")");
226         assertEquals("test",bsh.execute());
227
228
229         // Check init file functioning
230
JMeterUtils.getJMeterProperties()
231             .setProperty(BeanShell.INIT_FILE,fn);
232         bsh = BSHFParams("${SCR2}",null,null);
233         vars.put("SCR2","getprop(\""+BeanShell.INIT_FILE+"\")");
234         assertEquals(fn,bsh.execute());// Check that bsh has read the file
235
vars.put("SCR2","getprop(\"avavaav\",\"default\")");
236         assertEquals("default",bsh.execute());
237         vars.put("SCR2","++i");
238         assertEquals("1",bsh.execute());
239         vars.put("SCR2","++i");
240         assertEquals("2",bsh.execute());
241         
242     }
243     
244     public void SplitTest1() throws Exception JavaDoc
245     {
246         SplitFunction split=null;
247         String JavaDoc SRC="";
248         
249         try
250         {
251             split = SplitParams("a,b,c",null,null);
252             fail("Expected InvalidVariableException (wrong number of parameters)");
253         }
254         catch (InvalidVariableException e)
255         {
256             //OK
257
}
258         src="a,b,c";
259         split = SplitParams(src,"VAR1",null);
260         assertEquals(src,split.execute());
261         assertEquals(src,vars.get("VAR1"));
262         assertEquals("3",vars.get("VAR1_n"));
263         assertEquals("a",vars.get("VAR1_1"));
264         assertEquals("b",vars.get("VAR1_2"));
265         assertEquals("c",vars.get("VAR1_3"));
266         assertNull(vars.get("VAR1_4"));
267
268         split = SplitParams(src,"VAR2",",");
269         assertEquals(src,split.execute());
270         assertEquals(src,vars.get("VAR2"));
271         assertEquals("3",vars.get("VAR2_n"));
272         assertEquals("a",vars.get("VAR2_1"));
273         assertEquals("b",vars.get("VAR2_2"));
274         assertEquals("c",vars.get("VAR2_3"));
275         assertNull(vars.get("VAR2_4"));
276
277
278         src = "a|b|c";
279         split = SplitParams(src,"VAR3","|");
280         assertEquals(src,split.execute());
281         assertEquals(src,vars.get("VAR3"));
282         assertEquals("3",vars.get("VAR3_n"));
283         assertEquals("a",vars.get("VAR3_1"));
284         assertEquals("b",vars.get("VAR3_2"));
285         assertEquals("c",vars.get("VAR3_3"));
286         assertNull(vars.get("VAR3_4"));
287
288         src="a|b||";
289         split = SplitParams(src,"VAR4","|");
290         assertEquals(src,split.execute());
291         assertEquals(src,vars.get("VAR4"));
292         assertEquals("3",vars.get("VAR4_n"));
293         assertEquals("a",vars.get("VAR4_1"));
294         assertEquals("b",vars.get("VAR4_2"));
295         assertEquals("?",vars.get("VAR4_3"));
296         assertNull(vars.get("VAR4_5"));
297
298         src="a,,c";
299         vars.put("VAR",src);
300         split = SplitParams("${VAR}","VAR",null);
301         assertEquals(src,split.execute());
302         assertEquals("3",vars.get("VAR_n"));
303         assertEquals("a",vars.get("VAR_1"));
304         assertEquals("?",vars.get("VAR_2"));
305         assertEquals("c",vars.get("VAR_3"));
306     }
307     
308     public void SFFTest1() throws Exception JavaDoc
309     {
310         StringFromFile sff1 = SFFParams("testfiles/SFFTest#.txt","","1","3");
311         assertEquals("uno",sff1.execute());
312         assertEquals("dos",sff1.execute());
313         assertEquals("tres",sff1.execute());
314         assertEquals("cuatro",sff1.execute());
315         assertEquals("cinco",sff1.execute());
316         assertEquals("one",sff1.execute());
317         assertEquals("two",sff1.execute());
318         sff1.execute();
319         sff1.execute();
320         assertEquals("five",sff1.execute());
321         assertEquals("eins",sff1.execute());
322         sff1.execute();
323         sff1.execute();
324         sff1.execute();
325         assertEquals("fuenf",sff1.execute());
326         try
327         {
328             sff1.execute();
329             fail("Should have thrown JMeterStopThreadException");
330         }
331         catch (JMeterStopThreadException e)
332         {
333             // expected
334
}
335     }
336     
337     public void SFFTest2() throws Exception JavaDoc
338     {
339         StringFromFile sff = SFFParams("testfiles/SFFTest1.txt","",null,null);
340         assertEquals("uno",sff.execute());
341         assertEquals("dos",sff.execute());
342         assertEquals("tres",sff.execute());
343         assertEquals("cuatro",sff.execute());
344         assertEquals("cinco",sff.execute());
345         assertEquals("uno",sff.execute()); // Restarts
346
assertEquals("dos",sff.execute());
347         assertEquals("tres",sff.execute());
348         assertEquals("cuatro",sff.execute());
349         assertEquals("cinco",sff.execute());
350     }
351     
352     public void SFFTest3() throws Exception JavaDoc
353     {
354         StringFromFile sff = SFFParams("testfiles/SFFTest1.txt","","","");
355         assertEquals("uno",sff.execute());
356         assertEquals("dos",sff.execute());
357         assertEquals("tres",sff.execute());
358         assertEquals("cuatro",sff.execute());
359         assertEquals("cinco",sff.execute());
360         assertEquals("uno",sff.execute()); // Restarts
361
assertEquals("dos",sff.execute());
362         assertEquals("tres",sff.execute());
363         assertEquals("cuatro",sff.execute());
364         assertEquals("cinco",sff.execute());
365     }
366     
367     public void SFFTest4() throws Exception JavaDoc
368     {
369         StringFromFile sff = SFFParams("xxtestfiles/SFFTest1.txt","","","");
370         assertEquals(StringFromFile.ERR_IND,sff.execute());
371         assertEquals(StringFromFile.ERR_IND,sff.execute());
372     }
373     
374     // Test that only loops twice
375
public void SFFTest5() throws Exception JavaDoc
376     {
377         StringFromFile sff = SFFParams("testfiles/SFFTest1.txt","","","2");
378         assertEquals("uno",sff.execute());
379         assertEquals("dos",sff.execute());
380         assertEquals("tres",sff.execute());
381         assertEquals("cuatro",sff.execute());
382         assertEquals("cinco",sff.execute());
383         assertEquals("uno",sff.execute());
384         assertEquals("dos",sff.execute());
385         assertEquals("tres",sff.execute());
386         assertEquals("cuatro",sff.execute());
387         assertEquals("cinco",sff.execute());
388         try
389         {
390             sff.execute();
391             fail("Should have thrown JMeterStopThreadException");
392         }
393         catch (JMeterStopThreadException e)
394         {
395             // expected
396
}
397     }
398    
399     // Function objects to be tested
400
private static CSVRead cr1, cr2, cr3, cr4, cr5, cr6;
401     
402     // Helper class used to implement co-routine between two threads
403
private static class Baton{
404         void pass(){
405             done();
406             try
407             {
408                 //System.out.println(">wait:"+Thread.currentThread().getName());
409
wait(1000);
410             }
411             catch (InterruptedException JavaDoc e)
412             {
413                 System.out.println(e);
414             }
415             //System.out.println("<wait:"+Thread.currentThread().getName());
416

417         }
418         
419         void done(){
420             //System.out.println(">done:"+Thread.currentThread().getName());
421
notifyAll();
422         }
423
424     }
425     
426     private static Baton baton = new Baton();
427
428     public void CSVThread1() throws Exception JavaDoc
429     {
430         Thread.currentThread().setName("One");
431         synchronized(baton){
432             
433             assertEquals("b1",cr1.execute(null,null));
434
435             assertEquals("",cr4.execute(null,null));
436     
437             assertEquals("b2",cr1.execute(null,null));
438            
439             baton.pass();
440
441             assertEquals("",cr4.execute(null,null));
442     
443             assertEquals("b4",cr1.execute(null,null));
444
445             assertEquals("",cr4.execute(null,null));
446
447             baton.pass();
448
449             assertEquals("b3",cr1.execute(null,null));
450
451             assertEquals("",cr4.execute(null,null));
452
453             baton.done();
454         }
455     }
456
457     public void CSVThread2() throws Exception JavaDoc
458     {
459         Thread.currentThread().setName("Two");
460         Thread.sleep(500);// Allow other thread to start
461
synchronized(baton){
462
463             assertEquals("b3",cr1.execute(null,null));
464             
465             assertEquals("",cr4.execute(null,null));
466             
467             baton.pass();
468             
469             assertEquals("b1",cr1.execute(null,null));
470
471             assertEquals("",cr4.execute(null,null));
472
473             assertEquals("b2",cr1.execute(null,null));
474             
475             baton.pass();
476
477             assertEquals("",cr4.execute(null,null));
478
479             assertEquals("b4",cr1.execute(null,null));
480
481             baton.done();
482         }
483     }
484
485     
486     public void CSVRun() throws Exception JavaDoc
487     {
488         assertEquals("b1",cr1.execute(null,null));
489         assertEquals("c1",cr2.execute(null,null));
490         assertEquals("d1",cr3.execute(null,null));
491
492         assertEquals("",cr4.execute(null,null));
493         assertEquals("b2",cr1.execute(null,null));
494         assertEquals("c2",cr2.execute(null,null));
495         assertEquals("d2",cr3.execute(null,null));
496
497         assertEquals("",cr4.execute(null,null));
498         assertEquals("b3",cr1.execute(null,null));
499         assertEquals("c3",cr2.execute(null,null));
500         assertEquals("d3",cr3.execute(null,null));
501
502         assertEquals("",cr4.execute(null,null));
503         assertEquals("b4",cr1.execute(null,null));
504         assertEquals("c4",cr2.execute(null,null));
505         assertEquals("d4",cr3.execute(null,null));
506
507         assertEquals("",cr4.execute(null,null));
508         assertEquals("b1",cr1.execute(null,null));
509         assertEquals("c1",cr2.execute(null,null));
510         assertEquals("d1",cr3.execute(null,null));
511         
512         assertEquals("a1",cr5.execute(null,null));
513         assertEquals("",cr6.execute(null,null));
514         assertEquals("a2",cr5.execute(null,null));
515         
516     }
517     
518     public void CSVParams() throws Exception JavaDoc
519     {
520         try {
521             setParams(null,null);
522             fail("Should have failed");
523         }
524         catch (InvalidVariableException e)
525         {
526         }
527         try {
528             setParams(null,"");
529             fail("Should have failed");
530         }
531         catch (InvalidVariableException e)
532         {
533         }
534         try {
535             setParams("",null);
536             fail("Should have failed");
537         }
538         catch (InvalidVariableException e)
539         {
540         }
541     }
542
543     public void CSVSetup() throws Exception JavaDoc
544     {
545         cr1=setParams("testfiles/test.csv","1");
546         cr2=setParams("testfiles/test.csv","2");
547         cr3=setParams("testfiles/test.csv","3");
548         cr4=setParams("testfiles/test.csv","next");
549         cr5=setParams("","0");
550         cr6=setParams("","next");
551     }
552     
553     public void CSValias() throws Exception JavaDoc
554     {
555         cr1 = setParams("testfiles/test.csv","*A");
556         cr2 = setParams("*A","1");
557         cr3 = setParams("*A","next");
558
559         cr4 = setParams("testfiles/test.csv","*B");
560         cr5 = setParams("*B","2");
561         cr6 = setParams("*B","next");
562
563         String JavaDoc s;
564         
565         s = cr1.execute(null,null); // open as *A
566
assertEquals("",s);
567         s = cr2.execute(null,null); // col 1, line 1, *A
568
assertEquals("b1",s);
569         
570
571         s = cr4.execute(null,null);// open as *B
572
assertEquals("",s);
573         s = cr5.execute(null,null);// col2 line 1
574
assertEquals("c1",s);
575         
576         s = cr3.execute(null,null);// *A next
577
assertEquals("",s);
578         s = cr2.execute(null,null);// col 1, line 2, *A
579
assertEquals("b2",s);
580         
581         s = cr5.execute(null,null);// col2, line 1, *B
582
assertEquals("c1",s);
583
584         s = cr6.execute(null,null);// *B next
585
assertEquals("",s);
586
587         s = cr5.execute(null,null);// col2, line 2, *B
588
assertEquals("c2",s);
589
590     }
591
592     public void CSVNoFile() throws Exception JavaDoc
593     {
594         String JavaDoc s;
595
596         cr1 = setParams("xtestfiles/test.csv","1");
597         log.info("Expecting file not found");
598         s = cr1.execute(null,null);
599         assertEquals("",s);
600
601         cr2 = setParams("xtestfiles/test.csv","next");
602         log.info("Expecting no entry for file");
603         s = cr2.execute(null,null);
604         assertEquals("",s);
605
606         cr3 = setParams("xtestfiles/test.csv","*ABC");
607         log.info("Expecting file not found");
608         s = cr3.execute(null,null);
609         assertEquals("",s);
610
611         cr4 = setParams("*ABC","1");
612         log.info("Expecting cannot open file");
613         s = cr4.execute(null,null);
614         assertEquals("",s);
615     }
616     
617     // Check blank lines are treated as EOF
618
public void CSVBlankLine() throws Exception JavaDoc
619     {
620         CSVRead csv1 = setParams("testfiles/testblank.csv","1");
621         CSVRead csv2 = setParams("testfiles/testblank.csv","next");
622         
623         String JavaDoc s;
624         
625         for (int i = 1; i<=2; i++)
626         {
627             s= csv1.execute(null,null);
628             assertEquals("b1",s);
629             
630             s= csv2.execute(null,null);
631             assertEquals("",s);
632             
633             s= csv1.execute(null,null);
634             assertEquals("b2",s);
635             
636             s= csv2.execute(null,null);
637             assertEquals("",s);
638         }
639         
640     }
641 }
642
Popular Tags