001: //$Id: DDTDataRepository.java 282 2007-07-19 22:46:27Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data;
038:
039: import java.util.Map;
040: import java.util.Set;
041:
042: import junitx.ddtunit.data.db.DbDataRestorer;
043: import junitx.ddtunit.util.DDTConfiguration;
044: import junitx.ddtunit.util.SoftHashMap;
045:
046: import org.apache.log4j.Logger;
047:
048: /**
049: * Data repository containing test data per test class. <br/>Implementation as
050: * singleton.
051: *
052: * @author jg
053: */
054: public class DDTDataRepository extends DataSet {
055:
056: private static DDTDataRepository repository;
057:
058: private Map clusterDataSets;
059:
060: private Logger log = Logger.getLogger(DDTDataRepository.class);
061:
062: /**
063: * Default constructor
064: *
065: * @param hardCacheSize
066: * defines number of elements that can not be deleted by garbadge
067: * collection
068: */
069: private DDTDataRepository(int hardCacheSize) {
070: super ("DDTUnitDataRepository", null);
071: this .clusterDataSets = new SoftHashMap(hardCacheSize);
072: }
073:
074: /**
075: * @return DDTDataRepository as singleton
076: */
077: public static DDTDataRepository getInstance() {
078: if (repository == null) {
079: repository = new DDTDataRepository(DDTConfiguration
080: .getInstance().getHardCacheSize());
081: }
082: return repository;
083: }
084:
085: /**
086: * Check existance of testdata concerning provided class data.
087: *
088: * @param classId
089: * of class data used for test
090: * @return true if data is found
091: */
092: public boolean containsKey(String classId) {
093: boolean contains = false;
094: contains = this .clusterDataSets.containsKey(classId);
095: log.debug("containsKey(" + classId + ")=" + contains);
096: return contains;
097: }
098:
099: /**
100: * Add {@link TestClusterDataSet}to repository.
101: *
102: * @param classId
103: * @param dataSet
104: */
105: public void put(String classId, IDataSet dataSet) {
106: this .clusterDataSets.put(classId, dataSet);
107: log.debug("put(" + classId + ") - size " + this .size());
108: }
109:
110: /**
111: * Get TestClusterDataSet associated to the specified method of a class.
112: *
113: * @param resource
114: * name of resource of test class data
115: * @param clusterId
116: * to identify dataSet used unter test
117: * @return DataSet to process
118: */
119: public TestClusterDataSet get(String resource, String clusterId) {
120: if (!containsKey(clusterId)) {
121: restore(resource, clusterId);
122: }
123: return (TestClusterDataSet) this .clusterDataSets.get(clusterId);
124: }
125:
126: /**
127: * Restore test object definition from provided xml resource
128: *
129: * @param resource
130: * name of xml file
131: * @param clusterId
132: * specifiing the testdata cluster to process from within xml
133: * resource
134: */
135: public IDataSet restore(String resource, String clusterId) {
136: try {
137: // check for special key in resource - db:
138: IDataSet dataSet = null;
139: if (resource.startsWith("/db:")) {
140: IDataSetSerializer dbRetriever = new DbDataRestorer();
141: dataSet = dbRetriever.restore(resource, clusterId);
142: } else {
143: IDataSetSerializer xmlRetriever = new XmlDataRestorer(
144: this );
145: dataSet = xmlRetriever.restore(resource, clusterId);
146: }
147: put(clusterId, dataSet);
148: log.debug("restore(" + resource + ", " + clusterId + ")");
149: return dataSet;
150: } catch (DDTTestDataException ex) {
151: throw ex;
152: } catch (Throwable ex) {
153: throw new DDTTestDataException(ex.getMessage(), ex);
154: }
155: }
156:
157: /**
158: * @return number of entries in repository
159: */
160: public int size() {
161: log.debug("size = " + this .clusterDataSets.size());
162: return this .clusterDataSets.size();
163: }
164:
165: public Set entrySet() {
166: return this.clusterDataSets.entrySet();
167: }
168: }
|