1/*
2 * Copyright (c) 2006, 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 6372500
27 * @summary Test that KeyInfo.marshal works correctly
28 * @modules java.xml.crypto/org.jcp.xml.dsig.internal.dom
29 * @compile -XDignore.symbol.file Marshal.java
30 * @run main/othervm/java.security.policy==test.policy Marshal
31 * @author Sean Mullan
32 */
33
34import java.util.Collections;
35import javax.xml.parsers.DocumentBuilderFactory;
36import javax.xml.crypto.dom.DOMStructure;
37import javax.xml.crypto.dsig.keyinfo.KeyInfo;
38import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
39import org.w3c.dom.Document;
40import org.w3c.dom.Element;
41import org.jcp.xml.dsig.internal.dom.DOMUtils;
42
43public class Marshal {
44
45    public static void main(String[] args) throws Exception {
46        KeyInfoFactory fac = KeyInfoFactory.getInstance();
47        KeyInfo ki = fac.newKeyInfo
48            (Collections.singletonList(fac.newKeyName("foo")), "keyid");
49        try {
50            ki.marshal(null, null);
51            throw new Exception("Should raise a NullPointerException");
52        } catch (NullPointerException npe) {}
53
54        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
55        dbf.setNamespaceAware(true);
56        Document doc = dbf.newDocumentBuilder().newDocument();
57        Element elem = doc.createElementNS("http://acme.org", "parent");
58        doc.appendChild(elem);
59        DOMStructure parent = new DOMStructure(elem);
60        ki.marshal(parent, null);
61
62        Element kiElem = DOMUtils.getFirstChildElement(elem);
63        if (!kiElem.getLocalName().equals("KeyInfo")) {
64            throw new Exception
65                ("Should be KeyInfo element: " + kiElem.getLocalName());
66        }
67        Element knElem = DOMUtils.getFirstChildElement(kiElem);
68        if (!knElem.getLocalName().equals("KeyName")) {
69            throw new Exception
70                ("Should be KeyName element: " + knElem.getLocalName());
71        }
72    }
73}
74