View Javadoc

1   //$Id: AttributeListCreatorAction.java 351 2008-08-14 20:20:56Z 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 name 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.util.List;
41  import java.util.Map;
42  
43  import junitx.ddtunit.DDTException;
44  import junitx.ddtunit.data.TypedObject;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  public class AttributeListCreatorAction extends ActionBase {
50      protected Logger log = LoggerFactory.getLogger(AttributeListCreatorAction.class);
51  
52      public AttributeListCreatorAction(Map<String, String> attrMap) {
53          super(attrMap);
54          this.attrMap.put(ParserConstants.XML_ATTR_ID, "attrlist");
55          this.attrMap.put(ParserConstants.XML_ATTR_TYPE, "java.util.Vector");
56          this.attrMap.put(ParserConstants.XML_ATTR_HINT, "attrlist");
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see junitx.ddtunit.parser.ActionBase#process()
63       */
64      public IAction process() {
65          log.debug("process AttributeListCreator - START");
66          IAction rootAction = getPrevious();
67          if (rootAction == null) {
68              throw new DDTException(
69                      "AttributeListCreatorAction must follow another action in action stack.");
70          }
71          // process different types of actions according to rootAction type
72          String hintValue = rootAction.getHint();
73  
74          if (HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
75              rootAction.processSuccessor(this);
76          } else if (HintTypes.CONSTRUCTOR.equals(hintValue)
77                  || HintTypes.CALL.equals(hintValue)) {
78              rootAction.processSuccessor(this);
79          } else if (HintTypes.FIELDS.equals(hintValue)) {
80              rootAction.processSuccessor(this);
81          } else if (HintTypes.BEAN.equals(hintValue)) {
82              rootAction.processSuccessor(this);
83          } else if (HintTypes.ARRAY.equals(hintValue)) {
84              rootAction.processSuccessor(this);
85          } else {
86              throw new UnsupportedOperationException(
87                      "AttributeListCreatorAction does not support hint '"
88                              + hintValue + "'");
89          }
90          pop();
91          // do not forget to process the actual root element
92          if (rootAction.getPrevious() != null) {
93              rootAction = rootAction.process();
94          }
95          return rootAction;
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see junitx.ddtunit.parser.ActionBase#inject()
102      */
103     public IAction inject() {
104         String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
105         String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
106         // check if type is based on java.util.Collection
107         try {
108             if (type == null && ParserConstants.XML_ELEM_ITEM.equals(id)) {
109                 type = TypedObject.UNKNOWN_TYPE;
110             } else if (HintTypes.ARRAY.equals(this.getHint())) {
111                 // do nothing
112             } else {
113                 Class clazz = Class.forName(type);
114                 Class superclazz = clazz;
115                 boolean found = false;
116                 while (!found && superclazz != null) {
117                     if ("java.util.Collection".equals(superclazz.getName())
118                             || "java.util.AbstractCollection".equals(superclazz
119                                 .getName())) {
120                         found = true;
121                     }
122                     superclazz = superclazz.getSuperclass();
123                 }
124                 if (!found) {
125                     throw new DDTException("Container class '" + type
126                             + "' does not implement java.util.Collection.");
127                 }
128             }
129         } catch (Exception ex) {
130             throw new DDTException("Container class '" + type
131                     + "' does not implement java.util.Collection.", ex);
132         }
133         this.injectedObject = new TypedObject(id, type);
134         return this;
135     }
136 
137     public void processSuccessor(IAction successor) {
138         log.debug("processSuccessor(" + successor + ") - START");
139         ((List<TypedObject>) this.getValue()).add(successor.getObject());
140     }
141 
142 }