KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestSanityChecker


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hssf.usermodel;
19
20 import junit.framework.TestCase;
21 import junit.framework.AssertionFailedError;
22
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import org.apache.poi.hssf.record.*;
27
28 /**
29  * Okay, this may seem strange but I need to test my test logic.
30  *
31  * @author Glen Stampoultzis (glens at apache.org)
32  */

33 public class TestSanityChecker
34         extends TestCase
35 {
36     public TestSanityChecker( String JavaDoc s )
37     {
38         super( s );
39     }
40
41     public void testCheckRecordOrder()
42             throws Exception JavaDoc
43     {
44         final SanityChecker c = new SanityChecker();
45         List JavaDoc records = new ArrayList JavaDoc();
46         records.add(new BOFRecord());
47         records.add(new InterfaceHdrRecord());
48         records.add(new BoundSheetRecord());
49         records.add(new EOFRecord());
50         final SanityChecker.CheckRecord[] check = {
51             new SanityChecker.CheckRecord(BOFRecord.class, '1'),
52             new SanityChecker.CheckRecord(InterfaceHdrRecord.class, '0'),
53             new SanityChecker.CheckRecord(BoundSheetRecord.class, 'M'),
54             new SanityChecker.CheckRecord(NameRecord.class, '*'),
55             new SanityChecker.CheckRecord(EOFRecord.class, '1'),
56         };
57         // check pass
58
c.checkRecordOrder(records, check);
59         records.add(2, new BoundSheetRecord());
60         c.checkRecordOrder(records, check);
61         records.remove(1); // optional record missing
62
c.checkRecordOrder(records, check);
63         records.add(3, new NameRecord());
64         records.add(3, new NameRecord()); // optional multiple record occurs more than one time
65
c.checkRecordOrder(records, check);
66
67         // check fail
68
expectFail( new Runnable JavaDoc() {
69             public void run()
70             {
71                 // check optional in wrong spot
72
List JavaDoc records = new ArrayList JavaDoc();
73                 records.add(new BOFRecord());
74                 records.add(new BoundSheetRecord());
75                 records.add(new InterfaceHdrRecord());
76                 records.add(new EOFRecord());
77                 c.checkRecordOrder(records, check);
78             }
79         });
80
81         expectFail( new Runnable JavaDoc() {
82             public void run()
83             {
84                 // check optional one off occurs more than once
85
List JavaDoc records = new ArrayList JavaDoc();
86                 records.add(new BOFRecord());
87                 records.add(new InterfaceHdrRecord());
88                 records.add(new BoundSheetRecord());
89                 records.add(new InterfaceHdrRecord());
90                 records.add(new EOFRecord());
91                 c.checkRecordOrder(records, check);
92             }
93         });
94
95         expectFail( new Runnable JavaDoc() {
96             public void run()
97             {
98                 // check many scattered
99
List JavaDoc records = new ArrayList JavaDoc();
100                 records.add(new BOFRecord());
101                 records.add(new BoundSheetRecord());
102                 records.add(new NameRecord());
103                 records.add(new EOFRecord());
104                 records.add(new NameRecord());
105                 c.checkRecordOrder(records, check);
106             }
107         });
108
109         expectFail( new Runnable JavaDoc() {
110             public void run()
111             {
112                 // check missing manditory
113
List JavaDoc records = new ArrayList JavaDoc();
114                 records.add(new InterfaceHdrRecord());
115                 records.add(new BoundSheetRecord());
116                 records.add(new EOFRecord());
117                 c.checkRecordOrder(records, check);
118             }
119         });
120
121         expectFail( new Runnable JavaDoc() {
122             public void run()
123             {
124                 // check missing 1..many
125
List JavaDoc records = new ArrayList JavaDoc();
126                 records.add(new BOFRecord());
127                 records.add(new InterfaceHdrRecord());
128                 records.add(new EOFRecord());
129                 c.checkRecordOrder(records, check);
130             }
131         });
132
133         expectFail( new Runnable JavaDoc() {
134             public void run()
135             {
136                 // check wrong order
137
List JavaDoc records = new ArrayList JavaDoc();
138                 records.add(new InterfaceHdrRecord());
139                 records.add(new BoundSheetRecord());
140                 records.add(new BOFRecord());
141                 records.add(new EOFRecord());
142                 c.checkRecordOrder(records, check);
143             }
144         });
145
146         expectFail( new Runnable JavaDoc() {
147             public void run()
148             {
149                 // check optional record in wrong order
150
List JavaDoc records = new ArrayList JavaDoc();
151                 records.add(new BOFRecord());
152                 records.add(new BoundSheetRecord());
153                 records.add(new InterfaceHdrRecord());
154                 records.add(new EOFRecord());
155                 c.checkRecordOrder(records, check);
156             }
157         });
158
159     }
160
161     private void expectFail( Runnable JavaDoc runnable )
162     {
163         boolean fail = false;
164         try
165         {
166             runnable.run();
167             fail = true;
168         }
169         catch (AssertionFailedError pass)
170         {
171         }
172         assertTrue(!fail);
173     }
174
175 }
176
177
Popular Tags