001: /*
002: * @(#)DirClassMetaDataReaderUTest.java
003: *
004: * Copyright (C) 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.codecoverage.v2.datastore;
028:
029: import java.io.IOException;
030: import java.util.Vector;
031:
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
035: import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
036: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
037: import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedClass;
038: import net.sourceforge.groboutils.junit.v1.iftc.CxFactory;
039: import net.sourceforge.groboutils.junit.v1.iftc.InterfaceTestSuite;
040:
041: /**
042: * Tests the DirClassMetaDataReader class.
043: *
044: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
045: * @version $Date: 2004/04/15 05:48:28 $
046: * @since January 22, 2003
047: */
048: public class DirClassMetaDataReaderUTest extends TestCase {
049: //-------------------------------------------------------------------------
050: // Standard JUnit Class-specific declarations
051:
052: private static final Class THIS_CLASS = DirClassMetaDataReaderUTest.class;
053: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
054:
055: public DirClassMetaDataReaderUTest(String name) {
056: super (name);
057: }
058:
059: //-------------------------------------------------------------------------
060: // Tests
061:
062: public void testConstructor1() throws Exception {
063: try {
064: new DirClassMetaDataReader(null, null);
065: fail("Did not throw IllegalArgumentException.");
066: } catch (IllegalArgumentException ex) {
067: // test exception
068: }
069: }
070:
071: public void testConstructor2() throws Exception {
072: try {
073: new DirClassMetaDataReader(createIAnalysisModule("A", "B",
074: "C"), null);
075: fail("Did not throw IllegalArgumentException.");
076: } catch (IllegalArgumentException ex) {
077: // test exception
078: }
079: }
080:
081: public void testConstructor3() throws Exception {
082: try {
083: new DirClassMetaDataReader(null,
084: createDirMetaDataIO(createIAnalysisModule("A", "B",
085: "C")));
086: fail("Did not throw IllegalArgumentException.");
087: } catch (IllegalArgumentException ex) {
088: // test exception
089: }
090: }
091:
092: public void testGetClassSignatures1() throws Exception {
093: IAnalysisModule am = createIAnalysisModule("A", "B", "C");
094: DirMetaDataIO dmdio = createDirMetaDataIO(am);
095: String sig1 = addClassRecord(dmdio, THIS_CLASS, am);
096:
097: DirClassMetaDataReader dcmdr = new DirClassMetaDataReader(am,
098: dmdio);
099: String[] sigs = dcmdr.getClassSignatures();
100: assertNotNull("Returned null signatures.", sigs);
101: assertEquals("Returned incorrect number of signatures.", 1,
102: sigs.length);
103: assertEquals("Did not return the correct sig.", sig1, sigs[0]);
104: }
105:
106: public void testReadClass1() throws Exception {
107: IAnalysisModule am = createIAnalysisModule("A", "B", "C");
108: DirMetaDataIO dmdio = createDirMetaDataIO(am);
109: String sig1 = addClassRecord(dmdio, THIS_CLASS, am);
110:
111: DirClassMetaDataReader dcmdr = new DirClassMetaDataReader(am,
112: dmdio);
113: ClassRecord cr = dcmdr.readClass(sig1);
114: assertNotNull("Returned null class record.", cr);
115: assertEquals("Returned incorrect class record.", sig1, cr
116: .getClassSignature());
117: }
118:
119: public void testClose1() throws Exception {
120: IAnalysisModule am = createIAnalysisModule("A", "B", "C");
121: DirMetaDataIO dmdio = createDirMetaDataIO(am);
122: DirClassMetaDataReader dcmdr = new DirClassMetaDataReader(am,
123: dmdio);
124:
125: dcmdr.close();
126:
127: assertFalse("Reader closed the underlying IO object.", dmdio
128: .isClosed());
129: }
130:
131: //-------------------------------------------------------------------------
132: // Helpers
133:
134: protected static IAnalysisModule createIAnalysisModule(String name,
135: String unit, String mime) {
136: return CCCreatorUtil.createIAnalysisModule(name, unit, mime);
137: }
138:
139: protected static DirMetaDataIO createDirMetaDataIO(
140: IAnalysisModule am) throws IOException {
141: DirMetaDataIO dmd = new DirMetaDataIO(CCCreatorUtil
142: .createNewDirectory());
143: dmd.putAnalysisModuleSet(new AnalysisModuleSet(
144: new IAnalysisModule[] { am }));
145: return dmd;
146: }
147:
148: protected String addClassRecord(DirMetaDataIO dmd, Class c,
149: IAnalysisModule am) throws IOException {
150: ModifiedClass mc = CCCreatorUtil.createModifiedClass(c);
151: ClassRecord cr = mc.createClassRecord(new AnalysisModuleSet(
152: new IAnalysisModule[] { am }));
153: dmd.putClassRecord(am, cr);
154: return cr.getClassSignature();
155: }
156:
157: //-------------------------------------------------------------------------
158: // Standard JUnit declarations
159:
160: public static Test suite() {
161: InterfaceTestSuite suite = IClassMetaDataReaderUTestI.suite();
162: suite.addTestSuite(THIS_CLASS);
163: suite.addFactory(new CxFactory("A") {
164: Vector v = new Vector();
165:
166: public Object createImplObject() throws IOException {
167: IAnalysisModule am = createIAnalysisModule("a", "u",
168: "text/plain");
169: DirMetaDataIO dmd = createDirMetaDataIO(am);
170: v.addElement(dmd);
171: return new DirClassMetaDataReader(am, dmd);
172: }
173: });
174: suite.addFactory(new CxFactory("B") {
175: Vector v = new Vector();
176:
177: public Object createImplObject() throws IOException {
178: IAnalysisModule am = createIAnalysisModule("a", "u",
179: "text/plain");
180: DirMetaDataReader dmdr = CCCreatorUtil
181: .createDirMetaDataReader(CCCreatorUtil
182: .createNewDirectory(), new Class[0],
183: new IAnalysisModule[] { am });
184: DirClassMetaDataReader dcmdr = (DirClassMetaDataReader) dmdr
185: .getClassReader(am);
186: // prevent the closing of the DMDR
187: v.addElement(dmdr);
188: return dcmdr;
189: }
190: });
191: suite.addFactory(new CxFactory("C") {
192: Vector v = new Vector();
193:
194: public Object createImplObject() throws IOException {
195: IAnalysisModule am = createIAnalysisModule("a", "u",
196: "text/plain");
197: DirMetaDataReader dmdr = CCCreatorUtil
198: .createDirMetaDataReader(CCCreatorUtil
199: .createNewDirectory(),
200: new Class[] { THIS_CLASS, String.class,
201: Short.class },
202: new IAnalysisModule[] { am });
203: DirClassMetaDataReader dcmdr = (DirClassMetaDataReader) dmdr
204: .getClassReader(am);
205: // prevent the closing of the DMDR
206: v.addElement(dmdr);
207: return dcmdr;
208: }
209: });
210: return suite;
211: }
212:
213: public static void main(String[] args) {
214: String[] name = { THIS_CLASS.getName() };
215:
216: // junit.textui.TestRunner.main( name );
217: // junit.swingui.TestRunner.main( name );
218:
219: junit.textui.TestRunner.main(name);
220: }
221:
222: /**
223: *
224: * @exception Exception thrown under any exceptional condition.
225: */
226: protected void setUp() throws Exception {
227: super .setUp();
228:
229: // set ourself up
230: }
231:
232: /**
233: *
234: * @exception Exception thrown under any exceptional condition.
235: */
236: protected void tearDown() throws Exception {
237: // tear ourself down
238:
239: super.tearDown();
240: }
241: }
|