001: //$Id: MapCreatorAction.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 id 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.processing;
038:
039: import java.util.HashMap;
040: import java.util.List;
041: import java.util.Map;
042:
043: import junitx.ddtunit.DDTException;
044: import junitx.ddtunit.data.TypedObject;
045:
046: import org.apache.log4j.Logger;
047:
048: /**
049: * This class contains object state and other information to create object from
050: * SAX event stream.
051: *
052: * @author jg
053: */
054: public class MapCreatorAction extends ActionBase {
055: private Logger log = Logger.getLogger(MapCreatorAction.class);
056:
057: /**
058: *
059: * Constructor used as standard constructor to instanciate actions of this
060: * type
061: *
062: * @param attrMap
063: */
064: public MapCreatorAction(Map<String, String> attrMap) {
065: super (attrMap);
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see junitx.ddtunit.parser.ActionBase#process()
072: */
073: public IAction process() {
074: log.debug("process MapCreator Action - START");
075: if (!this .successorProcessed) {
076: processNoSuccessor();
077: }
078: IAction rootAction = getPrevious();
079: if (rootAction != null) {
080: String hintValue = rootAction.getHint();
081: if (HintTypes.CONSTRUCTOR.equals(hintValue)
082: || HintTypes.CALL.equals(hintValue)) {
083: log.debug("createRecord - process addition of field '"
084: + this .getId() + "'...");
085: // create attribute list action and insert after rootAction
086: Map attribMap = new HashMap();
087: IAction attribListAction = ActionFactory.getAction(
088: ActionState.ATTRLIST_CREATION, attribMap);
089: rootAction.insert(attribListAction);
090: try {
091: // create container
092: attribListAction.createObject();
093: // initialize with first list element
094: ((List) attribListAction.getValue()).add(this
095: .getObject());
096: rootAction = attribListAction;
097: } catch (Exception ex) {
098: throw new DDTException(
099: "Error on action processing", ex);
100: }
101:
102: } else if (HintTypes.FIELDS.equals(hintValue)) {
103: rootAction.processSuccessor(this );
104: } else if (HintTypes.COLLECTION.equals(hintValue)) {
105: rootAction.processSuccessor(this );
106: } else if (HintTypes.MAP.equals(hintValue)) {
107: rootAction.processSuccessor(this );
108: } else {
109: throw new UnsupportedOperationException(
110: "MapCreatorAction does not support hint '"
111: + hintValue + "'");
112: }
113: } else {
114: rootAction = this ;
115: }
116: pop();
117: return rootAction;
118: }
119:
120: public void processSuccessor(IAction successor) {
121: log.debug("processSuccessor(" + successor + ") - START");
122: if (HintTypes.CONTENT.equals(successor.getHint())) {
123: String content = successor.getValue().toString();
124: if (!ContentCreatorAction.CONTENT_NULL.equals(content)) {
125: throw new DDTException(
126: "Only \"!NULL!\" supported in Map type");
127: }
128: } else {
129: try {
130: this .createObject();
131: MapEntry entry = (MapEntry) successor.getValue();
132: if (entry == null || entry.getKey() == null
133: || entry.getValue() == null) {
134: throw new DDTException(
135: "<key> or <value> tag of map element missing");
136: }
137: ((Map<Object, Object>) this .getValue()).put(entry
138: .getKey().getValue(), entry.getValue()
139: .getValue());
140: } catch (Exception e) {
141: throw new DDTException("Error on map processing", e);
142: }
143: }
144: this .successorProcessed = true;
145: }
146:
147: public void processNoSuccessor() {
148: this .createObject();
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see junitx.ddtunit.parser.ActionBase#inject()
155: */
156: public IAction inject() {
157: String type = (String) this .attrMap
158: .get(ParserConstants.XML_ATTR_TYPE);
159: String id = (String) this .attrMap
160: .get(ParserConstants.XML_ATTR_ID);
161: // check if type is based on java.util.Collection
162: try {
163: Class clazz = Class.forName(type);
164: Class super clazz = clazz;
165:
166: boolean found = false;
167: while (!found && super clazz != null) {
168: Class[] interfaces = super clazz.getInterfaces();
169: for (int i = 0; i < interfaces.length; i++) {
170: Class iface = interfaces[i];
171: if ("java.util.Map".equals(iface.getName())) {
172: found = true;
173: break;
174: }
175: }
176: if (found) {
177: break;
178: }
179: super clazz = super clazz.getSuperclass();
180: }
181: if (!found) {
182: throw new DDTException("Container class '" + type
183: + "' does not implement java.util.Map.");
184: }
185: } catch (Exception ex) {
186: throw new DDTException("Container class '" + type
187: + "' does not implement java.util.Map.", ex);
188: }
189: this .injectedObject = new TypedObject(id, type);
190: return this;
191: }
192:
193: }
|