1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.HtmlUtil;
21  import com.liferay.portal.kernel.util.SortedProperties;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.util.InitUtil;
26  
27  import java.io.File;
28  import java.io.FileReader;
29  
30  import java.util.Enumeration;
31  import java.util.Properties;
32  
33  /**
34   * <a href="TCKtoJUnitConverter.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class TCKtoJUnitConverter {
39  
40      public static void main(String[] args) {
41          InitUtil.initWithSpring();
42  
43          if (args.length == 2) {
44              new TCKtoJUnitConverter(args[0], args[1]);
45          }
46          else {
47              throw new IllegalArgumentException();
48          }
49      }
50  
51      public TCKtoJUnitConverter(String inputFile, String outputDir) {
52          try {
53              _convert(new File(inputFile), new File(outputDir));
54          }
55          catch (Exception e) {
56              e.printStackTrace();
57          }
58      }
59  
60      private void _convert(File inputFile, File outputDir) throws Exception {
61          UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
62              new FileReader(inputFile));
63  
64          String s = StringPool.BLANK;
65  
66          while ((s = unsyncBufferedReader.readLine()) != null) {
67              if (s.startsWith("Test finished: ")) {
68                  int x = s.indexOf(StringPool.POUND);
69                  int y = s.lastIndexOf(StringPool.SLASH, x);
70  
71                  String className = s.substring(15, y);
72  
73                  className = StringUtil.replace(
74                      className, StringPool.SLASH, StringPool.PERIOD);
75  
76                  y = s.indexOf(StringPool.COLON, y);
77  
78                  if (y == -1) {
79                      y = s.length();
80                  }
81  
82                  className += StringPool.PERIOD + s.substring(x + 1, y);
83  
84                  String message = s.substring(y + 2);
85  
86                  _convert(className, message, outputDir);
87              }
88          }
89  
90          unsyncBufferedReader.close();
91      }
92  
93      private void _convert(String className, String message, File outputDir)
94          throws Exception {
95  
96          boolean passed = false;
97  
98          if (message.startsWith("Passed.")) {
99              passed = true;
100         }
101 
102         String hostname = GetterUtil.getString(
103             System.getProperty("env.USERDOMAIN")).toLowerCase();
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 }