mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Add tests for FileInputStream and FileOutputStream
This commit is contained in:
parent
a96b5912c7
commit
c9485ccbdc
|
@ -511,6 +511,7 @@ public class TFile implements Serializable, Comparable<TFile> {
|
||||||
} else {
|
} else {
|
||||||
tmpDirFile = directory;
|
tmpDirFile = directory;
|
||||||
}
|
}
|
||||||
|
tmpDirFile.mkdirs();
|
||||||
TFile result;
|
TFile result;
|
||||||
do {
|
do {
|
||||||
result = genTempFile(prefix, newSuffix, tmpDirFile);
|
result = genTempFile(prefix, newSuffix, tmpDirFile);
|
||||||
|
|
|
@ -39,12 +39,19 @@ public class TFileInputStream extends InputStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TFileInputStream(String path) throws FileNotFoundException {
|
||||||
|
this(new TFile(path));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] b, int off, int len) throws IOException {
|
public int read(byte[] b, int off, int len) throws IOException {
|
||||||
Objects.requireNonNull(b);
|
Objects.requireNonNull(b);
|
||||||
if (off < 0 || len < 0 || off + len > b.length) {
|
if (off < 0 || len < 0 || off > b.length - len) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
|
if (len == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if (eof) {
|
if (eof) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -59,11 +66,14 @@ public class TFileInputStream extends InputStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long skip(long n) throws IOException {
|
public long skip(long n) throws IOException {
|
||||||
|
if (n < 0) {
|
||||||
|
throw new IOException("Value must be positive: " + n);
|
||||||
|
}
|
||||||
ensureOpened();
|
ensureOpened();
|
||||||
if (eof) {
|
if (eof) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int newPos = Math.max(pos, Math.min(accessor.size(), pos));
|
int newPos = Math.max(pos, Math.min(accessor.size(), pos + (int) n));
|
||||||
int result = newPos - pos;
|
int result = newPos - pos;
|
||||||
pos = newPos;
|
pos = newPos;
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
|
|
|
@ -31,7 +31,18 @@ public class TFileOutputStream extends OutputStream {
|
||||||
this(file, false);
|
this(file, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TFileOutputStream(String path) throws FileNotFoundException {
|
||||||
|
this(new TFile(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public TFileOutputStream(String path, boolean append) throws FileNotFoundException {
|
||||||
|
this(new TFile(path), append);
|
||||||
|
}
|
||||||
|
|
||||||
public TFileOutputStream(TFile file, boolean append) throws FileNotFoundException {
|
public TFileOutputStream(TFile file, boolean append) throws FileNotFoundException {
|
||||||
|
if (file.getName().isEmpty()) {
|
||||||
|
throw new FileNotFoundException("Invalid file name");
|
||||||
|
}
|
||||||
VirtualFile virtualFile = file.findVirtualFile();
|
VirtualFile virtualFile = file.findVirtualFile();
|
||||||
if (virtualFile == null) {
|
if (virtualFile == null) {
|
||||||
VirtualFile parentVirtualFile = file.findParentFile();
|
VirtualFile parentVirtualFile = file.findParentFile();
|
||||||
|
@ -60,7 +71,7 @@ public class TFileOutputStream extends OutputStream {
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
Objects.requireNonNull(b);
|
Objects.requireNonNull(b);
|
||||||
if (off < 0 || len < 0 || off + len > b.length) {
|
if (off < 0 || len < 0 || off > b.length - len) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
ensureOpened();
|
ensureOpened();
|
||||||
|
|
|
@ -0,0 +1,387 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2017 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.teavm.junit.TeaVMTestRunner;
|
||||||
|
|
||||||
|
@RunWith(TeaVMTestRunner.class)
|
||||||
|
public class FileInputStreamTest {
|
||||||
|
public String fileName;
|
||||||
|
public String fileString = ""
|
||||||
|
+ "Test_All_Tests\n" + "Test_java_io_BufferedInputStream\n" + "Test_java_io_BufferedOutputStream\n"
|
||||||
|
+ "Test_java_io_ByteArrayInputStream\n" + "Test_java_io_ByteArrayOutputStream\n"
|
||||||
|
+ "Test_java_io_DataInputStream\n" + "Test_java_io_File\n" + "Test_java_io_FileDescriptor\n"
|
||||||
|
+ "Test_FileInputStream\n" + "Test_java_io_FileNotFoundException\n"
|
||||||
|
+ "Test_java_io_FileOutputStream\n" + "Test_java_io_FilterInputStream\n"
|
||||||
|
+ "Test_java_io_FilterOutputStream\n" + "Test_java_io_InputStream\n" + "Test_java_io_IOException\n"
|
||||||
|
+ "Test_java_io_OutputStream\n" + "Test_java_io_PrintStream\n" + "Test_java_io_RandomAccessFile\n"
|
||||||
|
+ "Test_java_io_SyncFailedException\n" + "Test_java_lang_AbstractMethodError\n"
|
||||||
|
+ "Test_java_lang_ArithmeticException\n" + "Test_java_lang_ArrayIndexOutOfBoundsException\n"
|
||||||
|
+ "Test_java_lang_ArrayStoreException\n" + "Test_java_lang_Boolean\n" + "Test_java_lang_Byte\n"
|
||||||
|
+ "Test_java_lang_Character\n" + "Test_java_lang_Class\n" + "Test_java_lang_ClassCastException\n"
|
||||||
|
+ "Test_java_lang_ClassCircularityError\n" + "Test_java_lang_ClassFormatError\n"
|
||||||
|
+ "Test_java_lang_ClassLoader\n" + "Test_java_lang_ClassNotFoundException\n"
|
||||||
|
+ "Test_java_lang_CloneNotSupportedException\n" + "Test_java_lang_Double\n"
|
||||||
|
+ "Test_java_lang_Error\n" + "Test_java_lang_Exception\n"
|
||||||
|
+ "Test_java_lang_ExceptionInInitializerError\n" + "Test_java_lang_Float\n"
|
||||||
|
+ "Test_java_lang_IllegalAccessError\n" + "Test_java_lang_IllegalAccessException\n"
|
||||||
|
+ "Test_java_lang_IllegalArgumentException\n" + "Test_java_lang_IllegalMonitorStateException\n"
|
||||||
|
+ "Test_java_lang_IllegalThreadStateException\n" + "Test_java_lang_IncompatibleClassChangeError\n"
|
||||||
|
+ "Test_java_lang_IndexOutOfBoundsException\n" + "Test_java_lang_InstantiationError\n"
|
||||||
|
+ "Test_java_lang_InstantiationException\n" + "Test_java_lang_Integer\n"
|
||||||
|
+ "Test_java_lang_InternalError\n" + "Test_java_lang_InterruptedException\n"
|
||||||
|
+ "Test_java_lang_LinkageError\n" + "Test_java_lang_Long\n" + "Test_java_lang_Math\n"
|
||||||
|
+ "Test_java_lang_NegativeArraySizeException\n" + "Test_java_lang_NoClassDefFoundError\n"
|
||||||
|
+ "Test_java_lang_NoSuchFieldError\n" + "Test_java_lang_NoSuchMethodError\n"
|
||||||
|
+ "Test_java_lang_NullPointerException\n" + "Test_java_lang_Number\n"
|
||||||
|
+ "Test_java_lang_NumberFormatException\n" + "Test_java_lang_Object\n"
|
||||||
|
+ "Test_java_lang_OutOfMemoryError\n" + "Test_java_lang_RuntimeException\n"
|
||||||
|
+ "Test_java_lang_SecurityManager\n" + "Test_java_lang_Short\n"
|
||||||
|
+ "Test_java_lang_StackOverflowError\n" + "Test_java_lang_String\n"
|
||||||
|
+ "Test_java_lang_StringBuffer\n" + "Test_java_lang_StringIndexOutOfBoundsException\n"
|
||||||
|
+ "Test_java_lang_System\n" + "Test_java_lang_Thread\n" + "Test_java_lang_ThreadDeath\n"
|
||||||
|
+ "Test_java_lang_ThreadGroup\n" + "Test_java_lang_Throwable\n" + "Test_java_lang_UnknownError\n"
|
||||||
|
+ "Test_java_lang_UnsatisfiedLinkError\n" + "Test_java_lang_VerifyError\n"
|
||||||
|
+ "Test_java_lang_VirtualMachineError\n" + "Test_java_lang_vm_Image\n"
|
||||||
|
+ "Test_java_lang_vm_MemorySegment\n" + "Test_java_lang_vm_ROMStoreException\n"
|
||||||
|
+ "Test_java_lang_vm_VM\n" + "Test_java_lang_Void\n" + "Test_java_net_BindException\n"
|
||||||
|
+ "Test_java_net_ConnectException\n" + "Test_java_net_DatagramPacket\n"
|
||||||
|
+ "Test_java_net_DatagramSocket\n" + "Test_java_net_DatagramSocketImpl\n"
|
||||||
|
+ "Test_java_net_InetAddress\n" + "Test_java_net_NoRouteToHostException\n"
|
||||||
|
+ "Test_java_net_PlainDatagramSocketImpl\n" + "Test_java_net_PlainSocketImpl\n"
|
||||||
|
+ "Test_java_net_Socket\n" + "Test_java_net_SocketException\n" + "Test_java_net_SocketImpl\n"
|
||||||
|
+ "Test_java_net_SocketInputStream\n" + "Test_java_net_SocketOutputStream\n"
|
||||||
|
+ "Test_java_net_UnknownHostException\n" + "Test_java_util_ArrayEnumerator\n"
|
||||||
|
+ "Test_java_util_Date\n" + "Test_java_util_EventObject\n" + "Test_java_util_HashEnumerator\n"
|
||||||
|
+ "Test_java_util_Hashtable\n" + "Test_java_util_Properties\n" + "Test_java_util_ResourceBundle\n"
|
||||||
|
+ "Test_java_util_tm\n" + "Test_java_util_Vector\n";
|
||||||
|
private InputStream is;
|
||||||
|
|
||||||
|
public FileInputStreamTest() throws IOException {
|
||||||
|
File file = File.createTempFile("tmp", "tmp");
|
||||||
|
OutputStream fos = new FileOutputStream(file);
|
||||||
|
fos.write(fileString.getBytes());
|
||||||
|
fos.close();
|
||||||
|
fileName = file.getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_io_File() throws IOException {
|
||||||
|
File f = new File(fileName);
|
||||||
|
is = new FileInputStream(f);
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_lang_String() throws IOException {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_lang_String_I() throws IOException {
|
||||||
|
try {
|
||||||
|
is = new FileInputStream("");
|
||||||
|
fail("should throw FileNotFoundException.");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
if (is != null) {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(new File(""));
|
||||||
|
fail("should throw FileNotFoundException.");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
if (is != null) {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void available() throws IOException {
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
assertTrue("Returned incorrect number of available bytes", is.available() == fileString.length());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void close() throws IOException {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
try {
|
||||||
|
is.read();
|
||||||
|
fail("Able to read from closed stream");
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read() throws IOException {
|
||||||
|
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName));
|
||||||
|
int c = isr.read();
|
||||||
|
isr.close();
|
||||||
|
assertTrue("read returned incorrect char", c == fileString.charAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read$B() throws IOException {
|
||||||
|
byte[] buf1 = new byte[100];
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.skip(3000);
|
||||||
|
is.read(buf1);
|
||||||
|
is.close();
|
||||||
|
assertEquals("Failed to read correct data", new String(buf1, 0, buf1.length), fileString.substring(3000, 3100));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read$BII() throws IOException {
|
||||||
|
byte[] buf1 = new byte[100];
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.skip(3000);
|
||||||
|
is.read(buf1, 0, buf1.length);
|
||||||
|
is.close();
|
||||||
|
assertTrue("Failed to read correct data",
|
||||||
|
new String(buf1, 0, buf1.length).equals(fileString.substring(3000, 3100)));
|
||||||
|
|
||||||
|
// Regression test for HARMONY-285
|
||||||
|
File file = new File("FileInputStream.tmp");
|
||||||
|
file.createNewFile();
|
||||||
|
file.deleteOnExit();
|
||||||
|
FileInputStream in = new FileInputStream(file);
|
||||||
|
try {
|
||||||
|
in.read(null, 0, 0);
|
||||||
|
fail("Should throw NullPointerException");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
in.close();
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read_$BII_IOException() throws IOException {
|
||||||
|
byte[] buf = new byte[1000];
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, -1, 0);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, 0, -1);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, -1, -1);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, 0, 1001);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, 1001, 0);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, 500, 501);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.close();
|
||||||
|
is.read(buf, 0, 100);
|
||||||
|
fail("should throw IOException");
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.close();
|
||||||
|
is.read(buf, 0, 0);
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read_$BII_NullPointerException() throws IOException {
|
||||||
|
byte[] buf = null;
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.read(buf, -1, 0);
|
||||||
|
fail("should throw NullPointerException");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void read_$BII_IndexOutOfBoundsException() throws IOException {
|
||||||
|
byte[] buf = new byte[1000];
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.close();
|
||||||
|
is.read(buf, -1, -1);
|
||||||
|
fail("should throw IndexOutOfBoundsException");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void skipJ() throws IOException {
|
||||||
|
byte[] buf1 = new byte[10];
|
||||||
|
is = new FileInputStream(fileName);
|
||||||
|
is.skip(1000);
|
||||||
|
is.read(buf1, 0, buf1.length);
|
||||||
|
is.close();
|
||||||
|
assertTrue("Failed to skip to correct position",
|
||||||
|
new String(buf1, 0, buf1.length).equals(fileString.substring(1000, 1010)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void regressionNNN() throws IOException {
|
||||||
|
// Regression for HARMONY-434
|
||||||
|
FileInputStream fis = new FileInputStream(fileName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
fis.read(new byte[1], -1, 1);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if off <0");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fis.read(new byte[1], 0, -1);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if len <0");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fis.read(new byte[1], 0, 5);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fis.read(new byte[10], Integer.MAX_VALUE, 5);
|
||||||
|
fail("IndexOutOfBoundsException expected");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fis.read(new byte[10], 5, Integer.MAX_VALUE);
|
||||||
|
fail("IndexOutOfBoundsException expected");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void skipNegativeArgumentJ() throws IOException {
|
||||||
|
FileInputStream fis = new FileInputStream(fileName);
|
||||||
|
try {
|
||||||
|
fis.skip(-5);
|
||||||
|
fail("IOException must be thrown if number of bytes to skip <0");
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected IOException
|
||||||
|
} finally {
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
new File(fileName).delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,293 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2017 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.teavm.classlib.java.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.teavm.junit.TeaVMTestRunner;
|
||||||
|
|
||||||
|
@RunWith(TeaVMTestRunner.class)
|
||||||
|
public class FileOutputStreamTest {
|
||||||
|
private String fileName;
|
||||||
|
private String fileString =
|
||||||
|
"Test_All_Tests\n" + "Test_java_io_BufferedInputStream\n"
|
||||||
|
+ "Test_java_io_BufferedOutputStream\n"
|
||||||
|
+ "Test_java_io_ByteArrayInputStream\n"
|
||||||
|
+ "Test_java_io_ByteArrayOutputStream\n"
|
||||||
|
+ "Test_java_io_DataInputStream\n"
|
||||||
|
+ "Test_java_io_File\n"
|
||||||
|
+ "Test_java_io_FileDescriptor\n"
|
||||||
|
+ "Test_java_io_FileInputStream\n"
|
||||||
|
+ "Test_java_io_FileNotFoundException\n"
|
||||||
|
+ "Test_FileOutputStream\n"
|
||||||
|
+ "Test_java_io_FilterInputStream\n"
|
||||||
|
+ "Test_java_io_FilterOutputStream\n"
|
||||||
|
+ "Test_java_io_InputStream\n"
|
||||||
|
+ "Test_java_io_IOException\n"
|
||||||
|
+ "Test_java_io_OutputStream\n"
|
||||||
|
+ "Test_java_io_PrintStream\n"
|
||||||
|
+ "Test_java_io_RandomAccessFile\n"
|
||||||
|
+ "Test_java_io_SyncFailedException\n"
|
||||||
|
+ "Test_java_lang_AbstractMethodError\n"
|
||||||
|
+ "Test_java_lang_ArithmeticException\n"
|
||||||
|
+ "Test_java_lang_ArrayIndexOutOfBoundsException\n"
|
||||||
|
+ "Test_java_lang_ArrayStoreException\n"
|
||||||
|
+ "Test_java_lang_Boolean\n"
|
||||||
|
+ "Test_java_lang_Byte\n"
|
||||||
|
+ "Test_java_lang_Character\n"
|
||||||
|
+ "Test_java_lang_Class\n"
|
||||||
|
+ "Test_java_lang_ClassCastException\n"
|
||||||
|
+ "Test_java_lang_ClassCircularityError\n"
|
||||||
|
+ "Test_java_lang_ClassFormatError\n"
|
||||||
|
+ "Test_java_lang_ClassLoader\n"
|
||||||
|
+ "Test_java_lang_ClassNotFoundException\n"
|
||||||
|
+ "Test_java_lang_CloneNotSupportedException\n"
|
||||||
|
+ "Test_java_lang_Double\n"
|
||||||
|
+ "Test_java_lang_Error\n"
|
||||||
|
+ "Test_java_lang_Exception\n"
|
||||||
|
+ "Test_java_lang_ExceptionInInitializerError\n"
|
||||||
|
+ "Test_java_lang_Float\n"
|
||||||
|
+ "Test_java_lang_IllegalAccessError\n" + "Test_java_lang_IllegalAccessException\n"
|
||||||
|
+ "Test_java_lang_IllegalArgumentException\n" + "Test_java_lang_IllegalMonitorStateException\n"
|
||||||
|
+ "Test_java_lang_IllegalThreadStateException\n" + "Test_java_lang_IncompatibleClassChangeError\n"
|
||||||
|
+ "Test_java_lang_IndexOutOfBoundsException\n" + "Test_java_lang_InstantiationError\n"
|
||||||
|
+ "Test_java_lang_InstantiationException\n" + "Test_java_lang_Integer\n"
|
||||||
|
+ "Test_java_lang_InternalError\n" + "Test_java_lang_InterruptedException\n"
|
||||||
|
+ "Test_java_lang_LinkageError\n" + "Test_java_lang_Long\n" + "Test_java_lang_Math\n"
|
||||||
|
+ "Test_java_lang_NegativeArraySizeException\n" + "Test_java_lang_NoClassDefFoundError\n"
|
||||||
|
+ "Test_java_lang_NoSuchFieldError\n" + "Test_java_lang_NoSuchMethodError\n"
|
||||||
|
+ "Test_java_lang_NullPointerException\n" + "Test_java_lang_Number\n"
|
||||||
|
+ "Test_java_lang_NumberFormatException\n" + "Test_java_lang_Object\n"
|
||||||
|
+ "Test_java_lang_OutOfMemoryError\n" + "Test_java_lang_RuntimeException\n"
|
||||||
|
+ "Test_java_lang_SecurityManager\n" + "Test_java_lang_Short\n"
|
||||||
|
+ "Test_java_lang_StackOverflowError\n" + "Test_java_lang_String\n"
|
||||||
|
+ "Test_java_lang_StringBuffer\n" + "Test_java_lang_StringIndexOutOfBoundsException\n"
|
||||||
|
+ "Test_java_lang_System\n" + "Test_java_lang_Thread\n" + "Test_java_lang_ThreadDeath\n"
|
||||||
|
+ "Test_java_lang_ThreadGroup\n" + "Test_java_lang_Throwable\n" + "Test_java_lang_UnknownError\n"
|
||||||
|
+ "Test_java_lang_UnsatisfiedLinkError\n" + "Test_java_lang_VerifyError\n"
|
||||||
|
+ "Test_java_lang_VirtualMachineError\n" + "Test_java_lang_vm_Image\n"
|
||||||
|
+ "Test_java_lang_vm_MemorySegment\n" + "Test_java_lang_vm_ROMStoreException\n"
|
||||||
|
+ "Test_java_lang_vm_VM\n" + "Test_java_lang_Void\n" + "Test_java_net_BindException\n"
|
||||||
|
+ "Test_java_net_ConnectException\n" + "Test_java_net_DatagramPacket\n"
|
||||||
|
+ "Test_java_net_DatagramSocket\n" + "Test_java_net_DatagramSocketImpl\n"
|
||||||
|
+ "Test_java_net_InetAddress\n" + "Test_java_net_NoRouteToHostException\n"
|
||||||
|
+ "Test_java_net_PlainDatagramSocketImpl\n" + "Test_java_net_PlainSocketImpl\n"
|
||||||
|
+ "Test_java_net_Socket\n" + "Test_java_net_SocketException\n" + "Test_java_net_SocketImpl\n"
|
||||||
|
+ "Test_java_net_SocketInputStream\n" + "Test_java_net_SocketOutputStream\n"
|
||||||
|
+ "Test_java_net_UnknownHostException\n" + "Test_java_util_ArrayEnumerator\n"
|
||||||
|
+ "Test_java_util_Date\n" + "Test_java_util_EventObject\n" + "Test_java_util_HashEnumerator\n"
|
||||||
|
+ "Test_java_util_Hashtable\n" + "Test_java_util_Properties\n" + "Test_java_util_ResourceBundle\n"
|
||||||
|
+ "Test_java_util_tm\n" + "Test_java_util_Vector\n";
|
||||||
|
private FileOutputStream fos;
|
||||||
|
private FileInputStream fis;
|
||||||
|
private File f;
|
||||||
|
private byte[] bytes;
|
||||||
|
|
||||||
|
public FileOutputStreamTest() {
|
||||||
|
bytes = new byte[10];
|
||||||
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
|
bytes[i] = (byte) i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_io_File() throws IOException {
|
||||||
|
fileName = System.getProperty("user.home");
|
||||||
|
f = new File(fileName, "fos.tst");
|
||||||
|
fos = new FileOutputStream(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_ConstructorLjava_lang_String() throws IOException {
|
||||||
|
fileName = System.getProperty("user.home");
|
||||||
|
f = new File(fileName, "fos.tst");
|
||||||
|
fileName = f.getAbsolutePath();
|
||||||
|
fos = new FileOutputStream(fileName);
|
||||||
|
|
||||||
|
// Regression test for HARMONY-4012
|
||||||
|
new FileOutputStream("nul");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_lang_StringZ() throws IOException {
|
||||||
|
f = new File(System.getProperty("user.home"), "fos.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath(), false);
|
||||||
|
fos.write("HI".getBytes(), 0, 2);
|
||||||
|
fos.close();
|
||||||
|
fos = new FileOutputStream(f.getPath(), true);
|
||||||
|
fos.write(fileString.getBytes());
|
||||||
|
fos.close();
|
||||||
|
byte[] buf = new byte[fileString.length() + 2];
|
||||||
|
fis = new FileInputStream(f.getPath());
|
||||||
|
fis.read(buf, 0, buf.length);
|
||||||
|
assertTrue("Failed to create appending stream", new String(buf, 0, buf.length).equals("HI" + fileString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructorLjava_lang_String_I() throws IOException {
|
||||||
|
try {
|
||||||
|
fos = new FileOutputStream("");
|
||||||
|
fail("should throw FileNotFoundException.");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
if (fos != null) {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
fos = new FileOutputStream(new File(""));
|
||||||
|
fail("should throw FileNotFoundException.");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
if (fos != null) {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void close() throws IOException {
|
||||||
|
f = new File(System.getProperty("user.home"), "output.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath());
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(fileString.getBytes());
|
||||||
|
fail("Close test failed - wrote to closed stream");
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void write$B() throws IOException {
|
||||||
|
f = new File(System.getProperty("user.home"), "output.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath());
|
||||||
|
fos.write(fileString.getBytes());
|
||||||
|
fis = new FileInputStream(f.getPath());
|
||||||
|
byte[] rbytes = new byte[4000];
|
||||||
|
fis.read(rbytes, 0, fileString.length());
|
||||||
|
assertTrue("Incorrect string returned", new String(rbytes, 0, fileString.length()).equals(fileString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void write$BII() throws IOException {
|
||||||
|
f = new File(System.getProperty("user.home"), "output.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath());
|
||||||
|
fos.write(fileString.getBytes(), 0, fileString.length());
|
||||||
|
fis = new FileInputStream(f.getPath());
|
||||||
|
byte[] rbytes = new byte[4000];
|
||||||
|
fis.read(rbytes, 0, fileString.length());
|
||||||
|
assertTrue("Incorrect bytes written", new String(rbytes, 0, fileString.length()).equals(fileString));
|
||||||
|
|
||||||
|
// Regression test for HARMONY-285
|
||||||
|
File file = new File("FileOutputStream.tmp");
|
||||||
|
file.deleteOnExit();
|
||||||
|
FileOutputStream out = new FileOutputStream(file);
|
||||||
|
try {
|
||||||
|
out.write(null, 0, 0);
|
||||||
|
fail("Should throw NullPointerException");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Expected
|
||||||
|
} finally {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void writeI() throws IOException {
|
||||||
|
f = new File(System.getProperty("user.home"), "output.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath());
|
||||||
|
fos.write('t');
|
||||||
|
fis = new FileInputStream(f.getPath());
|
||||||
|
assertEquals("Incorrect char written", 't', fis.read());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void write$BII2() throws IOException {
|
||||||
|
// Regression for HARMONY-437
|
||||||
|
f = new File(System.getProperty("user.home"), "output.tst");
|
||||||
|
fos = new FileOutputStream(f.getPath());
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(null, 1, 1);
|
||||||
|
fail("NullPointerException must be thrown");
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(new byte[1], -1, 1);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if off <0");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(new byte[1], 0, -1);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if len <0");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(new byte[1], 0, 5);
|
||||||
|
fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(new byte[10], Integer.MAX_VALUE, 5);
|
||||||
|
fail("IndexOutOfBoundsException expected");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fos.write(new byte[10], 5, Integer.MAX_VALUE);
|
||||||
|
fail("IndexOutOfBoundsException expected");
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
if (f != null) {
|
||||||
|
f.delete();
|
||||||
|
}
|
||||||
|
if (fis != null) {
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
if (fos != null) {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -181,7 +181,7 @@ public class FileTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
protected void tearDown() {
|
public void tearDown() {
|
||||||
if (tempDirectory != null) {
|
if (tempDirectory != null) {
|
||||||
deleteTempFolder(tempDirectory);
|
deleteTempFolder(tempDirectory);
|
||||||
tempDirectory = null;
|
tempDirectory = null;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user