001: /* StaticBehaviourFactory.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.io.IOException;
024: import java.util.Properties;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.ddsteps.data.DataLoader;
031: import org.ddsteps.dataset.DataTable;
032: import org.ddsteps.util.TestCaseInfo;
033: import org.ddsteps.util.TestCaseNameUtils;
034: import org.springframework.core.io.ClassPathResource;
035: import org.springframework.core.io.support.PropertiesLoaderSupport;
036:
037: /**
038: * @author adam
039: * @version $Id: StaticBehaviourFactory.java,v 1.1 2006/01/29 21:25:39
040: * adamskogman Exp $
041: */
042: public class StaticBehaviourFactory extends PropertiesLoaderSupport {
043:
044: /**
045: * Logger.
046: */
047: protected static final Log LOG = LogFactory
048: .getLog(StaticBehaviourFactory.class);
049:
050: /**
051: * Singleton: Instance of this class.
052: */
053: protected static StaticBehaviourFactory instance;
054:
055: /**
056: * Field: The loaded properties
057: */
058: protected Properties properties;
059:
060: /**
061: * Singleton getter.
062: *
063: * @return Singleton instance.
064: */
065: public synchronized static StaticBehaviourFactory getInstance() {
066:
067: if (instance == null) {
068: instance = new StaticBehaviourFactory();
069: instance.afterPropertiesSet();
070: }
071:
072: return instance;
073: }
074:
075: /**
076: * Call this method to get the appropriate behaviour for your testcase.
077: *
078: * @param testCase
079: * Your testcase, typically 'this'.
080: * @param dataLoader
081: * A dataLoader.
082: * @return The behaviour, not null.
083: */
084: public static DdBehaviour getBehaviour(TestCase testCase,
085: DataLoader dataLoader) {
086: return getInstance().createBehaviour(testCase, dataLoader);
087: }
088:
089: /**
090: *
091: */
092: public StaticBehaviourFactory() {
093: super ();
094: // TODO Refactor this to the DataLoader or into A dataloader
095: setLocation(new ClassPathResource(
096: "/ddsteps-defaults.properties"));
097: setIgnoreResourceNotFound(false);
098:
099: }
100:
101: /**
102: * Loads the global properties.
103: *
104: * @see org.ddsteps.properties.SystemPropertiesLoaderSupport#afterPropertiesSet()
105: */
106: public void afterPropertiesSet() {
107: try {
108: properties = mergeProperties();
109: } catch (IOException e) {
110: LOG
111: .info("No ddsteps-defaults.properties (default values for all datadriven tests)"
112: + " in the default package (root of the classpath).");
113: }
114: }
115:
116: /**
117: * Actual implementation.
118: *
119: * @param testCase
120: * @param dataLoader
121: * @return A behaviour, never null.
122: */
123: public DdBehaviour createBehaviour(TestCase testCase,
124: DataLoader dataLoader) {
125:
126: TestCaseInfo info = TestCaseNameUtils
127: .parseTestCaseName(testCase.getName());
128:
129: if (info.isMethodInstance()) {
130: // It's a method instance, so it may or may not be data driven
131:
132: // Try loading the data for this method
133: // TODO Improve by adding a hasTable method - save memory!
134: DataTable table = dataLoader.loadTable(testCase, info
135: .getMethodName());
136:
137: if (table != null) {
138: // Data Driven!
139: return new DdMethodBehaviour(testCase, info
140: .getMethodName(), dataLoader);
141: } else {
142: // Not Data Driven
143: // TODO Singleton
144: return new JUnitMethodBehaviour();
145: }
146:
147: } else {
148:
149: // DataDriven Method
150: // Let it load for itself
151: DdRowBehaviour rowBehaviour = new DdRowBehaviour(testCase,
152: info.getMethodName(), info.getRowId(), dataLoader);
153: rowBehaviour.setProperties(properties);
154: return rowBehaviour;
155:
156: }
157:
158: }
159:
160: }
|