1 /*
   2  * Copyright (c) 2018, 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 import java.io.IOException;
  25 import java.nio.charset.StandardCharsets;
  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import javax.xml.parsers.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;
  30 import org.xml.sax.Attributes;
  31 import org.xml.sax.SAXException;
  32 import org.xml.sax.helpers.DefaultHandler;
  33 
  34 /*
  35  * @test
  36  * @bug 8213734
  37  * @run main/othervm SAXParserTest
  38  * @summary Verifies that files opened by the SAXParser is closed when Exception
  39  */
  40 public class SAXParserTest {
  41 
  42     public void testCloseReaders() throws Exception {
  43         if (!System.getProperty("os.name").contains("Windows")) {
  44             System.out.println("This test only needs to be run on Windows.");
  45             return;
  46         }
  47         Path testFile = createTestFile(null, "Test");
  48         System.out.println("Test file: " + testFile.toString());
  49         SAXParserFactory factory = SAXParserFactory.newInstance();
  50         SAXParser parser = factory.newSAXParser();
  51         try {
  52             parser.parse(testFile.toFile(), new DefaultHandler() {
  53                 @Override
  54                 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  55                     throw new SAXException("Stop the parser.");
  56                 }
  57             });
  58         } catch (SAXException e) {
  59             // Do nothing
  60         }
  61 
  62         // deletion failes on Windows when the file is not closed
  63         Files.deleteIfExists(testFile);
  64     }
  65 
  66     private static Path createTestFile(Path dir, String name) throws IOException {
  67         Path path = Files.createTempFile(name, ".xml");
  68             byte[] bytes = "<?xml version=\"1.0\"?><test a1=\"x\" a2=\"y\"/>"
  69         .getBytes(StandardCharsets.UTF_8);
  70 
  71         Files.write(path, bytes);
  72         return path;
  73     }
  74 
  75     public static void main(String[] args) throws Exception {
  76         SAXParserTest test = new SAXParserTest();
  77         test.testCloseReaders();
  78     }
  79 }