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.List;
018: import java.util.Map;
019: import java.util.NoSuchElementException;
020:
021: import org.apache.tapestry.ioc.Location;
022: import org.apache.tapestry.runtime.Component;
023: import org.apache.tapestry.services.Environment;
024: import org.apache.tapestry.test.TapestryTestCase;
025: import org.testng.annotations.Test;
026:
027: public class EnvironmentImplTest extends TapestryTestCase {
028: @Test
029: public void peek_when_empty_returns_null() {
030: Environment e = new EnvironmentImpl();
031:
032: assertNull(e.peek(Runnable.class));
033: assertNull(e.peek(Map.class));
034: }
035:
036: @Test
037: public void push_and_pop() {
038: Environment e = new EnvironmentImpl();
039: Runnable r1 = mockRunnable();
040: Runnable r2 = mockRunnable();
041:
042: replay();
043:
044: assertNull(e.push(Runnable.class, r1));
045:
046: assertSame(r1, e.peek(Runnable.class));
047:
048: assertSame(r1, e.push(Runnable.class, r2));
049:
050: assertSame(r2, e.peek(Runnable.class));
051:
052: assertSame(r2, e.pop(Runnable.class));
053: assertSame(r1, e.pop(Runnable.class));
054:
055: verify();
056: }
057:
058: @Test
059: public void clear() {
060: Environment e = new EnvironmentImpl();
061: Runnable r1 = mockRunnable();
062: Runnable r2 = mockRunnable();
063:
064: replay();
065:
066: e.push(Runnable.class, r1);
067: e.push(Runnable.class, r2);
068:
069: e.clear();
070:
071: assertNull(e.peek(Runnable.class));
072:
073: verify();
074: }
075:
076: @Test
077: public void pop_when_empty_is_error() {
078: Environment e = new EnvironmentImpl();
079:
080: try {
081: e.pop(Runnable.class);
082: unreachable();
083: } catch (NoSuchElementException ex) {
084: }
085: }
086:
087: @Test
088: public void peek_required_when_available() {
089: Environment e = new EnvironmentImpl();
090: Location l = mockLocation();
091:
092: replay();
093:
094: e.push(Location.class, l);
095:
096: assertSame(l, e.peekRequired(Location.class));
097:
098: verify();
099: }
100:
101: @Test
102: public void peek_required_without_value_is_error() {
103: Environment e = new EnvironmentImpl();
104: Location l = mockLocation();
105: Component c = mockComponent();
106:
107: replay();
108:
109: e.push(Location.class, l);
110: e.push(Component.class, c);
111:
112: try {
113: e.peekRequired(List.class);
114: unreachable();
115: } catch (RuntimeException ex) {
116: assertEquals(
117: ex.getMessage(),
118: "No object of type java.util.List is available from the Environment. Available types are org.apache.tapestry.ioc.Location, org.apache.tapestry.runtime.Component.");
119: }
120:
121: verify();
122: }
123: }
|