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 Elena V. Sayapina
019: * @version $Revision: 1.3 $
020: */package javax.print.attribute;
021:
022: import junit.framework.TestCase;
023:
024: public class Size2DSyntaxTest extends TestCase {
025:
026: public static void main(String[] args) {
027: junit.textui.TestRunner.run(Size2DSyntaxTest.class);
028: }
029:
030: static {
031: System.out.println("Size2DSyntax testing...");
032: }
033:
034: size2DSyntax ss1, ss2;
035:
036: /*
037: * Size2DSyntax(float x, float y, int units) constructor testing.
038: */
039: public final void testSize2DSyntax() {
040:
041: ss1 = new size2DSyntax(0.1f, 22.22f, Size2DSyntax.MM);
042: assertTrue(100 == ss1.getXMicrometersEx());
043: assertTrue(22220 == ss1.getYMicrometersEx());
044:
045: try {
046: ss1 = new size2DSyntax(-0.01f, 700.45f, Size2DSyntax.MM);
047: fail("Size2DSyntax(float x, float y, int units) "
048: + "doesn't throw IllegalArgumentException x<0");
049: } catch (IllegalArgumentException e) {
050: }
051:
052: try {
053: ss1 = new size2DSyntax(0.1f, -700.45f, Size2DSyntax.MM);
054: fail("Size2DSyntax(float x, float y, int units) "
055: + "doesn't throw IllegalArgumentException y<0");
056: } catch (IllegalArgumentException e) {
057: }
058:
059: try {
060: ss1 = new size2DSyntax(0.1f, 0.1f, 0);
061: fail("Size2DSyntax(float x, float y, int units) "
062: + "doesn't throw IllegalArgumentException units<1");
063: } catch (IllegalArgumentException e) {
064: }
065:
066: }
067:
068: /*
069: * Size2DSyntax(int x, int y, int units) constructor testing.
070: */
071: public final void testSize2DSyntax1() {
072:
073: ss1 = new size2DSyntax(1, 10, Size2DSyntax.INCH);
074: assertTrue(25400 == ss1.getXMicrometersEx());
075: assertTrue(254000 == ss1.getYMicrometersEx());
076:
077: try {
078: ss1 = new size2DSyntax(-2, 1, Size2DSyntax.MM);
079: fail("Size2DSyntax(float x, float y, int units) "
080: + "doesn't throw IllegalArgumentException x<0");
081: } catch (IllegalArgumentException e) {
082: }
083:
084: try {
085: ss1 = new size2DSyntax(1, -1, Size2DSyntax.MM);
086: fail("Size2DSyntax(float x, float y, int units) "
087: + "doesn't throw IllegalArgumentException y<0");
088: } catch (IllegalArgumentException e) {
089: }
090:
091: try {
092: ss1 = new size2DSyntax(1, 1, -26);
093: fail("Size2DSyntax(float x, float y, int units) "
094: + "doesn't throw IllegalArgumentException units<1");
095: } catch (IllegalArgumentException e) {
096: }
097: }
098:
099: /*
100: * hashCode() method testing.
101: */
102: public final void testHashCode() {
103:
104: ss1 = new size2DSyntax(10, 10, 2);
105: ss2 = new size2DSyntax(1, 1, 20);
106: assertEquals(ss1.hashCode(), ss2.hashCode());
107:
108: ss1 = new size2DSyntax(10, 10, 2);
109: ss2 = new size2DSyntax(10, 10, 20);
110: assertFalse(ss1.hashCode() == ss2.hashCode());
111:
112: }
113:
114: /*
115: * equals(Object object) method testing.
116: */
117: public final void testEqualsObject() {
118: ss1 = new size2DSyntax(10, 10, 2);
119: ss2 = new size2DSyntax(1, 1, 20);
120: assertTrue(ss1.equals(ss2));
121:
122: ss1 = new size2DSyntax(10, 10, 2);
123: ss2 = new size2DSyntax(10, 10, 20);
124: assertFalse(ss1.equals(ss2));
125:
126: ss2 = null;
127: assertFalse(ss1.equals(ss2));
128: }
129:
130: /*
131: * getSize(int units) method testing.
132: */
133: public final void testGetSize() {
134: ss1 = new size2DSyntax(10.2f, 100f, 2);
135: float[] size = ss1.getSize(2);
136: assertEquals(10.2, size[0], 1);
137: assertEquals(100, size[1], 1);
138: try {
139: ss1.getSize(0);
140: fail("getSize(int units) "
141: + "doesn't throw IllegalArgumentException units<1");
142: } catch (IllegalArgumentException e) {
143: }
144: }
145:
146: /*
147: * getX(int units) method testing.
148: */
149: public final void testGetX() {
150: ss1 = new size2DSyntax(500.f, 800f, 1);
151: assertEquals(62.5f, ss1.getX(8), 0.0001);
152:
153: ss1 = new size2DSyntax(105f, 800f, 10);
154: assertEquals(5.25f, ss1.getX(200), 0.0001);
155:
156: try {
157: ss1.getX(0);
158: fail("getX(int units) "
159: + "doesn't throw IllegalArgumentException units<1");
160: } catch (IllegalArgumentException e) {
161: }
162: }
163:
164: /*
165: * getY(int units) method testing.
166: */
167: public final void testGetY() {
168: ss1 = new size2DSyntax(500, 700, 1);
169: assertEquals(17.5, ss1.getY(40), 0.0001);
170:
171: ss1 = new size2DSyntax(500, 700, 10);
172: assertEquals(583.3333, ss1.getY(12), 0.0001);
173:
174: try {
175: ss1.getY(-44);
176: fail("getY(int units) "
177: + "doesn't throw IllegalArgumentException units<1");
178: } catch (IllegalArgumentException e) {
179: }
180: }
181:
182: /*
183: * getXMicrometers() method testing.
184: */
185: public final void testGetXMicrometers() {
186: ss1 = new size2DSyntax(500.3f, 700.45f, 1);
187: assertEquals(500, ss1.getXMicrometersEx());
188:
189: ss1 = new size2DSyntax(500.08f, 700.5f, 10);
190: assertEquals(5001, ss1.getXMicrometersEx());
191: }
192:
193: /*
194: * getYMicrometers() method testing.
195: */
196: public final void testGetYMicrometers() {
197: ss1 = new size2DSyntax(500.3f, 700.45f, 1);
198: assertEquals(700, ss1.getYMicrometersEx());
199:
200: ss1 = new size2DSyntax(500.3f, 700.07f, 10);
201: assertEquals(7001, ss1.getYMicrometersEx());
202: }
203:
204: /*
205: * toString(int units, String unitsName) method testing.
206: */
207: public final void testToStringintString() {
208: size2DSyntax s = new size2DSyntax(500f, 50f, 10);
209: //System.out.println(s.toString(20, "mm"));
210: try {
211: s.toString(-1, "");
212: fail("toString(int units, String unitsName) "
213: + "doesn't throw IllegalArgumentException units<1");
214: } catch (IllegalArgumentException e) {
215: }
216: }
217:
218: /*
219: * Auxiliary class
220: */
221: public class size2DSyntax extends Size2DSyntax {
222:
223: public size2DSyntax(float a, float b, int c) {
224: super (a, b, c);
225: }
226:
227: public size2DSyntax(int a, int b, int c) {
228: super (a, b, c);
229: }
230:
231: protected int getXMicrometersEx() {
232: return getXMicrometers();
233: }
234:
235: protected int getYMicrometersEx() {
236: return getYMicrometers();
237: }
238: }
239:
240: }
|