001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.File;
021: import java.io.FileDescriptor;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025:
026: import junit.framework.TestCase;
027:
028: public class FileOutputStreamTest extends TestCase {
029:
030: public String fileName;
031:
032: FileOutputStream fos;
033:
034: FileInputStream fis;
035:
036: File f;
037:
038: byte[] ibuf = new byte[4096];
039:
040: public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
041:
042: /**
043: * @tests java.io.FileOutputStream#FileOutputStream(java.io.File)
044: */
045: public void test_ConstructorLjava_io_File() throws IOException {
046: f = new File(fileName = System.getProperty("user.home"),
047: "fos.tst");
048: fos = new FileOutputStream(f);
049: }
050:
051: /**
052: * @tests java.io.FileOutputStream#FileOutputStream(java.io.FileDescriptor)
053: */
054: public void test_ConstructorLjava_io_FileDescriptor()
055: throws IOException {
056: f = new File(fileName = System.getProperty("user.home"),
057: "fos.tst");
058: fileName = f.getAbsolutePath();
059: fos = new FileOutputStream(fileName);
060: fos.write('l');
061: fos.close();
062: fis = new FileInputStream(fileName);
063: fos = new FileOutputStream(fis.getFD());
064: fos.close();
065: fis.close();
066: }
067:
068: /**
069: * @tests java.io.FileOutputStream#FileOutputStream(java.lang.String)
070: */
071: public void test_ConstructorLjava_lang_String() throws IOException {
072: f = new File(fileName = System.getProperty("user.home"),
073: "fos.tst");
074: fileName = f.getAbsolutePath();
075: fos = new FileOutputStream(fileName);
076:
077: // Regression test for HARMONY-4012
078: new FileOutputStream("nul");
079: }
080:
081: /**
082: * @tests java.io.FileOutputStream#FileOutputStream(java.lang.String,
083: * boolean)
084: */
085: public void test_ConstructorLjava_lang_StringZ() throws IOException {
086: f = new File(System.getProperty("user.home"), "fos.tst");
087: fos = new FileOutputStream(f.getPath(), false);
088: fos.write("HI".getBytes(), 0, 2);
089: fos.close();
090: fos = new FileOutputStream(f.getPath(), true);
091: fos.write(fileString.getBytes());
092: fos.close();
093: byte[] buf = new byte[fileString.length() + 2];
094: fis = new FileInputStream(f.getPath());
095: fis.read(buf, 0, buf.length);
096: assertTrue("Failed to create appending stream", new String(buf,
097: 0, buf.length).equals("HI" + fileString));
098: }
099:
100: /**
101: * @tests java.io.FileOutputStream#close()
102: */
103: public void test_close() throws IOException {
104: f = new File(System.getProperty("user.home"), "output.tst");
105: fos = new FileOutputStream(f.getPath());
106: fos.close();
107:
108: try {
109: fos.write(fileString.getBytes());
110: fail("Close test failed - wrote to closed stream");
111: } catch (IOException e) {
112: // Expected
113: }
114: }
115:
116: /**
117: * @tests java.io.FileOutputStream#getFD()
118: */
119: public void test_getFD() throws IOException {
120: f = new File(fileName = System.getProperty("user.home"),
121: "testfd");
122: fileName = f.getAbsolutePath();
123: fos = new FileOutputStream(f);
124: assertTrue("Returned invalid fd", fos.getFD().valid());
125: fos.close();
126: assertTrue("Returned invalid fd", !fos.getFD().valid());
127: }
128:
129: /**
130: * @tests java.io.FileOutputStream#write(byte[])
131: */
132: public void test_write$B() throws IOException {
133: f = new File(System.getProperty("user.home"), "output.tst");
134: fos = new FileOutputStream(f.getPath());
135: fos.write(fileString.getBytes());
136: fis = new FileInputStream(f.getPath());
137: byte rbytes[] = new byte[4000];
138: fis.read(rbytes, 0, fileString.length());
139: assertTrue("Incorrect string returned", new String(rbytes, 0,
140: fileString.length()).equals(fileString));
141: }
142:
143: /**
144: * @tests java.io.FileOutputStream#write(byte[], int, int)
145: */
146: public void test_write$BII() throws IOException {
147: f = new File(System.getProperty("user.home"), "output.tst");
148: fos = new FileOutputStream(f.getPath());
149: fos.write(fileString.getBytes(), 0, fileString.length());
150: fis = new FileInputStream(f.getPath());
151: byte rbytes[] = new byte[4000];
152: fis.read(rbytes, 0, fileString.length());
153: assertTrue("Incorrect bytes written", new String(rbytes, 0,
154: fileString.length()).equals(fileString));
155:
156: // Regression test for HARMONY-285
157: File file = new File("FileOutputStream.tmp");
158: file.deleteOnExit();
159: FileOutputStream out = new FileOutputStream(file);
160: try {
161: out.write(null, 0, 0);
162: fail("Should throw NullPointerException");
163: } catch (NullPointerException e) {
164: // Expected
165: }
166: }
167:
168: /**
169: * @tests java.io.FileOutputStream#write(int)
170: */
171: public void test_writeI() throws IOException {
172: f = new File(System.getProperty("user.home"), "output.tst");
173: fos = new FileOutputStream(f.getPath());
174: fos.write('t');
175: fis = new FileInputStream(f.getPath());
176: assertEquals("Incorrect char written", 't', fis.read());
177: }
178:
179: /**
180: * @tests java.io.FileOutputStream#write(byte[], int, int)
181: */
182: public void test_write$BII2() throws IOException {
183: // Regression for HARMONY-437
184: f = new File(System.getProperty("user.home"), "output.tst");
185: fos = new FileOutputStream(f.getPath());
186:
187: try {
188: fos.write(null, 1, 1);
189: fail("NullPointerException must be thrown");
190: } catch (NullPointerException e) {
191: }
192:
193: try {
194: fos.write(new byte[1], -1, 1);
195: fail("IndexOutOfBoundsException must be thrown if off <0");
196: } catch (IndexOutOfBoundsException e) {
197: }
198:
199: try {
200: fos.write(new byte[1], 0, -1);
201: fail("IndexOutOfBoundsException must be thrown if len <0");
202: } catch (IndexOutOfBoundsException e) {
203: }
204:
205: try {
206: fos.write(new byte[1], 0, 5);
207: fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
208: } catch (IndexOutOfBoundsException e) {
209: }
210:
211: try {
212: fos.write(new byte[10], Integer.MAX_VALUE, 5);
213: fail("IndexOutOfBoundsException expected");
214: } catch (IndexOutOfBoundsException e) {
215: }
216:
217: try {
218: fos.write(new byte[10], 5, Integer.MAX_VALUE);
219: fail("IndexOutOfBoundsException expected");
220: } catch (IndexOutOfBoundsException e) {
221: }
222: fos.close();
223: }
224:
225: /**
226: * @tests java.io.FileOutputStream#write(byte[], int, int)
227: */
228: public void test_write$BII3() throws IOException {
229: // Regression for HARMONY-834
230: // no exception expected
231: new FileOutputStream(new FileDescriptor()).write(new byte[1],
232: 0, 0);
233: }
234:
235: /**
236: * @tests java.io.FileOutputStream#getChannel()
237: */
238: public void test_getChannel() throws IOException {
239: // Regression for HARMONY-508
240: File tmpfile = File.createTempFile("FileOutputStream", "tmp");
241: tmpfile.deleteOnExit();
242: FileOutputStream fos = new FileOutputStream(tmpfile);
243: byte[] b = new byte[10];
244: for (int i = 0; i < b.length; i++) {
245: b[i] = (byte) i;
246: }
247: fos.write(b);
248: fos.flush();
249: fos.close();
250: FileOutputStream f = new FileOutputStream(tmpfile, true);
251: assertEquals(10, f.getChannel().position());
252: }
253:
254: /**
255: * Tears down the fixture, for example, close a network connection. This
256: * method is called after a test is executed.
257: */
258: @Override
259: protected void tearDown() throws Exception {
260: super.tearDown();
261: if (f != null) {
262: f.delete();
263: }
264: if (fis != null) {
265: fis.close();
266: }
267: if (fos != null) {
268: fos.close();
269: }
270: }
271: }
|