KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.SitemapComponentTestCase;
24 import org.apache.cocoon.environment.mock.MockCookie;
25
26 public class CookieSelectorTestCase extends SitemapComponentTestCase {
27
28     private static final String JavaDoc COOKIE_SELECTOR = "cookie";
29     
30     /**
31      * Run this test suite from commandline
32      *
33      * @param args commandline arguments (ignored)
34      */

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

43     public static Test suite() {
44         TestSuite suite = new TestSuite(CookieSelectorTestCase.class);
45         return suite;
46     }
47     
48     /**
49      * A simple cookie select test
50      */

51     public void testCookieSelect() throws Exception JavaDoc {
52         final String JavaDoc cookieName = "cookieSelectorTestCase";
53         final String JavaDoc cookieValue = "cookieValue";
54         // create a cookie
55
// setting name := cookieName, value := cookieValue
56
Map JavaDoc cookies = getRequest().getCookieMap();
57         MockCookie mockCookie = new MockCookie();
58         mockCookie.setName( cookieName);
59         mockCookie.setValue( cookieValue );
60         cookies.put( cookieName, mockCookie );
61         
62         Parameters parameters = new Parameters();
63         boolean result;
64         
65         // test selection success
66
result = this.select( COOKIE_SELECTOR, cookieValue, parameters );
67         System.out.println( result );
68         assertTrue( "Test if a cookie is selected", result );
69         
70         // test selection failure
71
result = this.select( COOKIE_SELECTOR, "unknownCookieValue", parameters );
72         System.out.println( result );
73         assertTrue( "Test if a cookie is not selected", !result );
74     }
75
76     /**
77      * A simple cookie select test
78      */

79     public void testCookieSelectUsingParameters() throws Exception JavaDoc {
80         final String JavaDoc cookieName = "cookieSelectorTestCase1";
81         final String JavaDoc cookieValue = "cookieValue";
82         
83         // create a cookie
84
// setting name := cookieName, value := cookieValue
85
Map JavaDoc cookies = getRequest().getCookieMap();
86         MockCookie mockCookie = new MockCookie();
87         // this cookie shall get selected
88
mockCookie.setName( cookieName);
89         mockCookie.setValue( cookieValue );
90         cookies.put( cookieName, mockCookie );
91         
92         // this cookie shall be ignored, as its name differs
93
// from the parameterized cookie name
94
mockCookie = new MockCookie();
95         mockCookie.setName( "cookieSelectorTestCase" );
96         mockCookie.setValue( "unknownCookieValue" );
97         cookies.put( "cookieSelectorTestCase", mockCookie );
98
99         // check the cookie as defined by this parameter, not as
100
// defined in the component configuration
101
Parameters parameters = new Parameters();
102         parameters.setParameter( "cookie-name", cookieName );
103         
104         boolean result;
105         
106         // test selection success
107
result = this.select( COOKIE_SELECTOR, cookieValue, parameters );
108         System.out.println( result );
109         assertTrue( "Test if a cookie is selected", result );
110         
111         // test selection failure
112
result = this.select( COOKIE_SELECTOR, "unknownCookieValue", parameters );
113         System.out.println( result );
114         assertTrue( "Test if a cookie is not selected", !result );
115     }
116 }
117
Popular Tags