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 javax.print.PrintService;
023: import javax.print.PrintServiceLookup;
024: import javax.print.SimpleDoc;
025: import javax.print.attribute.standard.*;
026:
027: import junit.framework.TestCase;
028:
029: public class HashAttributeTest extends TestCase {
030:
031: public static void main(String[] args) {
032: junit.textui.TestRunner.run(HashAttributeTest.class);
033: }
034:
035: static {
036: System.out.println("HashAttribute testing...");
037: }
038:
039: private HashAttributeSet aset;
040:
041: /*
042: * HashAttributeSet() constructors testing.
043: */
044: public final void testHashAttributeSetConstructors() {
045:
046: try {
047: Attribute att = null;
048: AttributeSet aset = new HashAttributeSet(att);
049: fail("HashAttributeSet(Attribute attribute) "
050: + "doesn't throw NullPointerException "
051: + "if attribute is null");
052: } catch (NullPointerException e) {
053: }
054:
055: try {
056: Attribute att = null;
057: Attribute attrs[] = { att };
058: AttributeSet aset = new HashAttributeSet(attrs);
059: fail("HashAttributeSet(Attribute[] attributes) "
060: + "doesn't throw NullPointerException "
061: + "if attributes is null");
062: } catch (NullPointerException e) {
063: }
064:
065: AttributeSet aset1 = null;
066: AttributeSet aset2 = new HashAttributeSet(aset1);
067: }
068:
069: /*
070: * add(Attribute attribute) method testing. Tests that after adding
071: * some attribute to specified attribute set this attribute is in
072: * this attribute set.
073: */
074: public final void testAdd() {
075:
076: aset = new HashAttributeSet();
077: Attribute att1 = PrintQuality.HIGH;
078: boolean flag = aset.add(att1);
079:
080: Attribute att2 = aset.get(PrintQuality.class);
081:
082: if (flag) {
083: assertTrue("Results add() & get() is different: att1 = "
084: + att1 + ", att2 = " + att2, att1 == att2);
085: assertTrue(
086: "Attribute set doesn't contain added attribute value",
087: aset.containsValue(PrintQuality.HIGH));
088: } else {
089: fail("add() == false");
090: }
091:
092: }
093:
094: /*
095: * add(Attribute attribute) method testing. Tests if add some attribute
096: * that is already in current attributeSet than add() returns false.
097: */
098: public final void testAdd1() {
099:
100: aset = new HashAttributeSet();
101:
102: Attribute att = new Copies(10);
103: aset.add(att);
104: assertFalse(aset.add(att));
105:
106: att = Sides.DUPLEX;
107: aset.add(att);
108: assertFalse(aset.add(att));
109:
110: }
111:
112: /*
113: * add(Attribute attribute) method testing.
114: * Tests that if this attribute set does not support the add() operation
115: * UnmodifiableSetException is thrown.
116: */
117: public final void testAdd2() {
118:
119: PrintService service = PrintServiceLookup
120: .lookupDefaultPrintService();
121: if (service != null) {
122: PrintServiceAttributeSet psaset = service.getAttributes();
123: try {
124: psaset.add(ColorSupported.NOT_SUPPORTED);
125: fail("add() doesn't throw Exception when"
126: + "trying to add attribute to unmodifiable attribute set ");
127: } catch (UnmodifiableSetException e) {
128: }
129: } else {
130: System.out
131: .println("Warning: default print service wasn't found!"
132: + "\nPlease check if you have any printers connected to your"
133: + " computer");
134: }
135:
136: aset = new HashAttributeSet();
137: AttributeSet as = AttributeSetUtilities.unmodifiableView(aset);
138: try {
139: as.add(new Copies(3));
140: fail("add() doesn't throw Exception when"
141: + "trying to add attribute to unmodifiable attribute set ");
142: } catch (UnmodifiableSetException e) {
143: }
144: }
145:
146: /*
147: * addAll(AttributeSet attributeSet) method testing.
148: */
149: public final void testAddAll() {
150:
151: HashAttributeSet aset1 = new HashAttributeSet(new Attribute[] {
152: new Copies(10), Sides.DUPLEX });
153: HashAttributeSet aset2 = new HashAttributeSet();
154: aset2.addAll(aset1);
155: assertTrue(aset2.equals(aset1));
156: }
157:
158: /*
159: * addAll(AttributeSet attributeSet) method testing. Test that addAll()
160: * throws NullPointerException if attributeSet is null.
161: */
162: public final void testAddAll1() {
163:
164: HashAttributeSet aset1 = new HashAttributeSet();
165: HashAttributeSet aset2 = null;
166: try {
167: aset.addAll(aset2);
168: fail("addAll() doesn't throw NullPointerException when"
169: + "trying to add null attribute set");
170: } catch (NullPointerException e) {
171:
172: }
173: }
174:
175: /*
176: * addAll(AttributeSet attributeSet) method testing.
177: *
178: */
179: public final void testAddAll2() {
180:
181: HashPrintServiceAttributeSet aset1 = new HashPrintServiceAttributeSet();
182: HashAttributeSet aset2 = new HashAttributeSet();
183: aset2.add(new JobName("jobName", null));
184: try {
185: aset1.addAll(aset2);
186: fail("addAll() doesn't throw ClassCastException"
187: + "when specification req");
188: } catch (ClassCastException e) {
189:
190: }
191: }
192:
193: /*
194: * clear() method testing.
195: * Test that clear() remove all values from this attribute set.
196: */
197: public final void testClear() {
198:
199: aset = new HashAttributeSet();
200: aset.add(SheetCollate.COLLATED);
201: if (aset.get(SheetCollate.class) != null) {
202: aset.clear();
203: assertNull(aset.get(SheetCollate.class));
204: } else {
205: fail("add() or get() works wrong");
206: }
207:
208: aset.add(SheetCollate.COLLATED);
209: aset.add(ColorSupported.SUPPORTED);
210: if (!aset.isEmpty()) {
211: aset.clear();
212: assertNull(aset.get(SheetCollate.class));
213: assertNull(aset.get(ColorSupported.class));
214: } else {
215: fail("add() or isEmpty() works wrong");
216: }
217:
218: }
219:
220: /*
221: * containsKey(class attributeCategory) method testing. Tests if current
222: * attribute set contains value for given attribute category than
223: * containsKey() returns true.
224: */
225: public final void testContainsKey() {
226:
227: aset = new HashAttributeSet();
228: aset.add(SheetCollate.COLLATED);
229: assertTrue(aset.containsKey(SheetCollate.class));
230: }
231:
232: /*
233: * containsKey(Attribute attribute) method testing. Tests if current
234: * attribute set don't contain value for given attribute category than
235: * containsKey() returns false.
236: */
237: public final void testContainsKey1() {
238:
239: aset = new HashAttributeSet();
240: assertFalse(aset.containsKey(SheetCollate.class));
241: }
242:
243: /*
244: * containsValue(Attribute attribute) method testing. Tests if current
245: * attribute set doesn't contain value for given attribute for it's
246: * attribute than containsKey() returns false.
247: */
248: public final void testContainsValue() {
249:
250: aset = new HashAttributeSet();
251: aset.add(SheetCollate.UNCOLLATED);
252: assertFalse(aset.containsValue(SheetCollate.COLLATED));
253: }
254:
255: /*
256: * containsValue(Attribute attribute) method testing. Test if input
257: * parameter is null than containsValue() returns false.
258: */
259: public final void testContainsValue1() {
260:
261: aset = new HashAttributeSet();
262: assertFalse(aset.containsValue(null));
263: }
264:
265: /*
266: * equals(Object object) method testing.
267: */
268: public final void testEquals() {
269:
270: aset = new HashAttributeSet();
271: PrintService service = PrintServiceLookup
272: .lookupDefaultPrintService();
273: if (service != null) {
274: PrintServiceAttributeSet psaset = service.getAttributes();
275: if (aset.addAll(psaset)) {
276: assertTrue(aset.equals(psaset));
277: }
278: } else {
279: System.out
280: .println("Warning: default print service wasn't found!\n"
281: + "Please check if you have any printers connected to your "
282: + "computer.");
283: }
284:
285: JobPriority priority = new JobPriority(2);
286: PrintRequestAttributeSet set1 = new HashPrintRequestAttributeSet();
287: set1.add(priority);
288: set1.add(MediaName.ISO_A4_TRANSPARENT);
289: PrintRequestAttribute[] arr = { priority,
290: MediaName.ISO_A4_TRANSPARENT };
291: PrintRequestAttributeSet set2 = new HashPrintRequestAttributeSet(
292: arr);
293: assertTrue(set2.equals(set1));
294:
295: HashPrintRequestAttributeSet paset1 = new HashPrintRequestAttributeSet(
296: new PrintRequestAttribute[] { new Copies(3),
297: new JobName("JobName", null), MediaSizeName.A });
298: HashPrintRequestAttributeSet paset2 = new HashPrintRequestAttributeSet();
299: paset2.add(new JobName("JobName", null));
300: paset2.add(MediaSizeName.A);
301: assertFalse(paset1.equals(paset2));
302: paset2.add(new Copies(3));
303: assertTrue(paset1.equals(paset2));
304:
305: aset = new HashAttributeSet();
306: HashAttributeSet nullAset = null;
307: assertFalse(aset.equals(nullAset));
308: }
309:
310: /*
311: * equals(Object object) method testing. Tests if given object is not
312: * an attribute set equals() return false.
313: */
314: public final void testEquals1() {
315:
316: aset = new HashAttributeSet();
317: Attribute att = new Copies(1);
318: assertFalse("equals return true when given object is "
319: + "not an attribute set", aset.equals(att));
320: }
321:
322: /*
323: * get(Class attributeCategory) method testing. Tests if given object is not
324: * a class that implements interface Attribute than get() throw
325: * ClassCastException.
326: */
327: public final void testGet1() {
328:
329: aset = new HashAttributeSet();
330: try {
331: aset.get(SimpleDoc.class);
332: fail("get() doesn't throw ClassCastException when given object"
333: + "is not a Class that implements interface Attribute");
334: } catch (ClassCastException e) {
335:
336: }
337: }
338:
339: /*
340: * hashCode() method testing. Tests if two object is the same they must have
341: * same hashCode.
342: */
343: public final void testHashCode() {
344:
345: HashAttributeSet aset1 = new HashAttributeSet();
346: aset1.add(new Copies(5));
347: HashAttributeSet aset2 = new HashAttributeSet();
348: aset2.add(new Copies(5));
349: assertEquals("Two equals object have different hash codes",
350: aset1.hashCode(), aset2.hashCode());
351: }
352:
353: /*
354: * isEmpty() method testing. Tests if given attribute set is empty
355: * than isempty return true.
356: */
357: public final void testIsEmpty() {
358:
359: aset = new HashAttributeSet();
360: assertTrue(aset.isEmpty());
361: }
362:
363: /*
364: * isEmpty() method testing. Tests if given attribute set isn't empty
365: * than isempty return false.
366: */
367: public final void testIsEmpty1() {
368:
369: aset = new HashAttributeSet();
370: aset.add(new Copies(2));
371: assertFalse(aset.isEmpty());
372: }
373:
374: /*
375: * remove(Attribute attribute) method testing. Tests that if given attribute
376: * wasn't in attribute set than remove(Attribute attribute) returns false.
377: */
378: public final void testRemove() {
379:
380: aset = new HashAttributeSet();
381: aset.add(new Copies(2));
382: assertFalse(aset.remove(Sides.DUPLEX));
383: }
384:
385: /*
386: * remove(Class attributeCategory) method testing.
387: * Tests that remove(Class attributeCategory)
388: * is really remove attribute for given attribute category.
389: */
390: public final void testRemove1() {
391:
392: aset = new HashAttributeSet();
393: aset.add(Sides.DUPLEX);
394: assertTrue(aset.remove(Sides.class));
395: assertNull(aset.get(Sides.class));
396: }
397:
398: /*
399: * size() method testing. Tests that size return right values
400: * when attribute set contains no or one attribute value.
401: */
402: public final void testSize() {
403:
404: aset = new HashAttributeSet();
405: assertEquals("size() return not 0 for the empty attribute set",
406: 0, aset.size());
407: aset.add(new Copies(5));
408: assertEquals(1, aset.size());
409: }
410:
411: /*
412: * toArray() method testing. Tests that toArray() returns
413: * array of attribute with right attribute value.
414: */
415: public final void testToArray() {
416:
417: aset = new HashAttributeSet();
418: aset.add(PrintQuality.HIGH);
419: Attribute[] arr = aset.toArray();
420: assertEquals(PrintQuality.HIGH, arr[0]);
421: }
422:
423: /*
424: * toArray() method testing. Tests if given attribute set is empty toArray()
425: * returns zero length array.
426: */
427: public final void testToArray1() {
428:
429: aset = new HashAttributeSet();
430: assertEquals(0, (aset.toArray()).length);
431: }
432:
433: }
|