jdk/test/javax/xml/bind/marshal/8134111/UnmarshalTest.java
author aefimov
Fri, 01 Apr 2016 12:34:02 +0300
changeset 36754 6e5616c1e5a7
child 40261 86a49ba76f52
permissions -rw-r--r--
8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36754
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     1
/*
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     4
 *
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     8
 *
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    13
 * accompanied this code).
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    14
 *
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    18
 *
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    21
 * questions.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    22
 */
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    23
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    24
/**
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    25
 * @test
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    26
 * @bug 8134111
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    27
 * @summary test that elements without namespace is ignored by unmarshaller
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    28
 *          when elementFormDefault is set to QUALIFIED.
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    29
 * @compile testTypes/package-info.java testTypes/Root.java
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    30
 *          testTypes/WhenType.java testTypes/ObjectFactory.java
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    31
 * @modules java.xml.bind
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    32
 * @run testng/othervm UnmarshalTest
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    33
 */
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    34
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    35
import java.io.StringReader;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    36
import javax.xml.bind.JAXBContext;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    37
import javax.xml.bind.Unmarshaller;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    38
import org.testng.annotations.Test;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    39
import static org.testng.Assert.assertNull;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    40
import org.xml.sax.InputSource;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    41
import testTypes.Root;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    42
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    43
public class UnmarshalTest {
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    44
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    45
    @Test
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    46
    public void unmarshalUnexpectedNsTest() throws Exception {
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    47
        JAXBContext context;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    48
        Unmarshaller unm;
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    49
        // Create JAXB context from testTypes package
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    50
        context = JAXBContext.newInstance("testTypes");
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    51
        // Create unmarshaller from JAXB context
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    52
        unm = context.createUnmarshaller();
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    53
        // Unmarshall xml document with unqualified dtime element
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    54
        Root r = (Root) unm.unmarshal(new InputSource(new StringReader(DOC)));
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    55
        // Print dtime value and check if it is null
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    56
        System.out.println("dtime is:"+r.getWhen().getDtime());
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    57
        assertNull(r.getWhen().getDtime());
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    58
    }
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    59
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    60
    //Xml document to unmarshall with unqualified dtime element
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    61
    private final String DOC =
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    62
            "<tns:root xmlns:tns=\"http://www.example.org/testNamespace/\">" +
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    63
            "<tns:when>" +
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    64
            "<dtime>2015-06-24T13:16:14.933-04:00</dtime>" +
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    65
            "</tns:when>" +
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    66
            "</tns:root>";
6e5616c1e5a7 8134111: Unmarshaller unmarshalls XML element which doesn't have the expected namespace
aefimov
parents:
diff changeset
    67
}