1 /*
   2  * Copyright (c) 2014, 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 javax.xml.transform;
  25 
  26 import java.io.StringWriter;
  27 import java.util.HashMap;
  28 import java.util.Iterator;
  29 import java.util.Map;
  30 
  31 import javax.xml.transform.Transformer;
  32 import javax.xml.transform.TransformerFactory;
  33 import javax.xml.transform.stream.StreamResult;
  34 import javax.xml.transform.stream.StreamSource;
  35 
  36 import org.testng.Assert;
  37 import org.testng.annotations.Test;
  38 
  39 /*
  40  * @bug 6505031
  41  * @summary Test transformer parses keys and their values coming from different xml documents. 
  42  */
  43 public class Bug6505031 {
  44 
  45     private String getResource(String s) {
  46         return getClass().getResource(s).toString();
  47 
  48     }
  49 
  50     @Test
  51     public void test() {
  52         Map params = new HashMap();
  53 
  54         params.put("config", getResource("config.xml"));
  55         params.put("mapsFile", getResource("maps.xml"));
  56         generate(getResource("template.xml"), getResource("transform.xsl"), params);
  57     }
  58 
  59     private void generate(String in, String xsl, Map params) {
  60         try {
  61             Transformer transformer = getTransformer(xsl);
  62 
  63             for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
  64                 Map.Entry entry = (Map.Entry) i.next();
  65 
  66                 transformer.setParameter((String) entry.getKey(), entry.getValue());
  67             }
  68             transform(in, transformer);
  69         } catch (Exception e) {
  70             Assert.fail(e.getMessage());
  71         }
  72     }
  73 
  74     private Transformer getTransformer(String transform) throws Exception {
  75         TransformerFactory tfactory = TransformerFactory.newInstance();
  76 
  77         try {
  78             // tfactory.setAttribute("generate-translet", Boolean.TRUE);
  79         } catch (Exception e) {
  80             // Ignore
  81         }
  82 
  83         Transformer transformer = tfactory.newTransformer(new StreamSource(transform));
  84         return (transformer);
  85     }
  86 
  87     private void transform(String in, Transformer transformer) throws Exception {
  88         StringWriter sw = new StringWriter();
  89         transformer.transform(new StreamSource(in), new StreamResult(sw));
  90         String s = sw.toString();
  91         Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value"));
  92     }
  93 
  94 }