001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.util.Arrays;
018:
019: import org.apache.tapestry.Link;
020: import org.apache.tapestry.internal.test.InternalBaseTestCase;
021: import org.apache.tapestry.services.Response;
022: import org.testng.annotations.Test;
023:
024: public class LinkImplTest extends InternalBaseTestCase {
025: private static final String ENCODED = "*encoded*";
026:
027: @Test
028: public void url_with_parameters() {
029: Response response = mockResponse();
030:
031: train_encodeURL(response,
032: "/foo/bar?barney=rubble&fred=flintstone", ENCODED);
033:
034: replay();
035:
036: Link link = new LinkImpl(response, "/foo", "bar");
037:
038: link.addParameter("fred", "flintstone");
039: link.addParameter("barney", "rubble");
040:
041: assertEquals(link.toURI(), ENCODED);
042:
043: verify();
044: }
045:
046: @Test
047: public void retrieve_parameter_values() {
048: Response response = mockResponse();
049:
050: replay();
051:
052: Link link = new LinkImpl(response, "/foo", "bar");
053:
054: link.addParameter("fred", "flintstone");
055: link.addParameter("barney", "rubble");
056:
057: assertEquals(link.getParameterValue("fred"), "flintstone");
058: assertEquals(link.getParameterValue("barney"), "rubble");
059: assertNull(link.getParameterValue("wilma"));
060:
061: verify();
062: }
063:
064: @Test
065: public void parameter_names_are_returned_sorted() {
066: Response response = mockResponse();
067:
068: replay();
069:
070: Link link = new LinkImpl(response, "/foo", "bar");
071:
072: link.addParameter("fred", "flintstone");
073: link.addParameter("barney", "rubble");
074:
075: assertEquals(link.getParameterNames(), Arrays.asList("barney",
076: "fred"));
077:
078: verify();
079: }
080:
081: @Test
082: public void parameter_names_must_be_unique() {
083: Response response = mockResponse();
084:
085: replay();
086:
087: Link link = new LinkImpl(response, "/foo", "bar");
088:
089: link.addParameter("fred", "flintstone");
090: try {
091: link.addParameter("fred", "flintstone");
092: unreachable();
093: } catch (IllegalArgumentException ex) {
094: assertEquals(
095: ex.getMessage(),
096: "Parameter names are required to be unique. Parameter 'fred' already has the value 'flintstone'.");
097: }
098:
099: verify();
100: }
101:
102: @Test
103: public void to_form_URI_does_not_include_parameters() {
104: Response response = mockResponse();
105:
106: train_encodeURL(response, "/foo/bar", ENCODED);
107:
108: replay();
109:
110: Link link = new LinkImpl(response, "/foo", "bar", true);
111:
112: link.addParameter("fred", "flintstone");
113: link.addParameter("barney", "rubble");
114:
115: assertEquals(link.toURI(), ENCODED);
116:
117: verify();
118: }
119: }
|