1 /*
   2  * Copyright (c) 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 /**
  25  * @test
  26  * @bug 8134111
  27  * @summary test that elements without namespace is ignored by unmarshaller
  28  *          when elementFormDefault is set to QUALIFIED.
  29  * @compile testTypes/package-info.java testTypes/Root.java
  30  *          testTypes/WhenType.java testTypes/ObjectFactory.java
  31  * @modules java.xml.bind
  32  * @run testng/othervm UnmarshalTest
  33  */
  34 
  35 import java.io.StringReader;
  36 import javax.xml.bind.JAXBContext;
  37 import javax.xml.bind.Unmarshaller;
  38 import org.testng.annotations.Test;
  39 import static org.testng.Assert.assertNull;
  40 import org.xml.sax.InputSource;
  41 import testTypes.Root;
  42 
  43 public class UnmarshalTest {
  44 
  45     @Test
  46     public void unmarshalUnexpectedNsTest() throws Exception {
  47         JAXBContext context;
  48         Unmarshaller unm;
  49         // Create JAXB context from testTypes package
  50         context = JAXBContext.newInstance("testTypes");
  51         // Create unmarshaller from JAXB context
  52         unm = context.createUnmarshaller();
  53         // Unmarshall xml document with unqualified dtime element
  54         Root r = (Root) unm.unmarshal(new InputSource(new StringReader(DOC)));
  55         // Print dtime value and check if it is null
  56         System.out.println("dtime is:"+r.getWhen().getDtime());
  57         assertNull(r.getWhen().getDtime());
  58     }
  59 
  60     //Xml document to unmarshall with unqualified dtime element
  61     private final String DOC =
  62             "<tns:root xmlns:tns=\"http://www.example.org/testNamespace/\">" +
  63             "<tns:when>" +
  64             "<dtime>2015-06-24T13:16:14.933-04:00</dtime>" +
  65             "</tns:when>" +
  66             "</tns:root>";
  67 }