KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > ChainableIOExceptionTest


1 /*
2  * ChainableIOExceptionTest.java
3  * JUnit based test
4  *
5  * Created on 31. Januar 2006, 14:37
6  */

7
8 package de.schlichtherle.io;
9
10 import de.schlichtherle.io.ArchiveBusyException;
11 import de.schlichtherle.io.ArchiveException;
12 import de.schlichtherle.io.ArchiveWarningException;
13
14 import junit.framework.*;
15
16 /**
17  *
18  * @author Christian Schlichtherle
19  */

20 public class ChainableIOExceptionTest extends TestCase {
21     
22     public ChainableIOExceptionTest(String JavaDoc testName) {
23         super(testName);
24     }
25
26     protected void setUp() throws Exception JavaDoc {
27     }
28
29     protected void tearDown() throws Exception JavaDoc {
30     }
31
32     public static Test suite() {
33         TestSuite suite = new TestSuite(ChainableIOExceptionTest.class);
34         
35         return suite;
36     }
37
38     public void testSorting() {
39         ArchiveException exc = null;
40         final int max = 9;
41         assertEquals(0, max % 3);
42         final ChainableIOException[] appearance = new ArchiveException[max];
43         final ChainableIOException[] revAppearance = new ArchiveException[max];
44         final ChainableIOException[] priority = new ArchiveException[max];
45         for (int i = 0; i < max; i++) {
46             File dummy = (File) new File("" + i + 1).getAbsoluteFile();
47             switch (i % 3) {
48             case 0:
49                 exc = new ArchiveException(exc, dummy.getPath());
50                 break;
51             
52             case 1:
53                 exc = new ArchiveWarningException(exc, dummy.getPath());
54                 break;
55
56             case 2:
57                 exc = new ArchiveBusyException(exc, dummy.getPath());
58                 break;
59             }
60             appearance[i]
61                     = revAppearance[max - 1 - i]
62                     = priority[(i % 3) * (max / 3) + (2 - i / 3)]
63                     = exc;
64         }
65
66         final int maxAppearance = exc.maxAppearance;
67         final Check appearanceCheck = new Check() {
68             public boolean equals(ChainableIOException e1, ChainableIOException e2) {
69                 //return ArchiveException.APPEARANCE_COMP.compare(e1, e2) == 0;
70
return e1 == e2;
71             }
72         };
73         testChain(appearanceCheck, revAppearance, exc);
74
75         final ChainableIOException appearanceExc = exc.sortAppearance();
76         assertEquals(maxAppearance, appearanceExc.maxAppearance);
77         testChain(appearanceCheck, revAppearance, appearanceExc);
78
79         final Check priorityCheck = new Check() {
80             public boolean equals(ChainableIOException e1, ChainableIOException e2) {
81                 return ChainableIOException.PRIORITY_COMP.compare(e1, e2) == 0;
82             }
83         };
84         final ChainableIOException priorityExc = exc.sortPriority();
85         assertNotSame(exc, priorityExc);
86         assertEquals(maxAppearance, priorityExc.maxAppearance);
87         testChain(priorityCheck, priority, priorityExc);
88     }
89
90     private void testChain(
91             final Check c,
92             final ChainableIOException[] expected,
93             ChainableIOException exc) {
94         assertNotNull(c);
95         for (int i = 0; i < expected.length; i++) {
96             final ChainableIOException exp = expected[i];
97             assertNotNull(exp);
98             assertNotNull(exc);
99             assertTrue(c.equals(exp, exc));
100             exc = exc.getPrior();
101         }
102         assertNull(exc);
103     }
104
105     private static interface Check {
106         boolean equals(ChainableIOException e1, ChainableIOException e2);
107     }
108 }
109
Popular Tags