001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.util.FileUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.HtmlUtil;
021 import com.liferay.portal.kernel.util.SortedProperties;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.util.InitUtil;
026
027 import java.io.File;
028 import java.io.FileReader;
029
030 import java.util.Enumeration;
031 import java.util.Properties;
032
033
036 public class TCKtoJUnitConverter {
037
038 public static void main(String[] args) {
039 InitUtil.initWithSpring();
040
041 if (args.length == 2) {
042 new TCKtoJUnitConverter(args[0], args[1]);
043 }
044 else {
045 throw new IllegalArgumentException();
046 }
047 }
048
049 public TCKtoJUnitConverter(String inputFile, String outputDir) {
050 try {
051 _convert(new File(inputFile), new File(outputDir));
052 }
053 catch (Exception e) {
054 e.printStackTrace();
055 }
056 }
057
058 private void _convert(File inputFile, File outputDir) throws Exception {
059 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
060 new FileReader(inputFile));
061
062 String s = StringPool.BLANK;
063
064 while ((s = unsyncBufferedReader.readLine()) != null) {
065 if (s.startsWith("Test finished: ")) {
066 int x = s.indexOf(StringPool.POUND);
067 int y = s.lastIndexOf(StringPool.SLASH, x);
068
069 String className = s.substring(15, y);
070
071 className = StringUtil.replace(
072 className, StringPool.SLASH, StringPool.PERIOD);
073
074 y = s.indexOf(StringPool.COLON, y);
075
076 if (y == -1) {
077 y = s.length();
078 }
079
080 className += StringPool.PERIOD + s.substring(x + 1, y);
081
082 String message = s.substring(y + 2);
083
084 _convert(className, message, outputDir);
085 }
086 }
087
088 unsyncBufferedReader.close();
089 }
090
091 private void _convert(String className, String message, File outputDir)
092 throws Exception {
093
094 boolean passed = false;
095
096 if (message.startsWith("Passed.")) {
097 passed = true;
098 }
099
100 String hostname = GetterUtil.getString(
101 System.getProperty("env.USERDOMAIN"));
102
103 hostname = StringUtil.toLowerCase(hostname);
104
105 StringBundler sb = new StringBundler();
106
107 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
108
109 sb.append("<testsuite errors=\"");
110
111 if (passed) {
112 sb.append("0");
113 }
114 else {
115 sb.append("1");
116 }
117
118 sb.append("\" failures=\"");
119
120 if (passed) {
121 sb.append("1");
122 }
123 else {
124 sb.append("0");
125 }
126
127 sb.append("\" hostname=\"");
128 sb.append(hostname);
129 sb.append("\" name=\"");
130 sb.append(className);
131 sb.append("\" tests=\"1\" time=\"0.0\" timestamp=\"");
132 sb.append(System.currentTimeMillis());
133 sb.append("\">\n");
134 sb.append("\t<properties>\n");
135
136 Properties properties = new SortedProperties(System.getProperties());
137
138 Enumeration<String> keys =
139 (Enumeration<String>)properties.propertyNames();
140
141 while (keys.hasMoreElements()) {
142 String key = keys.nextElement();
143
144 String value = properties.getProperty(key);
145
146 sb.append("\t\t<property name=\"");
147 sb.append(HtmlUtil.escape(key));
148 sb.append("\" value=\"");
149 sb.append(HtmlUtil.escape(value));
150 sb.append("\" />\n");
151 }
152
153 sb.append("\t</properties>\n");
154 sb.append("\t<testcase classname=\"");
155 sb.append(className);
156 sb.append("\" name=\"test\" time=\"0.0\"");
157
158 if (passed) {
159 sb.append(" />\n");
160 }
161 else {
162 String failureMessage = HtmlUtil.escape(message.substring(8));
163
164 sb.append(">\n");
165 sb.append("\t\t<failure message=\"");
166 sb.append(failureMessage);
167 sb.append("\" type=\"junit.framework.AssertionFailedError\">\n");
168 sb.append(failureMessage);
169 sb.append("\n\t\t</failure>\n");
170 sb.append("\t</testcase>\n");
171 }
172
173 sb.append("\t<system-out><![CDATA[]]></system-out>\n");
174 sb.append("\t<system-err><![CDATA[]]></system-err>\n");
175 sb.append("</testsuite>");
176
177 FileUtil.write(
178 outputDir + "/TEST-" + className + ".xml", sb.toString());
179 }
180
181 }