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 java.util.Map;
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.cocoon.SitemapComponentTestCase;
025: import org.apache.cocoon.environment.mock.MockCookie;
026:
027: public class CookieSelectorTestCase extends SitemapComponentTestCase {
028:
029: private static final String COOKIE_SELECTOR = "cookie";
030:
031: /**
032: * Run this test suite from commandline
033: *
034: * @param args commandline arguments (ignored)
035: */
036: public static void main(String[] args) {
037: TestRunner.run(suite());
038: }
039:
040: /** Create a test suite.
041: * This test suite contains all test cases of this class.
042: * @return the Test object containing all test cases.
043: */
044: public static Test suite() {
045: TestSuite suite = new TestSuite(CookieSelectorTestCase.class);
046: return suite;
047: }
048:
049: /**
050: * A simple cookie select test
051: */
052: public void testCookieSelect() throws Exception {
053: final String cookieName = "cookieSelectorTestCase";
054: final String cookieValue = "cookieValue";
055: // create a cookie
056: // setting name := cookieName, value := cookieValue
057: Map cookies = getRequest().getCookieMap();
058: MockCookie mockCookie = new MockCookie();
059: mockCookie.setName(cookieName);
060: mockCookie.setValue(cookieValue);
061: cookies.put(cookieName, mockCookie);
062:
063: Parameters parameters = new Parameters();
064: boolean result;
065:
066: // test selection success
067: result = this .select(COOKIE_SELECTOR, cookieValue, parameters);
068: System.out.println(result);
069: assertTrue("Test if a cookie is selected", result);
070:
071: // test selection failure
072: result = this .select(COOKIE_SELECTOR, "unknownCookieValue",
073: parameters);
074: System.out.println(result);
075: assertTrue("Test if a cookie is not selected", !result);
076: }
077:
078: /**
079: * A simple cookie select test
080: */
081: public void testCookieSelectUsingParameters() throws Exception {
082: final String cookieName = "cookieSelectorTestCase1";
083: final String cookieValue = "cookieValue";
084:
085: // create a cookie
086: // setting name := cookieName, value := cookieValue
087: Map cookies = getRequest().getCookieMap();
088: MockCookie mockCookie = new MockCookie();
089: // this cookie shall get selected
090: mockCookie.setName(cookieName);
091: mockCookie.setValue(cookieValue);
092: cookies.put(cookieName, mockCookie);
093:
094: // this cookie shall be ignored, as its name differs
095: // from the parameterized cookie name
096: mockCookie = new MockCookie();
097: mockCookie.setName("cookieSelectorTestCase");
098: mockCookie.setValue("unknownCookieValue");
099: cookies.put("cookieSelectorTestCase", mockCookie);
100:
101: // check the cookie as defined by this parameter, not as
102: // defined in the component configuration
103: Parameters parameters = new Parameters();
104: parameters.setParameter("cookie-name", cookieName);
105:
106: boolean result;
107:
108: // test selection success
109: result = this .select(COOKIE_SELECTOR, cookieValue, parameters);
110: System.out.println(result);
111: assertTrue("Test if a cookie is selected", result);
112:
113: // test selection failure
114: result = this .select(COOKIE_SELECTOR, "unknownCookieValue",
115: parameters);
116: System.out.println(result);
117: assertTrue("Test if a cookie is not selected", !result);
118: }
119: }
|