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  public class TCKtoJUnitConverter {
46  
47      public static void main(String[] args) {
48          InitUtil.initWithSpring();
49  
50          if (args.length == 2) {
51              new TCKtoJUnitConverter(args[0], args[1]);
52          }
53          else {
54              throw new IllegalArgumentException();
55          }
56      }
57  
58      public TCKtoJUnitConverter(String inputFile, String outputDir) {
59          try {
60              _convert(new File(inputFile), new File(outputDir));
61          }
62          catch (Exception e) {
63              e.printStackTrace();
64          }
65      }
66  
67      private void _convert(File inputFile, File outputDir) throws Exception {
68          BufferedReader br = new BufferedReader(new FileReader(inputFile));
69  
70          String s = StringPool.BLANK;
71  
72          while ((s = br.readLine()) != null) {
73              if (s.startsWith("Test finished: ")) {
74                  int x = s.indexOf(StringPool.POUND);
75                  int y = s.lastIndexOf(StringPool.SLASH, x);
76  
77                  String className = s.substring(15, y);
78  
79                  className = StringUtil.replace(
80                      className, StringPool.SLASH, StringPool.PERIOD);
81  
82                  y = s.indexOf(StringPool.COLON, y);
83  
84                  if (y == -1) {
85                      y = s.length();
86                  }
87  
88                  className += StringPool.PERIOD + s.substring(x + 1, y);
89  
90                  String message = s.substring(y + 2);
91  
92                  _convert(className, message, outputDir);
93              }
94          }
95  
96          br.close();
97      }
98  
99      private void _convert(String className, String message, File outputDir)
100         throws Exception {
101 
102         boolean passed = false;
103 
104         if (message.startsWith("Passed.")) {
105             passed = true;
106         }
107 
108         String hostname = GetterUtil.getString(
109             System.getProperty("env.USERDOMAIN")).toLowerCase();
110 
111         StringBuilder sb = new StringBuilder();
112 
113         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
114 
115         sb.append("<testsuite errors=\"");
116 
117         if (passed) {
118             sb.append("0");
119         }
120         else {
121             sb.append("1");
122         }
123 
124         sb.append("\" failures=\"");
125 
126         if (passed) {
127             sb.append("1");
128         }
129         else {
130             sb.append("0");
131         }
132 
133         sb.append("\" hostname=\"");
134         sb.append(hostname);
135         sb.append("\" name=\"");
136         sb.append(className);
137         sb.append("\" tests=\"1\" time=\"0.0\" timestamp=\"");
138         sb.append(System.currentTimeMillis());
139         sb.append("\">\n");
140         sb.append("\t<properties>\n");
141 
142         Properties properties = new SortedProperties(System.getProperties());
143 
144         Enumeration<String> keys =
145             (Enumeration<String>)properties.propertyNames();
146 
147         while (keys.hasMoreElements()) {
148             String key = keys.nextElement();
149 
150             String value = properties.getProperty(key);
151 
152             sb.append("\t\t<property name=\"");
153             sb.append(HtmlUtil.escape(key));
154             sb.append("\" value=\"");
155             sb.append(HtmlUtil.escape(value));
156             sb.append("\" />\n");
157         }
158 
159         sb.append("\t</properties>\n");
160         sb.append("\t<testcase classname=\"");
161         sb.append(className);
162         sb.append("\" name=\"test\" time=\"0.0\"");
163 
164         if (passed) {
165             sb.append(" />\n");
166         }
167         else {
168             String failureMessage = HtmlUtil.escape(message.substring(8));
169 
170             sb.append(">\n");
171             sb.append("\t\t<failure message=\"");
172             sb.append(failureMessage);
173             sb.append("\" type=\"junit.framework.AssertionFailedError\">\n");
174             sb.append(failureMessage);
175             sb.append("\n\t\t</failure>\n");
176             sb.append("\t</testcase>\n");
177         }
178 
179         sb.append("\t<system-out><![CDATA[]]></system-out>\n");
180         sb.append("\t<system-err><![CDATA[]]></system-err>\n");
181         sb.append("</testsuite>");
182 
183         FileUtil.write(
184             outputDir + "/TEST-" + className + ".xml", sb.toString());
185     }
186 
187 }