Skip to main content
summaryrefslogtreecommitdiffstats
blob: 94509573cde751803f7c6dce3710e2cce8fc235c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.core.tests.dom;

import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.xml.core.internal.provisional.contenttype.ContentTypeIdForXML;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

import junit.framework.TestCase;

/**
 * 
 */
public class NodeContainerTests extends TestCase {

	private static final String CONTENT_1 = "<root><a id='a'></a></root>";
	private static final String CONTENT_2 = "<root><a id='a'><b id='b'></b></a></root>";

	/**
	 * Default Constructor
	 */
	public NodeContainerTests() {
		super("Test NodeContainer");
	}

	/**
	 * Constructor
	 * 
	 * @param name the name of this test run
	 */
	public NodeContainerTests(String name) {
		super(name);
	}

	public void testAppendValidChild() {
		IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
		model.getStructuredDocument().set(CONTENT_1);

		IDOMDocument doc = model.getDocument();

		Element a = doc.getElementById("a");
		assertNotNull("Could not find element with id 'a' in " + CONTENT_1, a);

		Element b = doc.createElement("b");
		b.setAttribute("id", "b");

		try {
			a.appendChild(b);
		} catch (DOMException e) {
			fail("Should have been able to append " + b + " as a child of " + a);
		}
	}

	public void testAppendParentAsChildOfChild() {
		IDOMModel model = (IDOMModel) StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(ContentTypeIdForXML.ContentTypeID_XML);
		model.getStructuredDocument().set(CONTENT_2);

		IDOMDocument doc = model.getDocument();

		Element a = doc.getElementById("a");
		assertNotNull("Could not find element with id 'a' in " + CONTENT_2, a);

		Element b = doc.getElementById("b");
		assertNotNull("Could not find element with id 'b' in " + CONTENT_2, b);

		boolean threwException = false;
		try {
			b.appendChild(a);
		} catch (DOMException e) {
			assertEquals("Wrong type of exception was thrown: " + e, DOMException.HIERARCHY_REQUEST_ERR, e.code);
			threwException = true;
		}

		assertTrue("A DOMException with code HIERARCHY_REQUEST_ERR should have been thrown when appending a parent to its own child", threwException);
	}

}

Back to the top