001: /* DdMethodBehaviour.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: * www.ddsteps.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License version 2.1 as published by the Free Software Foundation.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, visit
018: * http://www.opensource.org/licenses/lgpl-license.php
019: */
020:
021: package org.ddsteps.junit.behaviour;
022:
023: import java.util.Iterator;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestResult;
027: import junit.framework.TestSuite;
028:
029: import org.ddsteps.data.DataLoader;
030: import org.ddsteps.dataset.DataRow;
031: import org.ddsteps.dataset.DataTable;
032: import org.ddsteps.util.TestCaseNameUtils;
033:
034: /**
035: * Encapsulates the behaviour of the so called "method test case instance",
036: * which is the one that spawns the row instances.
037: *
038: * @author adam
039: * @version $Id: DdMethodBehaviour.java,v 1.1 2005/08/25 08:34:21 adamskogman
040: * Exp $
041: */
042: public class DdMethodBehaviour extends DdBehaviourBase implements
043: DdBehaviour {
044:
045: /**
046: * Constructor.
047: *
048: * @param testCase
049: * The test case to load.
050: * @param methodName
051: * Name of the test method to run.
052: * @param dataLoader
053: * The data loader to use.
054: */
055: public DdMethodBehaviour(TestCase testCase, String methodName,
056: DataLoader dataLoader) {
057: super (testCase, methodName, dataLoader);
058: }
059:
060: /**
061: * Asks the table how many rows there are.
062: *
063: * @see org.ddsteps.junit.behaviour.DdBehaviour#countTestCases()
064: */
065: public int countTestCases() {
066: DataTable table = dataLoader.loadTable(testCase, methodName);
067: return table.getRowCount();
068: }
069:
070: /**
071: * Goes through the rows in the data table and spawn a new row instance for
072: * each. Calls <code>setUp</code> and <code>tearDown</code> on the
073: * calling test case.
074: *
075: * @param result
076: * Collects the results for each row.
077: * @param ddTestCase
078: * The test case calling this method.
079: * @return Always false, since this method will run row instances.
080: * @see org.ddsteps.junit.behaviour.DdBehaviour#run(TestResult,
081: * DdBehaviourCallbackHandler)
082: */
083: public boolean run(TestResult result,
084: DdBehaviourCallbackHandler ddTestCase) {
085:
086: try {
087: // This call will possibly call back to this behaviuor, to the
088: // setUp() method.
089: ddTestCase.setUp();
090:
091: // Use testCase to load, not the callback handler
092: DataTable table = dataLoader
093: .loadTable(testCase, methodName);
094:
095: for (Iterator iter = table.rowIterator(); iter.hasNext();) {
096:
097: DataRow row = (DataRow) iter.next();
098:
099: LOG.info("Processing row with id '" + row.getId()
100: + "'.");
101:
102: // Create a NEW iteration instance of this test class
103: TestCase rowInstance = createTestCaseIteration(row
104: .getId());
105:
106: // Run it. It will handle data loading by itself
107: rowInstance.run(result);
108: }
109:
110: // After all instances
111: ddTestCase.tearDown();
112:
113: } catch (Exception e) {
114: // TODO Determine if this should move
115: result.addError(testCase, e);
116: }
117:
118: // Don't call super, there is nothing to run.
119: return false;
120:
121: }
122:
123: /**
124: * Factory method for creating the iteration instances.
125: *
126: * @param rowId
127: * The row identity.
128: * @return A test case iteration.
129: */
130: protected TestCase createTestCaseIteration(String rowId) {
131:
132: String instanceName = TestCaseNameUtils.assembleName(
133: methodName, rowId);
134:
135: // Create a NEW iteration instance of this test class
136: TestCase instance = (TestCase) TestSuite.createTest(testCase
137: .getClass(), instanceName);
138:
139: return instance;
140: }
141:
142: /**
143: * Delegate back to (@link DdBehaviourCallbackHandler#setUpMethod()).
144: *
145: * @see org.ddsteps.junit.behaviour.DdBehaviour#setUp(org.ddsteps.junit.behaviour.DdBehaviourCallbackHandler)
146: */
147: public void setUp(DdBehaviourCallbackHandler ddTestCase)
148: throws Exception {
149: ddTestCase.setUpMethod();
150: }
151:
152: /**
153: * Delegate back to (@link DdBehaviourCallbackHandler#tearDownMethod()).
154: *
155: * @see org.ddsteps.junit.behaviour.DdBehaviour#tearDown(org.ddsteps.junit.behaviour.DdBehaviourCallbackHandler)
156: */
157: public void tearDown(DdBehaviourCallbackHandler ddTestCase)
158: throws Exception {
159: ddTestCase.tearDownMethod();
160: }
161: }
|