test/java/util/Formatter/Constructors.java

Print this page

        

@@ -20,11 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 4981811 4984465 5064492 6240171
+ * @bug 4981811 4984465 5064492 6240171 7000511
  * @summary Unit test for all constructors introduced by the formatter feature
  */
 
 import java.io.*;
 import java.util.*;

@@ -83,73 +83,66 @@
             fail(x.getMessage());
         }
     }
 
     public static void main(String [] args) {
-
         // Formatter()
-        try {
-            Formatter f = new Formatter();
+        try (Formatter f = new Formatter()) {
             pass();
             out(f, StringBuilder.class);
             locale(f);
         } catch (Exception x) {
             fail("new Formatter()", x);
         }
 
         // Formatter(Appendable a)
-        try {
-            Formatter f = new Formatter((Appendable) null);
+        try (Formatter f = new Formatter((Appendable) null)) {
             pass();
             out(f, StringBuilder.class);
             locale(f);
         } catch (Exception x) {
             fail("new Formatter((Appendable)null)", x);
         }
 
         // Formatter(Locale l)
-        try {
-            Formatter f = new Formatter((Locale) null);
+        try (Formatter f = new Formatter((Locale) null)) {
             pass();
             out(f, StringBuilder.class);
             locale(f, null);
         } catch (Exception x) {
             fail("new Formatter((Locale)null)", x);
         }
 
         // Formatter(Appendable a, Locale l)
-        try {
-            Formatter f = new Formatter((Appendable) null, (Locale) null);
+        try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {
             pass();
             out(f, StringBuilder.class);
             locale(f, null);
         } catch (Exception x) {
             fail("new Formatter((Appendable) null, (Locale) null)", x);
         }
 
         // Formatter(String fileName)
-        try {
-            Formatter f = new Formatter("foo");
+        try (Formatter f = new Formatter("foo")) {
             pass();
             out(f, BufferedWriter.class);
             locale(f);
         } catch (Exception x) {
             fail("new Formatter(\"foo\")", x);
         }
 
         try {
-            Formatter f = new Formatter((String)null);
+            new Formatter((String)null);
             fail("new Formatter((String)null)");
         } catch (NullPointerException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter((String)null)", x);
         }
 
         // Formatter(String fileName, String csn)
-        try {
-            Formatter f = new Formatter("foo", "UTF-8");
+        try (Formatter f = new Formatter("foo", "UTF-8")) {
             pass();
             out(f, BufferedWriter.class);
             locale(f);
         } catch (Exception x) {
             fail("new Formatter(\"foo\", \"UTF-8\")", x);

@@ -165,47 +158,44 @@
         }
 
         try {
             new Formatter(".", "bar");
             fail("new Formatter(\".\", \"bar\")");
-        } catch (FileNotFoundException x) {
+        } catch (FileNotFoundException|UnsupportedEncodingException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter(\".\", \"bar\")", x);
         }
 
         // Formatter(String fileName, String csn, Locale l)
-        try {
-            Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY);
+        try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {
             pass();
             out(f, BufferedWriter.class);
             locale(f, Locale.GERMANY);
         } catch (Exception x) {
             fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);
         }
 
-        try {
-            Formatter f = new Formatter("foo", "ISO-8859-1", null);
+        try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {
             pass();
             locale(f, null);
             out(f, BufferedWriter.class);
         } catch (Exception x) {
             fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);
         }
 
         // Formatter(File)
-        try {
-            Formatter f = new Formatter(new File("foo"));
+        try (Formatter f = new Formatter(new File("foo"))) {
             pass();
             locale(f);
             out(f, BufferedWriter.class);
         } catch (Exception x) {
             fail("new Formatter(new File(\"foo\")", x);
         }
 
         try {
-            Formatter f = new Formatter((File)null);
+            new Formatter((File)null);
             fail("new Formatter((File)null)");
         } catch (NullPointerException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter((File)null)", x);

@@ -229,39 +219,36 @@
             pass();
         } catch (Exception x) {
             fail("new Formatter((PrintStream) null)", x);
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"));
+        try (Formatter f = new Formatter(new PrintStream("foo"))) {
             pass();
             locale(f);
             out(f, PrintStream.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\")", x);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\")", x);
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        Locale.JAPANESE);
+        try (Formatter f = new Formatter(new PrintStream("foo"),
+                                         Locale.JAPANESE)) {
             pass();
             locale(f, Locale.JAPANESE);
             out(f, PrintStream.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\")", x);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\")", x);
         }
 
-        try {
+        try (PrintStream ps = new PrintStream("foo")) {
             // The cast here is necessary to avoid an ambiguity error
             // between Formatter(Appendable a, Locale l)
             // and     Formatter(OutputStream os, String csn)
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        (String)null);
+            new Formatter(ps, (String)null);
             fail("new Formatter(new PrintStream(\"foo\"), (String)null)");
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
         } catch (NullPointerException x) {
             pass();

@@ -269,40 +256,37 @@
             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
         }
 
-        try {
             // The cast here is necessary to avoid an ambiguity error
             // between Formatter(Appendable a, Locale l)
             // and     Formatter(OutputStream os, String csn)
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        (Locale)null);
+        try (Formatter f = new Formatter(new PrintStream("foo"),
+                                         (Locale)null)) {
             pass();
             locale(f, null);
             out(f, PrintStream.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        Locale.KOREAN);
+        try (Formatter f = new Formatter(new PrintStream("foo"),
+                                         Locale.KOREAN)) {
             pass();
             locale(f, Locale.KOREAN);
             out(f, PrintStream.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        "UTF-16BE", null);
+        try (Formatter f = new Formatter(new PrintStream("foo"),
+                                         "UTF-16BE", null)) {
             pass();
             locale(f, null);
             out(f, BufferedWriter.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");

@@ -310,13 +294,12 @@
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"),
-                                        "UTF-16BE", Locale.ITALIAN);
+        try (Formatter f = new Formatter(new PrintStream("foo"),
+                                         "UTF-16BE", Locale.ITALIAN)) {
             pass();
             locale(f, Locale.ITALIAN);
             out(f, BufferedWriter.class);
         } catch (FileNotFoundException x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");

@@ -359,12 +342,11 @@
             pass();
         } catch (Exception x) {
             fail("new Formatter((OutputStream) null)", x);
         }
 
-        try {
-            Formatter f = new Formatter((OutputStream) new PrintStream("foo"));
+        try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {
             pass();
             locale(f);
             out(f, BufferedWriter.class);
         } catch (Exception x) {
             fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);

@@ -378,31 +360,30 @@
             pass();
         } catch (Exception x) {
             fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);
         }
 
-        try {
-            new Formatter((OutputStream) new PrintStream("foo"), null);
+        try (PrintStream ps = new PrintStream("foo")) {
+            new Formatter((OutputStream) ps, null);
             fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");
         } catch (NullPointerException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",
                  x);
         }
 
-        try {
-            new Formatter(new PrintStream("foo"), "bar");
+        try (PrintStream ps = new PrintStream("foo")) {
+            new Formatter(ps, "bar");
             fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");
         } catch (UnsupportedEncodingException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);
         }
 
-        try {
-            Formatter f = new Formatter(new PrintStream("foo"), "UTF-8");
+        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {
             pass();
             locale(f);
             out(f, BufferedWriter.class);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);

@@ -417,44 +398,41 @@
         } catch (Exception x) {
             fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",
                  x);
         }
 
-        try {
-            new Formatter(new PrintStream("foo"), null, Locale.UK);
+        try (PrintStream ps = new PrintStream("foo")) {
+            new Formatter(ps, null, Locale.UK);
             fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)");
         } catch (NullPointerException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)",
                  x);
         }
 
-        try {
-            new Formatter(new PrintStream("foo"), "bar", Locale.UK);
+        try (PrintStream ps = new PrintStream("foo")) {
+            new Formatter(ps, "bar", Locale.UK);
             fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");
         } catch (UnsupportedEncodingException x) {
             pass();
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",
                  x);
         }
 
-        try {
-            Formatter f
-                = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK);
+        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {
             pass();
             out(f, BufferedWriter.class);
             locale(f, Locale.UK);
         } catch (Exception x) {
             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",
                  x);
         }
 
         // PrintStream(String fileName)
-        try {
-            new PrintStream("foo");
+        try (PrintStream ps = new PrintStream("foo")) {
             pass();
         } catch (Exception x) {
             fail("new PrintStream(\"foo\")", x);
         }
 

@@ -467,12 +445,11 @@
         } catch (Exception x) {
             fail("new PrintStream(\"foo\", null)", x);
         }
 
         // PrintStream(File file)
-        try {
-            new PrintStream(new File("foo"));
+        try (PrintStream ps = new PrintStream(new File("foo"))) {
             pass();
         } catch (Exception x) {
             fail("new PrintStream(new File(\"foo\"))", x);
         }
 

@@ -485,12 +462,11 @@
         } catch (Exception x) {
             fail("new PrintStream(new File(\"foo\"), null)", x);
         }
 
         // PrintWriter(String fileName)
-        try {
-            new PrintWriter("foo");
+        try (PrintWriter pw = new PrintWriter("foo")) {
             pass();
         } catch (Exception x) {
             fail("new PrintWriter(\"foo\")", x);
         }
 

@@ -503,12 +479,11 @@
         } catch (Exception x) {
             fail("new PrintWriter(\"foo\"), null", x);
         }
 
         // PrintWriter(File file)
-        try {
-            new PrintWriter(new File("foo"));
+        try (PrintWriter pw = new PrintWriter(new File("foo"))) {
             pass();
         } catch (Exception x) {
             fail("new PrintWriter(new File(\"foo\"))", x);
         }