001 // Copyright 2005 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.annotations; 016 017 import java.lang.reflect.Method; 018 019 import org.apache.hivemind.ApplicationRuntimeException; 020 import org.apache.hivemind.Location; 021 import org.apache.tapestry.enhance.EnhancementOperation; 022 import org.apache.tapestry.spec.BindingType; 023 import org.apache.tapestry.spec.ComponentSpecification; 024 import org.apache.tapestry.spec.IBindingSpecification; 025 import org.apache.tapestry.spec.IComponentSpecification; 026 import org.apache.tapestry.spec.IContainedComponent; 027 import org.testng.annotations.Test; 028 029 /** 030 * Tests for {@link org.apache.tapestry.annotations.ComponentAnnotationWorker} 031 * 032 * @author Howard Lewis Ship 033 * @since 4.0 034 */ 035 @Test 036 public class TestComponentAnnotationWorker extends BaseAnnotationTestCase 037 { 038 private IContainedComponent run(String id, String methodName, Location location) 039 { 040 IComponentSpecification spec = new ComponentSpecification(); 041 042 return run(spec, id, methodName, location); 043 } 044 045 private IContainedComponent run(IComponentSpecification spec, String id, String methodName, Location location) 046 { 047 Method method = findMethod(AnnotatedPage.class, methodName); 048 049 EnhancementOperation op = newOp(); 050 051 replay(); 052 053 new ComponentAnnotationWorker().performEnhancement(op, spec, method, location); 054 055 verify(); 056 057 return spec.getComponent(id); 058 } 059 060 public void test_Simple() 061 { 062 Location l = newLocation(); 063 064 IContainedComponent cc = run("textField", "getTextField", l); 065 066 assertEquals("TextField", cc.getType()); 067 assertEquals(false, cc.getInheritInformalParameters()); 068 assertEquals(null, cc.getCopyOf()); 069 assertSame(l, cc.getLocation()); 070 assertEquals(true, cc.getBindingNames().isEmpty()); 071 assertEquals("textField", cc.getPropertyName()); 072 } 073 074 public void test_Without_Type() 075 { 076 IContainedComponent cc = run("usernameField", "getUsernameField", null); 077 078 assertEquals("TextField", cc.getType()); 079 } 080 081 public void test_Explicit_Id() 082 { 083 IContainedComponent cc = run("email", "getEmailField", null); 084 085 assertEquals("emailField", cc.getPropertyName()); 086 } 087 088 public void test_Inherit_Informal_Parameters() 089 { 090 IContainedComponent cc = run("inherit", "getInherit", null); 091 092 assertEquals(true, cc.getInheritInformalParameters()); 093 } 094 095 public void test_With_Bindings() 096 { 097 Location l = newLocation(); 098 IContainedComponent cc = run("componentWithBindings", "getComponentWithBindings", l); 099 100 assertBinding(cc, "condition", l, BindingType.PREFIXED, "message"); 101 assertBinding(cc, "element", l, BindingType.PREFIXED, "div"); 102 } 103 104 public void test_Binding_Whitespace_Trimmed() 105 { 106 Location l = newLocation(); 107 108 IContainedComponent cc = run("whitespace", "getWhitespace", l); 109 110 assertBinding(cc, "value", l, BindingType.PREFIXED, "email"); 111 assertBinding(cc, "displayName", l, BindingType.PREFIXED, "message:email-label"); 112 } 113 114 public void test_With_Type_And_CopyOf() 115 { 116 try 117 { 118 run("anEmailCopy", "getInvalidEmailCopy", null); 119 unreachable(); 120 } 121 catch (ApplicationRuntimeException ex) 122 { 123 assertExceptionSubstring(ex, "both type and copy-of"); 124 } 125 } 126 127 public void test_CopyOf() 128 { 129 Location l = newLocation(); 130 IComponentSpecification spec = new ComponentSpecification(); 131 run(spec, "componentWithBindings", "getComponentWithBindings", l); 132 IContainedComponent cc = run(spec, "aComponentCopy", "getComponentWithBindingsCopy", l); 133 134 assertBinding(cc, "condition", l, BindingType.PREFIXED, "message"); 135 assertBinding(cc, "element", l, BindingType.PREFIXED, "div"); 136 } 137 138 public void test_With_InheritedBindings() 139 { 140 Location l = newLocation(); 141 IContainedComponent cc = run("componentWithInheritedBindings", "getComponentWithInheritedBindings", l); 142 143 assertBinding(cc, "condition", l, BindingType.PREFIXED, "message"); 144 assertBinding(cc, "element", l, BindingType.PREFIXED, "div"); 145 assertBinding(cc, "title", l, BindingType.INHERITED, "pageTitle"); 146 assertBinding(cc, "email", l, BindingType.INHERITED, "email"); 147 } 148 149 void assertBinding(IContainedComponent cc, String name, Location location, BindingType type, String value) 150 { 151 IBindingSpecification spec = cc.getBinding(name); 152 if (location!=null) 153 assertSame(spec.getLocation(), location); 154 assertEquals(spec.getType(), type); 155 assertEquals(spec.getValue(), value); 156 } 157 }