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: import java.util.Collection;
019: import java.util.Map;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.tapestry.internal.test.InternalBaseTestCase;
023: import org.apache.tapestry.services.AliasContribution;
024: import org.apache.tapestry.services.AliasManager;
025: import org.testng.annotations.Test;
026:
027: public class AliasManagerImplTest extends InternalBaseTestCase {
028: @Test
029: public void no_conflict() {
030: Log log = mockLog();
031: Runnable r = mockRunnable();
032:
033: replay();
034:
035: AliasContribution[] contributions = {
036: AliasContribution.create(String.class, "FRED"),
037: AliasContribution.create(Runnable.class, r) };
038: Collection<AliasContribution> configuration = Arrays
039: .asList(contributions);
040:
041: AliasManager manager = new AliasManagerImpl(log, configuration);
042:
043: Map<Class, Object> map = manager.getAliasesForMode("foo");
044:
045: assertEquals(map.size(), 2);
046: assertEquals(map.get(String.class), "FRED");
047: assertSame(map.get(Runnable.class), r);
048:
049: verify();
050: }
051:
052: @Test
053: public void first_entry_wins_on_conflict() {
054: Log log = mockLog();
055: Runnable r = mockRunnable();
056:
057: log
058: .error("Contribution FRED-CONFLICT (for type java.lang.String) conflicts with existing contribution FRED and has been ignored.");
059:
060: replay();
061:
062: AliasContribution[] contributions = {
063: AliasContribution.create(String.class, "FRED"),
064: AliasContribution.create(String.class, "FRED-CONFLICT"),
065: AliasContribution.create(Runnable.class, r) };
066: Collection<AliasContribution> configuration = Arrays
067: .asList(contributions);
068:
069: AliasManager manager = new AliasManagerImpl(log, configuration);
070:
071: Map<Class, Object> map = manager.getAliasesForMode("foo");
072:
073: assertEquals(map.size(), 2);
074: assertEquals(map.get(String.class), "FRED");
075: assertSame(map.get(Runnable.class), r);
076:
077: verify();
078: }
079:
080: @Test
081: public void contributions_to_other_modes_are_ignored() {
082: Log log = mockLog();
083: Runnable r = mockRunnable();
084:
085: replay();
086:
087: AliasContribution[] contributions = {
088: AliasContribution.create(String.class, "FRED"),
089: AliasContribution.create(String.class, "bar",
090: "FRED-NON-CONFLICT"),
091: AliasContribution.create(Runnable.class, r) };
092: Collection<AliasContribution> configuration = Arrays
093: .asList(contributions);
094:
095: AliasManager manager = new AliasManagerImpl(log, configuration);
096:
097: Map<Class, Object> map = manager.getAliasesForMode("foo");
098:
099: assertEquals(map.size(), 2);
100: assertEquals(map.get(String.class), "FRED");
101: assertSame(map.get(Runnable.class), r);
102:
103: verify();
104: }
105:
106: @Test
107: public void mode_specific_contribution_overrides_general_contribution() {
108: Log log = mockLog();
109: Runnable r = mockRunnable();
110:
111: replay();
112:
113: AliasContribution[] contributions = {
114: AliasContribution.create(String.class, "FRED"),
115: AliasContribution.create(String.class, "bar",
116: "FRED-NON-CONFLICT"),
117: AliasContribution.create(Runnable.class, r) };
118: Collection<AliasContribution> configuration = Arrays
119: .asList(contributions);
120:
121: AliasManager manager = new AliasManagerImpl(log, configuration);
122:
123: Map<Class, Object> map = manager.getAliasesForMode("BAR");
124:
125: assertEquals(map.size(), 2);
126: assertEquals(map.get(String.class), "FRED-NON-CONFLICT");
127: assertSame(map.get(Runnable.class), r);
128:
129: verify();
130: }
131: }
|