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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import javax.swing.BasicSwingTestCase;
023: import javax.swing.text.AttributeSet;
024: import javax.swing.text.MutableAttributeSet;
025: import javax.swing.text.SimpleAttributeSet;
026: import javax.swing.text.StyleContext;
027: import javax.swing.text.AbstractDocument.AttributeContext;
028:
029: public class OptionTest extends BasicSwingTestCase {
030: private AttributeSet attrs;
031: private Option item;
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035: item = new Option(attrs = new SimpleAttributeSet());
036: }
037:
038: /**
039: * Tests constructor.
040: * Empty <code>SimpleAttributeSet</code> is passed
041: * as parameter (see initialization in <code>setUp()</code>).
042: */
043: public void testOption() {
044: assertTrue(item.getAttributes() instanceof MutableAttributeSet);
045: assertNotSame(attrs, item.getAttributes());
046: assertEquals(attrs, item.getAttributes());
047: assertNull(item.getLabel());
048: assertNull(item.getValue());
049: assertFalse(item.isSelected());
050: assertNull(item.toString());
051: }
052:
053: /**
054: * Tests constructor.
055: * Non-mutable (<code>StyleContext.SmallAttributeSet</code>) is passed
056: * as parameter.
057: */
058: public void testOptionSmallAttrSet() {
059: AttributeContext context = new StyleContext();
060: attrs = context.addAttribute(context.getEmptySet(), "key",
061: "value");
062:
063: item = new Option(attrs);
064: final AttributeSet itAttrs = item.getAttributes();
065: assertFalse(itAttrs instanceof MutableAttributeSet);
066: assertTrue(itAttrs instanceof StyleContext.SmallAttributeSet);
067: assertSame(attrs, itAttrs);
068: assertEquals(attrs, itAttrs);
069: assertNull(item.getLabel());
070: assertNull(item.getValue());
071: assertFalse(item.isSelected());
072: assertNull(item.toString());
073:
074: assertEquals("value", itAttrs.getAttribute("key"));
075: }
076:
077: /**
078: * Tests constructor.
079: * Attribute set passed contains <em>relevant</em> attributes:
080: * <ul>
081: * <li><code>HTML.Attribute.SELECTED</code> with no meaningful
082: * value,</li>
083: * <li><code>HTML.Attribute.VALUE</code>.</li>
084: * </ul>
085: */
086: public void testOptionSelected() {
087: MutableAttributeSet attr = (MutableAttributeSet) attrs;
088: attr
089: .addAttribute(HTML.Attribute.SELECTED, "no meaning" /*HTML.Attribute.SELECTED.toString()*/);
090: final String value = "iVal";
091: attr.addAttribute(HTML.Attribute.VALUE, value);
092:
093: item = new Option(attrs);
094: final AttributeSet itAttrs = item.getAttributes();
095: assertEquals(attrs, itAttrs);
096: assertNull(item.getLabel());
097: assertSame(value, item.getValue());
098: assertTrue(item.isSelected());
099: assertNull(item.toString());
100: }
101:
102: /**
103: * Tests constructor.
104: * Attribute set passed contains <em>relevant</em> attributes:
105: * <ul>
106: * <li><code>HTML.Attribute.SELECTED</code> with no meaningful
107: * value,</li>
108: * <li><code>HTML.Attribute.VALUE</code>.</li>
109: * </ul>
110: */
111: public void testOptionNull() {
112: testExceptionalCase(new NullPointerCase() {
113: public void exceptionalAction() throws Exception {
114: new Option(null);
115: }
116: });
117: }
118:
119: /**
120: * Tests <code>getAttributes</code>.
121: * See also tests for constructor: <code>testOption()</code> and
122: * <code>testOptionSmallAttrSet()</code>.
123: */
124: public void testGetAttributes() {
125: assertEquals(attrs, item.getAttributes());
126: }
127:
128: /**
129: * Tests three methods:
130: * <ul>
131: * <li><code>getLabel</code>,</li>
132: * <li><code>setLabel</code>,</li>
133: * <li><code>toString</code>.</li>
134: * </ul>
135: */
136: public void testGetLabel() {
137: assertNull(item.getLabel());
138: assertNull(item.toString());
139:
140: final String label = "itemLabel";
141: item.setLabel(label);
142:
143: assertSame(label, item.getLabel());
144: assertSame(label, item.toString());
145:
146: item.setLabel(null);
147: assertNull(item.getLabel());
148: assertNull(item.toString());
149: }
150:
151: /**
152: * Tests <code>getValue</code>.
153: * See also test for constructor <code>testOptionSelected()</code>.
154: */
155: public void testGetValue() {
156: assertNull(item.getValue());
157:
158: final String label = "label";
159: item.setLabel(label);
160: assertEquals(label, item.getValue());
161:
162: final String value = "value";
163: ((MutableAttributeSet) attrs).addAttribute(
164: HTML.Attribute.VALUE, value);
165: item = new Option(attrs);
166: assertEquals(value, item.getValue());
167:
168: ((MutableAttributeSet) attrs).addAttribute(
169: HTML.Attribute.VALUE, new Integer(1012));
170: item = new Option(attrs);
171: testExceptionalCase(new ClassCastCase() {
172: public void exceptionalAction() throws Exception {
173: item.getValue();
174: }
175: });
176: }
177:
178: /**
179: * Tests two methods:
180: * <ul>
181: * <li><code>isSelected</code>,</li>
182: * <li><code>setSelection</code>.</li>
183: * </ul>
184: */
185: public void testIsSelected() {
186: assertFalse(item.isSelected());
187:
188: item.setSelection(true);
189: assertTrue(item.isSelected());
190:
191: item.setSelection(false);
192: assertFalse(item.isSelected());
193: // See also testOptionSelected()
194: }
195:
196: /*
197: public void testSetLabel() { } ==> testGetLabel()
198:
199: public void testSetSelection() { } ==> testIsSelected()
200:
201: public void testToString() { } ==> testGetLabel()
202: */
203: }
|