001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.tests.framework.container;
016:
017: import java.lang.reflect.Field;
018: import java.util.HashMap;
019: import junit.framework.TestCase;
020: import org.araneaframework.Widget;
021: import org.araneaframework.core.StandardEnvironment;
022: import org.araneaframework.core.util.ExceptionUtil;
023: import org.araneaframework.framework.FlowContext;
024: import org.araneaframework.framework.container.StandardFlowContainerWidget;
025: import org.araneaframework.mock.core.MockEventfulBaseWidget;
026:
027: /**
028: * @author "Toomas Römer" <toomas@webmedia.ee>
029: *
030: */
031: public class StandardFlowContainerWidgetTests extends TestCase {
032: private StandardFlowContainerWidget stackWidget;
033: private MockEventfulBaseWidget topWidget;
034: private MockEventfulBaseWidget childWidget;
035: private MockEventfulBaseWidget childWidget2;
036: private StandardEnvironment env;
037:
038: private static String BASE_FLOW_KEY;
039: private static String TOP_FLOW_KEY;
040:
041: static {
042: try {
043: Field f = StandardFlowContainerWidget.class
044: .getDeclaredField("BASE_FLOW_KEY");
045: f.setAccessible(true);
046: BASE_FLOW_KEY = (String) f
047: .get(new StandardFlowContainerWidget());
048:
049: Field g = StandardFlowContainerWidget.class
050: .getDeclaredField("TOP_FLOW_KEY");
051: g.setAccessible(true);
052: TOP_FLOW_KEY = (String) g
053: .get(new StandardFlowContainerWidget());
054: } catch (Exception e) {
055: ExceptionUtil.uncheckException(e);
056: }
057: }
058:
059: public void setUp() throws Exception {
060: topWidget = new MockEventfulBaseWidget();
061: childWidget = new MockEventfulBaseWidget();
062: childWidget2 = new MockEventfulBaseWidget();
063:
064: env = new StandardEnvironment(null, new HashMap());
065:
066: stackWidget = new StandardFlowContainerWidget(topWidget);
067: stackWidget._getComponent().init(null, env);
068: }
069:
070: public void testCallingContract() throws Exception {
071: MemoizingFlowContextConfigurator configurator = new MemoizingFlowContextConfigurator();
072: MemoizingFlowContextHandler handler = new MemoizingFlowContextHandler();
073: stackWidget.start(childWidget, configurator, handler);
074:
075: assertTrue(topWidget.isDisableCalled());
076: assertTrue(configurator.isConfigured());
077: assertEquals(childWidget, stackWidget.getChildren().get(
078: BASE_FLOW_KEY + "1"));
079: }
080:
081: public void testCancelCallContract() throws Exception {
082: MemoizingFlowContextConfigurator configurator = new MemoizingFlowContextConfigurator();
083: MemoizingFlowContextHandler handler = new MemoizingFlowContextHandler();
084: stackWidget.start(childWidget, configurator, handler);
085:
086: stackWidget.cancel();
087: assertTrue(handler.isCancelled());
088: assertTrue(topWidget.isEnableCalled());
089: assertEquals(topWidget, stackWidget.getChildren().get(
090: TOP_FLOW_KEY));
091: }
092:
093: public void testReturnCallContract() throws Exception {
094: MemoizingFlowContextConfigurator configurator = new MemoizingFlowContextConfigurator();
095: MemoizingFlowContextHandler handler = new MemoizingFlowContextHandler();
096: stackWidget.start(childWidget, configurator, handler);
097:
098: stackWidget.finish("returnCall");
099: assertTrue("returnCall".equals(handler.getReturnValue()));
100: assertTrue(topWidget.isEnableCalled());
101: assertEquals(topWidget, stackWidget.getChildren().get(
102: TOP_FLOW_KEY));
103: }
104:
105: public void testDestroyDestroysChildrenOnStack() throws Exception {
106: stackWidget.start(childWidget,
107: new MemoizingFlowContextConfigurator(),
108: new MemoizingFlowContextHandler());
109: stackWidget.start(childWidget2,
110: new MemoizingFlowContextConfigurator(),
111: new MemoizingFlowContextHandler());
112:
113: stackWidget._getComponent().destroy();
114:
115: assertEquals(true, childWidget.getDestroyCalled());
116: assertEquals(true, childWidget2.getDestroyCalled());
117: }
118:
119: protected static class MemoizingFlowContextHandler implements
120: FlowContext.Handler {
121: private static final long serialVersionUID = 1L;
122: private Object returnValue = null;
123: private boolean finished;
124: private boolean cancelled;
125:
126: public void onCancel() throws Exception {
127: cancelled = true;
128: }
129:
130: public void onFinish(Object returnValue) throws Exception {
131: finished = true;
132: this .returnValue = returnValue;
133: }
134:
135: public Object getReturnValue() {
136: return returnValue;
137: }
138:
139: public boolean isFinished() {
140: return finished;
141: }
142:
143: public boolean isCancelled() {
144: return cancelled;
145: }
146: }
147:
148: protected static class MemoizingFlowContextConfigurator implements
149: FlowContext.Configurator {
150: private static final long serialVersionUID = 1L;
151: private boolean configured;
152: private Widget flow;
153:
154: public void configure(Widget flow) throws Exception {
155: this .configured = true;
156: this .flow = flow;
157: }
158:
159: public boolean isConfigured() {
160: return configured;
161: }
162:
163: public Widget getFlow() {
164: return flow;
165: }
166: }
167: }
|