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: package java.awt;
019:
020: import java.awt.font.FontRenderContext;
021: import java.awt.font.TextAttribute;
022: import java.text.AttributedString;
023: import java.text.CharacterIterator;
024: import java.text.StringCharacterIterator;
025: import java.text.AttributedCharacterIterator.Attribute;
026: import java.util.Collections;
027:
028: import junit.framework.TestCase;
029:
030: public class FontTest extends TestCase {
031:
032: private final Font f = new Font("dialog", Font.PLAIN, 12);
033:
034: /**
035: * Checks Font.getLineMetrics() methods if FontRenderContext parameter is
036: * NULL.
037: *
038: */
039: public void test_Font_getLineMetrics_WithNullFRC() {
040: // Regression for Harmony-1465
041: final String str = "test";
042: try {
043: f.getLineMetrics(str, null);
044: fail("NullPointerException expected but wasn't thrown!");
045: } catch (NullPointerException e) {
046: // as expected
047: }
048:
049: try {
050: f.getLineMetrics(str, 1, 3, null);
051: fail("NullPointerException expected but wasn't thrown!");
052: } catch (NullPointerException e) {
053: // as expected
054: }
055:
056: try {
057: f.getLineMetrics(str.toCharArray(), 1, 3, null);
058: fail("NullPointerException expected but wasn't thrown!");
059: } catch (NullPointerException e) {
060: // as expected
061: }
062:
063: try {
064: AttributedString as = new AttributedString("test");
065: as.addAttribute(TextAttribute.FONT, f, 0, 2);
066:
067: f.getLineMetrics(as.getIterator(), 1, 3, null);
068: fail("NullPointerException expected but wasn't thrown!");
069: } catch (NullPointerException e) {
070: // as expected
071: }
072: }
073:
074: public void test_Font_getMaxCharBounds_WithNullFRC() {
075: // Regression for HARMONY-1549
076: try {
077: Font font = Font.decode("dialog");
078: System.out.println(font.getMaxCharBounds(null));
079: fail("NullPointerException expected!");
080: } catch (NullPointerException e) {
081: // expected
082: }
083:
084: }
085:
086: public void test_Font_getFamily_WithNullLocale() {
087: // Regression for Harmony-1543
088: try {
089: Font fnt = Font.getFont(Collections
090: .<Attribute, Object> emptyMap());
091: fnt.getFamily(null);
092: fail("NullPointerException expected!");
093: } catch (NullPointerException e) {
094: // expected
095: }
096: }
097:
098: public void test_Font_getFont_WithNullSystemProperty() {
099: // Regression for HARMONY-1546
100: try {
101: Font.getFont((String) null);
102: fail("NullPointerException expected!");
103: } catch (NullPointerException e) {
104: // expected
105: }
106:
107: try {
108: Font.getFont((String) null, new Font("dialog", Font.PLAIN,
109: 12));
110: fail("NullPointerException expected!");
111: } catch (NullPointerException e) {
112: // expected
113: }
114: }
115:
116: /*
117: * Compatibility test. We check that Harmony throws same
118: * exceptions as RI does.
119: */
120: public void test_Font_getStringBounds_WithNullTextSource() {
121: // regression test for Harmony-1591
122: Font font = new Font("dialog", Font.PLAIN, 12);
123: FontRenderContext cnt = new FontRenderContext(null, false, true);
124: try {
125: font.getStringBounds((char[]) null, -16, 1, cnt);
126: } catch (IndexOutOfBoundsException e) {
127: // expected
128: }
129:
130: try {
131: font.getStringBounds((String) null, -16, 1, cnt);
132: } catch (NullPointerException e) {
133: // expected
134: }
135:
136: try {
137: font.getStringBounds((CharacterIterator) null, -16, 1, cnt);
138: } catch (NullPointerException e) {
139: // expected
140: }
141:
142: try {
143: font.getStringBounds((String) null, cnt);
144: } catch (NullPointerException e) {
145: // expected
146: }
147: }
148:
149: /**
150: * Checks Font.getStringBounds() methods if FontRenderContext parameter is NULL.
151: *
152: */
153: public void test_Font_getStringBounds_WithNullFRC() {
154: // regression test for Harmony-1595
155: Font font = Font.decode("Arial");
156: try {
157: font.getStringBounds(new char[] { 'a' }, 0, 1,
158: (FontRenderContext) null);
159: fail("NullPointerException expected!");
160: } catch (NullPointerException e) {
161: // expected
162: }
163:
164: try {
165: font.getStringBounds(new StringCharacterIterator("a"), 0,
166: 1, (FontRenderContext) null);
167: fail("NullPointerException expected!");
168: } catch (NullPointerException e) {
169: // expected
170: }
171:
172: String str = "str";
173: try {
174: font.getStringBounds(str, 0, 1, (FontRenderContext) null);
175: fail("NullPointerException expected!");
176: } catch (NullPointerException e) {
177: // expected
178: }
179:
180: try {
181: font.getStringBounds(str, (FontRenderContext) null);
182: fail("NullPointerException expected!");
183: } catch (NullPointerException e) {
184: // expected
185: }
186: }
187:
188: }
|