You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2004/06/17 23:32:02 UTC

cvs commit: jakarta-tapestry .classpath

hlship      2004/06/17 14:32:01

  Modified:    framework/src/org/apache/tapestry/test/assertions
                        RegexpMatch.java AssertOutput.java
                        AssertRegexp.java
               framework/src/org/apache/tapestry/test
                        ServletDescriptor.java ScriptMessages.java
                        ScriptDescriptor.java ParameterList.java
                        ScriptParser.java ScriptedTestSession.java
                        RequestDescriptor.java ResponseAssertion.java
                        ScriptStrings.properties
               framework/src/org/apache/tapestry/test/mock MockContext.java
                        InitParameterHolder.java
                        MockServletInputStream.java MockRequest.java
                        MockRequestDispatcher.java MockServletConfig.java
                        MockSession.java AttributeHolder.java
                        MockResponse.java
               .        .classpath
  Added:       junit/src/org/apache/tapestry/test InheritedSubject.java
                        IntSubject.java StringSubject.java TestCreator.java
                        ArraySubject.java BooleanSubject.java
               framework/src/org/apache/tapestry/test Creator.java
  Log:
  Add Creator, used in unit tests to instantiate abstract classes.
  
  Revision  Changes    Path
  1.2       +11 -11    jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/RegexpMatch.java
  
  Index: RegexpMatch.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/RegexpMatch.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RegexpMatch.java	17 Jun 2004 18:53:42 -0000	1.1
  +++ RegexpMatch.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -1,16 +1,16 @@
  -//Copyright 2004 The Apache Software Foundation
  +//  Copyright 2004 The Apache Software Foundation
   //
  -//Licensed under the Apache License, Version 2.0 (the "License");
  -//you may not use this file except in compliance with the License.
  -//You may obtain a copy of the License at
  +// Licensed under the Apache License, Version 2.0 (the "License");
  +// you may not use this file except in compliance with the License.
  +// You may obtain a copy of the License at
   //
  -//	http://www.apache.org/licenses/LICENSE-2.0
  +//     http://www.apache.org/licenses/LICENSE-2.0
   //
  -//Unless required by applicable law or agreed to in writing, software
  -//distributed under the License is distributed on an "AS IS" BASIS,
  -//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -//See the License for the specific language governing permissions and
  -//limitations under the License.
  +// Unless required by applicable law or agreed to in writing, software
  +// distributed under the License is distributed on an "AS IS" BASIS,
  +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  +// See the License for the specific language governing permissions and
  +// limitations under the License.
   
   package org.apache.tapestry.test.assertions;
   
  @@ -23,7 +23,7 @@
    * for error reporting purposes.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class RegexpMatch extends BaseLocatable
   {
  
  
  
  1.3       +1 -0      jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertOutput.java
  
  Index: AssertOutput.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertOutput.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AssertOutput.java	17 Jun 2004 18:53:42 -0000	1.2
  +++ AssertOutput.java	17 Jun 2004 21:32:01 -0000	1.3
  @@ -26,6 +26,7 @@
    * substring within the response text.
    *
    * @author Howard Lewis Ship
  + * @since 3.1
    */
   public class AssertOutput extends BaseLocatable implements ResponseAssertion
   {
  
  
  
  1.2       +11 -11    jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertRegexp.java
  
  Index: AssertRegexp.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/assertions/AssertRegexp.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AssertRegexp.java	17 Jun 2004 18:53:42 -0000	1.1
  +++ AssertRegexp.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -1,16 +1,16 @@
  -//Copyright 2004 The Apache Software Foundation
  +//  Copyright 2004 The Apache Software Foundation
   //
  -//Licensed under the Apache License, Version 2.0 (the "License");
  -//you may not use this file except in compliance with the License.
  -//You may obtain a copy of the License at
  +// Licensed under the Apache License, Version 2.0 (the "License");
  +// you may not use this file except in compliance with the License.
  +// You may obtain a copy of the License at
   //
  -//	http://www.apache.org/licenses/LICENSE-2.0
  +//     http://www.apache.org/licenses/LICENSE-2.0
   //
  -//Unless required by applicable law or agreed to in writing, software
  -//distributed under the License is distributed on an "AS IS" BASIS,
  -//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -//See the License for the specific language governing permissions and
  -//limitations under the License.
  +// Unless required by applicable law or agreed to in writing, software
  +// distributed under the License is distributed on an "AS IS" BASIS,
  +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  +// See the License for the specific language governing permissions and
  +// limitations under the License.
   
   package org.apache.tapestry.test.assertions;
   
  @@ -29,7 +29,7 @@
    * list of matches of a regular expression within the response.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class AssertRegexp extends BaseLocatable implements ResponseAssertion
   {
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/InheritedSubject.java
  
  Index: InheritedSubject.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  public abstract class InheritedSubject extends IntSubject
  {
  
      public abstract boolean getFlag();
      public abstract void setFlag(boolean flag);
  
  }
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/IntSubject.java
  
  Index: IntSubject.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  
  public abstract class IntSubject
  {
     public abstract int getPriority();
     public abstract void setPriority(int value);
  }
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/StringSubject.java
  
  Index: StringSubject.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  public abstract class StringSubject
  {
      public abstract String getTitle();
      public abstract void setTitle(String title);
  }
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/TestCreator.java
  
  Index: TestCreator.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.hivemind.test.HiveMindTestCase;
  
  /**
   * Tests for {@link org.apache.tapestry.test.Creator}.
   *
   * @author Howard Lewis Ship
   */
  public class TestCreator extends HiveMindTestCase
  {
  
      public void testNonAbstract() throws Exception
      {
          interceptLogging("org.apache.tapestry.test");
  
          Creator i = new Creator();
  
          Object result = i.getInstance(ArrayList.class);
  
          assertEquals(ArrayList.class, result.getClass());
  
          assertLoggedMessage("Class java.util.ArrayList is not an abstract class.");
  
      }
  
      public void testInterface() throws Exception
      {
  
          try
          {
              Creator i = new Creator();
  
              i.getInstance(List.class);
              unreachable();
          }
          catch (IllegalArgumentException ex)
          {
              assertEquals(
                  ex.getMessage(),
                  "Can not create instance of java.util.List. Interfaces, arrays and primitive types may not be enhanced.");
          }
  
      }
  
      public void testObjectType()
      {
          Creator i = new Creator();
  
          StringSubject s = (StringSubject) i.getInstance(StringSubject.class);
  
          s.setTitle("title");
  
          assertEquals("title", s.getTitle());
      }
  
      public void testPrimitiveType()
      {
          Creator i = new Creator();
  
          IntSubject s = (IntSubject) i.getInstance(IntSubject.class);
  
          s.setPriority(-1);
  
          assertEquals(-1, s.getPriority());
      }
  
      public void testArrayType()
      {
          Creator i = new Creator();
  
          ArraySubject s = (ArraySubject) i.getInstance(ArraySubject.class);
  
          int[] counts = new int[] { 3, 7, 9 };
  
          s.setCounts(counts);
  
          assertSame(counts, s.getCounts());
      }
  
      public void testInherited()
      {
          Creator i = new Creator();
  
          InheritedSubject s = (InheritedSubject) i.getInstance(InheritedSubject.class);
  
          s.setFlag(true);
          s.setPriority(5);
  
          assertEquals(true, s.getFlag());
          assertEquals(5, s.getPriority());
      }
  
      public void testMethodNameNotOverriden()
      {
          Creator i = new Creator();
  
          BooleanSubject s = (BooleanSubject) i.getInstance(BooleanSubject.class);
  
          s.setKnown(true);
  
          assertEquals(true, s.isKnown());
      }
  
      public void testUniqueInstances()
      {
          Creator i = new Creator();
  
          StringSubject s1 = (StringSubject) i.getInstance(StringSubject.class);
          StringSubject s2 = (StringSubject) i.getInstance(StringSubject.class);
  
          assertNotSame(s1, s2);
      }
  }
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/ArraySubject.java
  
  Index: ArraySubject.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  public abstract class ArraySubject
  {
      public abstract int[] getCounts();
      public abstract void setCounts(int[] value);
  }
  
  
  
  1.1                  jakarta-tapestry/junit/src/org/apache/tapestry/test/BooleanSubject.java
  
  Index: BooleanSubject.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  public abstract class BooleanSubject
  {
      public abstract boolean isKnown();
      public abstract void setKnown(boolean value);
  }
  
  
  
  1.2       +6 -0      jakarta-tapestry/framework/src/org/apache/tapestry/test/ServletDescriptor.java
  
  Index: ServletDescriptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ServletDescriptor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServletDescriptor.java	5 May 2004 02:41:19 -0000	1.1
  +++ ServletDescriptor.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -19,6 +19,12 @@
   
   import org.apache.hivemind.impl.BaseLocatable;
   
  +/**
  + * 
  + *
  + * @author Howard Lewis Ship
  + * @since 3.1
  + */
   public class ServletDescriptor extends BaseLocatable
   {
       private String _name;
  
  
  
  1.2       +39 -10    jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptMessages.java
  
  Index: ScriptMessages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptMessages.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ScriptMessages.java	17 Jun 2004 18:53:42 -0000	1.1
  +++ ScriptMessages.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -1,21 +1,22 @@
  -//Copyright 2004 The Apache Software Foundation
  +//  Copyright 2004 The Apache Software Foundation
   //
  -//Licensed under the Apache License, Version 2.0 (the "License");
  -//you may not use this file except in compliance with the License.
  -//You may obtain a copy of the License at
  +// Licensed under the Apache License, Version 2.0 (the "License");
  +// you may not use this file except in compliance with the License.
  +// You may obtain a copy of the License at
   //
  -//	http://www.apache.org/licenses/LICENSE-2.0
  +//     http://www.apache.org/licenses/LICENSE-2.0
   //
  -//Unless required by applicable law or agreed to in writing, software
  -//distributed under the License is distributed on an "AS IS" BASIS,
  -//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -//See the License for the specific language governing permissions and
  -//limitations under the License.
  +// Unless required by applicable law or agreed to in writing, software
  +// distributed under the License is distributed on an "AS IS" BASIS,
  +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  +// See the License for the specific language governing permissions and
  +// limitations under the License.
   
   package org.apache.tapestry.test;
   
   import org.apache.hivemind.Location;
   import org.apache.hivemind.impl.MessageFormatter;
  +import org.apache.hivemind.service.ClassFabUtils;
   
   /**
    * Container of static methods to format logging and exception messages, used
  @@ -81,5 +82,33 @@
                   location,
                   new Integer(expectedCount),
                   new Integer(actualCount)});
  +    }
  +
  +    public static String wrongTypeForEnhancement(Class type)
  +    {
  +        return _formatter.format(
  +            "wrong-type-for-enhancement",
  +            ClassFabUtils.getJavaClassName(type));
  +    }
  +
  +    public static String classNotAbstract(Class type)
  +    {
  +        return _formatter.format("class-not-abstract", type.getName());
  +    }
  +
  +    public static String unableToIntrospect(Class type, Throwable cause)
  +    {
  +        return _formatter.format(
  +            "unable-to-introspect",
  +            type.getName(),
  +            _formatter.extractMessage(cause));
  +    }
  +
  +    public static String unableToInstantiate(Class abstractClass, Throwable cause)
  +    {
  +        return _formatter.format(
  +            "unable-to-instantiate",
  +            abstractClass.getName(),
  +            _formatter.extractMessage(cause));
       }
   }
  
  
  
  1.2       +1 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptDescriptor.java
  
  Index: ScriptDescriptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptDescriptor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ScriptDescriptor.java	5 May 2004 02:41:19 -0000	1.1
  +++ ScriptDescriptor.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -26,7 +26,7 @@
    * Top level object for test scripts.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class ScriptDescriptor extends BaseLocatable
   {
  
  
  
  1.2       +1 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/ParameterList.java
  
  Index: ParameterList.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ParameterList.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParameterList.java	5 May 2004 02:41:19 -0000	1.1
  +++ ParameterList.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -22,7 +22,7 @@
    * {@link org.apache.tapestry.test.RequestDescriptor}.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class ParameterList
   {
  
  
  
  1.3       +1 -0      jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.java
  
  Index: ScriptParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ScriptParser.java	17 Jun 2004 18:53:42 -0000	1.2
  +++ ScriptParser.java	17 Jun 2004 21:32:01 -0000	1.3
  @@ -38,6 +38,7 @@
    * a sequence of operations and assertions.
    *
    * @author Howard Lewis Ship
  + * @since 3.1
    */
   public class ScriptParser extends AbstractParser
   {
  
  
  
  1.3       +1 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptedTestSession.java
  
  Index: ScriptedTestSession.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptedTestSession.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ScriptedTestSession.java	17 Jun 2004 18:53:42 -0000	1.2
  +++ ScriptedTestSession.java	17 Jun 2004 21:32:01 -0000	1.3
  @@ -33,7 +33,7 @@
    * a {@link org.apache.tapestry.test.ScriptDescriptor).
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class ScriptedTestSession
   {
  
  
  
  1.2       +1 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/RequestDescriptor.java
  
  Index: RequestDescriptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/RequestDescriptor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RequestDescriptor.java	5 May 2004 02:41:19 -0000	1.1
  +++ RequestDescriptor.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -27,7 +27,7 @@
    * 
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public class RequestDescriptor extends BaseLocatable
   {
  
  
  
  1.2       +1 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/ResponseAssertion.java
  
  Index: ResponseAssertion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ResponseAssertion.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResponseAssertion.java	5 May 2004 02:41:19 -0000	1.1
  +++ ResponseAssertion.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -20,7 +20,7 @@
    * response content for particular literal values or regular expressions.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
  + * @since 3.1
    */
   public interface ResponseAssertion
   {
  
  
  
  1.2       +5 -0      jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptStrings.properties
  
  Index: ScriptStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/ScriptStrings.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ScriptStrings.properties	17 Jun 2004 18:53:42 -0000	1.1
  +++ ScriptStrings.properties	17 Jun 2004 21:32:01 -0000	1.2
  @@ -21,3 +21,8 @@
   unexpected-attribute-in-element=Unexpected attribute ''{0}'' (in element {1}).
   missing-required-attribute=Required attribute ''{0}'' is not supplied for element {1}.
   invalid-int-attribute=Attribute ''{0}'' (of element {1}, at {2}) is ''{3}'', which is not an integer value.
  +
  +wrong-type-for-enhancement=Can not create instance of {0}. Interfaces, arrays and primitive types may not be enhanced.
  +class-not-abstract=Class {0} is not an abstract class.
  +unable-to-introspect=Unable to introspect class {0}: {1}
  +unable-to-instantiate=Unable to instantiate enhanced subclass of {0}: {2}
  \ No newline at end of file
  
  
  
  1.1                  jakarta-tapestry/framework/src/org/apache/tapestry/test/Creator.java
  
  Index: Creator.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.tapestry.test;
  
  import java.beans.BeanInfo;
  import java.beans.IntrospectionException;
  import java.beans.Introspector;
  import java.beans.PropertyDescriptor;
  import java.lang.reflect.Method;
  import java.lang.reflect.Modifier;
  import java.util.HashMap;
  import java.util.Map;
  
  import javassist.CtClass;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.hivemind.ApplicationRuntimeException;
  import org.apache.hivemind.ClassResolver;
  import org.apache.hivemind.service.ClassFab;
  import org.apache.hivemind.service.ClassFabUtils;
  import org.apache.hivemind.service.MethodSignature;
  import org.apache.hivemind.service.impl.ClassFabImpl;
  import org.apache.hivemind.service.impl.CtClassSource;
  
  /**
   * A utility class that is used to instantiate abstract Tapestry pages and
   * components. It creates, at runtime, a subclass where all abstract properties
   * are filled in (complete with instance variable, accessor and mutator methods).
   * This isn't the same as how the class is enhanced at runtime, but is sufficient
   * to unit test the class, especially listener methods.
   *
   * @author Howard Lewis Ship
   * @since 3.1
   */
  public class Creator
  {
      private static final Log LOG = LogFactory.getLog(Creator.class);
  
      /**
       * Keyed on Class, value is another class (fully enhanced).
       */
      private Map _classes = new HashMap();
      private final CtClassSource _classSource;
  
  	public Creator()
  	{
  		this(Thread.currentThread().getContextClassLoader());
  	}
  
      public Creator(ClassLoader loader)
      {
          _classSource = new CtClassSource(loader);
      }
  
  
      private void addAccessorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
      {
          String methodName = getMethodName(pd.getReadMethod(), "get", pd.getName());
  
          MethodSignature sig = new MethodSignature(pd.getPropertyType(), methodName, null, null);
  
          classFab.addMethod(Modifier.PUBLIC, sig, "return " + attributeName + ";");
      }
  
      private void addField(ClassFab classFab, String fieldName, Class fieldType)
      {
          classFab.addField(fieldName, fieldType);
      }
  
      private void addMissingProperties(ClassFab classFab, BeanInfo info)
      {
          PropertyDescriptor[] pd = info.getPropertyDescriptors();
  
          for (int i = 0; i < pd.length; i++)
              addMissingProperty(classFab, pd[i]);
      }
  
      private void addMissingProperty(ClassFab classFab, PropertyDescriptor pd)
      {
          Method readMethod = pd.getReadMethod();
          Method writeMethod = pd.getWriteMethod();
  
          boolean abstractRead = isAbstract(readMethod);
          boolean abstractWrite = isAbstract(writeMethod);
  
          if (!(abstractRead || abstractWrite))
              return;
  
          String attributeName = "_$" + pd.getName();
          Class propertyType = pd.getPropertyType();
  
          addField(classFab, attributeName, propertyType);
  
          if (abstractRead)
              addAccessorMethod(classFab, pd, attributeName);
  
          if (abstractWrite)
              addMutatorMethod(classFab, pd, attributeName);
  
      }
  
      private void addMutatorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
      {
          String methodName = getMethodName(pd.getWriteMethod(), "set", pd.getName());
  
          MethodSignature sig =
              new MethodSignature(void.class, methodName, new Class[] { pd.getPropertyType()}, null);
  
          classFab.addMethod(Modifier.PUBLIC, sig, attributeName + " = $1;");
      }
  
      private Class createEnhancedClass(Class inputClass)
      {
          if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
              throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
  
          if (!Modifier.isAbstract(inputClass.getModifiers()))
          {
              LOG.error(ScriptMessages.classNotAbstract(inputClass));
              return inputClass;
          }
  
          BeanInfo info = null;
  
          try
          {
              info = Introspector.getBeanInfo(inputClass, Object.class);
          }
          catch (IntrospectionException ex)
          {
              throw new ApplicationRuntimeException(
                  ScriptMessages.unableToIntrospect(inputClass, ex));
          }
  
          String name = ClassFabUtils.generateClassName("Enhance");
  
          CtClass newClass = _classSource.newClass(name, inputClass);
  
          ClassFab classFab = new ClassFabImpl(_classSource, newClass);
  
          addMissingProperties(classFab, info);
  
          return classFab.createClass();
      }
  
      public Class getEnhancedClass(Class inputClass)
      {
          Class result = (Class) _classes.get(inputClass);
  
          if (result == null)
          {
              result = createEnhancedClass(inputClass);
  
              _classes.put(inputClass, result);
          }
  
          return result;
      }
  
      /**
       * Given a particular abstract class; will create an instance of that class. A subclass
       * is created with all abstract properties filled in with ordinary implementations.
       */
      public Object getInstance(Class abstractClass)
      {
          Class enhancedClass = getEnhancedClass(abstractClass);
  
          try
          {
              return enhancedClass.newInstance();
          }
          catch (Exception ex)
          {
              throw new ApplicationRuntimeException(
                  ScriptMessages.unableToInstantiate(abstractClass, ex));
          }
  
      }
  
      private String getMethodName(Method m, String prefix, String propertyName)
      {
          if (m != null)
              return m.getName();
  
          StringBuffer buffer = new StringBuffer(prefix);
  
          buffer.append(propertyName.substring(0, 1).toUpperCase());
          buffer.append(propertyName.substring(1));
  
          return buffer.toString();
      }
  
      private boolean isAbstract(Method m)
      {
          return m == null || Modifier.isAbstract(m.getModifiers());
      }
  }
  
  
  
  1.2       +0 -2      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockContext.java
  
  Index: MockContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockContext.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockContext.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockContext.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -34,9 +34,7 @@
    * Mock implementation of {@link javax.servlet.ServletContext}.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
  - * 
    */
   
   public class MockContext extends AttributeHolder implements ServletContext, InitParameterHolder
  
  
  
  1.2       +1 -2      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/InitParameterHolder.java
  
  Index: InitParameterHolder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/InitParameterHolder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- InitParameterHolder.java	5 May 2004 02:41:20 -0000	1.1
  +++ InitParameterHolder.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -22,10 +22,9 @@
    *
    *
    *  @author Howard Lewis Ship
  - *  @version $Id$
    *  @since 3.1
    * 
  -**/
  + */
   
   public interface InitParameterHolder
   {
  
  
  
  1.2       +0 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockServletInputStream.java
  
  Index: MockServletInputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockServletInputStream.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockServletInputStream.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockServletInputStream.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -27,7 +27,6 @@
    * invocations to the inner stream.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
    */
   
  
  
  
  1.2       +0 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockRequest.java
  
  Index: MockRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockRequest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockRequest.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockRequest.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -38,7 +38,6 @@
    *
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
    */
   
  
  
  
  1.2       +0 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockRequestDispatcher.java
  
  Index: MockRequestDispatcher.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockRequestDispatcher.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockRequestDispatcher.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockRequestDispatcher.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -28,7 +28,6 @@
    * Used to enable mock testing of internal request forwarding.
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
    */
   public class MockRequestDispatcher implements RequestDispatcher
  
  
  
  1.2       +0 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockServletConfig.java
  
  Index: MockServletConfig.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockServletConfig.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockServletConfig.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockServletConfig.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -27,7 +27,6 @@
    * for Mock testing. 
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
    */
   
  
  
  
  1.2       +2 -4      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockSession.java
  
  Index: MockSession.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockSession.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockSession.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockSession.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -25,10 +25,8 @@
    *
    *
    *  @author Howard Lewis Ship
  - *  @version $Id$
  - *  @since 2.2
  - * 
  - **/
  + *  @since 3.1
  + */
   
   public class MockSession extends AttributeHolder implements HttpSession
   {
  
  
  
  1.2       +0 -1      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/AttributeHolder.java
  
  Index: AttributeHolder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/AttributeHolder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AttributeHolder.java	5 May 2004 02:41:20 -0000	1.1
  +++ AttributeHolder.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -36,7 +36,6 @@
    *
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
    */
   
  
  
  
  1.2       +0 -2      jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockResponse.java
  
  Index: MockResponse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/test/mock/MockResponse.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MockResponse.java	5 May 2004 02:41:20 -0000	1.1
  +++ MockResponse.java	17 Jun 2004 21:32:01 -0000	1.2
  @@ -29,9 +29,7 @@
    *
    *
    * @author Howard Lewis Ship
  - * @version $Id$
    * @since 3.1
  - * 
    */
   
   public class MockResponse implements HttpServletResponse
  
  
  
  1.45      +1 -0      jakarta-tapestry/.classpath
  
  Index: .classpath
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/.classpath,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- .classpath	17 Jun 2004 18:53:42 -0000	1.44
  +++ .classpath	17 Jun 2004 21:32:01 -0000	1.45
  @@ -25,5 +25,6 @@
       <classpathentry exported="true" kind="lib" path="lib/j2ee/servlet-api.jar"/>
       <classpathentry kind="lib" path="lib/ext/hivemind-1.0-beta-1-snapshot.jar"/>
       <classpathentry kind="lib" path="lib/ext/hivemind-lib-1.0-beta-1-snapshot.jar"/>
  +    <classpathentry kind="lib" path="lib/runtime/log4j-1.2.6.jar"/>
       <classpathentry kind="output" path="bin"/>
   </classpath>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org