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.util.Iterator;
  32 import java.util.TreeSet;
  33 
  34 import com.sun.javatest.Status;
  35 import com.sun.javatest.TestDescription;
  36 import com.sun.javatest.TestResult;
  37 import com.sun.javatest.TestResultTable;
  38 import com.sun.javatest.util.I18NResourceBundle;
  39 
  40 /**
  41  * Summarize the status, pass/fail/error of the tests which we are reporting on.
  42  * Also generate output in failed.html, error.html, etc...
  43  */
  44 class ResultSection extends HTMLSection {
  45     ResultSection(HTMLReport parent, ReportSettings settings, File dir, I18NResourceBundle i18n,
  46             TreeSet<TestResult>[] sortedResults) {
  47         super(i18n.getString("result.title"), settings, dir, parent);
  48         this.i18n = i18n;
  49 
  50         headings = new String[] {
  51             i18n.getString("result.heading.passed"),
  52             i18n.getString("result.heading.failed"),
  53             i18n.getString("result.heading.errors"),
  54             i18n.getString("result.heading.notRun")
  55         };
  56 
  57         resultTable = settings.getInterview().getWorkDirectory().getTestResultTable();
  58         initFiles = settings.getInitialFiles();
  59         lists = sortedResults;
  60 
  61         for (TreeSet s: sortedResults)
  62             totalFound += s.size();
  63         /*
  64         lists = new TreeSet[Status.NUM_STATES];
  65         for (int i = 0; i < lists.length; i++ )
  66             lists[i] = new TreeSet(new TestResultsByNameComparator());
  67 
  68         Iterator iter;
  69         try {
  70             TestFilter[] fs = null;
  71 
  72             // Note: settings.filter should not really be null, modernized clients
  73             //   of this class should set the filter before asking for a report.
  74             if (settings.filter == null)
  75                 fs = new TestFilter[0];
  76             else
  77                 fs = new TestFilter[] {settings.filter};
  78 
  79 
  80             iter = ((initFiles == null)
  81                     ? resultTable.getIterator(fs)
  82                     : resultTable.getIterator(initFiles, fs));
  83         }
  84         catch (TestResultTable.Fault f) {
  85             throw new JavaTestError(i18n.getString("result.testResult.err"));
  86         }
  87 
  88         for (; iter.hasNext(); ) {
  89             TestResult tr = (TestResult) (iter.next());
  90             Status s = tr.getStatus();
  91             TreeSet list = lists[s == null ? Status.NOT_RUN : s.getType()];
  92             list.add(tr);
  93             totalFound++;
  94         }
  95 
  96         parent.setResults(lists);
  97         */
  98     }
  99 
 100     @Override
 101     void writeSummary(ReportWriter out) throws IOException {
 102         super.writeSummary(out);
 103 
 104         out.startTag(HTMLWriterEx.TABLE);
 105         out.writeAttr(HTMLWriterEx.BORDER, 1);
 106 
 107 
 108         boolean thirdColumn = false;
 109         boolean secondColumn = false;
 110         for (int i = 0; i < lists.length; i++ ) {
 111             thirdColumn |= (settings.isStateFileEnabled(i) && hasGroupedReport(i));
 112             secondColumn |= settings.isStateFileEnabled(i) ;
 113         }
 114         String grouped = i18n.getString("result.grouped");
 115         String plain = i18n.getString("result.plain");
 116 
 117         for (int i = 0; i < lists.length; i++ ) {
 118             String reportFile = HTMLReport.files[fileCodes[i]];
 119             TreeSet l = lists[i];
 120 
 121             int n = l.size();
 122             if (n > 0) {
 123                 out.startTag(HTMLWriterEx.TR);
 124                 out.writeTH(headings[i], HTMLWriterEx.ROW);
 125                 out.startTag(HTMLWriterEx.TD);
 126                 out.writeAttr(HTMLWriterEx.STYLE, HTMLWriterEx.TEXT_RIGHT);
 127                 out.write(Integer.toString(n));
 128                 out.endTag(HTMLWriterEx.TD);
 129 
 130                 if (secondColumn) {
 131                     out.startTag(HTMLWriterEx.TD);
 132                     if (settings.isStateFileEnabled(i)) {
 133                         out.writeLink(reportFile, plain);
 134                     } else {
 135                         out.writeLine(" ");
 136                     }
 137                     out.endTag(HTMLWriterEx.TD);
 138                 }
 139 
 140                 if (thirdColumn) {
 141                     out.startTag(HTMLWriterEx.TD);
 142                     if (hasGroupedReport(i) && settings.isStateFileEnabled(i)) {
 143                         out.writeLink(HTMLReport.files[groupedFileCodes[i]], grouped);
 144                     } else {
 145                         out.writeLine(" ");
 146                     }
 147                     out.endTag(HTMLWriterEx.TD);
 148                 }
 149 
 150                 out.endTag(HTMLWriterEx.TR);
 151             }
 152 
 153         }
 154 
 155         out.startTag(HTMLWriterEx.TR);
 156         out.writeTH(i18n.getString("result.total"), HTMLWriterEx.ROW);
 157         out.writeTD(Integer.toString(totalFound));
 158 
 159         if (secondColumn) {
 160             out.writeTD("");
 161         }
 162 
 163         if (thirdColumn) {
 164             out.writeTD("");
 165         }
 166 
 167         out.endTag(HTMLWriterEx.TR);
 168         out.endTag(HTMLWriterEx.TABLE);
 169     }
 170 
 171     private boolean hasGroupedReport(int st) {
 172         return st == Status.FAILED || st == Status.PASSED || st == Status.ERROR ;
 173     }
 174 
 175     @Override
 176     void writeExtraFiles() throws IOException {
 177         writeStatusFiles();
 178     }
 179 
 180     private void writeStatusFiles() throws IOException {
 181         for (int i = 0; i < lists.length; i++ ) {
 182             // each file is optional
 183             if (!settings.isStateFileEnabled(i))
 184                 continue;
 185 
 186             writeUnGroupedReport(i);
 187 
 188             if (hasGroupedReport(i)) {
 189                 // re-sort it
 190                 TreeSet<TestResult> newS = new TreeSet<>(new TestResultsByStatusAndTitleComparator());
 191                 newS.addAll(lists[i]);
 192                 lists[i] = newS;
 193 
 194                 writeGroupedReport(i);
 195             }
 196        }
 197     }
 198 
 199     private void writeUnGroupedReport(int i) throws IOException {
 200 
 201         ReportWriter out = openAuxFile(fileCodes[i], headings[i], i18n);
 202         try {
 203             TreeSet list = lists[i];
 204             if (list.size() > 0) {
 205                 boolean inList = false;
 206 
 207                 for (Iterator iter = list.iterator(); iter.hasNext(); ) {
 208                     TestResult e = (TestResult) (iter.next());
 209                     String title;
 210                     try {
 211                         TestDescription e_td = e.getDescription();
 212                         title = e_td.getTitle();
 213                     }
 214                     catch (TestResult.Fault ex) {
 215                         title = null;
 216                     }
 217 
 218                     Status e_s = e.getStatus();
 219                     if (!inList) {
 220                         inList = true;
 221                     }
 222 
 223                     //File eFile = e.getFile();
 224                     String eWRPath = e.getWorkRelativePath();
 225                     File eFile = new File(workDirRoot, eWRPath.replace('/', File.separatorChar));
 226                     String eName = e.getTestName();
 227                     if (eFile == null || e_s.getType() == Status.NOT_RUN)
 228                         out.write(eName);
 229                     else
 230                         out.writeLink(eFile, eName);
 231 
 232                     if (title != null)
 233                         out.write(": " + title);
 234                     out.startTag(HTMLWriterEx.BR);
 235                     out.newLine();
 236                 }
 237             }
 238         }
 239         finally {
 240             out.close();
 241         }
 242 
 243 
 244     }
 245 
 246     private void writeGroupedReport(int i) throws IOException {
 247         ReportWriter out = openAuxFile(groupedFileCodes[i], headings[i], i18n);
 248         out.write(i18n.getString("result.groupByStatus"));
 249         try {
 250             TreeSet list = lists[i];
 251             if (list.size() > 0) {
 252                 boolean inList = false;
 253                 String currentHead = null;
 254                 for (Iterator iter = list.iterator(); iter.hasNext(); ) {
 255                     TestResult e = (TestResult) (iter.next());
 256                     String title;
 257                     try {
 258                         TestDescription e_td = e.getDescription();
 259                         title = e_td.getTitle();
 260                     }
 261                     catch (TestResult.Fault ex) {
 262                         title = null;
 263                     }
 264 
 265                     Status e_s = e.getStatus();
 266                     if (!e_s.getReason().equals(currentHead)) {
 267                         currentHead = e_s.getReason();
 268                         if (inList) {
 269                             inList = false;
 270                             out.endTag(HTMLWriterEx.UL);
 271                             out.newLine();
 272                         }
 273                         out.startTag(HTMLWriterEx.H4);
 274                         out.write(currentHead.length() == 0 ? i18n.getString("result.noReason") : currentHead);
 275                         out.endTag(HTMLWriterEx.H4);
 276                         out.newLine();
 277                     }
 278                     if (!inList) {
 279                         inList = true;
 280                         out.startTag(HTMLWriterEx.UL);
 281                     }
 282                     out.startTag(HTMLWriterEx.LI);
 283 
 284                     //File eFile = e.getFile();
 285                     String eWRPath = e.getWorkRelativePath();
 286                     File eFile = new File(workDirRoot, eWRPath.replace('/', File.separatorChar));
 287                     String eName = e.getTestName();
 288                     if (eFile == null || e_s.getType() == Status.NOT_RUN)
 289                         out.write(eName);
 290                     else
 291                         out.writeLink(eFile, eName);
 292 
 293                     if (title != null)
 294                         out.write(": " + title);
 295                     out.newLine();
 296                 }
 297                 if (inList) {
 298                     inList = false;
 299                     out.endTag(HTMLWriterEx.UL);
 300                 }
 301             }
 302         }
 303         finally {
 304             out.close();
 305         }
 306     }
 307 
 308     private TestResultTable resultTable;
 309     private File[] initFiles;
 310 
 311     TreeSet<TestResult>[] lists;
 312     private int totalFound;
 313 
 314     private final int[] fileCodes = {
 315         HTMLReport.PASSED_HTML,
 316         HTMLReport.FAILED_HTML,
 317         HTMLReport.ERROR_HTML,
 318         HTMLReport.NOTRUN_HTML
 319     };
 320 
 321     private final int[] groupedFileCodes = {
 322         HTMLReport.PASSED_HTML_2,
 323         HTMLReport.FAILED_HTML_2,
 324         HTMLReport.ERROR_HTML_2
 325     };
 326 
 327     private final I18NResourceBundle i18n;
 328 
 329     private final String[] headings;
 330 }