1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.io.Writer;
  26 
  27 import org.testng.annotations.AfterGroups;
  28 import org.testng.annotations.BeforeGroups;
  29 import org.testng.annotations.Test;
  30 
  31 import static org.testng.Assert.*;
  32 
  33 /*
  34  * @test
  35  * @bug 8196298
  36  * @run testng NullWriter
  37  * @summary Check for expected behavior of Writer.nullWriter().
  38  */
  39 public class NullWriter {
  40     private static Writer openWriter;
  41     private static Writer closedWriter;
  42 
  43     @BeforeGroups(groups = "open")
  44     public static void openStream() {
  45         openWriter = Writer.nullWriter();
  46     }
  47 
  48     @BeforeGroups(groups = "closed")
  49     public static void openAndCloseStream() throws IOException {
  50         closedWriter = Writer.nullWriter();
  51         closedWriter.close();
  52     }
  53 
  54     @AfterGroups(groups = "open")
  55     public static void closeStream() throws IOException {
  56         openWriter.close();
  57     }
  58 
  59     @Test(groups = "open")
  60     public static void testOpen() {
  61         assertNotNull(openWriter, "Writer.nullWriter() returned null");
  62     }
  63 
  64     @Test(groups = "open")
  65     public static void testAppendChar() throws IOException {
  66         assertSame(openWriter, openWriter.append('x'));
  67     }
  68 
  69     @Test(groups = "open")
  70     public static void testAppendCharSequence() throws IOException {
  71         CharSequence cs = "abc";
  72         assertSame(openWriter, openWriter.append(cs));
  73     }
  74 
  75     @Test(groups = "open")
  76     public static void testAppendCharSequenceNull() throws IOException {
  77         assertSame(openWriter, openWriter.append(null));
  78     }
  79 
  80     @Test(groups = "open")
  81     public static void testAppendCharSequenceII() throws IOException {
  82         CharSequence cs = "abc";
  83         assertSame(openWriter, openWriter.append(cs, 0, 1));
  84     }
  85 
  86     @Test(groups = "open")
  87     public static void testAppendCharSequenceIINull() throws IOException {
  88         assertSame(openWriter, openWriter.append(null, 2, 1));
  89     }
  90 
  91     @Test(groups = "open")
  92     public static void testFlush() throws IOException {
  93         openWriter.flush();
  94     }
  95 
  96     @Test(groups = "open")
  97     public static void testWrite() throws IOException {
  98         openWriter.write(62832);
  99     }
 100 
 101     @Test(groups = "open")
 102     public static void testWriteString() throws IOException {
 103         openWriter.write("");
 104     }
 105 
 106     @Test(groups = "open")
 107     public static void testWriteStringII() throws IOException {
 108         openWriter.write("", 0, 0);
 109     }
 110 
 111     @Test(groups = "open")
 112     public static void testWriteBII() throws IOException, Exception {
 113         openWriter.write(new char[]{(char) 6}, 0, 1);
 114     }
 115 
 116     @Test(groups = "closed", expectedExceptions = IOException.class)
 117     public static void testAppendCharClosed() throws IOException {
 118         closedWriter.append('x');
 119     }
 120 
 121     @Test(groups = "closed", expectedExceptions = IOException.class)
 122     public static void testAppendCharSequenceClosed() throws IOException {
 123         CharSequence cs = "abc";
 124         closedWriter.append(cs);
 125     }
 126 
 127     @Test(groups = "closed", expectedExceptions = IOException.class)
 128     public static void testAppendCharSequenceNullClosed() throws IOException {
 129         closedWriter.append(null);
 130     }
 131 
 132     @Test(groups = "closed", expectedExceptions = IOException.class)
 133     public static void testAppendCharSequenceIIClosed() throws IOException {
 134         CharSequence cs = "abc";
 135         closedWriter.append(cs, 0, 1);
 136     }
 137 
 138     @Test(groups = "closed", expectedExceptions = IOException.class)
 139     public static void testAppendCharSequenceIINullClosed() throws IOException {
 140         closedWriter.append(null, 2, 1);
 141     }
 142 
 143     @Test(groups = "closed", expectedExceptions = IOException.class)
 144     public static void testFlushClosed() throws IOException {
 145         closedWriter.flush();
 146     }
 147 
 148     @Test(groups = "closed", expectedExceptions = IOException.class)
 149     public static void testWriteClosed() throws IOException {
 150         closedWriter.write(62832);
 151     }
 152 
 153     @Test(groups = "closed", expectedExceptions = IOException.class)
 154     public static void testWriteStringClosed() throws IOException {
 155         closedWriter.write("");
 156     }
 157 
 158     @Test(groups = "closed", expectedExceptions = IOException.class)
 159     public static void testWriteStringIIClosed() throws IOException {
 160         closedWriter.write("", 0, 0);
 161     }
 162 
 163     @Test(groups = "closed", expectedExceptions = IOException.class)
 164     public static void testWriteBIIClosed() throws IOException {
 165         closedWriter.write(new char[]{(char) 6}, 0, 1);
 166     }
 167 }