tests/libjava-mauve/src/gnu/testlet/java/lang/Class/newInstance.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 // Uses: pkg/test1 pkg/test2 pkg/test3 pkg/test4
       
     4 
       
     5 // Copyright (C) 2005 Jeroen Frijters
       
     6 // Copyright (C) 2006 Mark J. Wielaard
       
     7 
       
     8 // This file is part of Mauve.
       
     9 
       
    10 // Mauve is free software; you can redistribute it and/or modify
       
    11 // it under the terms of the GNU General Public License as published by
       
    12 // the Free Software Foundation; either version 2, or (at your option)
       
    13 // any later version.
       
    14 
       
    15 // Mauve is distributed in the hope that it will be useful,
       
    16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    18 // GNU General Public License for more details.
       
    19 
       
    20 // You should have received a copy of the GNU General Public License
       
    21 // along with Mauve; see the file COPYING.  If not, write to
       
    22 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    23 // Boston, MA 02111-1307, USA.  */
       
    24 
       
    25 package gnu.testlet.java.lang.Class;
       
    26 
       
    27 import java.io.IOException;
       
    28 
       
    29 import gnu.testlet.Testlet;
       
    30 import gnu.testlet.TestHarness;
       
    31 
       
    32 public class newInstance implements Testlet
       
    33 {
       
    34   static class test1
       
    35   {
       
    36     private static class inner 
       
    37     {
       
    38       public inner()
       
    39       {
       
    40       }
       
    41     }
       
    42 
       
    43     test1()
       
    44     {
       
    45     }
       
    46 
       
    47     static void check(TestHarness harness)
       
    48     {
       
    49       try
       
    50         {
       
    51           harness.check(test1.class.isInstance(test1.class.newInstance()));
       
    52         }
       
    53         catch (Throwable t)
       
    54         {
       
    55           harness.debug(t);
       
    56           harness.check(false);
       
    57         }
       
    58     }
       
    59   }
       
    60 
       
    61   public static class test2
       
    62   {
       
    63     public test2()
       
    64     {
       
    65     }
       
    66 
       
    67     static void check(TestHarness harness)
       
    68     {
       
    69       try
       
    70         {
       
    71           harness.check(test2.class.isInstance(test2.class.newInstance()));
       
    72         }
       
    73         catch (Throwable t)
       
    74         {
       
    75           harness.debug(t);
       
    76           harness.check(false);
       
    77         }
       
    78     }
       
    79   }
       
    80 
       
    81   static class test3
       
    82   {
       
    83     public test3()
       
    84     {
       
    85     }
       
    86 
       
    87     static void check(TestHarness harness)
       
    88     {
       
    89       try
       
    90         {
       
    91           harness.check(test3.class.isInstance(test3.class.newInstance()));
       
    92         }
       
    93         catch (Throwable t)
       
    94         {
       
    95           harness.debug(t);
       
    96           harness.check(false);
       
    97         }
       
    98     }
       
    99   }
       
   100 
       
   101   public static class test4
       
   102   {
       
   103     test4()
       
   104     {
       
   105     }
       
   106 
       
   107     static void check(TestHarness harness)
       
   108     {
       
   109       try
       
   110         {
       
   111           harness.check(test4.class.isInstance(test4.class.newInstance()));
       
   112         }
       
   113         catch (Throwable t)
       
   114         {
       
   115           harness.debug(t);
       
   116           harness.check(false);
       
   117         }
       
   118     }
       
   119   }
       
   120 
       
   121   public static class test5
       
   122   {
       
   123     private test5()
       
   124     {
       
   125     }
       
   126 
       
   127     static void check(TestHarness harness)
       
   128     {
       
   129       try
       
   130         {
       
   131           harness.check(test5.class.isInstance(test5.class.newInstance()));
       
   132         }
       
   133         catch (Throwable t)
       
   134         {
       
   135           harness.debug(t);
       
   136           harness.check(false);
       
   137         }
       
   138     }
       
   139   }
       
   140 
       
   141   public static class test6
       
   142   {
       
   143     public test6() throws IOException
       
   144     {
       
   145       throw new IOException("hi bob");
       
   146     }
       
   147     
       
   148     static void check(TestHarness harness)
       
   149     {
       
   150       boolean ok = false;
       
   151       try
       
   152         {
       
   153     	  test6.class.newInstance();
       
   154         }
       
   155       catch (Throwable t)
       
   156         {
       
   157     	  harness.debug(t);
       
   158     	  ok = t instanceof IOException;
       
   159         }
       
   160       harness.check(ok);
       
   161     }
       
   162   }
       
   163 
       
   164   public void test(TestHarness harness)
       
   165   {
       
   166     test1.check(harness);
       
   167     test2.check(harness);
       
   168     test3.check(harness);
       
   169     test4.check(harness);
       
   170     test5.check(harness);
       
   171 
       
   172     checkSuccess(harness, test1.class);
       
   173     checkSuccess(harness, test2.class);
       
   174     checkSuccess(harness, test3.class);
       
   175     checkSuccess(harness, test4.class);
       
   176     // Just see to it that the following is legal.
       
   177     new test5();
       
   178     // If new test5() is legal, why should test5.class.newInstance()
       
   179     // throw IllegalAccessException?  The reason that it is different is
       
   180     // that 'new test5()' will call a compiler-generated accessor
       
   181     // constructor.  This accessor has package-private access and an
       
   182     // extra argument (to differentiate it from the user-written
       
   183     // constructor).
       
   184     checkFail(harness, test5.class);
       
   185 
       
   186     checkSuccess(harness, test1.inner.class);
       
   187 
       
   188     try
       
   189       {
       
   190         checkFail(harness, Class.forName("gnu.testlet.java.lang.Class.pkg.test1"));
       
   191         checkSuccess(harness, Class.forName("gnu.testlet.java.lang.Class.pkg.test2"));
       
   192         checkFail(harness, Class.forName("gnu.testlet.java.lang.Class.pkg.test3"));
       
   193         checkFail(harness, Class.forName("gnu.testlet.java.lang.Class.pkg.test4"));
       
   194       }
       
   195     catch (ClassNotFoundException x)
       
   196       {
       
   197         harness.debug(x);
       
   198         harness.fail("test configuration failure");
       
   199       }
       
   200     
       
   201     test6.check(harness);
       
   202 
       
   203     boolean thrown;
       
   204     // Interfaces cannot be instantiated
       
   205     try
       
   206       {
       
   207         Runnable.class.newInstance();
       
   208         thrown = false;
       
   209       }
       
   210     catch (IllegalAccessException iae)
       
   211       {
       
   212         thrown = false; // Wrong one
       
   213       }
       
   214     catch (InstantiationException ie)
       
   215       {
       
   216         thrown = true;
       
   217       }
       
   218     harness.check(thrown);
       
   219 
       
   220     // Abstract classes cannot be instantiated
       
   221     try
       
   222       {
       
   223         Number.class.newInstance();
       
   224         thrown = false;
       
   225       }
       
   226     catch (IllegalAccessException iae)
       
   227       {
       
   228         thrown = false; // Wrong one
       
   229       }
       
   230     catch (InstantiationException ie)
       
   231       {
       
   232         thrown = true;
       
   233       }
       
   234     harness.check(thrown);
       
   235 
       
   236     // Array classes cannot be instantiated
       
   237     try
       
   238       {
       
   239         new Object[1].getClass().newInstance();
       
   240         thrown = false;
       
   241       }
       
   242     catch (IllegalAccessException iae)
       
   243       {
       
   244         thrown = false; // Wrong one
       
   245       }
       
   246     catch (InstantiationException ie)
       
   247       {
       
   248         thrown = true;
       
   249       }
       
   250     harness.check(thrown);
       
   251 
       
   252     // Primitive classes cannot be instantiated
       
   253     try
       
   254       {
       
   255         Byte.TYPE.newInstance();
       
   256         thrown = false;
       
   257       }
       
   258     catch (IllegalAccessException iae)
       
   259       {
       
   260         thrown = false; // Wrong one
       
   261       }
       
   262     catch (InstantiationException ie)
       
   263       {
       
   264         thrown = true;
       
   265       }
       
   266     harness.check(thrown);
       
   267 
       
   268     // Void cannot be instantiated
       
   269     try
       
   270       {
       
   271         Void.TYPE.newInstance();
       
   272         thrown = false;
       
   273       }
       
   274     catch (IllegalAccessException iae)
       
   275       {
       
   276         thrown = false; // Wrong one
       
   277       }
       
   278     catch (InstantiationException ie)
       
   279       {
       
   280         thrown = true;
       
   281       }
       
   282     harness.check(thrown);
       
   283 
       
   284     // No nullary constructor cannot be instantiated
       
   285     try
       
   286       {
       
   287         Integer.class.newInstance();
       
   288         thrown = false;
       
   289       }
       
   290     catch (IllegalAccessException iae)
       
   291       {
       
   292         thrown = false; // Wrong one
       
   293       }
       
   294     catch (InstantiationException ie)
       
   295       {
       
   296         thrown = true;
       
   297       }
       
   298     harness.check(thrown);
       
   299 
       
   300   }
       
   301 
       
   302   static void checkSuccess(TestHarness harness, Class c)
       
   303   {
       
   304     try
       
   305       {
       
   306         harness.check(c.isInstance(c.newInstance()));
       
   307       }
       
   308     catch (Throwable t)
       
   309       {
       
   310         harness.debug(t);
       
   311         harness.check(false);
       
   312       }
       
   313   }
       
   314 
       
   315   static void checkFail(TestHarness harness, Class c)
       
   316   {
       
   317     try
       
   318       {
       
   319         c.newInstance();
       
   320         harness.check(false);
       
   321       }
       
   322     catch (IllegalAccessException x)
       
   323       {
       
   324         harness.check(true);
       
   325       }
       
   326     catch (Throwable t)
       
   327       {
       
   328         harness.debug(t);
       
   329         harness.check(false);
       
   330       }
       
   331   }
       
   332 }