Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/parsers/8213734/SAXParserTest.java
38860 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.nio.charset.StandardCharsets;25import java.nio.file.Files;26import java.nio.file.Path;27import javax.xml.parsers.SAXParser;28import javax.xml.parsers.SAXParserFactory;29import org.xml.sax.Attributes;30import org.xml.sax.SAXException;31import org.xml.sax.helpers.DefaultHandler;3233/*34* @test35* @bug 821373436* @run main/othervm SAXParserTest37* @summary Verifies that files opened by the SAXParser is closed when Exception38*/39public class SAXParserTest {4041public void testCloseReaders() throws Exception {42if (!System.getProperty("os.name").contains("Windows")) {43System.out.println("This test only needs to be run on Windows.");44return;45}46Path testFile = createTestFile(null, "Test");47System.out.println("Test file: " + testFile.toString());48SAXParserFactory factory = SAXParserFactory.newInstance();49SAXParser parser = factory.newSAXParser();50try {51parser.parse(testFile.toFile(), new DefaultHandler() {52@Override53public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {54throw new SAXException("Stop the parser.");55}56});57} catch (SAXException e) {58// Do nothing59}6061// deletion failes on Windows when the file is not closed62Files.deleteIfExists(testFile);63}6465private static Path createTestFile(Path dir, String name) throws IOException {66Path path = Files.createTempFile(name, ".xml");67byte[] bytes = "<?xml version=\"1.0\"?><test a1=\"x\" a2=\"y\"/>"68.getBytes(StandardCharsets.UTF_8);6970Files.write(path, bytes);71return path;72}7374public static void main(String[] args) throws Exception {75SAXParserTest test = new SAXParserTest();76test.testCloseReaders();77}78}798081