tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/test Creator.java
Date Fri, 03 Dec 2004 00:22:40 GMT
hlship      2004/12/02 16:22:40

  Modified:    framework/src/test/org/apache/tapestry/test TestCreator.java
               framework/src/java/org/apache/tapestry/test Creator.java
  Added:       framework/src/documentation/content/xdocs/tapestry/ComponentReference
                        index.xml
  Log:
  Add ability to easily initialize properties of instances created by the Creator.
  
  Revision  Changes    Path
  1.3       +25 -15    jakarta-tapestry/framework/src/test/org/apache/tapestry/test/TestCreator.java
  
  Index: TestCreator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/test/org/apache/tapestry/test/TestCreator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestCreator.java	2 Dec 2004 22:01:08 -0000	1.2
  +++ TestCreator.java	3 Dec 2004 00:22:40 -0000	1.3
  @@ -32,9 +32,9 @@
   
           try
           {
  -            Creator i = new Creator();
  +            Creator c = new Creator();
   
  -            i.getInstance(List.class);
  +            c.newInstance(List.class);
               unreachable();
           }
           catch (IllegalArgumentException ex)
  @@ -48,9 +48,9 @@
   
       public void testObjectType()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        StringSubject s = (StringSubject) i.getInstance(StringSubject.class);
  +        StringSubject s = (StringSubject) c.newInstance(StringSubject.class);
   
           s.setTitle("title");
   
  @@ -59,9 +59,9 @@
   
       public void testPrimitiveType()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        IntSubject s = (IntSubject) i.getInstance(IntSubject.class);
  +        IntSubject s = (IntSubject) c.newInstance(IntSubject.class);
   
           s.setPriority(-1);
   
  @@ -70,9 +70,9 @@
   
       public void testArrayType()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        ArraySubject s = (ArraySubject) i.getInstance(ArraySubject.class);
  +        ArraySubject s = (ArraySubject) c.newInstance(ArraySubject.class);
   
           int[] counts = new int[]
           { 3, 7, 9 };
  @@ -84,9 +84,9 @@
   
       public void testInherited()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        InheritedSubject s = (InheritedSubject) i.getInstance(InheritedSubject.class);
  +        InheritedSubject s = (InheritedSubject) c.newInstance(InheritedSubject.class);
   
           s.setFlag(true);
           s.setPriority(5);
  @@ -97,9 +97,9 @@
   
       public void testMethodNameNotOverriden()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        BooleanSubject s = (BooleanSubject) i.getInstance(BooleanSubject.class);
  +        BooleanSubject s = (BooleanSubject) c.newInstance(BooleanSubject.class);
   
           s.setKnown(true);
   
  @@ -108,11 +108,21 @@
   
       public void testUniqueInstances()
       {
  -        Creator i = new Creator();
  +        Creator c = new Creator();
   
  -        StringSubject s1 = (StringSubject) i.getInstance(StringSubject.class);
  -        StringSubject s2 = (StringSubject) i.getInstance(StringSubject.class);
  +        StringSubject s1 = (StringSubject) c.newInstance(StringSubject.class);
  +        StringSubject s2 = (StringSubject) c.newInstance(StringSubject.class);
   
           assertNotSame(s1, s2);
  +    }
  +
  +    public void testInitializer()
  +    {
  +        Creator c = new Creator();
  +
  +        StringSubject ss = (StringSubject) c.newInstance(StringSubject.class, new Object[]
  +        { "title", "Hitchhiker's Guide" });
  +
  +        assertEquals("Hitchhiker's Guide", ss.getTitle());
       }
   }
  
  
  
  1.1                  jakarta-tapestry/framework/src/documentation/content/xdocs/tapestry/ComponentReference/index.xml
  
  Index: index.xml
  ===================================================================
  <?xml version="1.0"?>
  <!--
     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.
  -->
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.2//EN" "./dtd/document-v12.dtd"
  [
  	<!ENTITY projectroot '../../'>
  	<!ENTITY % links.ent SYSTEM "../../links.ent">
  	%links.ent;
  ]>
  <document>
    <header>
      <title>Framework Component Reference</title>
    </header>
    
    <body>
        
  <p>This is the component reference for the Tapestry framework components.  Component
frameworks are in a special library with
  the prefix <code>framework:</code>.  However, if a non-prefixed component type
is used, and doesn't exist in the 
  corresponding application (or library) the framework is searched next.  This means that
framework components
  are always available, but can also always be overriden as well.    
        </p>
  
    </body>
  </document>
  
  
  1.5       +44 -16    jakarta-tapestry/framework/src/java/org/apache/tapestry/test/Creator.java
  
  Index: Creator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/java/org/apache/tapestry/test/Creator.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Creator.java	2 Dec 2004 22:01:08 -0000	1.4
  +++ Creator.java	3 Dec 2004 00:22:40 -0000	1.5
  @@ -14,30 +14,21 @@
   
   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.Iterator;
   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.impl.DefaultClassResolver;
  -import org.apache.hivemind.service.ClassFab;
  -import org.apache.hivemind.service.ClassFabUtils;
  +import org.apache.hivemind.impl.DefaultErrorHandler;
  +import org.apache.hivemind.impl.ErrorLogImpl;
   import org.apache.hivemind.service.ClassFactory;
  -import org.apache.hivemind.service.MethodSignature;
  -import org.apache.hivemind.service.impl.ClassFabImpl;
   import org.apache.hivemind.service.impl.ClassFactoryImpl;
  -import org.apache.hivemind.service.impl.CtClassSource;
  -import org.apache.hivemind.service.impl.HiveMindClassPool;
  +import org.apache.hivemind.util.PropertyUtils;
  +import org.apache.tapestry.Tapestry;
   import org.apache.tapestry.enhance.AbstractPropertyWorker;
   import org.apache.tapestry.enhance.EnhancementOperationImpl;
   import org.apache.tapestry.services.ComponentConstructor;
  @@ -70,10 +61,14 @@
           if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
               throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
   
  +        AbstractPropertyWorker w = new AbstractPropertyWorker();
  +
  +        w.setErrorLog(new ErrorLogImpl(new DefaultErrorHandler(), LOG));
  +
           EnhancementOperationImpl op = new EnhancementOperationImpl(_classResolver,
                   new ComponentSpecification(), inputClass, _classFactory);
   
  -        new AbstractPropertyWorker().performEnhancement(op);
  +        w.performEnhancement(op);
   
           return op.getConstructor();
       }
  @@ -96,7 +91,7 @@
        * 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)
  +    public Object newInstance(Class abstractClass)
       {
           ComponentConstructor constructor = getComponentConstructor(abstractClass);
   
  @@ -110,6 +105,39 @@
                       abstractClass,
                       ex));
           }
  +    }
  +
  +    /**
  +     * Creates a new instance of a given class, and then initializes properties of the
instance. The
  +     * map contains string keys that are property names, and object values.
  +     */
  +    public Object newInstance(Class abstractClass, Map properties)
  +    {
  +        Object result = newInstance(abstractClass);
  +
  +        Iterator i = properties.entrySet().iterator();
  +
  +        while (i.hasNext())
  +        {
  +            Map.Entry e = (Map.Entry) i.next();
  +
  +            String propertyName = (String) e.getKey();
  +
  +            PropertyUtils.write(result, propertyName, e.getValue());
  +        }
  +
  +        return result;
  +    }
  +
  +    /**
  +     * A convienience (useful in test code) for invoking {@link #newInstance(Class, Map)}.
The Map
  +     * is constructed from the properties array, which consists of alternating keys and
values.
  +     */
  +
  +    public Object newInstance(Class abstractClass, Object[] properties)
  +    {
  +        Map propertyMap = Tapestry.convertArrayToMap(properties);
   
  +        return newInstance(abstractClass, propertyMap);
       }
   }
  
  
  

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


Mime
View raw message