001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded.jsf;
034:
035: import com.flexive.faces.*;
036: import com.flexive.shared.content.FxPK;
037: import com.flexive.shared.exceptions.FxRuntimeException;
038: import static org.testng.Assert.assertEquals;
039: import static org.testng.Assert.assertTrue;
040: import org.testng.annotations.Test;
041:
042: import java.util.Arrays;
043: import java.util.HashMap;
044:
045: /**
046: * Tests for the {@link com.flexive.faces.ContentURIRoute} class.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: * @version $Rev: 1 $
050: */
051: public class URIRouteTest {
052: @Test(groups="jsf")
053: public void idContentUriTest() {
054: final ContentURIRoute contactMapper = new ContentURIRoute("",
055: "/contact/${id}.xhtml", "CONTACTDATA");
056: assertEquals(contactMapper.getMappedUri(new FxPK(21, 1)),
057: "/contact/21.xhtml");
058: final URIMatcher contactMatcher = contactMapper
059: .getMatcher("/contact/42.xhtml");
060: assertEquals(contactMatcher.isMatched(), true);
061: assertEquals(contactMatcher.getParameter("id"), "42");
062: }
063:
064: @Test(groups="jsf")
065: public void pkContentUriTest() {
066: final ContentURIRoute contactMapper = new ContentURIRoute("",
067: "/contact/${pk}/${id}.xhtml", "CONTACTDATA");
068: assertEquals(contactMapper.getMappedUri(new FxPK(21, 5)),
069: "/contact/21.5/21.xhtml");
070: final ContentURIMatcher contactMatcher = contactMapper
071: .getMatcher("/contact/21.5/21.xhtml");
072: assertEquals(contactMatcher.isMatched(), true);
073: assertEquals(contactMatcher.getParameter("id"), "21");
074: assertEquals(contactMatcher.getId(), 21);
075: assertEquals(
076: FxPK.fromString(contactMatcher.getParameter("pk")),
077: new FxPK(21, 5));
078: assertEquals(contactMatcher.getPk(), new FxPK(21, 5));
079: }
080:
081: @Test(groups="jsf")
082: public void pkContentUriTest2() {
083: final ContentURIRoute contactMapper = new ContentURIRoute("",
084: "/contact/${pk}/${id}.xhtml", "CONTACTDATA");
085: assertEquals(
086: contactMapper.getMappedUri(new FxPK(21, FxPK.MAX)),
087: "/contact/21.MAX/21.xhtml");
088: final ContentURIMatcher contactMatcher = contactMapper
089: .getMatcher("/contact/21.LIVE/21.xhtml");
090: assertEquals(contactMatcher.isMatched(), true);
091: assertEquals(contactMatcher.getParameter("id"), "21");
092: assertEquals(contactMatcher.getId(), 21);
093: assertEquals(
094: FxPK.fromString(contactMatcher.getParameter("pk")),
095: new FxPK(21, FxPK.LIVE));
096: assertEquals(contactMatcher.getPk(), new FxPK(21, FxPK.LIVE));
097: }
098:
099: @Test(groups="jsf")
100: public void invalidContentUriTest() {
101: for (String pattern : new String[] { "/contact/${id.xhtml",
102: "/contact/${}.xhtml", "/contact/${ }.xhtml",
103: "/contact/${unknown}.xhtml",
104: "/contact/${id}.${id}.xhtml" }) {
105: try {
106: new ContentURIRoute("", pattern, "TEST");
107: assert false : "Pattern "
108: + pattern
109: + " is invalid, but ContentURIMapper constructor succeeds.";
110: } catch (FxRuntimeException e) {
111: // pass
112: }
113: }
114: }
115:
116: private static class TestUriRoute extends URIRoute {
117: static {
118: PARAMETERS.put("param1", "(v\\d)");
119: PARAMETERS.put("param2", "(w\\d)");
120: }
121:
122: private TestUriRoute(String format) {
123: super ("", format);
124: }
125: }
126:
127: @Test(groups="jsf")
128: public void basicUriMapperTest() {
129: final URIRoute route = new TestUriRoute(
130: "/${param1}/misc/${param2}.xhtml");
131: final HashMap<String, String> parameters = new HashMap<String, String>();
132: parameters.put("param1", "v1");
133: parameters.put("param2", "w2");
134: assertEquals(route.getMappedUri(parameters),
135: "/v1/misc/w2.xhtml");
136: parameters.put("param3", "ignored");
137: assertEquals(route.getMappedUri(parameters),
138: "/v1/misc/w2.xhtml");
139: parameters.remove("param1");
140: try {
141: route.getMappedUri(parameters);
142: assert false : "Not all parameters specified";
143: } catch (FxRuntimeException e) {
144: // pass
145: }
146: final URIMatcher matcher = route
147: .getMatcher("/v8/misc/w3.xhtml");
148: assertTrue(matcher.isMatched());
149: assertEquals(matcher.getParameter("param1"), "v8");
150: assertEquals(matcher.getParameter("param2"), "w3");
151: }
152:
153: @Test(groups="jsf")
154: public void routeCollectionTest() {
155: final ContentURIRoute contactRoute = new ContentURIRoute(
156: "/contact.xhtml", "/c${id}.xhtml", "CONTACTDATA");
157: final ContentURIRoute folderRoute = new ContentURIRoute(
158: "/folder.xhtml", "/f${id}.xhtml", "FOLDER");
159: final URIRouteCollection<ContentURIRoute> coll = new URIRouteCollection<ContentURIRoute>(
160: Arrays.asList(contactRoute, folderRoute));
161: assertEquals(coll.findForTarget("/contact.xhtml"), contactRoute);
162: assertEquals(coll.findForTarget("/folder.xhtml"), folderRoute);
163: assertTrue(coll.getMatcher("/c25.xhtml").isMatched());
164: assertEquals(coll.findForUri("/c25.xhtml"), contactRoute);
165: assertEquals(coll.findForUri("/f25.xhtml"), folderRoute);
166: }
167: }
|