1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.io; 19 20import java.io.File; 21import java.io.FileDescriptor; 22import java.io.FileInputStream; 23import java.io.FileOutputStream; 24import java.io.IOException; 25import java.io.RandomAccessFile; 26import java.nio.charset.StandardCharsets; 27import junit.framework.TestCase; 28 29public class FileDescriptorTest extends TestCase { 30 31 public void test_sync() throws IOException { 32 File f = File.createTempFile("FileDescriptorText", "txt"); 33 34 FileOutputStream fos = null; 35 FileInputStream fis = null; 36 RandomAccessFile raf = null; 37 38 try { 39 fos = new FileOutputStream(f.getAbsolutePath()); 40 fos.write("Test String".getBytes(StandardCharsets.US_ASCII)); 41 42 fis = new FileInputStream(f.getPath()); 43 FileDescriptor fd = fos.getFD(); 44 fd.sync(); 45 46 int length = "Test String".length(); 47 assertEquals(length, fis.available()); 48 49 // Regression test for Harmony-1494 50 fd = fis.getFD(); 51 fd.sync(); 52 assertEquals(length, fis.available()); 53 54 raf = new RandomAccessFile(f, "r"); 55 fd = raf.getFD(); 56 fd.sync(); 57 } finally { 58 if (fos != null) { 59 fos.close(); 60 } 61 if (fis != null) { 62 fis.close(); 63 } 64 if (raf != null) { 65 raf.close(); 66 } 67 } 68 } 69 70 /** 71 * java.io.FileDescriptor#valid() 72 */ 73 public void test_valid() throws IOException { 74 File f = File.createTempFile("FileDescriptorText", "txt"); 75 76 FileOutputStream fos = null; 77 try { 78 fos = new FileOutputStream(f.getAbsolutePath()); 79 FileDescriptor fd = fos.getFD(); 80 assertTrue(fd.valid()); 81 fos.close(); 82 assertFalse(fd.valid()); 83 } finally { 84 if (fos != null) { 85 fos.close(); 86 } 87 } 88 } 89} 90