1/*
2 * Copyright (c) 2007, 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/**
25 *
26 * @test
27 * @bug 6524062
28 * @summary Test to ensure that FIS.finalize() invokes the close() method as per
29 * the specification.
30 */
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.FileNotFoundException;
34import java.io.IOException;
35
36public class FinalizeShdCallClose {
37
38    static final String FILE_NAME = "empty.txt";
39
40    public static class MyStream extends FileInputStream {
41        private boolean closed = false;
42
43        public MyStream(String name) throws FileNotFoundException {
44            super(name);
45        }
46
47        public void finalize() {
48            try {
49                super.finalize();
50            } catch (IOException ioe) {
51                ioe.printStackTrace();
52            }
53        }
54
55        public void close() {
56            try {
57                super.close();
58            } catch (IOException ioe) {
59                ioe.printStackTrace();
60            }
61            closed = true;
62        }
63
64        public boolean isClosed() {
65            return closed;
66        }
67    }
68
69    /* standalone interface */
70    public static void main(String argv[]) throws Exception {
71
72        File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
73        inFile.createNewFile();
74        inFile.deleteOnExit();
75
76        String name = inFile.getPath();
77        MyStream ms = null;
78        try {
79            ms = new MyStream(name);
80        } catch (FileNotFoundException e) {
81            System.out.println("Unexpected exception " + e);
82            throw(e);
83        }
84        ms.finalize();
85        if (!ms.isClosed()) {
86            throw new Exception("MyStream.close() method is not called");
87        }
88        System.out.println("OK");
89    }
90}
91