001: // Copyright 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.corelib.components;
016:
017: import static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.isA;
019:
020: import org.apache.tapestry.ComponentResources;
021: import org.apache.tapestry.beaneditor.BeanModel;
022: import org.apache.tapestry.integration.app1.data.RegistrationData;
023: import org.apache.tapestry.ioc.Location;
024: import org.apache.tapestry.ioc.internal.util.TapestryException;
025: import org.apache.tapestry.services.PropertyEditContext;
026: import org.apache.tapestry.services.BeanModelSource;
027: import org.apache.tapestry.services.Environment;
028: import org.apache.tapestry.test.TapestryTestCase;
029: import org.testng.annotations.Test;
030:
031: public class BeanEditFormTest extends TapestryTestCase {
032: @Test
033: public void object_created_as_needed() {
034: ComponentResources resources = mockComponentResources();
035: ComponentResources containerResources = mockComponentResources();
036: BeanModelSource source = mockBeanModelSource();
037: BeanModel model = mockBeanModel();
038: Environment environment = mockEnvironment();
039:
040: expect(resources.triggerEvent(Form.PREPARE, null, null))
041: .andReturn(false);
042:
043: train_push(environment, PropertyEditContext.class);
044:
045: train_getBoundType(resources, RegistrationData.class);
046:
047: train_getContainerResources(resources, containerResources);
048:
049: train_create(source, RegistrationData.class, true,
050: containerResources, model);
051:
052: replay();
053:
054: BeanEditForm component = new BeanEditForm();
055:
056: component.inject(resources, source, environment);
057:
058: assertTrue(component.onPrepareFromForm());
059:
060: Object object = component.getObject();
061:
062: assertNotNull(object);
063: assertSame(object.getClass(), RegistrationData.class);
064:
065: verify();
066: }
067:
068: @Test
069: public void object_can_not_be_instantiated() {
070: ComponentResources resources = mockComponentResources();
071: Location l = mockLocation();
072: Environment environment = mockEnvironment();
073:
074: expect(resources.triggerEvent(Form.PREPARE, null, null))
075: .andReturn(false);
076:
077: train_push(environment, PropertyEditContext.class);
078:
079: train_getBoundType(resources, Runnable.class);
080:
081: train_getCompleteId(resources, "Foo.bar");
082:
083: train_getLocation(resources, l);
084:
085: replay();
086:
087: BeanEditForm component = new BeanEditForm();
088:
089: component.inject(resources, null, environment);
090:
091: try {
092: component.onPrepareFromForm();
093: unreachable();
094: } catch (TapestryException ex) {
095: assertMessageContains(
096: ex,
097: "Exception instantiating instance of java.lang.Runnable (for component \'Foo.bar\'):");
098: assertSame(ex.getLocation(), l);
099: }
100:
101: verify();
102: }
103:
104: protected final <T> void train_push(Environment environment,
105: Class<T> type) {
106: expect(environment.push(eq(type), isA(type))).andReturn(null);
107: }
108: }
|