• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng;
2 
3 import org.testng.collections.Lists;
4 import org.testng.internal.ClassHelper;
5 import org.testng.internal.IPropertyUtils;
6 import org.testng.internal.PropertyUtilsFactory;
7 import org.testng.internal.Utils;
8 
9 import java.util.List;
10 
11 /**
12  * Stores the information regarding the configuration of a pluggable report listener. Used also
13  * in conjunction with the <reporter> sub-element of the Ant task
14  *
15  * NOTE: this class needs to be public. It's used by TestNG Ant task
16  *
17  * @author Cosmin Marginean, Apr 12, 2007
18  */
19 public class ReporterConfig {
20 
21   /**
22    * The class name of the reporter listener
23    */
24   private String m_className;
25 
26   /**
27    * The properties of the reporter listener
28    */
29   private List<Property> m_properties = Lists.newArrayList();
30 
31   /**
32    * JavaBeans properties access helper
33    */
34   private IPropertyUtils mPropertyUtils = PropertyUtilsFactory.newInstance();
35 
addProperty(Property property)36   public void addProperty(Property property) {
37     m_properties.add(property);
38   }
39 
getProperties()40   public List<Property> getProperties() {
41     return m_properties;
42   }
43 
getClassName()44   public String getClassName() {
45     return m_className;
46   }
47 
setClassName(String className)48   public void setClassName(String className) {
49     this.m_className = className;
50   }
51 
serialize()52   public String serialize() {
53     StringBuffer sb = new StringBuffer();
54     sb.append(m_className);
55     if (!m_properties.isEmpty()) {
56       sb.append(":");
57 
58       for (int i = 0; i < m_properties.size(); i++) {
59         ReporterConfig.Property property = m_properties.get(i);
60         sb.append(property.getName());
61         sb.append("=");
62         sb.append(property.getValue());
63         if (i < m_properties.size() - 1) {
64           sb.append(",");
65         }
66       }
67     }
68     return sb.toString();
69   }
70 
deserialize(String inputString)71   public static ReporterConfig deserialize(String inputString) {
72     ReporterConfig reporterConfig = null;
73     if (!Utils.isStringEmpty(inputString)) {
74       reporterConfig = new ReporterConfig();
75       int clsNameEndIndex = inputString.indexOf(":");
76       if (clsNameEndIndex == -1) {
77         reporterConfig.setClassName(inputString);
78       } else {
79         reporterConfig.setClassName(inputString.substring(0, clsNameEndIndex));
80         String propString = inputString.substring(clsNameEndIndex + 1, inputString.length());
81         String[] props = propString.split(",");
82         if ((props != null) && (props.length > 0)) {
83           for (String prop : props) {
84             String[] propNameAndVal = prop.split("=");
85             if ((propNameAndVal != null) && (propNameAndVal.length == 2)) {
86               Property property = new Property();
87               property.setName(propNameAndVal[0]);
88               property.setValue(propNameAndVal[1]);
89               reporterConfig.addProperty(property);
90             }
91           }
92         }
93       }
94 
95     }
96     return reporterConfig;
97   }
98 
99   /**
100    * Creates a reporter based on the current configuration
101    */
newReporterInstance()102   public Object newReporterInstance() {
103     Object result = null;
104     Class reporterClass = ClassHelper.forName(m_className);
105     if (reporterClass != null) {
106       result = ClassHelper.newInstance(reporterClass);
107       for (ReporterConfig.Property property : m_properties) {
108         mPropertyUtils.setProperty(result, property.getName(), property.getValue());
109       }
110     }
111     return result;
112   }
113 
114   public static class Property {
115     private String name;
116     private String value;
117 
getName()118     public String getName() {
119       return name;
120     }
121 
setName(String name)122     public void setName(String name) {
123       this.name = name;
124     }
125 
getValue()126     public String getValue() {
127       return value;
128     }
129 
setValue(String value)130     public void setValue(String value) {
131       this.value = value;
132     }
133   }
134 
135   @Override
toString()136   public String toString() {
137     StringBuffer buf = new StringBuffer();
138     buf.append("\nClass = " + m_className);
139     for (Property prop : m_properties) {
140       buf.append("\n\t " + prop.getName() + "=" + prop.getValue());
141     }
142     return buf.toString();
143   }
144 }
145