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