tests/libjava-mauve/src/gnu/testlet/java/lang/reflect/Proxy/ToString.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 /* ToString.java -- Tests which checks that the toString method on a proxy is
       
     2  correctly forwarded to the InvocationHandler
       
     3  Copyright (C) 2006 olivier jolly <olivier.jolly@pcedev.com>
       
     4  This file is part of Mauve.
       
     5 
       
     6  Mauve is free software; you can redistribute it and/or modify
       
     7  it under the terms of the GNU General Public License as published by
       
     8  the Free Software Foundation; either version 2, or (at your option)
       
     9  any later version.
       
    10 
       
    11  Mauve is distributed in the hope that it will be useful, but
       
    12  WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  General Public License for more details.
       
    15 
       
    16  You should have received a copy of the GNU General Public License
       
    17  along with Mauve; see the file COPYING.  If not, write to the
       
    18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
       
    19  02110-1301 USA.
       
    20 
       
    21  */
       
    22 
       
    23 // Tags: JDK1.3
       
    24 
       
    25 
       
    26 package gnu.testlet.java.lang.reflect.Proxy;
       
    27 
       
    28 import gnu.testlet.TestHarness;
       
    29 import gnu.testlet.Testlet;
       
    30 
       
    31 import java.io.Serializable;
       
    32 import java.lang.reflect.InvocationHandler;
       
    33 import java.lang.reflect.Method;
       
    34 import java.lang.reflect.Proxy;
       
    35 
       
    36 /**
       
    37  * Test which ensure that the toString method is properly forwarded to the
       
    38  * InvocationHandler. This tests notably fails with cacao 0.94
       
    39  * (http://b2.complang.tuwien.ac.at/cgi-bin/bugzilla/show_bug.cgi?id=17)
       
    40  * @author olivier jolly <olivier.jolly@pcedev.com>
       
    41  * @see java.lang.reflect.InvocationHandler
       
    42  */
       
    43 public class ToString implements Testlet
       
    44 {
       
    45 
       
    46   public void test(TestHarness harness)
       
    47   {
       
    48     InvocationHandler handler = new FacadeInvocationHandler(new Foo());
       
    49     Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(),
       
    50                                           new Class[] { Serializable.class },
       
    51                                           handler);
       
    52     harness.check(proxy.toString(), "foo toString() result", "toString() test");
       
    53   }
       
    54 
       
    55   /**
       
    56    * Very simple facade which delegates all calls to a target object.
       
    57    */
       
    58   private static class FacadeInvocationHandler implements InvocationHandler
       
    59   {
       
    60 
       
    61     Object facaded;
       
    62 
       
    63     public FacadeInvocationHandler(Object facaded)
       
    64     {
       
    65       this.facaded = facaded;
       
    66     }
       
    67 
       
    68     public Object invoke(Object proxy, Method method, Object[] args)
       
    69         throws Throwable
       
    70     {
       
    71       return method.invoke(facaded, args);
       
    72     }
       
    73 
       
    74   }
       
    75 
       
    76   /**
       
    77    * Very simple class with a predictable toString() result.
       
    78    */
       
    79   private static class Foo
       
    80   {
       
    81 
       
    82     public String toString()
       
    83     {
       
    84       return "foo toString() result";
       
    85     }
       
    86 
       
    87   }
       
    88 
       
    89 }