1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.SortedProperties;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.util.InitUtil;
32  
33  import java.io.BufferedReader;
34  import java.io.File;
35  import java.io.FileReader;
36  
37  import java.util.Enumeration;
38  import java.util.Properties;
39  
40  /**
41   * <a href="TCKtoJUnitConverter.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author  Brian Wing Shun Chan
44   *
45   */
46  public class TCKtoJUnitConverter {
47  
48      public static void main(String[] args) {
49          InitUtil.initWithSpring();
50  
51          if (args.length == 2) {
52              new TCKtoJUnitConverter(args[0], args[1]);
53          }
54          else {
55              throw new IllegalArgumentException();
56          }
57      }
58  
59      public TCKtoJUnitConverter(String inputFile, String outputDir) {
60          try {
61              _convert(new File(inputFile), new File(outputDir));
62          }
63          catch (Exception e) {
64              e.printStackTrace();
65          }
66      }
67  
68      private void _convert(File inputFile, File outputDir) throws Exception {
69          BufferedReader br = new BufferedReader(new FileReader(inputFile));
70  
71          String s = StringPool.BLANK;
72  
73          while ((s = br.readLine()) != null) {
74              if (s.startsWith("Test finished: ")) {
75                  int x = s.indexOf(StringPool.POUND);
76                  int y = s.lastIndexOf(StringPool.SLASH, x);
77  
78                  String className = s.substring(15, y);
79  
80                  className = StringUtil.replace(
81                      className, StringPool.SLASH, StringPool.PERIOD);
82  
83                  y = s.indexOf(StringPool.COLON, y);
84  
85                  if (y == -1) {
86                      y = s.length();
87                  }
88  
89                  className += StringPool.PERIOD + s.substring(x + 1, y);
90  
91                  String message = s.substring(y + 2);
92  
93                  _convert(className, message, outputDir);
94              }
95          }
96  
97          br.close();
98      }
99  
100     private void _convert(String className, String message, File outputDir)
101         throws Exception {
102 
103         boolean passed = false;
104 
105         if (message.startsWith("Passed.")) {
106             passed = true;
107         }
108 
109         String hostname = GetterUtil.getString(
110             System.getProperty("env.USERDOMAIN")).toLowerCase();
111 
112         StringBuilder sb = new StringBuilder();
113 
114         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
115 
116         sb.append("<testsuite errors=\"");
117 
118         if (passed) {
119             sb.append("0");
120         }
121         else {
122             sb.append("1");
123         }
124 
125         sb.append("\" failures=\"");
126 
127         if (passed) {
128             sb.append("1");
129         }
130         else {
131             sb.append("0");
132         }
133 
134         sb.append("\" hostname=\"");
135         sb.append(hostname);
136         sb.append("\" name=\"");
137         sb.append(className);
138         sb.append("\" tests=\"1\" time=\"0.0\" timestamp=\"");
139         sb.append(System.currentTimeMillis());
140         sb.append("\">\n");
141         sb.append("\t<properties>\n");
142 
143         Properties properties = new SortedProperties(System.getProperties());
144 
145         Enumeration<String> keys =
146             (Enumeration<String>)properties.propertyNames();
147 
148         while (keys.hasMoreElements()) {
149             String key = keys.nextElement();
150 
151             String value = properties.getProperty(key);
152 
153             sb.append("\t\t<property name=\"");
154             sb.append(HtmlUtil.escape(key));
155             sb.append("\" value=\"");
156             sb.append(HtmlUtil.escape(value));
157             sb.append("\" />\n");
158         }
159 
160         sb.append("\t</properties>\n");
161         sb.append("\t<testcase classname=\"");
162         sb.append(className);
163         sb.append("\" name=\"test\" time=\"0.0\"");
164 
165         if (passed) {
166             sb.append(" />\n");
167         }
168         else {
169             String failureMessage = HtmlUtil.escape(message.substring(8));
170 
171             sb.append(">\n");
172             sb.append("\t\t<failure message=\"");
173             sb.append(failureMessage);
174             sb.append("\" type=\"junit.framework.AssertionFailedError\">\n");
175             sb.append(failureMessage);
176             sb.append("\n\t\t</failure>\n");
177             sb.append("\t</testcase>\n");
178         }
179 
180         sb.append("\t<system-out><![CDATA[]]></system-out>\n");
181         sb.append("\t<system-err><![CDATA[]]></system-err>\n");
182         sb.append("</testsuite>");
183 
184         FileUtil.write(
185             outputDir + "/TEST-" + className + ".xml", sb.toString());
186     }
187 
188 }