< prev index next >

test/jdk/java/util/Formatter/EncodingTest.java

Print this page




  74 
  75     /**
  76      * Verifies that the overloading constructor behaves the same as the existing
  77      * one.
  78      * @param type the type of the constructor
  79      * @param file1 file1 written with the name of a charset
  80      * @param file2 file2 written with a charset
  81      * @param csn the charset name
  82      * @param charset the charset
  83      * @throws IOException
  84      */
  85     @Test(dataProvider = "parameters")
  86     public void testConstructor(ConstructorType type, File file1, File file2,
  87             String csn, Charset charset) throws Exception {
  88         format(getFormatter(type, file1.getPath(), csn, null));
  89         format(getFormatter(type, file2.getPath(), null, charset));
  90         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
  91                 Files.readAllLines(Paths.get(file2.getPath()), charset));
  92     }
  93 




















  94     void format(Formatter formatter)
  95             throws IOException {
  96         formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
  97         formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");








  98         formatter.flush();
  99         formatter.close();
 100     }
 101 
 102 
 103     Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
 104             throws IOException {
 105         Formatter formatter = null;
 106         if (csn != null) {
 107             switch (type) {
 108                 case STRING3:
 109                     formatter = new Formatter(path, csn, Locale.getDefault());
 110                     break;
 111                 case FILE3:
 112                     formatter = new Formatter(new File(path), csn, Locale.getDefault());
 113                     break;
 114                 case OUTPUTSTREAM3:
 115                     formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
 116                     break;
 117             }


  74 
  75     /**
  76      * Verifies that the overloading constructor behaves the same as the existing
  77      * one.
  78      * @param type the type of the constructor
  79      * @param file1 file1 written with the name of a charset
  80      * @param file2 file2 written with a charset
  81      * @param csn the charset name
  82      * @param charset the charset
  83      * @throws IOException
  84      */
  85     @Test(dataProvider = "parameters")
  86     public void testConstructor(ConstructorType type, File file1, File file2,
  87             String csn, Charset charset) throws Exception {
  88         format(getFormatter(type, file1.getPath(), csn, null));
  89         format(getFormatter(type, file2.getPath(), null, charset));
  90         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
  91                 Files.readAllLines(Paths.get(file2.getPath()), charset));
  92     }
  93 
  94 
  95     /**
  96      * Verifies that the overloading constructor behaves the same as the existing
  97      * one.
  98      * @param type the type of the constructor
  99      * @param file1 file1 written with the name of a charset
 100      * @param file2 file2 written with a charset
 101      * @param csn the charset name
 102      * @param charset the charset
 103      * @throws IOException
 104      */
 105     @Test(dataProvider = "parameters")
 106     public void testConstructorIntrinsic(ConstructorType type, File file1, File file2,
 107                                 String csn, Charset charset) throws Exception {
 108         formatIntrinsic(getFormatter(type, file1.getPath(), csn, null));
 109         formatIntrinsic(getFormatter(type, file2.getPath(), null, charset));
 110         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
 111                 Files.readAllLines(Paths.get(file2.getPath()), charset));
 112     }
 113 
 114     void format(Formatter formatter)
 115             throws IOException {
 116         formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
 117         formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
 118         formatter.flush();
 119         formatter.close();
 120     }
 121 
 122     void formatIntrinsic(Formatter formatter)
 123             throws IOException {
 124         JavacIntrinsicsSupport.formatterFormat(formatter, "abcde \u00FA\u00FB\u00FC\u00FD");
 125         JavacIntrinsicsSupport.formatterFormat(formatter, "Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
 126         formatter.flush();
 127         formatter.close();
 128     }
 129 
 130 
 131     Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
 132             throws IOException {
 133         Formatter formatter = null;
 134         if (csn != null) {
 135             switch (type) {
 136                 case STRING3:
 137                     formatter = new Formatter(path, csn, Locale.getDefault());
 138                     break;
 139                 case FILE3:
 140                     formatter = new Formatter(new File(path), csn, Locale.getDefault());
 141                     break;
 142                 case OUTPUTSTREAM3:
 143                     formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
 144                     break;
 145             }
< prev index next >