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.awt.Rectangle;
023: import java.util.BitSet;
024: import java.util.Vector;
025:
026: import junit.framework.TestCase;
027:
028: public class ElementTest extends TestCase {
029: Element elem = new Element();
030: AttributeList atts1 = new AttributeList("atts1");
031: ContentModel contentModel = new ContentModel(new Element());
032: Object data = new Rectangle();
033: BitSet exclusions = new BitSet();
034: BitSet inclusions = new BitSet();
035: int index = 4;
036: String name = "testElement";
037: boolean oEnd = false;
038: boolean oStart = true;
039: int type = 26;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043: elem = new Element();
044: }
045:
046: public void testElement() {
047: Utils.checkElement(elem, null, null, null, null, null, 0, null,
048: false, false, 19);
049: }
050:
051: public void testGetters() {
052: Utils.initElement(elem, atts1, contentModel, data, inclusions,
053: exclusions, index, name, oEnd, oStart, type);
054: Utils.checkElement(elem, atts1, contentModel, data, inclusions,
055: exclusions, index, name, oEnd, oStart, type);
056: }
057:
058: public void testAttributes() {
059: Vector values1 = new Vector();
060: values1.add("a");
061: values1.add("b");
062: atts1 = new AttributeList("atts1", 0, 0, "a", values1, null);
063:
064: elem.atts = atts1;
065: assertEquals(atts1, elem.getAttribute("atts1"));
066: assertNull(elem.getAttribute(null));
067: assertEquals(atts1, elem.getAttributeByValue("a"));
068: assertEquals(atts1, elem.getAttributeByValue("b"));
069: assertNull(elem.getAttributeByValue("c"));
070: assertNull(elem.getAttribute("atts2"));
071:
072: Vector values2 = new Vector();
073: values2.add("swing");
074: values2.add("awt");
075: AttributeList atts2 = new AttributeList("atts2", 0, 0, "swing",
076: values2, atts1);
077: elem.atts = atts2;
078: assertEquals(atts1, elem.getAttribute("atts1"));
079: assertEquals(atts2, elem.getAttribute("atts2"));
080: assertEquals(atts1, elem.getAttributeByValue("a"));
081: assertEquals(atts1, elem.getAttributeByValue("b"));
082: assertEquals(atts2, elem.getAttributeByValue("swing"));
083: assertEquals(atts2, elem.getAttributeByValue("awt"));
084: assertNull(elem.getAttributeByValue("c"));
085: assertNull(elem.getAttributeByValue("2d"));
086: }
087:
088: public void testIsEmpty() {
089: assertFalse(elem.isEmpty());
090: Utils.initElement(elem, atts1, contentModel, data, inclusions,
091: exclusions, index, name, oEnd, oStart, type);
092: assertFalse(elem.isEmpty());
093: Utils.initElement(elem, atts1, contentModel, data, inclusions,
094: exclusions, index, name, false, true, type);
095: assertFalse(elem.isEmpty());
096: Utils.initElement(elem, atts1, null, null, null, null, index,
097: name, oEnd, oStart, type);
098: assertFalse(elem.isEmpty());
099: Utils.initElement(elem, atts1, null, null, null, null, index,
100: name, oEnd, oStart, 17);
101: assertTrue(elem.isEmpty());
102: }
103:
104: public void testName2type() {
105: String[] names = Utils.getDTDConstantsNames();
106: for (int i = 0; i < names.length; i++) {
107: String key = names[i];
108: int type = Element.name2type(key);
109: if (key.equals("ANY")) {
110: assertEquals(DTDConstants.ANY, type);
111: } else if (key.equals("CDATA")) {
112: assertEquals(DTDConstants.CDATA, type);
113: } else if (key.equals("EMPTY")) {
114: assertEquals(DTDConstants.EMPTY, type);
115: } else if (key.equals("RCDATA")) {
116: assertEquals(DTDConstants.RCDATA, type);
117: } else {
118: assertEquals(0, type);
119: }
120: }
121: }
122:
123: public void testSerialization() {
124: Element intElement = (Element) contentModel.content;
125: intElement.name = "A";
126: Utils.initElement(elem, atts1, contentModel, data, inclusions,
127: exclusions, index, name, oEnd, oStart, type);
128: Element element = (Element) Utils.doSerialization(elem);
129: assertEquals(atts1.name, element.atts.getName());
130: assertEquals(contentModel.type, element.getContent().type);
131: assertEquals(intElement.getName(), ((Element) element
132: .getContent().content).getName());
133: Utils.checkElement(element, atts1, contentModel, data,
134: inclusions, exclusions, index, name, oEnd, oStart,
135: type, false);
136: }
137: }
|