Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d86f22cb6b76673378f02b533d624662a81fa3f4 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay 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:
 *     John Krasnay - initial API and implementation
 *     Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
 *     Florian Thienel - bug 299999 - completed implementation of validation
 *******************************************************************************/
package org.eclipse.vex.core.internal.dom;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.vex.core.internal.validator.AttributeDefinition;
import org.eclipse.vex.core.internal.validator.WTPVEXValidator;
import org.eclipse.vex.core.tests.TestResources;

public class DTDValidatorTest extends TestCase {

	private Validator validator = null;

	@Override
	protected void setUp() {
		try {
			validator = new WTPVEXValidator(TestResources.TEST_DTD);
		} catch (final Exception ex) {
			fail("Failed to load test1.dtd");
		}
	}

	public void testAttributeDefinition() throws Exception {
		final Document doc = new Document(new RootElement("section"));
		doc.setValidator(validator);
		final Element sectionElement = doc.getRootElement();
		final AttributeDefinition.Type adType = validator.getAttributeDefinitions(sectionElement).get(0).getType();
		final AttributeDefinition.Type adType2 = validator.getAttributeDefinitions(sectionElement).get(0).getType();

		assertSame(adType, adType2);
	}

	public void testEnumAttribute() throws Exception {
		final Document doc = new Document(new RootElement("section"));
		doc.setValidator(validator);
		final Element sectionElement = doc.getRootElement();
		final AttributeDefinition attributeDefinition = validator.getAttributeDefinitions(sectionElement).get(0);
		assertEquals("enatt", attributeDefinition.getName());

		final String[] enumValues = attributeDefinition.getValues();
		assertEquals(3, enumValues.length);
	}

	// public void testEmptyDTD() throws Exception {
	// VEXDocument doc;
	// Set expected;
	//
	// doc = new Document(new RootElement("empty"));
	// doc.setValidator(validator);
	// assertEquals(Collections.EMPTY_SET, getValidItemsAt(doc, 1));
	// }

	// public void testAnyDTD() throws Exception {
	// VEXDocument doc;
	// Set expected;
	//
	// doc = new Document(new RootElement("any"));
	// doc.setValidator(validator);
	// Set anySet = new HashSet();
	// anySet.add(Validator.PCDATA);
	// anySet.add("any");
	// anySet.add("empty");
	// anySet.add("section");
	// anySet.add("title");
	// anySet.add("para");
	// anySet.add("emphasis");
	// assertEquals(anySet, getValidItemsAt(doc, 1));
	//		
	// }

	public void testSectionElement() {
		// <section> <title> a b </title> <para> </para> </section>
		// 1 2 3 4 5 6 7
		final Document doc = new Document(new RootElement("section"));
		doc.setValidator(validator);
		doc.insertElement(1, new Element("title"));
		doc.insertText(2, "ab");
		doc.insertElement(5, new Element("para"));

		assertValidItemsAt(doc, 1, "title", "para");
		assertValidItemsAt(doc, 2);
		assertValidItemsAt(doc, 3);
		assertValidItemsAt(doc, 4);
		assertValidItemsAt(doc, 5, "title", "para");
		assertValidItemsAt(doc, 6, "emphasis");
		assertValidItemsAt(doc, 7, "title", "para");
	}

	public void testOneKindOfChild() {
		final Document doc = new Document(new RootElement("one-kind-of-child"));
		doc.setValidator(validator);
		assertValidItemsAt(doc, 1, "section");
	}

	private static void assertValidItemsAt(final Document doc, final int offset, final String... expectedItems) {
		final Set<QualifiedName> expected = new HashSet<QualifiedName>(expectedItems.length);
		for (final String expectedItem : expectedItems) {
			expected.add(new QualifiedName(null, expectedItem));
		}

		final Set<QualifiedName> validItems = doc.getValidator().getValidItems(doc.getElementAt(offset));
		assertEquals(expected, validItems);
	}

	public void testSequences() {
		assertFullyValidSequence("title", "#PCDATA");
		assertFullyValidSequence("para", "#PCDATA");

		assertInvalidSequence("empty", "#PCDATA");

		assertFullyValidSequence("index", "para");

		assertPartiallyValidSequence("section", "title"); // partially valid, para is still missing
		assertFullyValidSequence("section", "title", "para");
		assertFullyValidSequence("section", "para");
		assertFullyValidSequence("section", "para", "para");
		assertInvalidSequence("section", "para", "title");
		assertInvalidSequence("section", "title", "title");
		assertInvalidSequence("section", "title", "title", "para");
		assertInvalidSequence("section", "title", "#PCDATA");

		assertFullyValidSequence("document", "preface", "section", "index");
		assertFullyValidSequence("document", "title", "preface", "section", "index");
		assertFullyValidSequence("document", "title", "preface", "section", "section", "section", "index");
		assertPartiallyValidSequence("document", "title", "preface");
		assertPartiallyValidSequence("document", "preface", "section");
		assertPartiallyValidSequence("document", "preface", "section", "section");

		assertInvalidSequence("document", "title", "index");
		assertInvalidSequence("document", "title", "preface", "index");
		assertInvalidSequence("document", "preface", "index");
	}

	public void testValidateDocumentWithDTDAndNamespaces() throws Exception {
		final Document doc = new Document(new RootElement(new QualifiedName("http://namespace/uri/is/not/registered", "section")));
		doc.setValidator(validator);
		doc.insertElement(1, new Element("title"));
		doc.insertText(2, "ab");
		doc.insertElement(5, new Element("para"));

		validator.getAttributeDefinitions(doc.getRootElement());
	}

	private void assertFullyValidSequence(final String element, final String... sequence) {
		// fully includes partially
		assertValidSequence(true, element, true, true, sequence);
	}

	private void assertPartiallyValidSequence(final String element, final String... sequence) {
		// as partial sequence valid...
		assertValidSequence(true, element, false, true, sequence);

		// ... but as full sequence invalid
		assertValidSequence(false, element, true, false, sequence);
	}

	private void assertInvalidSequence(final String element, final String... sequence) {
		// partially _and_ fully
		assertValidSequence(false, element, true, true, sequence);
	}

	private void assertValidSequence(final boolean expected, final String element, final boolean validateFully, final boolean validatePartially, final String... sequence) {
		final QualifiedName elementName = new QualifiedName(null, element);
		for (int i = 0; i < sequence.length; i++) {
			final List<QualifiedName> prefix = createPrefix(i, sequence);
			final List<QualifiedName> toInsert = Collections.singletonList(new QualifiedName(null, sequence[i]));
			final List<QualifiedName> suffix = createSuffix(i, sequence);

			if (validateFully) {
				assertEquals(expected, validator.isValidSequence(elementName, prefix, toInsert, suffix, false));
			}
			if (validatePartially) {
				assertEquals(expected, validator.isValidSequence(elementName, prefix, toInsert, suffix, true));
			}
		}
	}

	private static List<QualifiedName> createPrefix(final int index, final String... sequence) {
		final List<QualifiedName> prefix = new ArrayList<QualifiedName>();
		for (int i = 0; i < index; i++) {
			prefix.add(new QualifiedName(null, sequence[i]));
		}
		return prefix;
	}

	private static List<QualifiedName> createSuffix(final int index, final String... sequence) {
		final List<QualifiedName> suffix = new ArrayList<QualifiedName>();
		for (int i = index + 1; i < sequence.length; i++) {
			suffix.add(new QualifiedName(null, sequence[i]));
		}
		return suffix;
	}
}

Back to the top