       1 /*
       2  * $Id$
       3  *
       4  * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
       5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       6  *
       7  * This code is free software; you can redistribute it and/or modify it
       8  * under the terms of the GNU General Public License version 2 only, as
       9  * published by the Free Software Foundation.  Oracle designates this
      10  * particular file as subject to the "Classpath" exception as provided
      11  * by Oracle in the LICENSE file that accompanied this code.
      12  *
      13  * This code is distributed in the hope that it will be useful, but WITHOUT
      14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      16  * version 2 for more details (a copy is included in the LICENSE file that
      17  * accompanied this code).
      18  *
      19  * You should have received a copy of the GNU General Public License version
      20  * 2 along with this work; if not, write to the Free Software Foundation,
      21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      22  *
      23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      24  * or visit www.oracle.com if you need additional information or have any
      25  * questions.
      26  */
      27 package com.sun.javatest.cof;
      28
      29 import java.io.File;
      30 import java.io.IOException;
      31 import java.util.Iterator;
      32 import java.util.regex.Pattern;
      33
      34 import com.sun.javatest.Status;
      35 import com.sun.javatest.TestResult;
      36 import com.sun.javatest.TestResultTable;
      37 import com.sun.javatest.TestSuite;
      38 import com.sun.javatest.WorkDirectory;
      39 import com.sun.javatest.util.I18NResourceBundle;
      40 import com.sun.javatest.util.XMLWriter;
      41 import java.util.Collection;
      42 import java.util.HashMap;
      43
      44 class COFTestSuite extends COFItem {
      45
      46         private static final String[] cofStatus = new String[Status.NUM_STATES];
      47
      48         private static I18NResourceBundle i18n = I18NResourceBundle
      49                         .getBundleForClass(COFTestSuite.class);
      50
      51         static {
      52                 cofStatus[Status.PASSED] = "pass";
      53                 cofStatus[Status.FAILED] = "fail";
      54                 cofStatus[Status.ERROR] = "error";
      55                 cofStatus[Status.NOT_RUN] = "did_not_run";
      56         }
      57
      58         private COFData cofData;
      59
      60         private boolean legacyMode = false; // modern workdir or not
      61
      62         private String name;
      63
      64         protected Pattern testCasePattern = Pattern
      65                         .compile("^(\\S+): (Passed\\.|Failed\\.|Error\\.|Not\\ run\\.)(.*)");
      66
      67
      68         private TestResultTable trt;
      69
      70         COFTestSuite(File dir) {
      71                 trt = new TestResultTable();
      72                 scan(dir);
      73                 legacyMode = true;
      74         }
      75
      76         COFTestSuite(File dir, COFData cd) {
      77                 cofData = cd;
      78                 trt = new TestResultTable();
      79                 name = cofData.get("testsuites.testsuite.name");
      80                 scan(dir);
      81                 legacyMode = true;
      82         }
      83
      84         COFTestSuite(TestResultTable trt) {
      85                 this.trt = trt;
      86         }
      87
      88         COFTestSuite(WorkDirectory wd) {
      89                 this(wd, null);
      90         }
      91
      92         COFTestSuite(WorkDirectory wd, COFData cd) {
      93                 cofData = cd;
      94                 TestSuite ts = wd.getTestSuite();
      95                 name = ts.getID();
      96                 trt = wd.getTestResultTable();
      97         }
      98
      99         COFTestSuite() {
     100         }
     101
     102         void scan(File dir) {
     103                 String[] entries = dir.list();
     104                 if (entries != null) {
     105                         for (int i = 0; i < entries.length; i++) {
     106                                 File f = new File(dir, entries[i]);
     107                                 if (f.isDirectory())
     108                                         scan(f);
     109                                 else if (TestResult.isResultFile(f)) {
     110                                         try {
     111                                                 TestResult tr = new TestResult(f);
     112                                                 trt.update(tr);
     113                                         } catch (TestResult.Fault e) {
     114                                                 // ignore errors for now
     115                                                 // an error handler might report errors to stderr
     116                                                 System.err.println(i18n.getString("ts.badTest",
     117                                                                 new Object[] { f, e.getMessage() }));
     118                                         }
     119                                 }
     120                         }
     121                 }
     122         }
     123
     124         void write(XMLWriter out) throws IOException {
     125
     126                 out.startTag("testsuite");
     127                 out.writeAttr("id", "unknownTestSuite:0");
     128
     129                 // name
     130                 out.startTag("name");
     131                 out.write(name == null ? "unknown" : name);
     132                 out.endTag("name");
     133                 // version (optional)
     134                 // tests
     135                 out.startTag("tests");
     136
     137                 // might need to wait for workdir to fully load
     138                 if (!legacyMode)
     139                         trt.waitUntilReady();
     140                 for (Iterator iter = trt.getIterator(); iter.hasNext();) {
     141                         TestResult tr = (TestResult) (iter.next());
     142                         out.newLine();
     143                         new COFTest(tr, cofData).write(out);
     144                 }
     145                 out.endTag("tests");
     146                 out.endTag("testsuite");
     147         }
     148
     149     public COFEnvironment[] getCOFEnvironments(COFData data) {
     150         if (!legacyMode) {
     151             trt.waitUntilReady();
     152         }
     153         HashMap<String, COFEnvironment> map = new HashMap<>();
     154         String id = cofData.get("environment.id", "env:0").split(":")[0] + ":";
     155         int id_n = 0;
     156         for (Iterator iter = trt.getIterator(); iter.hasNext();) {
     157             TestResult tr = (TestResult) (iter.next());
     158             try {
     159                 String os = tr.getProperty(TestResult.JAVATEST_OS);
     160                 if (!map.containsKey(os) && os != null) {
     161                     try {
     162                         String[] split;
     163                         split = os.split("[()]");
     164                         String arch = split[split.length - 1];
     165                         split = split[0].split(" ", 2);
     166                         String name = split[0];
     167                         String version = split[1].trim();
     168                         data.put("environment.os.arch", arch);
     169                         data.put("environment.os.name", name);
     170                         data.put("environment.os.version", version);
     171
     172                         COFEnvironment e = new COFEnvironment(data, id + id_n++);
     173                         map.put(os, e);
     174                     } catch (Throwable t) {
     175                     }
     176                 }
     177             } catch (TestResult.Fault ex) {
     178             }
     179         }
     180         Collection values = map.values();
     181         COFEnvironment[] evs;
     182         if (values.isEmpty()) {
     183             evs = new COFEnvironment[] {new COFEnvironment(data, "env:0")};
     184         } else {
     185             evs = new COFEnvironment[values.size()];
     186             Iterator e = values.iterator();
     187             int i = 0;
     188             while (e.hasNext()) {
     189                 evs[i++] = (COFEnvironment) e.next();
     190             }
     191         }
     192         return evs;
     193     }
     194 }