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 transform; 25 26 import java.io.File; 27 28 import javax.xml.transform.Source; 29 import javax.xml.transform.Transformer; 30 import javax.xml.transform.TransformerFactory; 31 import javax.xml.transform.dom.DOMResult; 32 import javax.xml.transform.sax.SAXResult; 33 import javax.xml.transform.stax.StAXResult; 34 import javax.xml.transform.stream.StreamResult; 35 36 import org.testng.Assert; 37 import org.testng.annotations.AfterMethod; 38 import org.testng.annotations.BeforeMethod; 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 42 import transform.util.DOMUtil; 43 import transform.util.SAXUtil; 44 import transform.util.StAXUtil; 45 import transform.util.StreamUtil; 46 47 /* 48 * @bug 4892774 49 * @summary Test identity transformer with all possible types of Source and Result combinations for doucment version and encoding information. 50 */ 51 52 @Listeners({jaxp.library.FilePolicy.class}) 53 public class Bug4892774 { 54 55 private final String XML_FILE = "catalog.xml"; 56 private final String XML10_FILE = "catalog_10.xml"; // 1.0 version document 57 private final String TEMP_FILE = "tmp.xml"; 58 private final String EXPECTED_VERSION = "1.1"; 59 static private Transformer idTransform = null; 60 61 private static DOMUtil domUtil = null; 62 private static StreamUtil streamUtil = null; 63 private static SAXUtil saxUtil = null; 64 private static StAXUtil staxUtil = null; 65 66 @BeforeMethod 67 protected void setUp() { 68 File tmpFile = new File(TEMP_FILE); 69 if (tmpFile.exists()) 70 tmpFile.delete(); 71 try { 72 73 if (idTransform == null) 74 idTransform = getIdTransformer(); 75 else 76 idTransform.reset(); 77 78 initializeUtils(); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 Assert.fail("Exception occured during setUp(): " + e.getMessage()); 82 } 83 } 84 85 @AfterMethod 86 protected void tearDown() { 87 File tmpFile = new File(TEMP_FILE); 88 if (tmpFile.exists()) 89 tmpFile.delete(); 90 } 91 92 private void initializeUtils() throws Exception { 93 if (domUtil == null) 94 domUtil = (DOMUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.DOM); 95 if (saxUtil == null) 96 saxUtil = (SAXUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.SAX); 97 if (streamUtil == null) 98 streamUtil = (StreamUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.STREAM); 99 if (staxUtil == null) 100 staxUtil = (StAXUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.StAX); 101 } 102 103 @Test 104 public void testDOM2DOM() { 105 try { 106 Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 107 DOMResult domResult = (DOMResult) domUtil.prepareResult(); 108 idTransform.transform(input, domResult); 109 domUtil.checkResult(domResult, EXPECTED_VERSION); 110 } catch (Exception e) { 111 e.printStackTrace(); 112 Assert.fail("Exception occured: " + e.getMessage()); 113 } 114 } 115 116 private Transformer getIdTransformer() throws Exception { 117 return TransformerFactory.newInstance().newTransformer(); 118 } 119 120 @Test 121 public void testDOM2Stream() { 122 try { 123 124 Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 125 StreamResult strResult = (StreamResult) streamUtil.prepareResult(); 126 idTransform.transform(input, strResult); 127 streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8"); 128 129 } catch (Exception e) { 130 e.printStackTrace(); 131 Assert.fail("Exception occured: " + e.getMessage()); 132 } 133 } 134 135 @Test 136 public void testDOM2SAX() { 137 try { 138 Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 139 SAXResult saxResult = (SAXResult) saxUtil.prepareResult(); 140 idTransform.transform(input, saxResult); 141 saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8"); 142 143 } catch (Exception e) { 144 e.printStackTrace(); 145 Assert.fail("Exception occured: " + e.getMessage()); 146 } 147 } 148 149 @Test 150 public void testDOM2StAX() { 151 try { 152 Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 153 StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); 154 idTransform.transform(input, staxResult); 155 staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); 156 157 } catch (Exception e) { 158 e.printStackTrace(); 159 Assert.fail("Exception occured: " + e.getMessage()); 160 } 161 } 162 163 @Test 164 public void testDOM2StAXStream() { 165 try { 166 Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 167 StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); 168 idTransform.transform(input, staxResult); 169 staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); 170 171 } catch (Exception e) { 172 e.printStackTrace(); 173 Assert.fail("Exception occured: " + e.getMessage()); 174 } 175 } 176 177 @Test 178 public void testSAX2DOM() { 179 try { 180 Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 181 DOMResult domResult = (DOMResult) domUtil.prepareResult(); 182 idTransform.transform(input, domResult); 183 domUtil.checkResult(domResult, EXPECTED_VERSION); 184 185 } catch (Exception e) { 186 e.printStackTrace(); 187 Assert.fail("Exception occured: " + e.getMessage()); 188 } 189 } 190 191 @Test 192 public void testSAX2SAX() { 193 try { 194 Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 195 SAXResult saxResult = (SAXResult) saxUtil.prepareResult(); 196 idTransform.transform(input, saxResult); 197 saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8"); 198 199 } catch (Exception e) { 200 e.printStackTrace(); 201 Assert.fail("Exception occured: " + e.getMessage()); 202 } 203 } 204 205 @Test 206 public void testSAX2Stream() { 207 try { 208 Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 209 StreamResult strResult = (StreamResult) streamUtil.prepareResult(); 210 idTransform.transform(input, strResult); 211 streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8"); 212 } catch (Exception e) { 213 e.printStackTrace(); 214 Assert.fail("Exception occured: " + e.getMessage()); 215 } 216 } 217 218 @Test 219 public void testSAX2StAX() { 220 try { 221 Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 222 StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); 223 idTransform.transform(input, staxResult); 224 staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); 225 } catch (Exception e) { 226 e.printStackTrace(); 227 Assert.fail("Exception occured: " + e.getMessage()); 228 } 229 } 230 231 @Test 232 public void testSAX2StAXStream() { 233 try { 234 Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 235 StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); 236 idTransform.transform(input, staxResult); 237 staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); 238 239 } catch (Exception e) { 240 e.printStackTrace(); 241 Assert.fail("Exception occured: " + e.getMessage()); 242 } 243 } 244 245 @Test 246 public void testStream2DOM() { 247 try { 248 Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 249 DOMResult domResult = (DOMResult) domUtil.prepareResult(); 250 idTransform.transform(input, domResult); 251 domUtil.checkResult(domResult, EXPECTED_VERSION); 252 } catch (Exception e) { 253 e.printStackTrace(); 254 Assert.fail("Exception occured: " + e.getMessage()); 255 } 256 } 257 258 @Test 259 public void testStream2Stream() { 260 try { 261 Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 262 StreamResult strResult = (StreamResult) streamUtil.prepareResult(); 263 idTransform.transform(input, strResult); 264 streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8"); 265 } catch (Exception e) { 266 e.printStackTrace(); 267 Assert.fail("Exception occured: " + e.getMessage()); 268 } 269 } 270 271 @Test 272 public void testStream2Stax() { 273 try { 274 Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 275 StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); 276 idTransform.transform(input, staxResult); 277 staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8"); 278 } catch (Exception e) { 279 e.printStackTrace(); 280 Assert.fail("Exception occured: " + e.getMessage()); 281 } 282 } 283 284 @Test 285 public void testStream2StaxStream() { 286 try { 287 Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 288 StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult(); 289 idTransform.transform(input, staxResult); 290 staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION); 291 292 } catch (Exception e) { 293 e.printStackTrace(); 294 Assert.fail("Exception occured: " + e.getMessage()); 295 } 296 } 297 298 @Test 299 public void testStream2SAX() { 300 try { 301 Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE)); 302 SAXResult saxResult = (SAXResult) saxUtil.prepareResult(); 303 idTransform.transform(input, saxResult); 304 saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8"); 305 } catch (Exception e) { 306 e.printStackTrace(); 307 Assert.fail("Exception occured: " + e.getMessage()); 308 } 309 } 310 311 @Test 312 public void testStAX2DOM() { 313 try { 314 Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE)); 315 DOMResult domResult = (DOMResult) domUtil.prepareResult(); 316 idTransform.transform(input, domResult); 317 domUtil.checkResult(domResult, "1.0"); 318 } catch (Exception e) { 319 e.printStackTrace(); 320 Assert.fail("Exception occured: " + e.getMessage()); 321 } 322 } 323 324 @Test 325 public void testStAX2Stream() { 326 try { 327 Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE)); 328 StreamResult strResult = (StreamResult) streamUtil.prepareResult(); 329 idTransform.transform(input, strResult); 330 streamUtil.checkResult(strResult, "1.0", "UTF-8"); 331 } catch (Exception e) { 332 e.printStackTrace(); 333 Assert.fail("Exception occured: " + e.getMessage()); 334 } 335 } 336 337 @Test 338 public void testStAX2StAX() { 339 try { 340 Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE)); 341 StAXResult staxResult = (StAXResult) staxUtil.prepareResult(); 342 idTransform.transform(input, staxResult); 343 staxUtil.checkResult(staxResult, "1.0", "UTF-8"); 344 } catch (Exception e) { 345 e.printStackTrace(); 346 Assert.fail("Exception occured: " + e.getMessage()); 347 } 348 } 349 350 @Test 351 public void testStAXEvent2DOM() { 352 try { 353 Source input = staxUtil.prepareSource(this.getClass().getResourceAsStream(XML10_FILE)); 354 DOMResult domResult = (DOMResult) domUtil.prepareResult(); 355 idTransform.transform(input, domResult); 356 domUtil.checkResult(domResult, "1.0"); 357 } catch (Exception e) { 358 e.printStackTrace(); 359 Assert.fail("Exception occured: " + e.getMessage()); 360 } 361 } 362 363 @Test 364 public void testStAXEvent2Stream() { 365 try { 366 Source input = staxUtil.prepareSource(this.getClass().getResourceAsStream(XML10_FILE)); 367 StreamResult strResult = (StreamResult) streamUtil.prepareResult(); 368 idTransform.transform(input, strResult); 369 streamUtil.checkResult(strResult, "1.0", "UTF-8"); 370 } catch (Exception e) { 371 e.printStackTrace(); 372 Assert.fail("Exception occured: " + e.getMessage()); 373 } 374 } 375 }