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.save.SaveService;
022: import org.apache.jmeter.testelement.TestElement;
023: import org.apache.jmeter.testelement.TestPlan;
024: import org.apache.jmeter.testelement.property.JMeterProperty;
025: import org.apache.jmeter.testelement.property.PropertyIterator;
026: import org.apache.jorphan.logging.LoggingManager;
027: import org.apache.log.Logger;
028:
029: import com.thoughtworks.xstream.mapper.Mapper;
030: import com.thoughtworks.xstream.converters.MarshallingContext;
031: import com.thoughtworks.xstream.converters.UnmarshallingContext;
032: import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
033: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
034: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
035:
036: /**
037: * @author mstover
038: *
039: */
040: public class TestElementConverter extends AbstractCollectionConverter {
041: private static final Logger log = LoggingManager
042: .getLoggerForClass();
043:
044: private final boolean testFormat22 = SaveService
045: .isSaveTestPlanFormat22();
046:
047: /**
048: * Returns the converter version; used to check for possible
049: * incompatibilities
050: */
051: public static String getVersion() {
052: return "$Revision: 586393 $"; //$NON-NLS-1$
053: }
054:
055: /*
056: * (non-Javadoc)
057: *
058: * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)
059: */
060: public boolean canConvert(Class arg0) {
061: return TestElement.class.isAssignableFrom(arg0);
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,
068: * com.thoughtworks.xstream.io.HierarchicalStreamWriter,
069: * com.thoughtworks.xstream.converters.MarshallingContext)
070: */
071: public void marshal(Object arg0, HierarchicalStreamWriter writer,
072: MarshallingContext context) {
073: TestElement el = (TestElement) arg0;
074: if (testFormat22) {
075: ConversionHelp.saveSpecialProperties(el, writer);
076: }
077: PropertyIterator iter = el.propertyIterator();
078: while (iter.hasNext()) {
079: JMeterProperty jmp = iter.next();
080: // Skip special properties if required
081: if (!testFormat22
082: || !ConversionHelp.isSpecialProperty(jmp.getName())) {
083: // Don't save empty comments - except for the TestPlan (to maintain compatibility)
084: if (!(TestElement.COMMENTS.equals(jmp.getName())
085: && jmp.getStringValue().length() == 0 && !el
086: .getClass().equals(TestPlan.class))) {
087: writeItem(jmp, context, writer);
088: }
089: }
090: }
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,
097: * com.thoughtworks.xstream.converters.UnmarshallingContext)
098: */
099: public Object unmarshal(HierarchicalStreamReader reader,
100: UnmarshallingContext context) {
101: String classAttribute = reader
102: .getAttribute(ConversionHelp.ATT_CLASS);
103: Class type;
104: if (classAttribute == null) {
105: type = mapper().realClass(reader.getNodeName());
106: } else {
107: type = mapper().realClass(classAttribute);
108: }
109: try {
110: TestElement el = (TestElement) type.newInstance();
111: // No need to check version, just process the attributes if present
112: ConversionHelp.restoreSpecialProperties(el, reader);
113: while (reader.hasMoreChildren()) {
114: reader.moveDown();
115: JMeterProperty prop = (JMeterProperty) readItem(reader,
116: context, el);
117: el.setProperty(prop);
118: reader.moveUp();
119: }
120: return el;
121: } catch (Exception e) {
122: log.error("TestElement not instantiable: " + type, e);
123: return null;
124: }
125: }
126:
127: /**
128: * @param arg0
129: */
130: public TestElementConverter(Mapper arg0) {
131: super(arg0);
132: }
133: }
|