commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt LoopBean.java TestBeanWriter.java
Date Sat, 05 Jan 2002 15:52:01 GMT
rdonkin     02/01/05 07:52:01

  Modified:    betwixt/src/java/org/apache/commons/betwixt
                        XMLIntrospector.java
               betwixt/src/java/org/apache/commons/betwixt/expression
                        ConstantExpression.java Context.java
                        EmptyExpression.java IteratorExpression.java
                        MethodExpression.java StringExpression.java
                        VariableExpression.java
               betwixt/src/java/org/apache/commons/betwixt/io
                        BeanWriter.java
               betwixt/src/test/org/apache/commons/betwixt
                        TestBeanWriter.java
  Added:       betwixt/src/test/org/apache/commons/betwixt LoopBean.java
  Log:
  Added logging for expressions. Also added more java docs comments.
  
  Revision  Changes    Path
  1.11      +15 -4     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
  
  Index: XMLIntrospector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- XMLIntrospector.java	19 Dec 2001 20:14:26 -0000	1.10
  +++ XMLIntrospector.java	5 Jan 2002 15:52:00 -0000	1.11
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: XMLIntrospector.java,v 1.10 2001/12/19 20:14:26 rdonkin Exp $
  + * $Id: XMLIntrospector.java,v 1.11 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -38,7 +38,7 @@
   /** <p><code>XMLIntrospector</code> an introspector of beans to create
a XMLBeanInfo instance.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.10 $
  +  * @version $Revision: 1.11 $
     */
   public class XMLIntrospector {
   
  @@ -171,7 +171,12 @@
       // Implementation methods
       //-------------------------------------------------------------------------    
       /** Loop through properties and process each one */
  -    protected void addProperties(BeanInfo beanInfo, List elements, List attributes) throws
IntrospectionException {
  +    protected void addProperties(
  +                                    BeanInfo beanInfo, 
  +                                    List elements, 
  +                                    List attributes) 
  +                                        throws 
  +                                            IntrospectionException {
           PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
           if ( descriptors != null ) {
               for ( int i = 0, size = descriptors.length; i < size; i++ ) {
  @@ -185,7 +190,13 @@
        * Go through and work out whether it's a loop property, a primitive or a standard.
        * The class property is ignored.
        */
  -    protected void addProperty(BeanInfo beanInfo, PropertyDescriptor propertyDescriptor,
List elements, List attributes) throws IntrospectionException {
  +    protected void addProperty(
  +                                BeanInfo beanInfo, 
  +                                PropertyDescriptor propertyDescriptor, 
  +                                List elements, 
  +                                List attributes) 
  +                                    throws 
  +                                        IntrospectionException {
           Class type = propertyDescriptor.getPropertyType();
           NodeDescriptor nodeDescriptor = null;
           Method readMethod = propertyDescriptor.getReadMethod();
  
  
  
  1.2       +14 -2     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/ConstantExpression.java
  
  Index: ConstantExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/ConstantExpression.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConstantExpression.java	22 Aug 2001 12:25:02 -0000	1.1
  +++ ConstantExpression.java	5 Jan 2002 15:52:00 -0000	1.2
  @@ -5,27 +5,39 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: ConstantExpression.java,v 1.1 2001/08/22 12:25:02 jstrachan Exp $
  + * $Id: ConstantExpression.java,v 1.2 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
   /** <p><code>ConstantExpression</code> represents a constant expression.</p>
     *
  +  * <p> In other words, {@link #evaluate} returns a value independent of the context.
</p>
  +  *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.1 $
  +  * @version $Revision: 1.2 $
     */
   public class ConstantExpression implements Expression {
   
       /** The value of this expression */
       private Object value;
       
  +    /** Base constructor
  +     */
       public ConstantExpression() {
       }
       
  +    /** Convenience constructor sets <code>value</code> property.
  +     */
       public ConstantExpression(Object value) {
           this.value = value;
       }
       
  +    /** 
  +      * Evaluate expression against given context.
  +      *
  +      * @param context evaluate expression against this context
  +      * @return current value of <code>value</code> property  
  +      */
       public Object evaluate(Context context) {
           return value;
       }
  
  
  
  1.3       +61 -14    jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java
  
  Index: Context.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Context.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Context.java	22 Aug 2001 18:30:48 -0000	1.2
  +++ Context.java	5 Jan 2002 15:52:00 -0000	1.3
  @@ -5,65 +5,112 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: Context.java,v 1.2 2001/08/22 18:30:48 jstrachan Exp $
  + * $Id: Context.java,v 1.3 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
   import java.util.HashMap;
   import java.util.Map;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogSource;
  +
   /** <p><code>Context</code> describes the context used to evaluate
  -  * bean expressions. This is mostly a bean together with a number of variables.</p>
  +  * bean expressions.
  +  * This is mostly a bean together with a number of context variables.
  +  * Context variables are named objects.
  +  * In other words, 
  +  * a context variable associates an object with a string.</p>
  +  *
  +  * <p> Logging during expression evaluation is done through the logging
  +  * instance held by this class. 
  +  * The object initiating the evaluation should control this logging 
  +  * and so passing a <code>Log</code> instance is enforced by the constructors.
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.2 $
  +  * @version $Revision: 1.3 $
     */
   public class Context {
   
  +    /** Evaluate this bean */
       private Object bean;
  +    /** Variables map */
       private Map variables = new HashMap();
  +    /** Logging uses commons-logging <code>Log</code> named <code>org.apache.commons.betwixt</code>
*/
  +    private Log log; 
       
  -    
  -    public Context() {
  -    }
  -
  -    public Context(Object bean) {
  +    /** Convenience constructor sets evaluted bean and log.
  +      *
  +      * @param bean evaluate expressions against this bean
  +      * @param log log to this logger
  +      */
  +    public Context(Object bean, Log log) {
           this.bean = bean;
  +        this.log = log;
       }
  -
  -    public Context(Object bean, Map variables) {
  +    
  +    /** Convenience constructor sets evaluted bean, context variables and log.
  +      *
  +      * @param bean evaluate expressions against this bean 
  +      * @param variables context variables
  +      * @param log log to this logger
  +      */
  +    public Context(Object bean, Map variables, Log log) {
           this.bean = bean;
           this.variables = variables;
  +        this.log = log;
       }
   
  -    /** Returns a new child context with the given bean */
  +    /** Returns a new child context with the given bean but the same log and variables.
*/
       public Context newContext(Object newBean) {
  -        return new Context(newBean, variables);
  +        return new Context(newBean, variables, log);
       }
       
  -    /** Returns the current bean
  +    /** Returns the current bean.
         */
       public Object getBean() {
           return bean;
       }
   
  +    /** Set the current bean.
  +     */
       public void setBean(Object bean) {
           this.bean = bean;
       }    
       
  +    /** Get context variables.
  +     */
       public Map getVariables() {
           return variables;
       }
   
  +    /** Set context variables. 
  +     */
       public void setVariables(Map variables) {
           this.variables = variables;
       }    
   
  +    /** Get the value of a particular context variable.
  +     */
       public Object getVariable(String name) {
           return variables.get( name );
       }
  -    
  +
  +    /** Set the value of a particular context variable.
  +     */    
       public void setVariable(String name, Object value) {
           variables.put( name, value );
  +    }
  +    
  +    /** Get the current log.  
  +      */
  +    public Log getLog() {
  +        return log;
  +    }
  +
  +    /** Set the logger used to log (Doh!).
  +     */
  +    public void setLog() {
  +        this.log = log;
       }
   }
  
  
  
  1.2       +12 -3     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/EmptyExpression.java
  
  Index: EmptyExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/EmptyExpression.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EmptyExpression.java	23 Aug 2001 14:25:57 -0000	1.1
  +++ EmptyExpression.java	5 Jan 2002 15:52:00 -0000	1.2
  @@ -5,26 +5,35 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: EmptyExpression.java,v 1.1 2001/08/23 14:25:57 jstrachan Exp $
  + * $Id: EmptyExpression.java,v 1.2 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
  -/** <p><code>EmptyExpression</code> returns the same value as is passed
in.</p>
  +/** <p><code>EmptyExpression</code> returns the same value as is passed
in. </p>
  +  * 
  +  * <p> See {@link #evaluate}. </p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.1 $
  +  * @version $Revision: 1.2 $
     */
   public class EmptyExpression implements Expression {
  -
  +    
  +    /** Don't need more than one <code>EmptyExpression</code>*/
       private static final EmptyExpression singleton = new EmptyExpression();
       
  +    /** Return the <code>EmptyExpression</code> singleton. 
  +     */
       public static EmptyExpression getInstance() {
           return singleton;
       }
       
  +    /** Should this be private?
  +     */
       public EmptyExpression() {
       }
       
  +    /** Return the bean we're evaluating.
  +     */ 
       public Object evaluate(Context context) {
           return context.getBean();
       }
  
  
  
  1.3       +17 -2     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/IteratorExpression.java
  
  Index: IteratorExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/IteratorExpression.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IteratorExpression.java	23 Aug 2001 14:25:57 -0000	1.2
  +++ IteratorExpression.java	5 Jan 2002 15:52:00 -0000	1.3
  @@ -13,39 +13,54 @@
   /** <p><code>IteratorExpression</code> returns an iterator over the current
context.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.2 $
  +  * @version $Revision: 1.3 $
     */
   public class IteratorExpression implements Expression {
  -
  +    
  +    /** Use this <code>Expression</code> to perform initial evaluation*/
       private Expression expression;
       
  +    /** Construct <code>IteratorExpression</code> using given expression for
initial evaluation.
  +     */
       public IteratorExpression(Expression expression) {
           this.expression = expression;
       }
       
       /** Returns an interator over the current context */
       public Object evaluate(Context context) {        
  +        // evaluate wrapped expression against context
           Object value = expression.evaluate( context );
  +        
  +        // based on the class of the result,
  +        // return an appropriate iterator
           if ( value instanceof Iterator ) {
  +            // if the value is an iterator, we're done
               return (Iterator) value;
           }
           else if ( value instanceof Collection ) {
  +            // if it's a collection, return an iterator for that collection
               Collection collection = (Collection) value;
               return collection.iterator();
           }
           else if ( value instanceof Map ) {
  +            // if it's a map, return an iterator for the map entries
               Map map = (Map) value;
               return map.entrySet().iterator();
           }
           else if ( value instanceof Enumeration ) {
  +            // if it's an enumeration, wrap it in an EnumerationIterator
               return new EnumerationIterator( (Enumeration) value );
           }
           else if ( value != null ) {
  +            // if we have an array return an ArrayIterator
               Class type = value.getClass();
               if ( type.isArray() ) {
                   return new ArrayIterator( value );
               }
           }
  +        
  +        // we've got something we can't deal with
  +        // so return an empty iterator
           return Collections.EMPTY_LIST.iterator();
       }
   }
  
  
  
  1.3       +24 -14    jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/MethodExpression.java
  
  Index: MethodExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/MethodExpression.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MethodExpression.java	23 Aug 2001 14:25:57 -0000	1.2
  +++ MethodExpression.java	5 Jan 2002 15:52:00 -0000	1.3
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: MethodExpression.java,v 1.2 2001/08/23 14:25:57 jstrachan Exp $
  + * $Id: MethodExpression.java,v 1.3 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
  @@ -14,7 +14,7 @@
   /** <p><code>MethodExpression</code> evaluates a method on the current
bean context.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.2 $
  +  * @version $Revision: 1.3 $
     */
   public class MethodExpression implements Expression {
   
  @@ -25,14 +25,16 @@
       /** The method to call on the bean */
       private Method method;
       
  -    
  +    /** Base constructor */
       public MethodExpression() {
       }
       
  +    /** Convenience constructor sets method property */
       public MethodExpression(Method method) {
           this.method = method;
       }
  -    
  +
  +    /** Evaluate by calling method on bean */
       public Object evaluate(Context context) {
           Object bean = context.getBean();
           if ( bean != null ) {
  @@ -50,11 +52,11 @@
                       }
                   }
                   catch (Exception e2) {
  -                    handleException(e2);
  +                    handleException(context, e2);
                   }
               }
               catch (Exception e) {
  -                handleException(e);
  +                handleException(context, e);
               }
           }
           return null;
  @@ -79,9 +81,14 @@
       }
       
       /** Tries to find an alternate method for the given type using interfaces
  -     * which gets around the problem of inner classes, such as on Map.Entry implementations
  -     */
  -    protected Method findAlternateMethod( Class type, Method method ) throws NoSuchMethodException
{
  +      * which gets around the problem of inner classes, 
  +      * such as on Map.Entry implementations.
  +      */
  +    protected Method findAlternateMethod( 
  +                                            Class type, 
  +                                            Method method ) 
  +                                                throws 
  +                                                    NoSuchMethodException {
           Class[] interfaces = type.getInterfaces();
           if ( interfaces != null ) {
               String name = method.getName();
  @@ -96,10 +103,13 @@
           return null;
       }
       
  -    /** Allows derived objects to handle exceptions differently */
  -    protected void handleException(Exception e) {
  -        // XXXX: should use a logging API...
  -        System.out.println( "Caught: " + e );
  -        e.printStackTrace();
  +    /** 
  +      * <p> Log error to context's logger. </p> 
  +      *
  +      * <p> Allows derived objects to handle exceptions differently. </p>
  +      */
  +    protected void handleException(Context context, Exception e) {
  +        // use the context's logger to log the problem
  +        context.getLog().error("[MethodExpression] Cannot evaluate expression", e);
       }
   }
  
  
  
  1.2       +10 -2     jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/StringExpression.java
  
  Index: StringExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/StringExpression.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StringExpression.java	23 Aug 2001 04:34:40 -0000	1.1
  +++ StringExpression.java	5 Jan 2002 15:52:00 -0000	1.2
  @@ -5,26 +5,34 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: StringExpression.java,v 1.1 2001/08/23 04:34:40 jstrachan Exp $
  + * $Id: StringExpression.java,v 1.2 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
   /** <p><code>StringExpression</code> returns the current context object
as a string.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.1 $
  +  * @version $Revision: 1.2 $
     */
   public class StringExpression implements Expression {
  -
  +    
  +    /** We only need only <code>StringExpression</code> */
       private static final StringExpression singleton = new StringExpression();
       
  +    /** Get the singleton */
       public static StringExpression getInstance() {
           return singleton;
       }
       
  +    /** Should this be private? */
       public StringExpression() {
       }
       
  +    /** Return the context bean as a string
  +      *
  +      * @param context evaluate expression against this context
  +      * @return the <code>toString()</code> representation of the context bean
  +      */
       public Object evaluate(Context context) {
           Object value = context.getBean();
           if ( value != null ) {
  
  
  
  1.2       +8 -1      jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/VariableExpression.java
  
  Index: VariableExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/VariableExpression.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- VariableExpression.java	22 Aug 2001 13:18:57 -0000	1.1
  +++ VariableExpression.java	5 Jan 2002 15:52:00 -0000	1.2
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: VariableExpression.java,v 1.1 2001/08/22 13:18:57 jstrachan Exp $
  + * $Id: VariableExpression.java,v 1.2 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.expression;
   
  @@ -13,20 +13,27 @@
     * <code>$foo</code> which returns the value of the given variable.</p>
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.1 $
  +  * @version $Revision: 1.2 $
     */
   public class VariableExpression implements Expression {
   
       /** The variable name */
       private String variableName;
       
  +    /** Base constructor */
       public VariableExpression() {
       }
       
  +    /** Convenience constructor sets <code>VariableName</code> property */
       public VariableExpression(String variableName) {
           this.variableName = variableName;
       }
       
  +    /** Return the value of a context variable.
  +      *
  +      * @param context evaluate against this context
  +      * @return the value of the context variable named by the <code>VariableName</code>
property
  +      */
       public Object evaluate(Context context) {
           return context.getVariable( variableName );
       }
  
  
  
  1.10      +4 -4      jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java
  
  Index: BeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BeanWriter.java	19 Dec 2001 20:15:02 -0000	1.9
  +++ BeanWriter.java	5 Jan 2002 15:52:00 -0000	1.10
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: BeanWriter.java,v 1.9 2001/12/19 20:15:02 rdonkin Exp $
  + * $Id: BeanWriter.java,v 1.10 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt.io;
   
  @@ -46,7 +46,7 @@
     * The indent string used is set by {@link #setIndent}.
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.9 $
  +  * @version $Revision: 1.10 $
     */
   public class BeanWriter {
   
  @@ -104,7 +104,7 @@
           if ( beanInfo != null ) {
               ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
               if ( elementDescriptor != null ) {
  -                Context context = new Context( bean );
  +                Context context = new Context( bean, log );
                   write( elementDescriptor.getQualifiedName(), elementDescriptor, context
);
               }
           }
  @@ -200,7 +200,7 @@
           if ( beanInfo != null ) {
               ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
               if ( elementDescriptor != null ) {
  -                Context context = new Context( bean );
  +                Context context = new Context( bean, log );
                   if ( qualifiedName == null ) {
                       qualifiedName = elementDescriptor.getQualifiedName();
                   }
  
  
  
  1.4       +39 -2     jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java
  
  Index: TestBeanWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestBeanWriter.java	22 Aug 2001 19:44:56 -0000	1.3
  +++ TestBeanWriter.java	5 Jan 2002 15:52:00 -0000	1.4
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: TestBeanWriter.java,v 1.3 2001/08/22 19:44:56 jstrachan Exp $
  + * $Id: TestBeanWriter.java,v 1.4 2002/01/05 15:52:00 rdonkin Exp $
    */
   package org.apache.commons.betwixt;
   
  @@ -15,11 +15,15 @@
   
   import org.apache.commons.betwixt.io.BeanWriter;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogSource;
  +import org.apache.commons.logging.SimpleLog;
  +
   
   /** Test harness for the BeanWriter
     *
     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  -  * @version $Revision: 1.3 $
  +  * @version $Revision: 1.4 $
     */
   public class TestBeanWriter extends AbstractTestCase {
       
  @@ -50,6 +54,39 @@
           writer = new BeanWriter();
           writer.enablePrettyPrint();
           writer.write( bean );
  +    }
  +    
  +    
  +    public void testLooping() throws Exception
  +    {
  +        //LogSource.setLogImplementation(SimpleLog.class);
  +        
  +        
  +        // set up for that writer writes to a buffer
  +        StringWriter buffer = new StringWriter();
  +        BeanWriter writer = new BeanWriter( buffer );
  +        //BeanWriter writer = new BeanWriter();
  +        
  +        //writer.setLogLevel(Log.ERROR);
  +
  +        // set up a cyclic reference
  +        LoopBean alpha = new LoopBean("alpha");
  +        LoopBean beta = new LoopBean("beta");
  +        
  +        beta.setFriend(alpha);
  +        alpha.setFriend(beta);
  +        
  +        // loop bean will throw a runtime exception
  +        // when it look like everything's looping
  +        try {
  +            // try to write the bean
  +            writer.write(alpha);
  +        
  +        } catch (Throwable t) {
  +            // failure!
  +            fail("Cyclic reference produces loop. " + t.getMessage());
  +        
  +        }
       }
   }
   
  
  
  
  1.1                  jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/LoopBean.java
  
  Index: LoopBean.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   * 
   * $Id: LoopBean.java,v 1.1 2002/01/05 15:52:00 rdonkin Exp $
   */
  package org.apache.commons.betwixt;
  
  import java.io.Serializable;
  
  /** <p> This is a bean specifically designed to test cyclic references. 
    * The idea is that there's a count that counts every time <code>getFriend</code>
    * gets called and throws a <code>RuntimeException</code> if the count gets
too high.</p>
    *
    * @author Robert Burrell Donkin
    * @version $Revision: 1.1 $
    */
  public class LoopBean  {
      
      private static int count = 0;
      
      private static final int max_count = 100;
      
      
      private LoopBean friend;
      
      private String name;
      
      
      public LoopBean(String name) {}
      
      public LoopBean getFriend()
      {
          if (++count > max_count)
              throw new RuntimeException("Looping!");
          
          return friend;
      }
      
      public void setFriend(LoopBean friend)
      {
          this.friend = friend;
      }
      
      public String getName()
      {
          return name;
      }
      
      
      public String toString()
      {
          return "[LoopBean] name=" + name;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message