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
026 import java.io.File;
027 import java.io.FileReader;
028
029 import java.util.Enumeration;
030 import java.util.Properties;
031
032
035 public class TCKtoJUnitConverter {
036
037 public static void main(String[] args) {
038 ToolDependencies.wireBasic();
039
040 if (args.length == 2) {
041 new TCKtoJUnitConverter(args[0], args[1]);
042 }
043 else {
044 throw new IllegalArgumentException();
045 }
046 }
047
048 public TCKtoJUnitConverter(String inputFile, String outputDir) {
049 try {
050 _convert(new File(inputFile), new File(outputDir));
051 }
052 catch (Exception e) {
053 e.printStackTrace();
054 }
055 }
056
057 private void _convert(File inputFile, File outputDir) throws Exception {
058 try (UnsyncBufferedReader unsyncBufferedReader =
059 new UnsyncBufferedReader(new FileReader(inputFile))) {
060
061 String s = StringPool.BLANK;
062
063 while ((s = unsyncBufferedReader.readLine()) != null) {
064 if (!s.startsWith("Test finished: ")) {
065 continue;
066 }
067
068 int x = s.indexOf(StringPool.POUND);
069 int y = s.lastIndexOf(StringPool.SLASH, x);
070
071 String className = s.substring(15, y);
072
073 className = StringUtil.replace(
074 className, StringPool.SLASH, StringPool.PERIOD);
075
076 y = s.indexOf(StringPool.COLON, y);
077
078 if (y == -1) {
079 y = s.length();
080 }
081
082 className += StringPool.PERIOD + s.substring(x + 1, y);
083
084 String message = s.substring(y + 2);
085
086 _convert(className, message, outputDir);
087 }
088 }
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 }