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 org.apache.poi.hssf.usermodel;
019:
020: import junit.framework.TestCase;
021: import junit.framework.AssertionFailedError;
022:
023: import java.util.List;
024: import java.util.ArrayList;
025:
026: import org.apache.poi.hssf.record.*;
027:
028: /**
029: * Okay, this may seem strange but I need to test my test logic.
030: *
031: * @author Glen Stampoultzis (glens at apache.org)
032: */
033: public class TestSanityChecker extends TestCase {
034: public TestSanityChecker(String s) {
035: super (s);
036: }
037:
038: public void testCheckRecordOrder() throws Exception {
039: final SanityChecker c = new SanityChecker();
040: List records = new ArrayList();
041: records.add(new BOFRecord());
042: records.add(new InterfaceHdrRecord());
043: records.add(new BoundSheetRecord());
044: records.add(new EOFRecord());
045: final SanityChecker.CheckRecord[] check = {
046: new SanityChecker.CheckRecord(BOFRecord.class, '1'),
047: new SanityChecker.CheckRecord(InterfaceHdrRecord.class,
048: '0'),
049: new SanityChecker.CheckRecord(BoundSheetRecord.class,
050: 'M'),
051: new SanityChecker.CheckRecord(NameRecord.class, '*'),
052: new SanityChecker.CheckRecord(EOFRecord.class, '1'), };
053: // check pass
054: c.checkRecordOrder(records, check);
055: records.add(2, new BoundSheetRecord());
056: c.checkRecordOrder(records, check);
057: records.remove(1); // optional record missing
058: c.checkRecordOrder(records, check);
059: records.add(3, new NameRecord());
060: records.add(3, new NameRecord()); // optional multiple record occurs more than one time
061: c.checkRecordOrder(records, check);
062:
063: // check fail
064: expectFail(new Runnable() {
065: public void run() {
066: // check optional in wrong spot
067: List records = new ArrayList();
068: records.add(new BOFRecord());
069: records.add(new BoundSheetRecord());
070: records.add(new InterfaceHdrRecord());
071: records.add(new EOFRecord());
072: c.checkRecordOrder(records, check);
073: }
074: });
075:
076: expectFail(new Runnable() {
077: public void run() {
078: // check optional one off occurs more than once
079: List records = new ArrayList();
080: records.add(new BOFRecord());
081: records.add(new InterfaceHdrRecord());
082: records.add(new BoundSheetRecord());
083: records.add(new InterfaceHdrRecord());
084: records.add(new EOFRecord());
085: c.checkRecordOrder(records, check);
086: }
087: });
088:
089: expectFail(new Runnable() {
090: public void run() {
091: // check many scattered
092: List records = new ArrayList();
093: records.add(new BOFRecord());
094: records.add(new BoundSheetRecord());
095: records.add(new NameRecord());
096: records.add(new EOFRecord());
097: records.add(new NameRecord());
098: c.checkRecordOrder(records, check);
099: }
100: });
101:
102: expectFail(new Runnable() {
103: public void run() {
104: // check missing manditory
105: List records = new ArrayList();
106: records.add(new InterfaceHdrRecord());
107: records.add(new BoundSheetRecord());
108: records.add(new EOFRecord());
109: c.checkRecordOrder(records, check);
110: }
111: });
112:
113: expectFail(new Runnable() {
114: public void run() {
115: // check missing 1..many
116: List records = new ArrayList();
117: records.add(new BOFRecord());
118: records.add(new InterfaceHdrRecord());
119: records.add(new EOFRecord());
120: c.checkRecordOrder(records, check);
121: }
122: });
123:
124: expectFail(new Runnable() {
125: public void run() {
126: // check wrong order
127: List records = new ArrayList();
128: records.add(new InterfaceHdrRecord());
129: records.add(new BoundSheetRecord());
130: records.add(new BOFRecord());
131: records.add(new EOFRecord());
132: c.checkRecordOrder(records, check);
133: }
134: });
135:
136: expectFail(new Runnable() {
137: public void run() {
138: // check optional record in wrong order
139: List records = new ArrayList();
140: records.add(new BOFRecord());
141: records.add(new BoundSheetRecord());
142: records.add(new InterfaceHdrRecord());
143: records.add(new EOFRecord());
144: c.checkRecordOrder(records, check);
145: }
146: });
147:
148: }
149:
150: private void expectFail(Runnable runnable) {
151: boolean fail = false;
152: try {
153: runnable.run();
154: fail = true;
155: } catch (AssertionFailedError pass) {
156: }
157: assertTrue(!fail);
158: }
159:
160: }
|