View Javadoc

1   //$Id: ConstantCreatorAction.java 282 2007-07-19 22:46:27Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the id of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   ********************************************************************************/
38  package junitx.ddtunit.data.processing;
39  
40  import java.lang.reflect.Field;
41  import java.lang.reflect.Modifier;
42  import java.util.Map;
43  
44  import junitx.ddtunit.DDTException;
45  import junitx.ddtunit.data.DDTTestDataException;
46  import junitx.ddtunit.data.TypedObject;
47  import junitx.ddtunit.util.ClassAnalyser;
48  
49  /**
50   * This class contains object state and other information to create object from
51   * SAX event stream.
52   * 
53   * @author jg
54   */
55  public class ConstantCreatorAction extends ActionBase {
56  	/**
57  	 * 
58  	 * Constructor used as standard constructor to instanciate actions of this
59  	 * type
60  	 * 
61  	 * @param attrMap
62  	 */
63  	public ConstantCreatorAction(Map<String, String> attrMap) {
64  		super(attrMap);
65  	}
66  
67  	/*
68  	 * (non-Javadoc)
69  	 * 
70  	 * @see junitx.ddtunit.parser.ActionBase#process()
71  	 */
72  	public IAction process() {
73  		log.debug("process ConstantCreator - START");
74  		IAction rootAction = this.getPrevious();
75  		if (rootAction != null) {
76  			String hintValue = rootAction.getHint();
77  
78  			if (HintTypes.COLLECTION.equals(hintValue)
79  					|| HintTypes.MAP.equals(hintValue)
80  					|| HintTypes.ATTRLIST.equals(hintValue)
81  					|| HintTypes.FIELDS.equals(hintValue)
82  					|| HintTypes.CONSTRUCTOR.equals(hintValue)
83  					|| HintTypes.CALL.equals(hintValue)
84  					|| HintTypes.BEAN.equals(hintValue)
85  					|| HintTypes.ARRAY.equals(hintValue)
86  					|| HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
87  				rootAction.processSuccessor(this);
88  			} else {
89  				throw new DDTException("Unknown hint (" + hintValue
90  						+ ")- stop processing.");
91  			}
92  		} else {
93  			if (hasReferenceInfo()) {
94  				TypedObject destObject;
95  				if (this.attrMap.get(ParserConstants.XML_ATTR_TYPE) == null) {
96  					destObject = new TypedObject(getAttribute("refid"));
97  				} else {
98  					destObject = new TypedObject(getAttribute("refid"),
99  							getType());
100 				}
101 				IReferenceInfo refInfo = new ObjectReferenceInfo(this
102 						.getObject(), destObject);
103 				add(refInfo);
104 			}
105 			rootAction = this;
106 		}
107 		this.pop();
108 		return this;
109 	}
110 
111 	/*
112 	 * (non-Javadoc)
113 	 * 
114 	 * @see junitx.ddtunit.parser.ActionBase#inject()
115 	 */
116 	public IAction inject() {
117 		String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
118 		String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
119 		this.injectedObject = new TypedObject(id, type);
120 		return this;
121 	}
122 
123 	public void processSuccessor(IAction successor) {
124 		log.debug("processSuccessor(" + successor + ") - START");
125 		// create attribute list action and insert after rootAction
126 		if (HintTypes.CONTENT.equals(successor.getHint())) {
127 			try {
128 				Field field = ClassAnalyser.getSelectedField(this.getType(),
129 						successor.getValue().toString());
130 				if (((Modifier.STATIC & field.getModifiers()) == Modifier.STATIC)
131 						// or java 5 enumeration is found
132 						// Modifier.ENUM=16384
133 						|| ((16384 & field.getModifiers()) == 16384)) {
134 					field.setAccessible(true);
135 					Object obj = field.get(null);
136 					this.setValue(obj);
137 					this.setType(field.getType().getName());
138 				} else {
139 					throw new DDTTestDataException(
140 							"Constant field must be defined static: "
141 									+ this.getType() + "."
142 									+ successor.getValue().toString());
143 				}
144 			} catch (Exception ex) {
145 				throw new DDTTestDataException(
146 						"Error on creation of constant field: "
147 								+ this.getType() + "."
148 								+ successor.getValue().toString(), ex);
149 			}
150 
151 		} else {
152 			throw new DDTTestDataException("Unsupported successor action");
153 		}
154 	}
155 
156 }