blob: a0c53e7f2c0ec65ef67ce6eeb5e4392b3135aa4a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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
33import java.util.Collections;
34import javax.xml.parsers.DocumentBuilderFactory;
35import javax.xml.crypto.dom.DOMStructure;
36import javax.xml.crypto.dsig.keyinfo.KeyInfo;
37import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;
38import org.w3c.dom.Document;
39import org.w3c.dom.Element;
40import org.jcp.xml.dsig.internal.dom.DOMUtils;
41
42public class Marshal {
43
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}