KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > RegexpHeaderSelectorTestCase


1 /*
2 * Copyright 1999-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 package org.apache.cocoon.selection;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20 import junit.textui.TestRunner;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.SitemapComponentTestCase;
23
24 /**
25  * Test case for RegexpHeaderSelector.
26  *
27  * @version CVS $Id: RegexpHeaderSelectorTestCase.java 158726 2005-03-23 05:00:32Z crossley $
28  */

29 public class RegexpHeaderSelectorTestCase extends SitemapComponentTestCase {
30
31     private static final String JavaDoc REGEXP_HEADER_SELECTOR = "regexp-header";
32
33     /**
34      * Run this test suite from commandline
35      *
36      * @param args commandline arguments (ignored)
37      */

38     public static void main( String JavaDoc[] args ) {
39         TestRunner.run(suite());
40     }
41     
42     /** Create a test suite.
43      * This test suite contains all test cases of this class.
44      * @return the Test object containing all test cases.
45      */

46     public static Test suite() {
47         TestSuite suite = new TestSuite(RegexpHeaderSelectorTestCase.class);
48         return suite;
49     }
50     
51
52     /**
53      * A simple regexp-header selector test
54      */

55     public void testRegexpHeaderSelectEmpty() throws Exception JavaDoc {
56         // create a header
57
final String JavaDoc headerName = "headerSelectorTestCase";
58         getRequest().setHeader(headerName, "");
59         
60         Parameters parameters = new Parameters();
61         boolean result;
62         
63         result = this.select( REGEXP_HEADER_SELECTOR, "empty", parameters );
64         System.out.println( result );
65         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " selects successfully", result );
66         
67         result = this.select( REGEXP_HEADER_SELECTOR, "number", parameters );
68         System.out.println( result );
69         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " does not select successfully", !result );
70         
71         result = this.select( REGEXP_HEADER_SELECTOR, "non-defined-name", parameters );
72         System.out.println( result );
73         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " does not select successfully", !result );
74     }
75
76     /**
77      * A simple regexp-header selector test
78      */

79     public void testRegexpHeaderSelectNumber() throws Exception JavaDoc {
80         // create a header
81
final String JavaDoc headerName = "headerSelectorTestCase";
82         final String JavaDoc headerName2 = "headerSelectorTestCase1";
83
84         Parameters parameters = new Parameters();
85         boolean result;
86         
87         // test w/o set request parameter
88
result = this.select( REGEXP_HEADER_SELECTOR, "empty", parameters );
89         System.out.println( result );
90         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " does not select successfully", !result );
91
92     // this time, set the header
93
getRequest().setHeader(headerName, "");
94
95     // create another header
96
getRequest().setHeader(headerName2, "123");
97
98         // override configured header name
99
parameters.setParameter( "header-name", headerName2 );
100         
101         result = this.select( REGEXP_HEADER_SELECTOR, "empty", parameters );
102         System.out.println( result );
103         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " does not select successfully", !result );
104
105         result = this.select( REGEXP_HEADER_SELECTOR, "number", parameters );
106         System.out.println( result );
107         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " selects successfully", result );
108
109         result = this.select( REGEXP_HEADER_SELECTOR, "non-defined-name", parameters );
110         System.out.println( result );
111         assertTrue( "Test is " + REGEXP_HEADER_SELECTOR + " does not select successfully", !result );
112     }
113 }
114
Popular Tags