jtutils.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 789 行 · 第 1/2 页

JAVA
789 行
字号
	    
	    out.endTag(HTMLWriter.BODY);
	    out.endTag(HTMLWriter.HTML);
	    out.close();
	}
	catch (IOException e) {
	    // should not happen with StringWriter
	}
	return sw.toString();
    }

    public static String createNotRunSummary() {
	StringWriter sw = new StringWriter();
	try {
	    HTMLWriter out = new HTMLWriter(sw);
	    out.startTag(HTMLWriter.HTML);
	    out.startTag(HTMLWriter.BODY);
	    out.writeI18N("This test has not yet been run.");
	    out.endTag(HTMLWriter.BODY);
	    out.endTag(HTMLWriter.HTML);
	    out.close();
	}
	catch (IOException e) {
	    // should not happen, with StringWriter
	}
	return sw.toString();
    }

    public static String createSummary(TestResult testResult) {
	StringWriter sw = new StringWriter();
	try {
	    HTMLWriter out = new HTMLWriter(sw);
	    out.startTag(HTMLWriter.HTML);
	    out.startTag(HTMLWriter.BODY);
	    //out.writeStyleAttr(bodyStyle);
	    
	    String[] scriptAndArgs;
	    try {
		scriptAndArgs = StringArray.split(testResult.getProperty(TestResult.SCRIPT));
	    }
	    catch (TestResult.Fault e) {
		scriptAndArgs = null;
	    }
	    
	    String script = (((scriptAndArgs == null) || (scriptAndArgs.length == 0))
			     ? "unknown" : scriptAndArgs[0]);
	    out.write("The test was run by the following script class:");
	    out.startTag(HTMLWriter.TABLE);
	    out.writeAttr(HTMLWriter.BORDER, "0");
	    //out.writeStyleAttr(tableStyle);
	    out.startTag(HTMLWriter.TR);
	    out.startTag(HTMLWriter.TD);
	    out.startTag(HTMLWriter.CODE);
	    out.write(script);
	    out.endTag(HTMLWriter.CODE);
	    out.endTag(HTMLWriter.TD);
	    out.endTag(HTMLWriter.TR);
	    out.endTag(HTMLWriter.TABLE);
	    if (scriptAndArgs != null && scriptAndArgs.length > 1) {
		out.write("The script was invoked with the following arguments:");
		out.startTag(HTMLWriter.TABLE);
		out.writeAttr(HTMLWriter.BORDER, "0");
		//out.writeStyleAttr(tableStyle);
		for (int i = 1; i < scriptAndArgs.length; i++) {
		    out.startTag(HTMLWriter.TR);
		    out.startTag(HTMLWriter.TD);
		    out.startTag(HTMLWriter.CODE);
		    out.write(scriptAndArgs[i]);
		    out.endTag(HTMLWriter.CODE);
		    out.endTag(HTMLWriter.TD);
		    out.endTag(HTMLWriter.TR);
		}
		out.endTag(HTMLWriter.TABLE);
	    }

	    if (testResult.getSectionCount() > 0) {
		TestResult.Section s = testResult.getSection(0);
		if (s.getTitle().equals(TestResult.MSG_SECTION_NAME)) {
		    out.write("The script logged the following messages:");
		    out.startTag(HTMLWriter.TABLE);
		    out.writeAttr(HTMLWriter.BORDER, "0");
		    //out.writeStyleAttr(tableStyle);
		    String[] names = s.getOutputNames();
		    for (int i = 0; i < names.length; i++) {
			String name = names[i];
			String text = s.getOutput(name);
			out.startTag(HTMLWriter.TR);
			out.startTag(HTMLWriter.TD);
			out.writeLink("#" + name, name/*, linkStyle*/);
			out.endTag(HTMLWriter.TD);
			out.endTag(HTMLWriter.TR);
		    }
		    out.endTag(HTMLWriter.TABLE);
		}
	    }
	   
	    // generate a table showing the various sections
	    if (testResult.getSectionCount() > 0) {
		out.startTag(HTMLWriter.H3);
		//out.writeStyleAttr(h3Style);
		out.write("Result Sections");
		out.endTag(HTMLWriter.H3);
		out.write("The following result sections were recorded:");
		out.startTag(HTMLWriter.TABLE);
		out.writeAttr(HTMLWriter.BORDER, "0");
		//out.writeStyleAttr(tableStyle);
		for (int i = 0; i < testResult.getSectionCount(); i++) {
		    TestResult.Section s = testResult.getSection(i);
		    if (s.getTitle().equals(TestResult.MSG_SECTION_NAME)) 
			continue; // already done, above
		    out.startTag(HTMLWriter.TR);
		    out.startTag(HTMLWriter.TD);
		    out.startTag(HTMLWriter.OBJECT);
		    out.writeAttr(HTMLWriter.CLASSID, "com.sun.javatest.tool.IconLabel");
		    out.writeParam("type", "testSection");
		    out.writeParam("state", getStatusKey(s.getStatus().getType()));
		    out.endTag(HTMLWriter.OBJECT);
		    out.writeLink(String.valueOf(i), s.getTitle()/*, linkStyle*/);
		    out.endTag(HTMLWriter.TD);
		    out.endTag(HTMLWriter.TR);
		}
		out.endTag(HTMLWriter.TABLE);
		
		out.startTag(HTMLWriter.H3);
		//out.writeStyleAttr(h3Style);
		out.write("Result");
		out.endTag(HTMLWriter.H3);
		out.write("Based on the results of the preceding sections, the script determined that the result of the test is:");
	    }
	    else {
		out.startTag(HTMLWriter.H3);
		//out.writeStyleAttr(h3Style);
		out.write("Result");
		out.endTag(HTMLWriter.H3);
		out.write("The script did not run any commands, but determined that that the result of the test was:");
	    } 
	    
	    Status s = testResult.getStatus();
	    out.startTag(HTMLWriter.TABLE);
	    out.writeAttr(HTMLWriter.BORDER, "0");
	    //out.writeStyleAttr(tableStyle);
	    out.startTag(HTMLWriter.TR);
	    out.startTag(HTMLWriter.TD);
	    out.startTag(HTMLWriter.OBJECT);
	    out.writeAttr(HTMLWriter.CLASSID, "com.sun.javatest.tool.IconLabel");
	    out.writeParam("type", "test");
	    out.writeParam("state", getStatusKey(s.getType()));
	    out.endTag(HTMLWriter.OBJECT);
	    out.endTag(HTMLWriter.TD);
	    out.startTag(HTMLWriter.TD);
	    out.write(s.toString());
	    out.endTag(HTMLWriter.TD);
	    out.endTag(HTMLWriter.TR);
	    out.endTag(HTMLWriter.TABLE);
	    
	    out.endTag(HTMLWriter.BODY);
	    out.endTag(HTMLWriter.HTML);
	    out.close();
	}
	catch (IOException e) {
	    // should not happen, writing to StringWriter
	}
	catch (TestResult.ReloadFault e) {
	    throw new JavaTestError("Error loading result file for " +
				    testResult.getTestName());
	}

	return sw.toString();
    }
    
    public static String createStatusSummary(TestResult testResult) {
	StringWriter sw = new StringWriter();
	try {
	    HTMLWriter out = new HTMLWriter(sw);
	    out.startTag(HTMLWriter.HTML);
	    out.startTag(HTMLWriter.BODY);
	    //out.writeStyleAttr(bodyStyle);

	    if (testResult.getSectionCount() > 0)
		out.write("Based on the results of the preceding sections, the script determined that the result of the test is:");
	    else
		out.write("The script did not run any commands, but determined that that the result of the test was:");
	    
	    Status s = testResult.getStatus();
	    out.startTag(HTMLWriter.TABLE);
	    out.writeAttr(HTMLWriter.BORDER, "0");
	    out.startTag(HTMLWriter.TR);
	    out.startTag(HTMLWriter.TD);
	    out.startTag(HTMLWriter.OBJECT);
	    out.writeAttr(HTMLWriter.CLASSID, "com.sun.javatest.tool.IconLabel");
	    out.writeParam("type", "test");
	    out.writeParam("state", getStatusKey(s.getType()));
	    out.endTag(HTMLWriter.OBJECT);
	    out.endTag(HTMLWriter.TD);
	    out.startTag(HTMLWriter.TD);
	    out.write(s.toString());
	    out.endTag(HTMLWriter.TD);
	    out.endTag(HTMLWriter.TR);
	    out.endTag(HTMLWriter.TABLE);
	    out.endTag(HTMLWriter.BODY);
	    out.endTag(HTMLWriter.HTML);
	    out.close();
	}
	catch (IOException e) {
	    // should not happen, for StringWriter
	}
	return sw.toString();	
    }

    
    private static boolean checkFilters(File file, String [] filters) {
        for (int i = 0; i < filters.length; i++) {
            if (filters[i].equals(file.getName())) {
                return true;
            }
        }
        return false;
    }
    
    public static File[] getFiles(File file, boolean updateFinder, String [] filters) {
        if (updateFinder) {
            testFinder.read(file);
        }
        File [] files = testFinder.getFiles();
        ArrayList <File> filesList = new ArrayList();
        for (int i = 0; i < files.length; i++) {
            if (checkFilters(files[i], filters)) {
                filesList.add(files[i]);
            }
        }
        File [] outFiles = new File[filesList.size()];
        filesList.toArray(outFiles);
        return outFiles;
    }
    
    private static String getStatusKey(int i) {
	String s;
	switch (i) {
	case Status.PASSED:  return "passed"; 
	case Status.FAILED:  return "failed";
	case Status.ERROR:   return "error";
	case Status.NOT_RUN: return "notRun";
	default:	     return "unknown";
	}
    }
    
    public static boolean isScriptMessagesSection(TestResult.Section section) {
        return (section != null && section.getTitle().equals(TestResult.MSG_SECTION_NAME));
    }
}

class Exec {

  private static boolean verbose = true;

  /** Determines if the Exec class should print which commands
    * are being executed, and prints error messages if a problem
    * is found. Default is true.
    *
    * @param verboseFlag true: print messages, false: don抰.
    */

  public static void setVerbose(boolean verboseFlag) {
    verbose = verboseFlag;
  }

  /** Will Exec print status messages? */

  public static boolean getVerbose() {
    return(verbose);
  }

  /** Starts a process to execute the command. Returns
    * immediately, even if the new process is still running.
    *
    * @param command The <B>full</B> pathname of the command to
    * be executed. No shell built-ins (e.g., "cd") or shell
    * meta-chars (e.g. ">") are allowed.
    * @return false if a problem is known to occur, but since
    * this returns immediately, problems aren抰 usually found
    * in time. Returns true otherwise.
    */

  public static boolean exec(String command) {
    return(exec(command, false, false));
  }

  /** Starts a process to execute the command. Waits for the
    * process to finish before returning.
    *
    * @param command The <B>full</B> pathname of the command to
    * be executed. No shell built-ins or shell metachars are
    * allowed.
    * @return false if a problem is known to occur, either due
    * to an exception or from the subprocess returning a
    * nonzero value. Returns true otherwise.
    */

  public static boolean execWait(String command) {
    return(exec(command, false, true));
  }

  /** Starts a process to execute the command. Prints any output
    * the command produces.
    *
    * @param command The <B>full</B> pathname of the command to
    * be executed. No shell built-ins or shell meta-chars are
    * allowed.
    * @return false if a problem is known to occur, either due
    * to an exception or from the subprocess returning a
    * nonzero value. Returns true otherwise.
    */

  public static boolean execPrint(String command) {
    return(exec(command, true, true));
  }

  /** This creates a Process object via Runtime.getRuntime.exec()
    * Depending on the flags, it may call waitFor on the process
    * to avoid continuing until the process terminates, and open
    * an input stream from the process to read the results.
    */

  private static boolean exec(String command,
                              boolean printResults,
                              boolean wait) {
    if (verbose) {
      printSeparator();
      System.out.println("Executing '" + command + "'.");
    }
    try {
      // Start running command, returning immediately.
      Process p  = Runtime.getRuntime().exec(command);

      // Print the output. Since we read until there is no more
      // input, this causes us to wait until the process is
      // completed.
      if(printResults) {
        BufferedReader buffer = new BufferedReader(
          new InputStreamReader(p.getInputStream()));
        String s = null;
        try {
          while ((s = buffer.readLine()) != null) {
            System.out.println("Output: " + s);
          }
          buffer.close();
          if (p.exitValue() != 0) {
            if (verbose) {
              printError(command + " -- p.exitValue() != 0");
            }
            return(false);
          }
        } catch (Exception e) {
          // Ignore read errors; they mean the process is done.
        }

      // If not printing the results, then we should call waitFor
      // to stop until the process is completed.
      } else if (wait) {
        try {
          System.out.println(" ");
          int returnVal = p.waitFor();
          if (returnVal != 0) {
            if (verbose) {
              printError(command);
            }
            return(false);
          }
        } catch (Exception e) {
          if (verbose) {
            printError(command, e);
          }
          return(false);
        }
      }
    } catch (Exception e) {
      if (verbose) {
        printError(command, e);
      }
      return(false);
    }
    return(true);
  }

  private static void printError(String command,
                                 Exception e) {
    System.out.println("Error doing exec(" + command + "): " +
                        e.getMessage());
    System.out.println("Did you specify the full " +
                       "pathname?");
  }

  private static void printError(String command) {
    System.out.println("Error executing 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?