KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.cocoon.environment.Session;
24
25 public class SessionAttributeSelectorTestCase extends SitemapComponentTestCase {
26
27     /**
28      * Run this test suite from commandline
29      *
30      * @param args commandline arguments (ignored)
31      */

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

40     public static Test suite() {
41         TestSuite suite = new TestSuite(SessionAttributeSelectorTestCase.class);
42         return suite;
43     }
44     
45     /**
46      * A session-attribute select test
47      */

48     public void testSessionAttributeSelect() throws Exception JavaDoc {
49         final String JavaDoc attributeName = "sessionAttributeSelector";
50         final String JavaDoc attributeValue = "sessionAttributeSelectorValue";
51         
52         Session session = getRequest().getSession(true);
53         session.setAttribute( attributeName, attributeValue );
54         Parameters parameters = new Parameters();
55         boolean result;
56         
57         // test selection success
58
result = this.select( "session-attribute", attributeValue, parameters );
59         System.out.println( result );
60         assertTrue( "Test if a session attribtue is selected", result );
61         
62         // test selection failure
63
result = this.select( "session-attribute", "unknownValue", parameters );
64         System.out.println( result );
65         assertTrue( "Test if a session attribute is not selected", !result );
66     }
67
68     /**
69      * A session-attribute select test
70      */

71     public void testSessionAttributeSelectOverridden() throws Exception JavaDoc {
72         final String JavaDoc attributeName = "sessionAttributeSelector1";
73         final String JavaDoc attributeValue = "sessionAttributeSelectorValue1";
74         Session session = getRequest().getSession(true);
75         session.setAttribute( attributeName, attributeValue );
76         
77         final String JavaDoc attributeNameOverridden = "sessionAttributeSelector";
78         final String JavaDoc attributeValueOverridden = "sessionAttributeSelectorValue";
79         session.setAttribute( attributeNameOverridden, attributeValueOverridden );
80         
81         Parameters parameters = new Parameters();
82         parameters.setParameter( "attribute-name", attributeName );
83         boolean result;
84         
85         // test selection success
86
result = this.select( "session-attribute", attributeValue, parameters );
87         System.out.println( result );
88         assertTrue( "Test if a requst attribtue is selected", result );
89         
90         // test selection failure
91
result = this.select( "session-attribute", attributeValueOverridden, parameters );
92         System.out.println( result );
93         assertTrue( "Test if a session attribute is not selected", !result );
94     }
95     
96     /**
97      * A session-attribute select test
98      */

99     public void testSessionAttributeSelectMissingSession() throws Exception JavaDoc {
100         final String JavaDoc attributeValue = "sessionAttributeSelectorValue";
101
102         // test w/o session
103
getRequest().clearSession();
104         
105         Parameters parameters = new Parameters();
106         boolean result;
107         
108         // test selection fails
109
result = this.select( "session-attribute", attributeValue, parameters );
110         System.out.println( result );
111         assertTrue( "Test if a session attribtue is not selected", !result );
112     }
113 }
114
Popular Tags