1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2002, 2013, 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.report;
  28 
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.text.NumberFormat;
  32 import java.util.*;
  33 
  34 import com.sun.interview.WizPrint;
  35 import com.sun.javatest.ExcludeList;
  36 import com.sun.javatest.Keywords;
  37 import com.sun.javatest.Parameters;
  38 import com.sun.javatest.TestEnvironment;
  39 import com.sun.javatest.TestSuite;
  40 import com.sun.javatest.WorkDirectory;
  41 import com.sun.javatest.util.I18NResourceBundle;
  42 import com.sun.javatest.util.StringArray;
  43 
  44 /**
  45  * A report generator for sets of test results.
  46  */
  47 class ConfigSection extends HTMLSection {
  48     ConfigSection(HTMLReport parent, ReportSettings s, File dir, I18NResourceBundle i18n) {
  49         super(i18n.getString("config.title"), s, dir, parent);
  50         this.i18n = i18n;
  51     }
  52 
  53     void writeContents(ReportWriter out) throws IOException {
  54         super.writeContents(out);
  55 
  56         if (settings.isQuestionLogEnabled()) {
  57             out.startTag(HTMLWriterEx.UL);
  58             out.startTag(HTMLWriterEx.LI);
  59             out.writeLink(HTMLReport.files[HTMLReport.CONFIG_HTML],
  60                           i18n.getString("config.confInterview"));
  61             out.endTag(HTMLWriterEx.LI);
  62             out.endTag(HTMLWriterEx.UL);
  63         }
  64 
  65         if (settings.isStdEnabled()) {
  66             out.startTag(HTMLWriterEx.UL);
  67             out.startTag(HTMLWriterEx.LI);
  68             out.writeLink("#" + HTMLReport.anchors[HTMLReport.SELECT_ANCHOR],
  69                           i18n.getString("config.selectValue"));
  70             out.endTag(HTMLWriterEx.LI);
  71 
  72             if (settings.isEnvEnabled()) {
  73                 out.startTag(HTMLWriterEx.LI);
  74                 out.writeLink("#" + HTMLReport.anchors[HTMLReport.EXEC_ANCHOR],
  75                               i18n.getString("config.execValue"));
  76                 out.endTag(HTMLWriterEx.LI);
  77             }
  78 
  79             out.startTag(HTMLWriterEx.LI);
  80             out.writeLink("#" + HTMLReport.anchors[HTMLReport.LOC_ANCHOR],
  81                           i18n.getString("config.locValue"));
  82             out.endTag(HTMLWriterEx.LI);
  83             out.endTag(HTMLWriterEx.UL);
  84         }
  85     }
  86 
  87     void writeSummary(ReportWriter out) throws IOException {
  88         super.writeSummary(out);
  89 
  90         // info about test suite
  91         out.startTag(HTMLWriterEx.TABLE);
  92         out.writeAttr(HTMLWriterEx.BORDER, 1);
  93         out.startTag(HTMLWriterEx.TR);
  94         out.writeTH(i18n.getString("config.testSuite"), HTMLWriterEx.ROW);
  95         out.startTag(HTMLWriterEx.TD);
  96 
  97         TestSuite ts = settings.getInterview().getTestSuite();
  98         if (ts != null)
  99             out.writeLink(ts.getRoot());
 100         else
 101             out.write(i18n.getString("config.noTestSuite"));
 102 
 103         out.endTag(HTMLWriterEx.TD);
 104         out.endTag(HTMLWriterEx.TR);
 105         out.endTag(HTMLWriterEx.TABLE);
 106 
 107         // standard values
 108         if (settings.isStdEnabled())
 109             writeStdValSummary(out);
 110 
 111         // optional section
 112         if (settings.isEnvEnabled())
 113             writeExecutionSummary(out);
 114 
 115         // non-optional section
 116         // shows workdir, and report dir
 117         writeLocationSummary(out);
 118     }
 119 
 120     void writeExtraFiles() throws IOException {
 121         // optional section
 122         // tied to question log option
 123         if (settings.isQuestionLogEnabled())
 124             writeConfigInterview();
 125 
 126         // optional section
 127         // tied to writing env values in main report
 128         if (settings.isEnvEnabled())
 129             writeEnvironment();
 130 
 131         // tied to writing standard values in main report
 132         if (settings.isStdEnabled())
 133             writeExcludeList();
 134     }
 135 
 136     private void writeStdValSummary(ReportWriter out) throws IOException {
 137         out.startTag(HTMLWriterEx.H3);
 138         out.writeLinkDestination(HTMLReport.anchors[HTMLReport.SELECT_ANCHOR],
 139                       i18n.getString("config.selectValue"));
 140         out.endTag(HTMLWriterEx.H3);
 141 
 142         out.startTag(HTMLWriterEx.TABLE);
 143         out.writeAttr(HTMLWriterEx.BORDER, 1);
 144 
 145         TestSuite ts = settings.getInterview().getTestSuite();
 146 
 147         String[] tests = settings.getInterview().getTests();
 148         out.startTag(HTMLWriterEx.TR);
 149         out.writeTH(i18n.getString("config.tests.hdr"), HTMLWriterEx.ROW);
 150         out.startTag(HTMLWriterEx.TD);
 151 
 152         if (tests != null && tests.length > 0) {
 153             for (int i = 0; i < tests.length; i++) {
 154                 if (i > 0)
 155                     out.startTag(HTMLWriterEx.BR);
 156                 File file = new File(ts.getTestsDir(), tests[i]);
 157 
 158                 // don't try to link if it doesn't exist _now_
 159                 // this is a combination of legacy behavior (link always)
 160                 // and better behavior for new test suites which are more
 161                 // virtual and that may remap file locations
 162                 if (file.exists())
 163                     out.writeLink(file, tests[i]);
 164                 else
 165                     out.write(tests[i]);
 166             }
 167         }
 168         else {
 169             out.write(i18n.getString("config.tests.all"));
 170         }
 171         out.endTag(HTMLWriterEx.TD);
 172         out.endTag(HTMLWriterEx.TR);
 173 
 174         out.startTag(HTMLWriterEx.TR);
 175         out.writeTH(i18n.getString("config.previous.hdr"), HTMLWriterEx.ROW);
 176 
 177         boolean[] b = settings.getInterview().getPriorStatusValues();
 178         if (b != null) {
 179             String[] ss = { i18n.getString("config.status.passed"),
 180                             i18n.getString("config.status.failed"),
 181                             i18n.getString("config.status.error"),
 182                             i18n.getString("config.status.notRun") };
 183 
 184             StringBuffer sb = new StringBuffer();
 185 
 186             for (int i = 0; i < b.length; i++) {
 187                 if (b[i]) {
 188                     if (sb.length() > 0)
 189                         sb.append(" or "); // XXX needs i18n
 190                     sb.append(ss[i]);
 191                 }
 192              }  // for
 193 
 194             out.writeTD(sb.toString());
 195         }
 196         else
 197             out.writeTD(i18n.getString("config.previous.none"));
 198 
 199         out.endTag(HTMLWriterEx.TR);
 200 
 201         // Print exclude list summary
 202         // NOTE: there may be more than one exclude list
 203         //       the single ExcludeList from params contains the union
 204 
 205         out.startTag(HTMLWriterEx.TR);
 206         out.writeTH(i18n.getString("config.excludeTests"), HTMLWriterEx.ROW);
 207         out.startTag(HTMLWriterEx.TD);
 208 
 209         ExcludeList excludeList = settings.getInterview().getExcludeList();
 210         if (excludeList != null) {
 211             // content cell
 212             out.writeI18N("config.entries", new Object[] { new Integer(excludeList.size()) });
 213 
 214             Parameters.ExcludeListParameters exclParams = settings.getInterview().getExcludeListParameters();
 215             File[] excludeFiles = null;
 216             if (exclParams instanceof Parameters.MutableExcludeListParameters)
 217                 excludeFiles =
 218                     ((Parameters.MutableExcludeListParameters) (exclParams)).getExcludeFiles();
 219 
 220             if (excludeFiles != null && excludeFiles.length > 0) {
 221                 for (int i = 0; i < excludeFiles.length; i++) {
 222                     out.startTag(HTMLWriterEx.BR);
 223                     out.writeLink(HTMLReport.files[HTMLReport.EXCLUDED_HTML],
 224                                     excludeFiles[i].getPath());
 225                 }   // for
 226             }
 227         }
 228         else
 229             out.write(i18n.getString("config.jtx.nofiles"));
 230 
 231         out.endTag(HTMLWriterEx.TD);
 232         out.endTag(HTMLWriterEx.TR);
 233 
 234         // do not display if immutable?
 235         int concurrency = settings.getInterview().getConcurrency();
 236         out.startTag(HTMLWriterEx.TR);
 237         out.writeTH(i18n.getString("config.concurrency"), HTMLWriterEx.ROW);
 238         out.startTag(HTMLWriterEx.TD);
 239         out.write(Integer.toString(concurrency));
 240         out.endTag(HTMLWriterEx.TD);
 241         out.endTag(HTMLWriterEx.TR);
 242 
 243         float timeout = settings.getInterview().getTimeoutFactor();
 244         out.startTag(HTMLWriterEx.TR);
 245         out.writeTH(i18n.getString("config.timeOut"), HTMLWriterEx.ROW);
 246         out.startTag(HTMLWriterEx.TD);
 247         out.write(NumberFormat.getInstance().format(timeout));
 248         out.endTag(HTMLWriterEx.TD);
 249         out.endTag(HTMLWriterEx.TR);
 250 
 251         // keywords
 252         out.startTag(HTMLWriterEx.TR);
 253         out.writeTH(i18n.getString("config.keywords.hdr"), HTMLWriterEx.ROW);
 254 
 255         Keywords keywords = settings.getInterview().getKeywords();
 256         if (keywords != null) {
 257             out.writeTD(settings.getInterview().getKeywords().toString());
 258         }
 259         else {
 260             out.writeTD(i18n.getString("config.keywords.none"));
 261         }
 262         out.endTag(HTMLWriterEx.TR);
 263 
 264         out.endTag(HTMLWriterEx.TABLE);
 265     }
 266 
 267     private void writeExecutionSummary(ReportWriter out) throws IOException {
 268         out.startTag(HTMLWriterEx.H3);
 269         out.writeLinkDestination(HTMLReport.anchors[HTMLReport.EXEC_ANCHOR],
 270                       i18n.getString("config.execValue"));
 271         out.endTag(HTMLWriterEx.H3);
 272 
 273         out.startTag(HTMLWriterEx.TABLE);
 274         out.writeAttr(HTMLWriterEx.BORDER, 1);
 275 
 276         File[] envFiles = null;
 277 
 278         // Question Log
 279         if (settings.isQuestionLogEnabled()) {
 280             out.startTag(HTMLWriterEx.TR);
 281             out.writeTH(i18n.getString("config.configInterview"), HTMLWriterEx.ROW);
 282             out.startTag(HTMLWriterEx.TD);
 283             String name = settings.getInterview().getName();
 284             if (name == null)
 285                 out.writeLink(HTMLReport.files[HTMLReport.CONFIG_HTML],
 286                               i18n.getString("config.confInterview"));
 287             else
 288                 out.writeLink(HTMLReport.files[HTMLReport.CONFIG_HTML], name);
 289 
 290             if (!settings.getInterview().isValid())
 291                out.writeWarning(i18n.getString("config.intIncomplete"));
 292             out.endTag(HTMLWriterEx.TD);
 293             out.endTag(HTMLWriterEx.TR);
 294         }
 295 
 296         // env
 297         Parameters.EnvParameters envParams = settings.getInterview().getEnvParameters();
 298         if (envParams != null &&
 299             envParams instanceof Parameters.LegacyEnvParameters) {
 300             envFiles = ((Parameters.LegacyEnvParameters) envParams).getEnvFiles();
 301             if (envFiles != null) {
 302                 out.startTag(HTMLWriterEx.TR);
 303                 out.writeTH(i18n.getString("config.envFiles"), HTMLWriterEx.ROW);
 304                 out.startTag(HTMLWriterEx.TD);
 305                 for (int i = 0; i < envFiles.length; i++) {
 306                     out.writeLink(new File(envFiles[i].getPath()));
 307                     out.startTag(HTMLWriterEx.BR);
 308                 }
 309                 out.endTag(HTMLWriterEx.TD);
 310                 out.endTag(HTMLWriterEx.TR);
 311             }
 312         }
 313 
 314         TestEnvironment env = settings.getInterview().getEnv();
 315         String envName = (env != null ? env.getName() : null);
 316 
 317         if (envName != null && envName.length() > 0) {
 318             out.startTag(HTMLWriterEx.TR);
 319             out.writeTH(i18n.getString("config.env"), HTMLWriterEx.ROW);
 320             out.startTag(HTMLWriterEx.TD);
 321             out.writeLink(HTMLReport.files[HTMLReport.ENV_HTML], envName);
 322             out.endTag(HTMLWriterEx.TD);
 323             out.endTag(HTMLWriterEx.TR);
 324         }
 325 
 326         out.endTag(HTMLWriterEx.TABLE);
 327     }
 328 
 329     private void writeLocationSummary(ReportWriter out) throws IOException {
 330         WorkDirectory workDir = settings.getInterview().getWorkDirectory();
 331         out.startTag(HTMLWriterEx.H3);
 332         out.writeLinkDestination(HTMLReport.anchors[HTMLReport.LOC_ANCHOR],
 333                       i18n.getString("config.locValue"));
 334         out.endTag(HTMLWriterEx.H3);
 335 
 336         out.startTag(HTMLWriterEx.TABLE);
 337         out.writeAttr(HTMLWriterEx.BORDER, 1);
 338 
 339         out.startTag(HTMLWriterEx.TR);
 340         out.writeTH(i18n.getString("config.wd"), HTMLWriterEx.ROW);
 341         out.startTag(HTMLWriterEx.TD);
 342         out.writeLink(workDir.getRoot());
 343         out.endTag(HTMLWriterEx.TD);
 344         out.endTag(HTMLWriterEx.TR);
 345 
 346         out.startTag(HTMLWriterEx.TR);
 347         out.writeTH(i18n.getString("config.rd"), HTMLWriterEx.ROW);
 348         out.startTag(HTMLWriterEx.TD);
 349         out.writeLink(reportDir);
 350         out.endTag(HTMLWriterEx.TD);
 351         out.endTag(HTMLWriterEx.TR);
 352 
 353         out.endTag(HTMLWriterEx.TABLE);
 354     }
 355 
 356     private void writeConfigInterview() throws IOException {
 357         WizPrint wp = new WizPrint(settings.getInterview());
 358         wp.setShowResponses(true);
 359         wp.write(openWriter(HTMLReport.CONFIG_HTML));
 360     }
 361 
 362     private void writeEnvironment() throws IOException {
 363         TestEnvironment env = settings.getInterview().getEnv();
 364         String envName = (env != null) ? env.getName() : null;
 365 
 366         ReportWriter out = openAuxFile(HTMLReport.ENV_HTML,
 367                     i18n.getString("config.env.title", envName), i18n);
 368 
 369         if (env == null) {
 370             out.write(i18n.getString("config.noEnv"));
 371             return;
 372         }
 373 
 374 
 375         String desc = env.getDescription();
 376 
 377         if (desc != null) {
 378             out.startTag(HTMLWriterEx.H2);
 379             out.write(desc);
 380             out.endTag(HTMLWriterEx.H2);
 381         }
 382 
 383         Set<String[]> envTable = new TreeSet<>(new StringArrayComparator());
 384 
 385         for (Iterator<TestEnvironment.Element> i = env.elements().iterator(); i.hasNext(); ) {
 386             TestEnvironment.Element envElem = (i.next());
 387             String[] envTableRow = {envElem.getKey(), envElem.getValue()};
 388             envTable.add(envTableRow);
 389         }
 390 
 391         out.startTag(HTMLWriterEx.TABLE);
 392         out.writeAttr(HTMLWriterEx.BORDER, 1);
 393         for (Iterator<String[]> i = envTable.iterator(); i.hasNext(); ) {
 394             String[] envEntry = (i.next());
 395             out.startTag(HTMLWriterEx.TR);
 396 
 397             for (int j = 0; j < envEntry.length; j++ ) {
 398                 out.startTag(HTMLWriterEx.TD);
 399                 out.write(envEntry[j]);
 400                 out.endTag(HTMLWriterEx.TD);
 401             }
 402 
 403             out.endTag(HTMLWriterEx.TR);
 404         }
 405         out.endTag(HTMLWriterEx.TABLE);
 406         out.close();
 407     }
 408 
 409     private void writeExcludeList() throws IOException {
 410         ReportWriter out = openAuxFile(HTMLReport.EXCLUDED_HTML,
 411                             i18n.getString("config.excludedTests"), i18n);
 412 
 413         ExcludeList excludeList = settings.getInterview().getExcludeList();
 414 
 415         if (excludeList == null || excludeList.size() == 0){
 416             out.writeI18N("config.excl.none");
 417         }
 418         else {
 419             SortedSet<ExcludeList.Entry> sortedEntries = new TreeSet<>(new ExcludeListEntryComparator());
 420             for (Iterator<?> iter = excludeList.getIterator(false); iter.hasNext(); )
 421                 sortedEntries.add((ExcludeList.Entry)iter.next());
 422 
 423             out.startTag(HTMLWriterEx.TABLE);
 424             out.writeAttr(HTMLWriterEx.BORDER, 1);
 425             out.startTag(HTMLWriterEx.TR);
 426             out.startTag(HTMLWriterEx.TH);
 427             out.writeI18N("config.excl.name");
 428             out.endTag(HTMLWriterEx.TH);
 429             out.startTag(HTMLWriterEx.TH);
 430             out.writeI18N("config.excl.testcase");
 431             out.endTag(HTMLWriterEx.TH);
 432             out.startTag(HTMLWriterEx.TH);
 433             out.writeI18N("config.excl.bugids");
 434             out.endTag(HTMLWriterEx.TH);
 435             out.startTag(HTMLWriterEx.TH);
 436             out.writeI18N("config.excl.platforms");
 437             out.endTag(HTMLWriterEx.TH);
 438             out.startTag(HTMLWriterEx.TH);
 439             out.writeI18N("config.excl.synopsis");
 440             out.endTag(HTMLWriterEx.TH);
 441             out.endTag(HTMLWriterEx.TR);
 442 
 443             for (Iterator<ExcludeList.Entry> iter = sortedEntries.iterator(); iter.hasNext(); ) {
 444                 ExcludeList.Entry e = (iter.next());
 445                 out.startTag(HTMLWriterEx.TR);
 446                 writeTD(out, e.getRelativeURL());
 447                 writeTD(out, e.getTestCases());
 448 
 449                 out.startTag(HTMLWriterEx.TD);
 450                 String[] bugIds = e.getBugIdStrings();
 451                 for (int i = 0; i < bugIds.length; i++) {
 452                     if (i > 0)
 453                         out.write(" ");
 454                     out.write(bugIds[i]);
 455                 }
 456                 out.endTag(HTMLWriterEx.TD);
 457 
 458                 writeTD(out, StringArray.join(e.getPlatforms()));
 459                 writeTD(out, e.getSynopsis());
 460 
 461                 out.endTag(HTMLWriterEx.TR);
 462             }
 463             out.endTag(HTMLWriterEx.TABLE);
 464         }
 465 
 466         out.newLine();
 467         out.close();
 468     }
 469 
 470     private void writeTD(ReportWriter out, String text) throws IOException {
 471         out.startTag(HTMLWriterEx.TD);
 472         if (text == null || text.length() ==0)
 473             out.writeEntity("&nbsp;");
 474         else
 475             out.write(text);
 476         out.endTag(HTMLWriterEx.TD);
 477     }
 478 
 479     private static class ExcludeListEntryComparator implements Comparator<ExcludeList.Entry>
 480     {
 481         public int compare(ExcludeList.Entry e1, ExcludeList.Entry e2) {
 482             int x = compare(e1.getRelativeURL(), e2.getRelativeURL());
 483             if (x == 0)
 484                 x = compare(e1.getTestCases(), e2.getTestCases());
 485             return x;
 486 
 487         }
 488         private static int compare(String[] a, String[] b) {
 489             int alen = (a == null ? 0 : a.length);
 490             int blen = (b == null ? 0 : b.length);
 491             for (int i = 0; i < Math.min(alen, blen); i++) {
 492                 int c = compare(a[i], b[i]);
 493                 if (c != 0)
 494                     return c;
 495             }
 496             return (alen < blen ? -1 : alen == blen ? 0 : +1);
 497     }
 498 
 499         private static int compare(String a, String b) {
 500             return (a == null && b == null ? 0
 501                     : a == null ? -1
 502                     : b == null ? +1
 503                     : a.compareTo(b));
 504         }
 505     }
 506 
 507     private final I18NResourceBundle i18n;
 508 
 509 }