001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text.html.parser;
021:
022: import java.io.IOException;
023: import java.util.BitSet;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.Set;
027: import java.util.Vector;
028:
029: import javax.swing.SwingTestCase;
030: import junit.framework.TestCase;
031:
032: /**
033: * That's a test for default dtd, doesn't check dtd 4.01 and read method.
034: */
035: public class DTDTest extends TestCase {
036: static final String PCDATA = conv("#pcdata");
037: static final String HTML = conv("html"); //1
038: static final String META = conv("meta"); //2
039: static final String BASE = conv("base"); //3
040: static final String ISINDEX = conv("isindex"); //4
041: static final String HEAD = conv("head"); //5
042: static final String BODY = conv("body"); //6
043: static final String APPLET = conv("applet"); //7
044: static final String PARAM = conv("param"); //8
045: static final String P = conv("p"); //9
046: static final String TITLE = conv("title"); //10
047: static final String STYLE = conv("style"); //11
048: static final String LINK = conv("link"); //12
049: static final String UNKNOWN = conv("unknown"); //13
050:
051: DTD dtd = new DTD("DTDTest1");
052: Vector elementNames;
053:
054: public static final String SPACE_ENTITY_NAME = "#SPACE";
055: public static final String RS_ENTITY_NAME = "#RS";
056: public static final String RE_ENTITY_NAME = "#RE";
057: private static final int DEFAULT_SIZE = 14;
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: public void testGetPutDTD() {
068: try {
069: DTD dtd1 = DTD.getDTD("DTDTest1");
070: DTD dtd2 = DTD.getDTD("DTDTest1");
071:
072: int hashCode1 = dtd1.hashCode();
073: int hashCode2 = dtd2.hashCode();
074: assertEquals("DTDTest1".toLowerCase(), dtd1.getName());
075: assertTrue(hashCode1 != hashCode2);
076: assertNotSame(dtd, DTD.getDTD("DTDTest1".toLowerCase()));
077:
078: String name = "name";
079: dtd1 = new DTD("abcd");
080: DTD.putDTDHash(name, dtd1);
081:
082: assertEquals(dtd1, DTD.getDTD(name));
083: assertNotSame(dtd1, DTD.getDTD("abcd"));
084: assertEquals(dtd1, DTD.getDTD("Name"));
085: assertEquals(dtd1, DTD.getDTD("name"));
086:
087: dtd2 = new DTD("abcdef");
088: DTD.putDTDHash(name, dtd2);
089: assertEquals(dtd2, DTD.getDTD(name));
090:
091: DTD.putDTDHash("name1", dtd2);
092: assertEquals(dtd2, DTD.getDTD("name"));
093: assertEquals(dtd2, DTD.getDTD("name1"));
094:
095: //If name contains upper characters that willn't be put to DTD.
096: //That's isn't logically and not documented.
097: if (SwingTestCase.isHarmony()) {
098: name = "Name";
099: dtd1 = new DTD("abcd");
100: DTD.putDTDHash(name, dtd1);
101:
102: assertEquals(dtd1, DTD.getDTD(name));
103: assertNotSame(dtd1, DTD.getDTD("abcd"));
104: assertEquals(dtd1, DTD.getDTD("Name"));
105: assertEquals(dtd1, DTD.getDTD("name"));
106: }
107:
108: if (SwingTestCase.isHarmony()) {
109: try {
110: dtd = DTD.getDTD(null);
111: assertFalse(
112: "IllegalArgumentException wasn't thrown",
113: true);
114: } catch (IllegalArgumentException e) {
115: }
116: try {
117: DTD.putDTDHash(null, dtd1);
118: assertFalse(
119: "IllegalArgumentException wasn't thrown",
120: true);
121: } catch (IllegalArgumentException e) {
122: }
123: try {
124: DTD.putDTDHash("nn", null);
125: assertFalse(
126: "IllegalArgumentException wasn't thrown",
127: true);
128: } catch (IllegalArgumentException e) {
129: }
130: }
131:
132: } catch (IOException e) {
133: assertFalse("Unexpected IOException", true);
134: }
135: }
136:
137: public void testFILE_VERSION() {
138: assertEquals(1, DTD.FILE_VERSION);
139: }
140:
141: private void checkDefaultElements(final DTD dtd) {
142: Vector elements = dtd.elements;
143: assertEquals(DEFAULT_SIZE, dtd.elements.size());
144: for (int i = 0; i < DEFAULT_SIZE - 1; i++) {
145: Utils.checkDTDDefaultElement(((Element) elements.get(i)),
146: ((String) elementNames.get(i)), i);
147: }
148:
149: Utils.checkElement(((Element) elements.get(DEFAULT_SIZE - 1)),
150: null, null, null, null, null, DEFAULT_SIZE - 1,
151: UNKNOWN, true, false, 17);
152:
153: }
154:
155: private void initDefaultElementsVector() {
156: elementNames = new Vector();
157: elementNames.add(PCDATA); //0
158: elementNames.add(HTML); //1
159: elementNames.add(META); //2
160: elementNames.add(BASE); //3
161: elementNames.add(ISINDEX); //4
162: elementNames.add(HEAD); //5
163: elementNames.add(BODY); //6
164: elementNames.add(APPLET); //7
165: elementNames.add(PARAM); //8
166: elementNames.add(P); //9
167: elementNames.add(TITLE); //10
168: elementNames.add(STYLE); //11
169: elementNames.add(LINK); //12
170: elementNames.add(UNKNOWN); //13
171: }
172:
173: private void checkDefaultElementHash(final DTD dtd) {
174: Hashtable elementHash = dtd.elementHash;
175: assertEquals(DEFAULT_SIZE, elementHash.size());
176:
177: for (int i = 0; i < DEFAULT_SIZE - 1; i++) {
178: String name = (String) elementNames.get(i);
179: Utils.checkDTDDefaultElement(((Element) elementHash
180: .get(name)), name, i);
181: }
182:
183: Utils.checkElement(((Element) elementHash.get(UNKNOWN)), null,
184: null, null, null, null, DEFAULT_SIZE - 1, UNKNOWN,
185: true, false, 17);
186: }
187:
188: private void checkDefaultEntityHash(final DTD dtd) {
189: Hashtable entityHash = dtd.entityHash;
190: assertEquals(3, entityHash.size());
191: Set keys = entityHash.keySet();
192: Iterator iter = keys.iterator();
193: int count = 0;
194: while (iter.hasNext()) {
195: String name = (String) iter.next();
196: Entity entity = (Entity) entityHash.get(name);
197: if (SPACE_ENTITY_NAME.equals(name)) {
198: count++;
199: Utils.checkEntity(entity, name, 0, " ", true, false);
200: } else if (RS_ENTITY_NAME.equals(name)) {
201: count++;
202: Utils.checkEntity(entity, name, 0, "\n", true, false);
203: } else if (RE_ENTITY_NAME.equals(name)) {
204: count++;
205: Utils.checkEntity(entity, name, 0, "\r", true, false);
206: }
207: }
208: assertEquals(3, count);
209: }
210:
211: public void testDTD() {
212: DTD dtd1 = new DTD(null);
213: assertNull(dtd1.getName());
214:
215: assertEquals("DTDTest1", dtd.name);
216: initDefaultElementsVector();
217: checkDefaultElements(dtd);
218: checkDefaultElementHash(dtd);
219: checkDefaultEntityHash(dtd);
220:
221: Utils.checkDTDDefaultElement(dtd.pcdata, PCDATA, 0);
222: Utils.checkDTDDefaultElement(dtd.html, HTML, 1);
223: Utils.checkDTDDefaultElement(dtd.meta, META, 2);
224: Utils.checkDTDDefaultElement(dtd.base, BASE, 3);
225: Utils.checkDTDDefaultElement(dtd.isindex, ISINDEX, 4);
226: Utils.checkDTDDefaultElement(dtd.head, HEAD, 5);
227: Utils.checkDTDDefaultElement(dtd.body, BODY, 6);
228: Utils.checkDTDDefaultElement(dtd.applet, APPLET, 7);
229: Utils.checkDTDDefaultElement(dtd.param, PARAM, 8);
230: Utils.checkDTDDefaultElement(dtd.p, P, 9);
231: Utils.checkDTDDefaultElement(dtd.title, TITLE, 10);
232: }
233:
234: public void testToString() {
235: assertEquals(dtd.getName(), dtd.toString());
236: }
237:
238: public void testDefContentModel() {
239: Element e1 = new Element();
240: e1.name = "e1";
241: Element e2 = new Element();
242: e2.name = "e2";
243: ContentModel contentModel1 = new ContentModel(e1);
244: ContentModel contentModel = dtd.defContentModel('|', e2,
245: contentModel1);
246: Utils.checkContentModel(contentModel, e2, '|', contentModel1);
247: }
248:
249: public void testDefAttributeList() {
250: String name = "name";
251: int type = 22;
252: int modifier = 23;
253: String value = "value";
254: String values = "value1|value2|value3|";
255: AttributeList next = new AttributeList("next");
256: AttributeList attl = dtd.defAttributeList(name, type, modifier,
257: value, values, next);
258: Vector v = new Vector();
259: v.add("value1");
260: v.add("value2");
261: v.add("value3");
262: Utils.checkAttributeList(attl, modifier, type, name, next, v,
263: value, true);
264:
265: attl = dtd.defAttributeList(name, type, modifier, value, null,
266: next);
267: Utils.checkAttributeList(attl, modifier, type, name, next,
268: null, value, true);
269: v = new Vector();
270: attl = dtd.defAttributeList(name, type, modifier, value, "",
271: next);
272: Utils.checkAttributeList(attl, modifier, type, name, next, v,
273: value, true);
274: }
275:
276: public void testDefElement() {
277: String name = "newElement";
278: int type = 234;
279: boolean omitStart = true;
280: boolean omitEnd = true;
281: ContentModel contentModel = null;
282: String[] exclusions = new String[] { HTML, BASE };
283: String[] inclusions = new String[] { APPLET, BODY };
284: AttributeList attl = new AttributeList("attributeList");
285: Element elem = dtd.defElement(name, type, omitStart, omitEnd,
286: contentModel, exclusions, inclusions, attl);
287: BitSet excl = new BitSet();
288: excl.set(1);
289: excl.set(3);
290: BitSet incl = new BitSet();
291: incl.set(6);
292: incl.set(7);
293: Utils.checkElement(elem, attl, contentModel, null, incl, excl,
294: DEFAULT_SIZE, name, omitEnd, omitStart, 234);
295: assertEquals(DEFAULT_SIZE + 1, dtd.elements.size());
296: assertEquals(name, ((Element) dtd.elements.get(DEFAULT_SIZE))
297: .getName());
298: dtd.elements.remove(DEFAULT_SIZE);
299: dtd.elementHash.remove(name);
300:
301: elem = dtd.defElement(HTML, 123, false, false, null, null,
302: null, null);
303: assertEquals(14, dtd.elements.size());
304: elem = (Element) dtd.elementHash.get(HTML);
305: assertEquals(123, elem.getType());
306: elem.type = 19;
307: }
308:
309: static String conv(final String name) {
310: return SwingTestCase.isHarmony() ? name.toUpperCase() : name;
311: }
312:
313: public void testDefEntityStringintString() {
314: String name = "newStringEntity";
315: int type = 123;
316: String data = "AbcD";
317: Entity entity = dtd.defEntity(name, type, data);
318: Utils.checkEntity(entity, name, type, data, false, false);
319: assertEquals(entity, dtd.entityHash.get(name));
320: dtd.entityHash.remove(name);
321:
322: name = "#SPACE";
323: entity = dtd.defEntity(name, type, data);
324: Utils.checkEntity(entity, name, 0, " ", true, false);
325: assertEquals(3, dtd.entityHash.size());
326: }
327:
328: public void testDefEntityStringintint() {
329: String name = "newCharEntity";
330: int type = 123;
331: char data = 'J';
332: Entity entity = dtd.defEntity(name, type, data);
333: Utils.checkEntity(entity, name, type, Character.toString(data),
334: false, false);
335: assertEquals(entity, dtd.entityHash.get(name));
336: assertEquals(4, dtd.entityHash.size());
337: dtd.entityHash.remove(name);
338:
339: name = "#SPACE";
340: entity = dtd.defEntity(name, type, data);
341: Utils.checkEntity(entity, name, 0, " ", true, false);
342: assertEquals(3, dtd.entityHash.size());
343: }
344:
345: public void testDefineAttributes() {
346: String name = HTML;
347: AttributeList attl = new AttributeList("new AttributeList");
348: dtd.defineAttributes(HTML, attl);
349: Element element = (Element) dtd.elementHash.get(name);
350: assertEquals(attl, element.getAttributes());
351: element.atts = null;
352:
353: name = "newElement";
354: dtd.defineAttributes(name, attl);
355: element = (Element) dtd.elementHash.get(name);
356: assertEquals(DEFAULT_SIZE + 1, dtd.elements.size());
357: assertEquals(attl, element.getAttributes());
358: dtd.elements.remove(DEFAULT_SIZE);
359: dtd.elementHash.remove(name);
360: }
361:
362: public void testDefineElement() {
363: Element elem = (Element) dtd.elementHash.get(HTML);
364: String name = elem.getName();
365: int type = elem.getType();
366: boolean omitStart = elem.omitStart();
367: boolean omitEnd = elem.omitEnd();
368: ContentModel content = elem.getContent();
369: BitSet exclusions = elem.exclusions;
370: BitSet inclusions = elem.inclusions;
371: AttributeList atts = elem.getAttributes();
372: //check exactly the same
373: Element elem1 = dtd.defineElement(name, type, omitStart,
374: omitEnd, content, exclusions, inclusions, atts);
375: assertEquals(elem, elem1);
376: Utils.checkElement(elem1, atts, content, null, exclusions,
377: inclusions, 1, name, omitEnd, omitStart, type);
378: assertEquals(DEFAULT_SIZE, dtd.elements.size());
379:
380: // change fields
381: type = 245;
382: elem1 = dtd.defineElement(name, type, omitStart, omitEnd,
383: content, exclusions, inclusions, atts);
384: Utils.checkElement(elem1, atts, content, null, exclusions,
385: inclusions, 1, name, omitEnd, omitStart, type);
386: elem1 = (Element) dtd.elementHash.get(name); //TODO dtdHash isn't changed
387: elem1 = (Element) dtd.elements.get(1);
388: Utils.checkElement(elem1, atts, content, null, exclusions,
389: inclusions, 1, name, omitEnd, omitStart, type);
390: elem1.type = 19;
391: assertEquals(DEFAULT_SIZE, dtd.elements.size());
392:
393: // other name
394: name = "test";
395: elem1 = dtd.defineElement(name, type, omitStart, omitEnd,
396: content, exclusions, inclusions, atts);
397: Utils.checkElement(elem1, atts, content, null, exclusions,
398: inclusions, DEFAULT_SIZE, name, omitEnd, omitStart,
399: type);
400:
401: elem1 = (Element) dtd.elementHash.get(name);
402: Utils.checkElement(elem1, atts, content, null, exclusions,
403: inclusions, DEFAULT_SIZE, name, omitEnd, omitStart,
404: type);
405: assertEquals(DEFAULT_SIZE + 1, dtd.elements.size());
406: dtd.elementHash.remove(name);
407: dtd.elements.remove(DEFAULT_SIZE);
408: }
409:
410: public void testDefineEntity() {
411: Entity entity = (Entity) dtd.entityHash.get("#SPACE");
412: String name = entity.getName();
413: int type = entity.getType();
414: String data = entity.getString();
415: boolean isGeneral = entity.isGeneral();
416: boolean isParameter = entity.isParameter();
417:
418: Entity entity1 = dtd.defineEntity(name, type, data
419: .toCharArray());
420: Utils.checkEntity(entity1, name, type, data, isGeneral,
421: isParameter);
422: assertEquals(3, dtd.entityHash.size());
423:
424: entity1 = (Entity) dtd.entityHash.get(name);
425: Utils.checkEntity(entity1, name, type, data, isGeneral,
426: isParameter);
427:
428: data = "data";
429: entity1 = dtd.defineEntity(name, type, data.toCharArray());
430: //Attention: data wasn't updated
431: Utils.checkEntity(entity1, name, type, " ", isGeneral,
432: isParameter);
433: assertEquals(3, dtd.entityHash.size());
434:
435: data = entity1.getString();
436: type = 235;
437: entity1 = dtd.defineEntity(name, type, data.toCharArray());
438: //Attention: type wasn't updated
439: Utils
440: .checkEntity(entity1, name, 0, " ", isGeneral,
441: isParameter);
442: assertEquals(3, dtd.entityHash.size());
443:
444: name = "newEntity";
445: entity1 = dtd.defineEntity(name, type, data.toCharArray());
446: Utils.checkEntity(entity1, name, type, data, false, false);
447: assertEquals(4, dtd.entityHash.size());
448: entity1 = (Entity) dtd.entityHash.get(name);
449: Utils.checkEntity(entity1, name, type, data, false, false);
450: dtd.entityHash.remove(name);
451: }
452:
453: public void testGetElementint() {
454: initDefaultElementsVector();
455: for (int i = 0; i < DEFAULT_SIZE; i++) {
456: assertEquals(elementNames.get(i), dtd.getElement(i)
457: .getName());
458: }
459: //ArrayIndexOutOfBoundsException on RI
460: if (SwingTestCase.isHarmony()) {
461: assertNull(dtd.getElement(DEFAULT_SIZE));
462: }
463: }
464:
465: public void testGetElementString() {
466: initDefaultElementsVector();
467: for (int i = 0; i < DEFAULT_SIZE; i++) {
468: String name = (String) elementNames.get(i);
469: assertEquals(name, dtd.getElement(name).getName());
470: }
471: String name = "test1";
472: Utils.checkDTDDefaultElement(dtd.getElement(name), name,
473: DEFAULT_SIZE);
474: assertEquals(DEFAULT_SIZE + 1, dtd.elementHash.size());
475: assertEquals(DEFAULT_SIZE + 1, dtd.elements.size());
476:
477: dtd.elements.remove(DEFAULT_SIZE);
478: dtd.elementHash.remove(name);
479: }
480:
481: public void testGetEntityint() {
482: //RI
483: if (!SwingTestCase.isHarmony()) {
484: assertNull(dtd.getEntity(0));
485: assertNull(dtd.getEntity(1));
486: assertNull(dtd.getEntity(2));
487: assertNull(dtd.getEntity(' '));
488: assertNull(dtd.getEntity('\n'));
489: assertNull(dtd.getEntity('\r'));
490: }
491: }
492:
493: public void testGetEntityString() {
494: assertEquals(dtd.entityHash.get(SPACE_ENTITY_NAME), dtd
495: .getEntity(SPACE_ENTITY_NAME));
496: assertEquals(dtd.entityHash.get(RS_ENTITY_NAME), dtd
497: .getEntity(RS_ENTITY_NAME));
498: assertEquals(dtd.entityHash.get(RE_ENTITY_NAME), dtd
499: .getEntity(RE_ENTITY_NAME));
500: assertNull(dtd.getEntity("Test"));
501: }
502:
503: public void testGetName() {
504: assertEquals("DTDTest1", dtd.getName());
505: }
506: }
|