001    /**
002     * Copyright (c) 2000-present 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.CharPool;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.SortedProperties;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
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                    ToolDependencies.wireBasic();
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                    try (UnsyncBufferedReader unsyncBufferedReader =
060                                    new UnsyncBufferedReader(new FileReader(inputFile))) {
061    
062                            String s = StringPool.BLANK;
063    
064                            while ((s = unsyncBufferedReader.readLine()) != null) {
065                                    if (!s.startsWith("Test finished: ")) {
066                                            continue;
067                                    }
068    
069                                    int x = s.indexOf(StringPool.POUND);
070                                    int y = s.lastIndexOf(StringPool.SLASH, x);
071    
072                                    String className = s.substring(15, y);
073    
074                                    className = StringUtil.replace(
075                                            className, CharPool.SLASH, CharPool.PERIOD);
076    
077                                    y = s.indexOf(StringPool.COLON, y);
078    
079                                    if (y == -1) {
080                                            y = s.length();
081                                    }
082    
083                                    className += StringPool.PERIOD + s.substring(x + 1, y);
084    
085                                    String message = s.substring(y + 2);
086    
087                                    _convert(className, message, outputDir);
088                            }
089                    }
090            }
091    
092            private void _convert(String className, String message, File outputDir)
093                    throws Exception {
094    
095                    boolean passed = false;
096    
097                    if (message.startsWith("Passed.")) {
098                            passed = true;
099                    }
100    
101                    String hostname = GetterUtil.getString(
102                            System.getProperty("env.USERDOMAIN"));
103    
104                    hostname = StringUtil.toLowerCase(hostname);
105    
106                    StringBundler sb = new StringBundler();
107    
108                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
109    
110                    sb.append("<testsuite errors=\"");
111    
112                    if (passed) {
113                            sb.append("0");
114                    }
115                    else {
116                            sb.append("1");
117                    }
118    
119                    sb.append("\" failures=\"");
120    
121                    if (passed) {
122                            sb.append("1");
123                    }
124                    else {
125                            sb.append("0");
126                    }
127    
128                    sb.append("\" hostname=\"");
129                    sb.append(hostname);
130                    sb.append("\" name=\"");
131                    sb.append(className);
132                    sb.append("\" tests=\"1\" time=\"0.0\" timestamp=\"");
133                    sb.append(System.currentTimeMillis());
134                    sb.append("\">\n");
135                    sb.append("\t<properties>\n");
136    
137                    Properties properties = new SortedProperties(System.getProperties());
138    
139                    Enumeration<String> keys =
140                            (Enumeration<String>)properties.propertyNames();
141    
142                    while (keys.hasMoreElements()) {
143                            String key = keys.nextElement();
144    
145                            String value = properties.getProperty(key);
146    
147                            sb.append("\t\t<property name=\"");
148                            sb.append(HtmlUtil.escape(key));
149                            sb.append("\" value=\"");
150                            sb.append(HtmlUtil.escape(value));
151                            sb.append("\" />\n");
152                    }
153    
154                    sb.append("\t</properties>\n");
155                    sb.append("\t<testcase classname=\"");
156                    sb.append(className);
157                    sb.append("\" name=\"test\" time=\"0.0\"");
158    
159                    if (passed) {
160                            sb.append(" />\n");
161                    }
162                    else {
163                            String failureMessage = HtmlUtil.escape(message.substring(8));
164    
165                            sb.append(">\n");
166                            sb.append("\t\t<failure message=\"");
167                            sb.append(failureMessage);
168                            sb.append("\" type=\"junit.framework.AssertionFailedError\">\n");
169                            sb.append(failureMessage);
170                            sb.append("\n\t\t</failure>\n");
171                            sb.append("\t</testcase>\n");
172                    }
173    
174                    sb.append("\t<system-out><![CDATA[]]></system-out>\n");
175                    sb.append("\t<system-err><![CDATA[]]></system-err>\n");
176                    sb.append("</testsuite>");
177    
178                    FileUtil.write(
179                            outputDir + "/TEST-" + className + ".xml", sb.toString());
180            }
181    
182    }