< prev index next >

test/javax/xml/jaxp/unittest/sax/Bug7057778Test.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 2015, 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 package sax;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.OutputStreamWriter;
  34 import java.io.UnsupportedEncodingException;
  35 import java.io.Writer;
  36 
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.parsers.SAXParser;
  39 import javax.xml.parsers.SAXParserFactory;
  40 
  41 import org.testng.annotations.Test;

  42 import org.testng.Assert;


  43 import org.xml.sax.Attributes;
  44 import org.xml.sax.ErrorHandler;
  45 import org.xml.sax.SAXException;
  46 import org.xml.sax.SAXParseException;
  47 import org.xml.sax.XMLReader;
  48 import org.xml.sax.ext.DefaultHandler2;
  49 
  50 /*
  51  * @bug 7057778
  52  * @summary Test the file can be deleted after SAXParser.parse(File, DefaultHandler).
  53  */

  54 public class Bug7057778Test {
  55 
  56     static final String xml = "Bug7057778.xml";
  57     static final String xml1 = "Bug7057778_1.xml";
  58 
  59     @Test
  60     public void testParse() {
  61         File src = new File(getClass().getResource(xml).getFile());
  62         File dst = new File(src.getParent() + File.separator + xml1);
  63         try {
  64             copyFile(src, dst);
  65             SAXParserFactory spf = SAXParserFactory.newInstance();
  66             SAXParser parser = spf.newSAXParser();
  67             XMLReader xmlReader = parser.getXMLReader();
  68             xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", new MyHandler1());
  69             parser.parse(dst, new MyHandler1());
  70         } catch (SAXException ex) {
  71             ex.printStackTrace();
  72         } catch (IOException ex) {
  73             // shouldn't happen
  74         } catch (ParserConfigurationException ex) {
  75             // shouldn't happen
  76         }
  77         if (dst != null) {
  78             if (dst.delete()) {
  79                 System.out.println("Delete: OK");
  80             } else {
  81                 System.out.println("Delete: NG");
  82                 Assert.fail("Error: denied to delete the file");
  83             }
  84         }
  85 
  86     }
  87 
  88     private void copyFile(File src, File dst) throws FileNotFoundException, IOException {
  89         InputStream in = new FileInputStream(src);
  90         OutputStream out = new FileOutputStream(dst);
  91         // Transfer bytes
  92         byte[] buf = new byte[1024];
  93         int len;
  94         while ((len = in.read(buf)) > 0) {
  95             out.write(buf, 0, len);
  96         }
  97         in.close();
  98         out.close();
  99     }
 100 
 101     public class MyHandler1 extends DefaultHandler2 implements ErrorHandler {

 102         private Writer out;
 103 
 104         StringBuffer textBuffer;
 105         private String indentString = "    "; // Amount to indent
 106         private int indentLevel = 0;
 107 
 108         public MyHandler1() {
 109             try {
 110                 out = new OutputStreamWriter(System.out, "UTF8");
 111             } catch (UnsupportedEncodingException ex) {
 112                 ex.printStackTrace();
 113             }
 114         }
 115 
 116         public void startDocument() throws SAXException {
 117         }
 118 
 119         public void endDocument() throws SAXException {
 120         }
 121 


   1 /*
   2  * Copyright (c) 2014, 2016, 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 package sax;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.OutputStreamWriter;
  34 import java.io.UnsupportedEncodingException;
  35 import java.io.Writer;
  36 
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.parsers.SAXParser;
  39 import javax.xml.parsers.SAXParserFactory;
  40 
  41 import jaxp.library.JAXPTestUtilities;
  42 
  43 import org.testng.Assert;
  44 import org.testng.annotations.Listeners;
  45 import org.testng.annotations.Test;
  46 import org.xml.sax.Attributes;
  47 import org.xml.sax.ErrorHandler;
  48 import org.xml.sax.SAXException;
  49 import org.xml.sax.SAXParseException;
  50 import org.xml.sax.XMLReader;
  51 import org.xml.sax.ext.DefaultHandler2;
  52 
  53 /*
  54  * @bug 7057778
  55  * @summary Test the file can be deleted after SAXParser.parse(File, DefaultHandler).
  56  */
  57 @Listeners({jaxp.library.FilePolicy.class})
  58 public class Bug7057778Test {
  59 
  60     static final String xml = "Bug7057778.xml";
  61     static final String xml1 = "Bug7057778_1.xml";
  62 
  63     @Test
  64     public void testParse() {
  65         File src = new File(getClass().getResource(xml).getFile());
  66         File dst = new File(xml1);
  67         try {
  68             copyFile(src, dst);
  69             SAXParserFactory spf = SAXParserFactory.newInstance();
  70             SAXParser parser = spf.newSAXParser();
  71             XMLReader xmlReader = parser.getXMLReader();
  72             xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", new MyHandler1());
  73             parser.parse(dst, new MyHandler1());
  74         } catch (SAXException ex) {
  75             ex.printStackTrace();
  76         } catch (IOException ex) {
  77             // shouldn't happen
  78         } catch (ParserConfigurationException ex) {
  79             // shouldn't happen
  80         }
  81         if (dst != null) {
  82             if (dst.delete()) {
  83                 System.out.println("Delete: OK");
  84             } else {
  85                 System.out.println("Delete: NG");
  86                 Assert.fail("Error: denied to delete the file");
  87             }
  88         }
  89 
  90     }
  91 
  92     private void copyFile(File src, File dst) throws FileNotFoundException, IOException {
  93         InputStream in = new FileInputStream(src);
  94         OutputStream out = new FileOutputStream(dst);
  95         // Transfer bytes
  96         byte[] buf = new byte[1024];
  97         int len;
  98         while ((len = in.read(buf)) > 0) {
  99             out.write(buf, 0, len);
 100         }
 101         in.close();
 102         out.close();
 103     }
 104 
 105     @Listeners({jaxp.library.BasePolicy.class})
 106 public class MyHandler1 extends DefaultHandler2 implements ErrorHandler {
 107         private Writer out;
 108 
 109         StringBuffer textBuffer;
 110         private String indentString = "    "; // Amount to indent
 111         private int indentLevel = 0;
 112 
 113         public MyHandler1() {
 114             try {
 115                 out = new OutputStreamWriter(System.out, "UTF8");
 116             } catch (UnsupportedEncodingException ex) {
 117                 ex.printStackTrace();
 118             }
 119         }
 120 
 121         public void startDocument() throws SAXException {
 122         }
 123 
 124         public void endDocument() throws SAXException {
 125         }
 126 


< prev index next >