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 static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.Map;
020:
021: import org.apache.tapestry.Binding;
022: import org.apache.tapestry.ComponentResources;
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.ioc.Location;
025: import org.apache.tapestry.ioc.internal.util.TapestryException;
026: import org.apache.tapestry.services.BindingFactory;
027: import org.apache.tapestry.services.BindingSource;
028: import org.testng.annotations.Test;
029:
030: public class BindingSourceImplTest extends InternalBaseTestCase {
031: @Test
032: public void expression_has_no_prefix() {
033: BindingFactory factory = mockBindingFactory();
034: Binding binding = mockBinding();
035: ComponentResources container = mockComponentResources();
036: ComponentResources component = mockComponentResources();
037: Location l = mockLocation();
038:
039: String defaultPrefix = "def";
040: String description = "descrip";
041: String expression = "full expression";
042:
043: train_newBinding(factory, description, container, component,
044: expression, l, binding);
045:
046: replay();
047:
048: Map<String, BindingFactory> map = newMap();
049:
050: map.put(defaultPrefix, factory);
051:
052: BindingSource source = new BindingSourceImpl(map);
053:
054: Binding actual = source.newBinding(description, container,
055: component, defaultPrefix, expression, l);
056:
057: assertSame(actual, binding);
058:
059: verify();
060: }
061:
062: @Test
063: public void expression_prefix_not_in_configuration() {
064: BindingFactory factory = mockBindingFactory();
065: Binding binding = mockBinding();
066: ComponentResources container = mockComponentResources();
067: ComponentResources component = mockComponentResources();
068: Location l = mockLocation();
069:
070: String defaultPrefix = "def";
071: String description = "descrip";
072: String expression = "javascript:not-a-known-prefix";
073:
074: train_newBinding(factory, description, container, component,
075: expression, l, binding);
076:
077: replay();
078:
079: Map<String, BindingFactory> map = newMap();
080:
081: map.put(defaultPrefix, factory);
082:
083: BindingSource source = new BindingSourceImpl(map);
084:
085: Binding actual = source.newBinding(description, container,
086: component, defaultPrefix, expression, l);
087:
088: assertSame(actual, binding);
089:
090: verify();
091: }
092:
093: @Test
094: public void known_prefix() {
095: BindingFactory factory = mockBindingFactory();
096: Binding binding = mockBinding();
097: ComponentResources container = mockComponentResources();
098: ComponentResources component = mockComponentResources();
099: Location l = mockLocation();
100:
101: String defaultPrefix = "literal";
102: String description = "descrip";
103:
104: // The "prop:" prefix is stripped off ...
105: train_newBinding(factory, description, container, component,
106: "myproperty", l, binding);
107:
108: replay();
109:
110: Map<String, BindingFactory> map = newMap();
111:
112: map.put("prop", factory);
113:
114: BindingSource source = new BindingSourceImpl(map);
115:
116: Binding actual = source.newBinding(description, container,
117: component, defaultPrefix, "prop:myproperty", l);
118:
119: assertSame(actual, binding);
120:
121: verify();
122: }
123:
124: @Test
125: public void factory_throws_exception() {
126: BindingFactory factory = mockBindingFactory();
127: ComponentResources container = mockComponentResources();
128: ComponentResources component = mockComponentResources();
129: Location l = mockLocation();
130: Throwable t = new RuntimeException("Simulated failure.");
131:
132: String defaultPrefix = "def";
133: String description = "descrip";
134: String expression = "full expression";
135:
136: factory.newBinding(description, container, component,
137: expression, l);
138: setThrowable(t);
139:
140: replay();
141:
142: Map<String, BindingFactory> map = newMap();
143:
144: map.put(defaultPrefix, factory);
145:
146: BindingSource source = new BindingSourceImpl(map);
147:
148: try {
149: source.newBinding(description, container, component,
150: defaultPrefix, expression, l);
151: unreachable();
152: } catch (TapestryException ex) {
153: assertTrue(ex
154: .getMessage()
155: .contains(
156: "Could not convert 'full expression' into a component parameter binding"));
157: assertTrue(ex.getMessage().contains(t.getMessage()));
158: assertSame(ex.getLocation(), l);
159: assertSame(ex.getCause(), t);
160: }
161:
162: verify();
163: }
164: }
|