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;
021:
022: import junit.framework.TestCase;
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Graphics;
026: import javax.swing.Icon;
027: import javax.swing.JLabel;
028:
029: public class StyleConstantsTest extends TestCase {
030: protected StyleConstants sc;
031:
032: protected SimpleAttributeSet attr = new SimpleAttributeSet();
033:
034: String message = "Test for StyleConstants";
035:
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: sc = new StyleConstants(message);
040: }
041:
042: public void testToString() {
043: assertEquals(message, sc.toString());
044: }
045:
046: protected void putAttribute(final Object key, final Object value) {
047: attr.removeAttributes(attr);
048: attr.addAttribute(key, value);
049: }
050:
051: public void testFirstLineIndent() {
052: assertEquals(StyleConstants.ParagraphConstants.class,
053: StyleConstants.FirstLineIndent.getClass());
054: assertEquals("FirstLineIndent", StyleConstants.FirstLineIndent
055: .toString());
056: }
057:
058: public void testGetFirstLineIndent() {
059: assertTrue(0.0f == StyleConstants
060: .getFirstLineIndent(SimpleAttributeSet.EMPTY));
061: float value = 1.23f;
062: putAttribute(StyleConstants.FirstLineIndent, new Float(value));
063: assertTrue(value == StyleConstants.getFirstLineIndent(attr));
064: }
065:
066: public void testLeftIndent() {
067: assertEquals(StyleConstants.ParagraphConstants.class,
068: StyleConstants.LeftIndent.getClass());
069: assertEquals("LeftIndent", StyleConstants.LeftIndent.toString());
070: }
071:
072: public void testGetLeftIndent() {
073: assertTrue(0.0f == StyleConstants
074: .getLeftIndent(SimpleAttributeSet.EMPTY));
075: float value = 1.234f;
076: putAttribute(StyleConstants.LeftIndent, new Float(value));
077: assertTrue(value == StyleConstants.getLeftIndent(attr));
078: }
079:
080: public void testLineSpacing() {
081: assertEquals(StyleConstants.ParagraphConstants.class,
082: StyleConstants.LineSpacing.getClass());
083: assertEquals("LineSpacing", StyleConstants.LineSpacing
084: .toString());
085: }
086:
087: public void testGetLineSpacing() {
088: assertTrue(0.0f == StyleConstants
089: .getLineSpacing(SimpleAttributeSet.EMPTY));
090: float value = 1.2345f;
091: putAttribute(StyleConstants.LineSpacing, new Float(value));
092: assertTrue(value == StyleConstants.getLineSpacing(attr));
093: }
094:
095: public void testRightIndent() {
096: assertEquals(StyleConstants.ParagraphConstants.class,
097: StyleConstants.RightIndent.getClass());
098: assertEquals("RightIndent", StyleConstants.RightIndent
099: .toString());
100: }
101:
102: public void testGetRightIndent() {
103: assertTrue(0.0f == StyleConstants
104: .getRightIndent(SimpleAttributeSet.EMPTY));
105: float value = 1.23456f;
106: putAttribute(StyleConstants.RightIndent, new Float(value));
107: assertTrue(value == StyleConstants.getRightIndent(attr));
108: }
109:
110: public void testSpaceAbove() {
111: assertEquals(StyleConstants.ParagraphConstants.class,
112: StyleConstants.SpaceAbove.getClass());
113: assertEquals("SpaceAbove", StyleConstants.SpaceAbove.toString());
114: }
115:
116: public void testGetSpaceAbove() {
117: assertTrue(0.0f == StyleConstants
118: .getSpaceAbove(SimpleAttributeSet.EMPTY));
119: float value = 1.234567f;
120: putAttribute(StyleConstants.SpaceAbove, new Float(value));
121: assertTrue(value == StyleConstants.getSpaceAbove(attr));
122: }
123:
124: public void testSpaceBelow() {
125: assertEquals(StyleConstants.ParagraphConstants.class,
126: StyleConstants.SpaceBelow.getClass());
127: assertEquals("SpaceBelow", StyleConstants.SpaceBelow.toString());
128: }
129:
130: public void testGetSpaceBelow() {
131: assertTrue(0.0f == StyleConstants
132: .getSpaceBelow(SimpleAttributeSet.EMPTY));
133: float value = 1.2345678f;
134: putAttribute(StyleConstants.SpaceBelow, new Float(value));
135: assertTrue(value == StyleConstants.getSpaceBelow(attr));
136: }
137:
138: public void testALIGN_CENTER() {
139: assertEquals(1, StyleConstants.ALIGN_CENTER);
140: }
141:
142: public void testALIGN_JUSTIFIED() {
143: assertEquals(3, StyleConstants.ALIGN_JUSTIFIED);
144: }
145:
146: public void testALIGN_LEFT() {
147: assertEquals(0, StyleConstants.ALIGN_LEFT);
148: }
149:
150: public void testALIGN_RIGHT() {
151: assertEquals(2, StyleConstants.ALIGN_RIGHT);
152: }
153:
154: public void testComponentElementName() {
155: assertEquals("component", StyleConstants.ComponentElementName);
156: }
157:
158: public void testIconElementName() {
159: assertEquals("icon", StyleConstants.IconElementName);
160: }
161:
162: public void testAlignment() {
163: assertEquals(StyleConstants.ParagraphConstants.class,
164: StyleConstants.Alignment.getClass());
165: assertEquals("Alignment", StyleConstants.Alignment.toString());
166: }
167:
168: public void testGetAlignment() {
169: assertTrue(StyleConstants.ALIGN_LEFT == StyleConstants
170: .getAlignment(SimpleAttributeSet.EMPTY));
171: putAttribute(StyleConstants.Alignment, new Integer(
172: StyleConstants.ALIGN_JUSTIFIED));
173: assertTrue(StyleConstants.ALIGN_JUSTIFIED == StyleConstants
174: .getAlignment(attr));
175: }
176:
177: public void testBidiLevel() {
178: assertEquals(StyleConstants.CharacterConstants.class,
179: StyleConstants.BidiLevel.getClass());
180: assertEquals("bidiLevel", StyleConstants.BidiLevel.toString());
181: }
182:
183: public void testGetBidiLevel() {
184: assertTrue(0 == StyleConstants
185: .getBidiLevel(SimpleAttributeSet.EMPTY));
186: int val = 1;
187: putAttribute(StyleConstants.BidiLevel, new Integer(val));
188: assertTrue(val == StyleConstants.getBidiLevel(attr));
189: }
190:
191: public void testFontSize() {
192: assertEquals(StyleConstants.FontConstants.class,
193: StyleConstants.FontSize.getClass());
194: assertEquals("size", StyleConstants.FontSize.toString());
195: }
196:
197: public void testGetFontSize() {
198: assertTrue(12 == StyleConstants
199: .getFontSize(SimpleAttributeSet.EMPTY));
200: int val = 2;
201: putAttribute(StyleConstants.FontSize, new Integer(val));
202: assertTrue(val == StyleConstants.getFontSize(attr));
203: }
204:
205: public void testBold() {
206: assertEquals(StyleConstants.FontConstants.class,
207: StyleConstants.Bold.getClass());
208: assertEquals("bold", StyleConstants.Bold.toString());
209: }
210:
211: public void testIsBold() {
212: assertFalse(StyleConstants.isBold(SimpleAttributeSet.EMPTY));
213: putAttribute(StyleConstants.Bold, Boolean.TRUE);
214: assertTrue(StyleConstants.isBold(attr));
215: putAttribute(StyleConstants.Bold, Boolean.FALSE);
216: assertFalse(StyleConstants.isBold(attr));
217: }
218:
219: public void testItalic() {
220: assertEquals(StyleConstants.FontConstants.class,
221: StyleConstants.Italic.getClass());
222: assertEquals("italic", StyleConstants.Italic.toString());
223: }
224:
225: public void testIsItalic() {
226: assertFalse(StyleConstants.isItalic(SimpleAttributeSet.EMPTY));
227: putAttribute(StyleConstants.Italic, Boolean.TRUE);
228: assertTrue(StyleConstants.isItalic(attr));
229: putAttribute(StyleConstants.Italic, Boolean.FALSE);
230: assertFalse(StyleConstants.isItalic(attr));
231: }
232:
233: public void testStrikeThrough() {
234: assertEquals(StyleConstants.CharacterConstants.class,
235: StyleConstants.StrikeThrough.getClass());
236: assertEquals("strikethrough", StyleConstants.StrikeThrough
237: .toString());
238: }
239:
240: public void testIsStrikeThrough() {
241: assertFalse(StyleConstants
242: .isStrikeThrough(SimpleAttributeSet.EMPTY));
243: putAttribute(StyleConstants.StrikeThrough, Boolean.TRUE);
244: assertTrue(StyleConstants.isStrikeThrough(attr));
245: putAttribute(StyleConstants.StrikeThrough, Boolean.FALSE);
246: assertFalse(StyleConstants.isStrikeThrough(attr));
247: }
248:
249: public void testSubscript() {
250: assertEquals(StyleConstants.CharacterConstants.class,
251: StyleConstants.Subscript.getClass());
252: assertEquals("subscript", StyleConstants.Subscript.toString());
253: }
254:
255: public void testIsSubscript() {
256: assertFalse(StyleConstants
257: .isSubscript(SimpleAttributeSet.EMPTY));
258: putAttribute(StyleConstants.Subscript, Boolean.TRUE);
259: assertTrue(StyleConstants.isSubscript(attr));
260: putAttribute(StyleConstants.Subscript, Boolean.FALSE);
261: assertFalse(StyleConstants.isSubscript(attr));
262: }
263:
264: public void testSuperscript() {
265: assertEquals(StyleConstants.CharacterConstants.class,
266: StyleConstants.Superscript.getClass());
267: assertEquals("superscript", StyleConstants.Superscript
268: .toString());
269: }
270:
271: public void testIsSuperscript() {
272: assertFalse(StyleConstants
273: .isSuperscript(SimpleAttributeSet.EMPTY));
274: putAttribute(StyleConstants.Superscript, Boolean.TRUE);
275: assertTrue(StyleConstants.isSuperscript(attr));
276: putAttribute(StyleConstants.Superscript, Boolean.FALSE);
277: assertFalse(StyleConstants.isSuperscript(attr));
278: }
279:
280: public void testUnderline() {
281: assertEquals(StyleConstants.CharacterConstants.class,
282: StyleConstants.Underline.getClass());
283: assertEquals("underline", StyleConstants.Underline.toString());
284: }
285:
286: public void testIsUnderline() {
287: assertFalse(StyleConstants
288: .isUnderline(SimpleAttributeSet.EMPTY));
289: putAttribute(StyleConstants.Underline, Boolean.TRUE);
290: assertTrue(StyleConstants.isUnderline(attr));
291: putAttribute(StyleConstants.Underline, Boolean.FALSE);
292: assertFalse(StyleConstants.isUnderline(attr));
293: }
294:
295: public void testSetFirstLineIndent() {
296: attr.removeAttributes(attr);
297: float val = 1.2f;
298: StyleConstants.setFirstLineIndent(attr, val);
299: assertTrue(val == ((Float) attr
300: .getAttribute(StyleConstants.FirstLineIndent))
301: .floatValue());
302: }
303:
304: public void testSetLeftIndent() {
305: attr.removeAttributes(attr);
306: float val = 1.23f;
307: StyleConstants.setLeftIndent(attr, val);
308: assertTrue(val == ((Float) attr
309: .getAttribute(StyleConstants.LeftIndent)).floatValue());
310: }
311:
312: public void testSetLineSpacing() {
313: attr.removeAttributes(attr);
314: float val = 1.234f;
315: StyleConstants.setLineSpacing(attr, val);
316: assertTrue(val == ((Float) attr
317: .getAttribute(StyleConstants.LineSpacing)).floatValue());
318: }
319:
320: public void testSetRightIndent() {
321: attr.removeAttributes(attr);
322: float val = 1.2345f;
323: StyleConstants.setRightIndent(attr, val);
324: assertTrue(val == ((Float) attr
325: .getAttribute(StyleConstants.RightIndent)).floatValue());
326: }
327:
328: public void testSetSpaceAbove() {
329: attr.removeAttributes(attr);
330: float val = 1.23456f;
331: StyleConstants.setSpaceAbove(attr, val);
332: assertTrue(val == ((Float) attr
333: .getAttribute(StyleConstants.SpaceAbove)).floatValue());
334: }
335:
336: public void testSetSpaceBelow() {
337: attr.removeAttributes(attr);
338: float val = 1.234567f;
339: StyleConstants.setSpaceBelow(attr, val);
340: assertTrue(val == ((Float) attr
341: .getAttribute(StyleConstants.SpaceBelow)).floatValue());
342: }
343:
344: public void testSetAlignment() {
345: attr.removeAttributes(attr);
346: StyleConstants.setAlignment(attr,
347: StyleConstants.ALIGN_JUSTIFIED);
348: assertTrue(StyleConstants.ALIGN_JUSTIFIED == ((Integer) attr
349: .getAttribute(StyleConstants.Alignment)).intValue());
350: }
351:
352: public void testSetBidiLevel() {
353: attr.removeAttributes(attr);
354: int val = 2;
355: StyleConstants.setBidiLevel(attr, val);
356: assertTrue(val == ((Integer) attr
357: .getAttribute(StyleConstants.BidiLevel)).intValue());
358: }
359:
360: public void testSetFontSize() {
361: attr.removeAttributes(attr);
362: int val = 10;
363: StyleConstants.setFontSize(attr, val);
364: assertTrue(val == ((Integer) attr
365: .getAttribute(StyleConstants.FontSize)).intValue());
366: }
367:
368: public void testSetBold() {
369: attr.removeAttributes(attr);
370: StyleConstants.setBold(attr, true);
371: assertTrue(((Boolean) attr.getAttribute(StyleConstants.Bold))
372: .booleanValue());
373: StyleConstants.setBold(attr, false);
374: assertFalse(((Boolean) attr.getAttribute(StyleConstants.Bold))
375: .booleanValue());
376: }
377:
378: public void testSetItalic() {
379: attr.removeAttributes(attr);
380: StyleConstants.setItalic(attr, true);
381: assertTrue(((Boolean) attr.getAttribute(StyleConstants.Italic))
382: .booleanValue());
383: StyleConstants.setItalic(attr, false);
384: assertFalse(((Boolean) attr.getAttribute(StyleConstants.Italic))
385: .booleanValue());
386: }
387:
388: public void testSetStrikeThrough() {
389: attr.removeAttributes(attr);
390: StyleConstants.setStrikeThrough(attr, true);
391: assertTrue(((Boolean) attr
392: .getAttribute(StyleConstants.StrikeThrough))
393: .booleanValue());
394: StyleConstants.setStrikeThrough(attr, false);
395: assertFalse(((Boolean) attr
396: .getAttribute(StyleConstants.StrikeThrough))
397: .booleanValue());
398: }
399:
400: public void testSetSubscript() {
401: attr.removeAttributes(attr);
402: StyleConstants.setSubscript(attr, true);
403: assertTrue(((Boolean) attr
404: .getAttribute(StyleConstants.Subscript)).booleanValue());
405: StyleConstants.setSubscript(attr, false);
406: assertFalse(((Boolean) attr
407: .getAttribute(StyleConstants.Subscript)).booleanValue());
408: }
409:
410: public void testSetSuperscript() {
411: attr.removeAttributes(attr);
412: StyleConstants.setSuperscript(attr, true);
413: assertTrue(((Boolean) attr
414: .getAttribute(StyleConstants.Superscript))
415: .booleanValue());
416: StyleConstants.setSuperscript(attr, false);
417: assertFalse(((Boolean) attr
418: .getAttribute(StyleConstants.Superscript))
419: .booleanValue());
420: }
421:
422: public void testSetUnderline() {
423: attr.removeAttributes(attr);
424: StyleConstants.setUnderline(attr, true);
425: assertTrue(((Boolean) attr
426: .getAttribute(StyleConstants.Underline)).booleanValue());
427: StyleConstants.setUnderline(attr, false);
428: assertFalse(((Boolean) attr
429: .getAttribute(StyleConstants.Underline)).booleanValue());
430: }
431:
432: public void testBackground() {
433: assertEquals(StyleConstants.ColorConstants.class,
434: StyleConstants.Background.getClass());
435: assertEquals("background", StyleConstants.Background.toString());
436: }
437:
438: public void testGetBackground() {
439: assertTrue(Color.black == StyleConstants
440: .getBackground(SimpleAttributeSet.EMPTY));
441: Color val = new Color(10, 11, 12);
442: putAttribute(StyleConstants.Background, val);
443: assertEquals(val, StyleConstants.getBackground(attr));
444: }
445:
446: public void testForeground() {
447: assertEquals(StyleConstants.ColorConstants.class,
448: StyleConstants.Foreground.getClass());
449: assertEquals("foreground", StyleConstants.Foreground.toString());
450: }
451:
452: public void testGetForeground() {
453: assertTrue(Color.black == StyleConstants
454: .getForeground(SimpleAttributeSet.EMPTY));
455: Color val = new Color(11, 12, 13);
456: putAttribute(StyleConstants.Foreground, val);
457: assertEquals(val, StyleConstants.getForeground(attr));
458: }
459:
460: public void testSetBackground() {
461: attr.removeAttributes(attr);
462: Color val = new Color(13, 14, 15);
463: StyleConstants.setBackground(attr, val);
464: assertEquals(val, attr.getAttribute(StyleConstants.Background));
465: }
466:
467: public void testSetForeground() {
468: attr.removeAttributes(attr);
469: Color val = new Color(15, 16, 17);
470: StyleConstants.setForeground(attr, val);
471: assertEquals(val, attr.getAttribute(StyleConstants.Foreground));
472: }
473:
474: public void testComponentAttribute() {
475: assertEquals(StyleConstants.CharacterConstants.class,
476: StyleConstants.ComponentAttribute.getClass());
477: assertEquals("component", StyleConstants.ComponentAttribute
478: .toString());
479: }
480:
481: public void testGetComponent() {
482: assertNull(StyleConstants
483: .getComponent(SimpleAttributeSet.EMPTY));
484: Component val = new JLabel("test component");
485: putAttribute(StyleConstants.ComponentAttribute, val);
486: assertEquals(val, StyleConstants.getComponent(attr));
487: }
488:
489: public void testSetComponent() {
490: attr.removeAttributes(attr);
491: Component val = new JLabel("test component");
492: StyleConstants.setComponent(attr, val);
493: assertEquals(AbstractDocument.ElementNameAttribute
494: + "=component component=" + val + " ", attr.toString());
495: assertEquals(val, attr
496: .getAttribute(StyleConstants.ComponentAttribute));
497: assertEquals(StyleConstants.ComponentElementName, attr
498: .getAttribute(AbstractDocument.ElementNameAttribute));
499: }
500:
501: public void testFontFamily() {
502: assertEquals(StyleConstants.FontConstants.class,
503: StyleConstants.FontFamily.getClass());
504: assertEquals("family", StyleConstants.FontFamily.toString());
505: }
506:
507: public void testGetFontFamily() {
508: assertEquals("Monospaced", StyleConstants
509: .getFontFamily(SimpleAttributeSet.EMPTY));
510: String val = "arial";
511: putAttribute(StyleConstants.FontFamily, val);
512: assertEquals(val, StyleConstants.getFontFamily(attr));
513: }
514:
515: public void testSetFontFamily() {
516: attr.removeAttributes(attr);
517: String val = "arial";
518: StyleConstants.setFontFamily(attr, val);
519: assertEquals(val, attr.getAttribute(StyleConstants.FontFamily));
520: }
521:
522: public void testGetIcon() {
523: assertNull(StyleConstants.getIcon(SimpleAttributeSet.EMPTY));
524: Icon val = new Icon() {
525: public int getIconHeight() {
526: return 0;
527: }
528:
529: public int getIconWidth() {
530: return 0;
531: }
532:
533: public void paintIcon(final Component arg0,
534: final Graphics arg1, final int arg2, final int arg3) {
535: }
536: };
537: putAttribute(StyleConstants.IconAttribute, val);
538: assertEquals(val, StyleConstants.getIcon(attr));
539: }
540:
541: public void testSetIcon() {
542: attr.removeAttributes(attr);
543: Icon val = new Icon() {
544: public int getIconHeight() {
545: return 0;
546: }
547:
548: public int getIconWidth() {
549: return 0;
550: }
551:
552: public void paintIcon(final Component arg0,
553: final Graphics arg1, final int arg2, final int arg3) {
554: }
555: };
556: StyleConstants.setIcon(attr, val);
557: assertEquals(AbstractDocument.ElementNameAttribute
558: + "=icon icon=" + val + " ", attr.toString());
559: assertEquals(val, attr
560: .getAttribute(StyleConstants.IconAttribute));
561: assertEquals(StyleConstants.IconElementName, attr
562: .getAttribute(AbstractDocument.ElementNameAttribute));
563: }
564:
565: public void testTabSet() {
566: assertEquals(StyleConstants.ParagraphConstants.class,
567: StyleConstants.TabSet.getClass());
568: assertEquals("TabSet", StyleConstants.TabSet.toString());
569: }
570:
571: public void testGetTabSet() {
572: assertNull(StyleConstants.getTabSet(SimpleAttributeSet.EMPTY));
573: TabSet val = new TabSet(new TabStop[] { new TabStop(0.1f) });
574: putAttribute(StyleConstants.TabSet, val);
575: assertEquals(val, StyleConstants.getTabSet(attr));
576: }
577:
578: public void testSetTabSet() {
579: attr.removeAttributes(attr);
580: TabSet val = new TabSet(new TabStop[] { new TabStop(0.2f) });
581: StyleConstants.setTabSet(attr, val);
582: assertEquals(val, attr.getAttribute(StyleConstants.TabSet));
583: }
584:
585: public void testNameAttribute() {
586: assertEquals(StyleConstants.class, StyleConstants.NameAttribute
587: .getClass());
588: assertEquals("name", StyleConstants.NameAttribute.toString());
589: }
590:
591: public void testResolveAttribute() {
592: assertEquals(StyleConstants.class,
593: StyleConstants.ResolveAttribute.getClass());
594: assertEquals("resolver", StyleConstants.ResolveAttribute
595: .toString());
596: }
597:
598: public void testModelAttribute() {
599: assertEquals(StyleConstants.class,
600: StyleConstants.ModelAttribute.getClass());
601: assertEquals("model", StyleConstants.ModelAttribute.toString());
602: }
603:
604: public void testCharacterConstants() {
605: assertEquals(StyleConstants.ColorConstants.class,
606: StyleConstants.Background.getClass());
607: assertEquals("background", StyleConstants.Background.toString());
608: assertEquals(StyleConstants.ColorConstants.class,
609: StyleConstants.Foreground.getClass());
610: assertEquals("foreground", StyleConstants.Foreground.toString());
611: assertEquals(StyleConstants.FontConstants.class,
612: StyleConstants.Family.getClass());
613: assertEquals("family", StyleConstants.Family.toString());
614: assertEquals(StyleConstants.FontConstants.class,
615: StyleConstants.Size.getClass());
616: assertEquals("size", StyleConstants.Size.toString());
617: assertEquals(StyleConstants.FontConstants.class,
618: StyleConstants.Bold.getClass());
619: assertEquals("bold", StyleConstants.Bold.toString());
620: assertEquals(StyleConstants.FontConstants.class,
621: StyleConstants.Italic.getClass());
622: assertEquals("italic", StyleConstants.Italic.toString());
623: }
624:
625: public void testComposedTextAttribute() {
626: assertEquals(StyleConstants.class,
627: StyleConstants.ComposedTextAttribute.getClass());
628: assertEquals("composed text",
629: StyleConstants.ComposedTextAttribute.toString());
630: }
631: }
|