001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
034     * @author Brian Wing Shun Chan
035     */
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")).toLowerCase();
102    
103                    StringBundler sb = new StringBundler();
104    
105                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
106    
107                    sb.append("<testsuite errors=\"");
108    
109                    if (passed) {
110                            sb.append("0");
111                    }
112                    else {
113                            sb.append("1");
114                    }
115    
116                    sb.append("\" failures=\"");
117    
118                    if (passed) {
119                            sb.append("1");
120                    }
121                    else {
122                            sb.append("0");
123                    }
124    
125                    sb.append("\" hostname=\"");
126                    sb.append(hostname);
127                    sb.append("\" name=\"");
128                    sb.append(className);
129                    sb.append("\" tests=\"1\" time=\"0.0\" timestamp=\"");
130                    sb.append(System.currentTimeMillis());
131                    sb.append("\">\n");
132                    sb.append("\t<properties>\n");
133    
134                    Properties properties = new SortedProperties(System.getProperties());
135    
136                    Enumeration<String> keys =
137                            (Enumeration<String>)properties.propertyNames();
138    
139                    while (keys.hasMoreElements()) {
140                            String key = keys.nextElement();
141    
142                            String value = properties.getProperty(key);
143    
144                            sb.append("\t\t<property name=\"");
145                            sb.append(HtmlUtil.escape(key));
146                            sb.append("\" value=\"");
147                            sb.append(HtmlUtil.escape(value));
148                            sb.append("\" />\n");
149                    }
150    
151                    sb.append("\t</properties>\n");
152                    sb.append("\t<testcase classname=\"");
153                    sb.append(className);
154                    sb.append("\" name=\"test\" time=\"0.0\"");
155    
156                    if (passed) {
157                            sb.append(" />\n");
158                    }
159                    else {
160                            String failureMessage = HtmlUtil.escape(message.substring(8));
161    
162                            sb.append(">\n");
163                            sb.append("\t\t<failure message=\"");
164                            sb.append(failureMessage);
165                            sb.append("\" type=\"junit.framework.AssertionFailedError\">\n");
166                            sb.append(failureMessage);
167                            sb.append("\n\t\t</failure>\n");
168                            sb.append("\t</testcase>\n");
169                    }
170    
171                    sb.append("\t<system-out><![CDATA[]]></system-out>\n");
172                    sb.append("\t<system-err><![CDATA[]]></system-err>\n");
173                    sb.append("</testsuite>");
174    
175                    FileUtil.write(
176                            outputDir + "/TEST-" + className + ".xml", sb.toString());
177            }
178    
179    }