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.luni.tests.java.io;
19
20import java.io.BufferedOutputStream;
21import java.io.File;
22import java.io.FileDescriptor;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.RandomAccessFile;
27
28import junit.framework.TestCase;
29
30public class FileDescriptorTest extends TestCase {
31
32    private static String platformId = "JDK"
33            + System.getProperty("java.vm.version").replace('.', '-');
34
35    FileOutputStream fos;
36
37    BufferedOutputStream os;
38
39    FileInputStream fis;
40
41    File f;
42
43    /**
44     * @tests java.io.FileDescriptor#FileDescriptor()
45     */
46    public void test_Constructor() {
47        FileDescriptor fd = new FileDescriptor();
48        assertTrue("Failed to create FileDescriptor",
49                fd instanceof FileDescriptor);
50    }
51
52    /**
53     * @tests java.io.FileDescriptor#sync()
54     */
55    public void test_sync() throws IOException {
56        f = new File(System.getProperty("user.dir"), "fd" + platformId + ".tst");
57        f.delete();
58        fos = new FileOutputStream(f.getPath());
59        fos.write("Test String".getBytes());
60        fis = new FileInputStream(f.getPath());
61        FileDescriptor fd = fos.getFD();
62        fd.sync();
63        int length = "Test String".length();
64        assertEquals("Bytes were not written after sync", length, fis
65                .available());
66
67        // Regression test for Harmony-1494
68        fd = fis.getFD();
69        fd.sync();
70        assertEquals("Bytes were not written after sync", length, fis
71                .available());
72
73        RandomAccessFile raf = new RandomAccessFile(f, "r");
74        fd = raf.getFD();
75        fd.sync();
76        raf.close();
77    }
78
79    /**
80     * @tests java.io.FileDescriptor#valid()
81     */
82    public void test_valid() throws IOException {
83        f = new File(System.getProperty("user.dir"), "fd.tst");
84        f.delete();
85        os = new BufferedOutputStream(fos = new FileOutputStream(f.getPath()),
86                4096);
87        FileDescriptor fd = fos.getFD();
88        assertTrue("Valid fd returned false", fd.valid());
89        os.close();
90        assertTrue("Invalid fd returned true", !fd.valid());
91    }
92
93    /**
94     * Tears down the fixture, for example, close a network connection. This
95     * method is called after a test is executed.
96     */
97    protected void tearDown() {
98        try {
99            os.close();
100        } catch (Exception e) {
101        }
102        try {
103            fis.close();
104        } catch (Exception e) {
105        }
106        try {
107            fos.close();
108        } catch (Exception e) {
109        }
110        try {
111            f.delete();
112        } catch (Exception e) {
113        }
114    }
115}
116