001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.save.converters;
020:
021: import org.apache.jmeter.config.ConfigTestElement;
022: import org.apache.jmeter.save.SaveService;
023: import org.apache.jmeter.testelement.TestElement;
024: import org.apache.jmeter.testelement.property.JMeterProperty;
025: import org.apache.jmeter.testelement.property.PropertyIterator;
026: import org.apache.jmeter.testelement.property.TestElementProperty;
027: import org.apache.jorphan.logging.LoggingManager;
028: import org.apache.log.Logger;
029:
030: import com.thoughtworks.xstream.mapper.Mapper;
031: import com.thoughtworks.xstream.converters.MarshallingContext;
032: import com.thoughtworks.xstream.converters.UnmarshallingContext;
033: import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
034: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
035: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
036:
037: /**
038: * @author mstover
039: *
040: * To change the template for this generated type comment go to Window -
041: * Preferences - Java - Code Generation - Code and Comments
042: */
043: public class TestElementPropertyConverter extends
044: AbstractCollectionConverter {
045: private static final Logger log = LoggingManager
046: .getLoggerForClass();
047:
048: private final boolean testFormat22 = SaveService
049: .isSaveTestPlanFormat22();
050:
051: private static final String HEADER_CLASSNAME = "org.apache.jmeter.protocol.http.control.Header"; // $NON-NLS-1$
052:
053: /**
054: * Returns the converter version; used to check for possible
055: * incompatibilities
056: */
057: public static String getVersion() {
058: return "$Revision: 592814 $"; // $NON-NLS-1$
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
065: */
066: public boolean canConvert(Class arg0) {
067: return arg0.equals(TestElementProperty.class);
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
074: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
075: * com.thoughtworks.xstream.converters.MarshallingContext)
076: */
077: public void marshal(Object arg0, HierarchicalStreamWriter writer,
078: MarshallingContext context) {
079: TestElementProperty prop = (TestElementProperty) arg0;
080: writer.addAttribute(ConversionHelp.ATT_NAME, ConversionHelp
081: .encode(prop.getName()));
082: Class clazz = prop.getObjectValue().getClass();
083: writer.addAttribute(ConversionHelp.ATT_ELEMENT_TYPE,
084: testFormat22 ? mapper().serializedClass(clazz) : clazz
085: .getName());
086: if (testFormat22) {
087: TestElement te = (TestElement) prop.getObjectValue();
088: ConversionHelp.saveSpecialProperties(te, writer);
089: }
090: PropertyIterator iter = prop.iterator();
091: while (iter.hasNext()) {
092: JMeterProperty jmp = iter.next();
093: // Skip special properties if required
094: if (!testFormat22
095: || !ConversionHelp.isSpecialProperty(jmp.getName())) {
096: // Don't save empty comments
097: if (!(TestElement.COMMENTS.equals(jmp.getName()) && jmp
098: .getStringValue().length() == 0)) {
099: writeItem(jmp, context, writer);
100: }
101: }
102: }
103: //TODO clazz is probably always the same as testclass
104: }
105:
106: /*
107: * TODO - convert to work more like upgrade.properties/NameUpdater.java
108: *
109: * Special processing is carried out for the Header Class The String
110: * property TestElement.name is converted to Header.name for example:
111: * <elementProp name="User-Agent"
112: * elementType="org.apache.jmeter.protocol.http.control.Header"> <stringProp
113: * name="Header.value">Mozilla%2F4.0+%28compatible%3B+MSIE+5.5%3B+Windows+98%29</stringProp>
114: * <stringProp name="TestElement.name">User-Agent</stringProp>
115: * </elementProp> becomes <elementProp name="User-Agent"
116: * elementType="org.apache.jmeter.protocol.http.control.Header"> <stringProp
117: * name="Header.value">Mozilla%2F4.0+%28compatible%3B+MSIE+5.5%3B+Windows+98%29</stringProp>
118: * <stringProp name="Header.name">User-Agent</stringProp> </elementProp>
119: */
120: /*
121: * (non-Javadoc)
122: *
123: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
124: * com.thoughtworks.xstream.converters.UnmarshallingContext)
125: */
126: public Object unmarshal(HierarchicalStreamReader reader,
127: UnmarshallingContext context) {
128: try {
129: TestElementProperty prop = (TestElementProperty) createCollection(context
130: .getRequiredType());
131: prop.setName(ConversionHelp.decode(reader
132: .getAttribute(ConversionHelp.ATT_NAME)));
133: String element = reader
134: .getAttribute(ConversionHelp.ATT_ELEMENT_TYPE);
135: boolean isHeader = HEADER_CLASSNAME.equals(element);
136: prop.setObjectValue(mapper().realClass(element)
137: .newInstance());// Always decode
138: TestElement te = (TestElement) prop.getObjectValue();
139: // No need to check version, just process the attributes if present
140: ConversionHelp.restoreSpecialProperties(te, reader);
141: while (reader.hasMoreChildren()) {
142: reader.moveDown();
143: JMeterProperty subProp = (JMeterProperty) readItem(
144: reader, context, prop);
145: if (isHeader) {
146: String name = subProp.getName();
147: if (TestElement.NAME.equals(name)) {
148: subProp.setName("Header.name");// $NON-NLS-1$
149: // Must be same as Header.HNAME - but that is built
150: // later
151: }
152: }
153: prop.addProperty(subProp);
154: reader.moveUp();
155: }
156: return prop;
157: } catch (Exception e) {
158: log.error("Couldn't unmarshall TestElementProperty", e);
159: return new TestElementProperty("ERROR",
160: new ConfigTestElement());// $NON-NLS-1$
161: }
162: }
163:
164: /**
165: * @param arg0
166: */
167: public TestElementPropertyConverter(Mapper arg0) {
168: super(arg0);
169: }
170: }
|