001 // Copyright 2004, 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.enhance; 016 017 import org.apache.hivemind.ErrorLog; 018 import org.apache.hivemind.Location; 019 import org.apache.tapestry.BaseComponentTestCase; 020 import org.apache.tapestry.spec.ComponentSpecification; 021 import org.apache.tapestry.spec.IComponentSpecification; 022 import org.testng.annotations.Test; 023 024 /** 025 * Tests for {@link org.apache.tapestry.enhance.EnhancedClassValidatorImpl}. 026 * 027 * @author Howard M. Lewis Ship 028 * @since 4.0 029 */ 030 @Test 031 public class EnhancedClassValidatorTest extends BaseComponentTestCase 032 { 033 /** 034 * Test for a class that fulfills its requirements (by implementing all inherited abstract 035 * methods. 036 */ 037 038 public void test_Complete() 039 { 040 EnhancedClassValidatorImpl v = new EnhancedClassValidatorImpl(); 041 v.setClassInspector(new ClassInspectorImpl()); 042 043 v.validate(AbstractBase.class, Complete.class, new ComponentSpecification()); 044 } 045 046 public void test_Generics_Complete() 047 { 048 EnhancedClassValidatorImpl v = new EnhancedClassValidatorImpl(); 049 v.setClassInspector(new GenericsClassInspectorImpl()); 050 051 v.validate(AbstractGenericBase.class, FooGenericComponent.class, new ComponentSpecification()); 052 } 053 054 /** 055 * Pass in an abstract class (with enhancement, its possible that a supposedly concrete class 056 * may omit implementing an inherited abstract method, which is the whole point of the 057 * validator. 058 */ 059 060 public void test_Incomplete() 061 { 062 ErrorLog log = newErrorLog(); 063 Location l = newLocation(); 064 065 IComponentSpecification spec = newSpec(); 066 067 trainGetLocation(spec, l); 068 069 log.error("Method 'public abstract void org.apache.tapestry.enhance.AbstractBase.foo()' " 070 + "(declared in class org.apache.tapestry.enhance.AbstractBase) has no implementation in class " 071 + "org.apache.tapestry.enhance.AbstractBase (or enhanced subclass org.apache.tapestry.enhance.Incomplete).", 072 l, 073 null); 074 075 replay(); 076 077 EnhancedClassValidatorImpl v = new EnhancedClassValidatorImpl(); 078 v.setClassInspector(new ClassInspectorImpl()); 079 v.setErrorLog(log); 080 081 v.validate(AbstractBase.class, Incomplete.class, spec); 082 083 verify(); 084 } 085 086 public void test_Inherits_Missing_Method() 087 { 088 ErrorLog log = newErrorLog(); 089 Location l = newLocation(); 090 091 IComponentSpecification spec = newSpec(); 092 093 trainGetLocation(spec, l); 094 095 log.error("Method 'public abstract void java.lang.Runnable.run()' " 096 + "has no implementation in class org.apache.tapestry.enhance.AbstractRunnable " 097 + "(or enhanced subclass org.apache.tapestry.enhance.AbstractRunnableSubclass).", 098 l, 099 null); 100 101 replay(); 102 103 EnhancedClassValidatorImpl v = new EnhancedClassValidatorImpl(); 104 v.setErrorLog(log); 105 v.setClassInspector(new ClassInspectorImpl()); 106 107 v.validate(AbstractRunnable.class, AbstractRunnableSubclass.class, spec); 108 109 verify(); 110 } 111 112 private ErrorLog newErrorLog() 113 { 114 return newMock(ErrorLog.class); 115 } 116 117 /** 118 * Ensures that the code works when passed java.lang.Object (which has different inheritance 119 * than other classes. 120 */ 121 122 public void test_Object() 123 { 124 EnhancedClassValidatorImpl v = new EnhancedClassValidatorImpl(); 125 v.setClassInspector(new ClassInspectorImpl()); 126 127 v.validate(Object.class, Object.class, new ComponentSpecification()); 128 } 129 }