001: /*
002: * @(#)IParserCollatorUTestI.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.pmti.v1.itf.parser;
028:
029: import net.sourceforge.groboutils.pmti.v1.itf.*;
030: import net.sourceforge.groboutils.pmti.v1.itf.impl.*;
031:
032: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
033: import org.easymock.EasyMock;
034: import org.easymock.MockControl;
035: import net.sourceforge.groboutils.junit.v1.iftc.*;
036: import junit.framework.Test;
037: import junit.framework.TestCase;
038: import junit.framework.TestSuite;
039:
040: /**
041: * Tests the IParserCollator interface.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @since July 14, 2002
045: * @version $Date: 2003/02/10 22:52:09 $
046: */
047: public class IParserCollatorUTestI extends InterfaceTestCase {
048: //-------------------------------------------------------------------------
049: // Standard JUnit Class-specific declarations
050:
051: private static final Class THIS_CLASS = IParserCollatorUTestI.class;
052: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
053:
054: public IParserCollatorUTestI(String name, ImplFactory f) {
055: super (name, IParserCollator.class, f);
056: }
057:
058: public IParserCollator createIParserCollator() {
059: return (IParserCollator) createImplObject();
060: }
061:
062: //-------------------------------------------------------------------------
063: // Tests
064:
065: public void testAddParser1() {
066: IParserCollator pc = createIParserCollator();
067: pc.addParser(null);
068: }
069:
070: public static class MyParser implements IParser {
071: public ITestIssueRecord[] records;
072:
073: public MyParser() {
074: this .records = new ITestIssueRecord[0];
075: }
076:
077: public MyParser(ITestIssueRecord[] r) {
078: this .records = r;
079: }
080:
081: public ITestIssueRecord[] parse() {
082: return this .records;
083: }
084: }
085:
086: public void testAddParser2() {
087: IParserCollator pc = createIParserCollator();
088: pc.addParser(new MyParser());
089: pc.addParser(new MyParser());
090: }
091:
092: public void testAddParser3() {
093: IParserCollator pc = createIParserCollator();
094: pc.addParser(new MyParser());
095: pc.getRecords();
096: try {
097: pc.addParser(new MyParser());
098: fail("Did not throw IllegalStateException.");
099: } catch (IllegalStateException ise) {
100: // check exception??
101: }
102: }
103:
104: public void testGetRecords1() {
105: IParserCollator pc = createIParserCollator();
106: ITestIssueRecordSet set = pc.getRecords();
107: assertNotNull("Returned null set.", set);
108: ITestIssueRecord[] records = set.getTestIssueRecords();
109: assertNotNull("Returned null record array.", records);
110: assertEquals("Returned a non-empty record set.", 0,
111: records.length);
112: }
113:
114: public void testGetRecords2() {
115: IParserCollator pc = createIParserCollator();
116: pc.addParser(new MyParser());
117: pc.addParser(new MyParser());
118: ITestIssueRecordSet set = pc.getRecords();
119: assertNotNull("Returned null set.", set);
120: ITestIssueRecord[] records = set.getTestIssueRecords();
121: assertNotNull("Returned null record array.", records);
122: assertEquals("Returned a non-empty record set.", 0,
123: records.length);
124: }
125:
126: public void testGetRecords3() {
127: IParserCollator pc = createIParserCollator();
128: ITestIssueRecord[] real = { createRecord1(), };
129: pc.addParser(new MyParser(real));
130: ITestIssueRecordSet set = pc.getRecords();
131: assertNotNull("Returned null set.", set);
132: assertContainsOnly(set, real);
133: }
134:
135: public void testGetRecords4() {
136: IParserCollator pc = createIParserCollator();
137: ITestIssueRecord[] real = { createRecord1(), createRecord2(),
138: createRecord2(), createRecord2(), createRecord3(), };
139: pc.addParser(new MyParser(real));
140: pc.addParser(new MyParser());
141: ITestIssueRecordSet set = pc.getRecords();
142: assertNotNull("Returned null set.", set);
143: assertContainsOnly(set, real);
144: }
145:
146: //-------------------------------------------------------------------------
147: // Helpers
148:
149: /**
150: * NOTE: changes <tt>records</tt>
151: */
152: protected void assertContainsOnly(ITestIssueRecordSet set,
153: ITestIssueRecord[] records) {
154: int foundCount = 0;
155: ITestIssueRecord[] actualRecords = set.getTestIssueRecords();
156: assertNotNull(
157: "test issue record set contains null record array.",
158: actualRecords);
159: for (int arIndex = 0; arIndex < actualRecords.length; ++arIndex) {
160: assertNotNull("Returned a null test issue record in set.",
161: actualRecords[arIndex]);
162: boolean foundRecord = false;
163: for (int rIndex = 0; rIndex < records.length; ++rIndex) {
164: if (records[rIndex] != null
165: && actualRecords[arIndex] == records[rIndex]) {
166: foundRecord = true;
167: ++foundCount;
168: records[rIndex] = null;
169: break;
170: }
171: }
172: assertTrue(
173: "Found record "
174: + actualRecords[arIndex]
175: + " in result set that was not passed into the collate method.",
176: foundRecord);
177: }
178:
179: assertEquals(
180: "Did not find all the input records to the collate method in its "
181: + "results.", records.length, foundCount);
182:
183: }
184:
185: protected ITestIssueRecord createRecord1() {
186: IIssueRecord ir = new DefaultIssueRecord("", null);
187: DefaultTestRecord tr = new DefaultTestRecord();
188: ITestIssueRecord tir = new DefaultTestIssueRecord(ir, tr,
189: "text 1");
190: return tir;
191: }
192:
193: protected ITestIssueRecord createRecord2() {
194: IIssueRecord ir = new DefaultIssueRecord("", null);
195: DefaultTestRecord tr = new DefaultTestRecord();
196: tr.setTestSuite("suite");
197:
198: ITestIssueRecord tir = new DefaultTestIssueRecord(ir, tr,
199: "text 2");
200: return tir;
201: }
202:
203: protected ITestIssueRecord createRecord3() {
204: IIssueRecord ir = new DefaultIssueRecord("", null);
205: DefaultTestRecord tr = new DefaultTestRecord();
206: tr.setTestSuite("suite");
207: tr.setTestName("name");
208:
209: ITestIssueRecord tir = new DefaultTestIssueRecord(ir, tr,
210: "text 3");
211: return tir;
212: }
213:
214: //-------------------------------------------------------------------------
215: // Standard JUnit declarations
216:
217: public static InterfaceTestSuite suite() {
218: InterfaceTestSuite suite = new InterfaceTestSuite(THIS_CLASS);
219:
220: return suite;
221: }
222:
223: public static void main(String[] args) {
224: String[] name = { THIS_CLASS.getName() };
225:
226: // junit.textui.TestRunner.main( name );
227: // junit.swingui.TestRunner.main( name );
228:
229: junit.textui.TestRunner.main(name);
230: }
231:
232: /**
233: *
234: * @exception Exception thrown under any exceptional condition.
235: */
236: protected void setUp() throws Exception {
237: super .setUp();
238:
239: // set ourself up
240: }
241:
242: /**
243: *
244: * @exception Exception thrown under any exceptional condition.
245: */
246: protected void tearDown() throws Exception {
247: // tear ourself down
248:
249: super.tearDown();
250: }
251: }
|