tests/libjava-mauve/src/gnu/testlet/java/util/Properties/getProperty.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.1
       
     2 
       
     3 // Copyright (C) 2005 Roman Kennke
       
     4 
       
     5 // This file is part of Mauve.
       
     6 
       
     7 // Mauve is free software; you can redistribute it and/or modify
       
     8 // it under the terms of the GNU General Public License as published by
       
     9 // the Free Software Foundation; either version 2, or (at your option)
       
    10 // any later version.
       
    11 
       
    12 // Mauve is distributed in the hope that it will be useful,
       
    13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 // GNU General Public License for more details.
       
    16 
       
    17 // You should have received a copy of the GNU General Public License
       
    18 // along with Mauve; see the file COPYING.  If not, write to
       
    19 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    20 // Boston, MA 02111-1307, USA.  */
       
    21 
       
    22 package gnu.testlet.java.util.Properties;
       
    23 
       
    24 import gnu.testlet.Testlet;
       
    25 import gnu.testlet.TestHarness;
       
    26 import java.util.Properties;
       
    27 
       
    28 /**
       
    29  * Tests if getProperty(String) calls getProperty(String, String) or vice versa
       
    30  * or if both call an internal method. This is important to avoid infinite
       
    31  * recursion in derived classes that only implement one of these methods
       
    32  *
       
    33  * @author Roman Kennke
       
    34  */
       
    35 public class getProperty implements Testlet
       
    36 {
       
    37   static class Properties1 extends Properties
       
    38   {
       
    39     boolean called = false;
       
    40     public String getProperty(String key)
       
    41     {
       
    42       called = true;
       
    43       return super.getProperty(key);
       
    44     }
       
    45   }
       
    46 
       
    47   static class Properties2 extends Properties
       
    48   {
       
    49     boolean called = false;
       
    50     public String getProperty(String key, String def)
       
    51     {
       
    52       called = true;
       
    53       return super.getProperty(key, def);
       
    54     }
       
    55   }
       
    56 
       
    57   /**
       
    58    * This should not lead to infinite recursion.
       
    59    */
       
    60   static class Properties3 extends Properties
       
    61   {
       
    62     public String getProperty(String key, String def)
       
    63     {
       
    64       return getProperty(key + "." + def);
       
    65     }
       
    66   }
       
    67 
       
    68   public void test (TestHarness harness)
       
    69   {
       
    70     // First we override getProperty(String) and test if
       
    71     // getProperty(String, String)  calls this.
       
    72     Properties1 p1 = new Properties1();
       
    73     p1.setProperty("key", "value");
       
    74     p1.called = false;
       
    75     p1.getProperty("key", "default");
       
    76     harness.check(p1.called, true,
       
    77                   "getProperty(String, String) calls getProperty(String)");
       
    78 
       
    79     // Now we override getProperty(String, String) and test if
       
    80     // getProperty(String)  calls this.
       
    81     Properties2 p2 = new Properties2();
       
    82     p2.setProperty("key", "value");
       
    83     p2.called = false;
       
    84     p2.getProperty("key");
       
    85     harness.check(p2.called, false,
       
    86               "getProperty(String) does not call getProperty(String, String)");
       
    87 
       
    88     // Now we override getProperty(String, String) so that it calls
       
    89     // getProperty(String) and test if
       
    90     // getProperty(String, String) leads to infinite recursion.
       
    91     Properties3 p3 = new Properties3();
       
    92     p3.setProperty("key", "value");
       
    93     try
       
    94       {
       
    95         p3.getProperty("key", "def");
       
    96         harness.check(true, "overriding getProperty(String, String) must not "
       
    97                       + " lead to inifinite recursion.");
       
    98       }
       
    99     catch (Throwable ex)
       
   100       {
       
   101         harness.fail("overriding getProperty(String, String) must not "
       
   102                       + " lead to inifinite recursion.");
       
   103       }
       
   104   }
       
   105 
       
   106 }