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: package org.apache.cocoon.selection;
018:
019: import junit.framework.Test;
020: import junit.framework.TestSuite;
021: import junit.textui.TestRunner;
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.cocoon.SitemapComponentTestCase;
024: import org.apache.cocoon.environment.Session;
025:
026: public class SessionAttributeSelectorTestCase extends
027: SitemapComponentTestCase {
028:
029: /**
030: * Run this test suite from commandline
031: *
032: * @param args commandline arguments (ignored)
033: */
034: public static void main(String[] args) {
035: TestRunner.run(suite());
036: }
037:
038: /** Create a test suite.
039: * This test suite contains all test cases of this class.
040: * @return the Test object containing all test cases.
041: */
042: public static Test suite() {
043: TestSuite suite = new TestSuite(
044: SessionAttributeSelectorTestCase.class);
045: return suite;
046: }
047:
048: /**
049: * A session-attribute select test
050: */
051: public void testSessionAttributeSelect() throws Exception {
052: final String attributeName = "sessionAttributeSelector";
053: final String attributeValue = "sessionAttributeSelectorValue";
054:
055: Session session = getRequest().getSession(true);
056: session.setAttribute(attributeName, attributeValue);
057: Parameters parameters = new Parameters();
058: boolean result;
059:
060: // test selection success
061: result = this .select("session-attribute", attributeValue,
062: parameters);
063: System.out.println(result);
064: assertTrue("Test if a session attribtue is selected", result);
065:
066: // test selection failure
067: result = this .select("session-attribute", "unknownValue",
068: parameters);
069: System.out.println(result);
070: assertTrue("Test if a session attribute is not selected",
071: !result);
072: }
073:
074: /**
075: * A session-attribute select test
076: */
077: public void testSessionAttributeSelectOverridden() throws Exception {
078: final String attributeName = "sessionAttributeSelector1";
079: final String attributeValue = "sessionAttributeSelectorValue1";
080: Session session = getRequest().getSession(true);
081: session.setAttribute(attributeName, attributeValue);
082:
083: final String attributeNameOverridden = "sessionAttributeSelector";
084: final String attributeValueOverridden = "sessionAttributeSelectorValue";
085: session.setAttribute(attributeNameOverridden,
086: attributeValueOverridden);
087:
088: Parameters parameters = new Parameters();
089: parameters.setParameter("attribute-name", attributeName);
090: boolean result;
091:
092: // test selection success
093: result = this .select("session-attribute", attributeValue,
094: parameters);
095: System.out.println(result);
096: assertTrue("Test if a requst attribtue is selected", result);
097:
098: // test selection failure
099: result = this .select("session-attribute",
100: attributeValueOverridden, parameters);
101: System.out.println(result);
102: assertTrue("Test if a session attribute is not selected",
103: !result);
104: }
105:
106: /**
107: * A session-attribute select test
108: */
109: public void testSessionAttributeSelectMissingSession()
110: throws Exception {
111: final String attributeValue = "sessionAttributeSelectorValue";
112:
113: // test w/o session
114: getRequest().clearSession();
115:
116: Parameters parameters = new Parameters();
117: boolean result;
118:
119: // test selection fails
120: result = this .select("session-attribute", attributeValue,
121: parameters);
122: System.out.println(result);
123: assertTrue("Test if a session attribtue is not selected",
124: !result);
125: }
126: }
|