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