< prev index next >

test/lib/jdk/test/lib/cds/CDSTestUtils.java

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.test.lib.cds;
  24 
  25 import java.io.IOException;
  26 import java.io.File;
  27 import java.io.FileOutputStream;
  28 import java.io.PrintStream;
  29 import java.text.SimpleDateFormat;
  30 import java.util.ArrayList;
  31 import java.util.Date;
  32 import jdk.test.lib.Utils;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 
  37 // This class contains common test utilities for testing CDS
  38 public class CDSTestUtils {
  39     public interface Checker {
  40         public void check(OutputAnalyzer output) throws Exception;
  41     }
  42 
  43     /*
  44      * INTRODUCTION
  45      *
  46      * When testing various CDS functionalities, we need to launch JVM processes
  47      * using a "launch method" (such as TestCommon.run), and analyze the results of these
  48      * processes.
  49      *
  50      * While typical jtreg tests would use OutputAnalyzer in such cases, due to the
  51      * complexity of CDS failure modes, we have added the CDSTestUtils.Result class
  52      * to make the analysis more convenient and less error prone.
  53      *
  54      * A Java process can end in one of the following 4 states:
  55      *


 206         public Result assertAbnormalExit(String... matches) throws Exception {
 207             if (!hasMappingFailure) {
 208                 checkMatches(output, matches);
 209                 output.shouldNotHaveExitValue(0);
 210             }
 211 
 212             return this;
 213         }
 214     }
 215 
 216     // Specify this property to copy sdandard output of the child test process to
 217     // the parent/main stdout of the test.
 218     // By default such output is logged into a file, and is copied into the main stdout.
 219     public static final boolean CopyChildStdoutToMainStdout =
 220         Boolean.valueOf(System.getProperty("test.cds.copy.child.stdout", "true"));
 221 
 222     // This property is passed to child test processes
 223     public static final String TestTimeoutFactor = System.getProperty("test.timeout.factor", "1.0");
 224 
 225     public static final String UnableToMapMsg =
 226         "Unable to map shared archive: test did not complete; assumed PASS";
 227 
 228     // Create bootstrap CDS archive,
 229     // use extra JVM command line args as a prefix.
 230     // For CDS tests specifying prefix makes more sense than specifying suffix, since
 231     // normally there are no classes or arguments to classes, just "-version"
 232     // To specify suffix explicitly use CDSOptions.addSuffix()
 233     public static OutputAnalyzer createArchive(String... cliPrefix)
 234         throws Exception {
 235         return createArchive((new CDSOptions()).addPrefix(cliPrefix));
 236     }
 237 
 238     // Create bootstrap CDS archive
 239     public static OutputAnalyzer createArchive(CDSOptions opts)
 240         throws Exception {
 241 
 242         startNewArchiveName();
 243 
 244         ArrayList<String> cmd = new ArrayList<String>();
 245 
 246         for (String p : opts.prefix) cmd.add(p);


 297     // exceptions match. Pass null if you wish not to re-throw any exception.
 298     public static boolean checkCommonExecExceptions(OutputAnalyzer output, Exception e)
 299         throws Exception {
 300         if (output.getStdout().contains("http://bugreport.java.com/bugreport/crash.jsp")) {
 301             throw new RuntimeException("Hotspot crashed");
 302         }
 303         if (output.getStdout().contains("TEST FAILED")) {
 304             throw new RuntimeException("Test Failed");
 305         }
 306         if (output.getOutput().contains("shared class paths mismatch")) {
 307 //            throw new RuntimeException("shared class paths mismatch");
 308         }
 309         if (output.getOutput().contains("Unable to unmap shared space")) {
 310             throw new RuntimeException("Unable to unmap shared space");
 311         }
 312 
 313         // Special case -- sometimes Xshare:on fails because it failed to map
 314         // at given address. This behavior is platform-specific, machine config-specific
 315         // and can be random (see ASLR).
 316         if (isUnableToMap(output)) {
 317             System.out.println(UnableToMapMsg);
 318             return true;
 319         }
 320 
 321         if (e != null) {
 322             throw e;
 323         }
 324         return false;
 325     }
 326 
 327     public static boolean checkCommonExecExceptions(OutputAnalyzer output) throws Exception {
 328         return checkCommonExecExceptions(output, null);
 329     }
 330 
 331 
 332     // Check the output for indication that mapping of the archive failed.
 333     // Performance note: this check seems to be rather costly - searching the entire
 334     // output stream of a child process for multiple strings. However, it is necessary
 335     // to detect this condition, a failure to map an archive, since this is not a real
 336     // failure of the test or VM operation, and results in a test being "skipped".
 337     // Suggestions to improve:
 338     // 1. VM can designate a special exit code for such condition.


 429                                      String... extraMatches) throws Exception {
 430         try {
 431             if ("on".equals(opts.xShareMode)) {
 432                 output.shouldContain("sharing");
 433             }
 434             output.shouldHaveExitValue(0);
 435         } catch (RuntimeException e) {
 436             checkCommonExecExceptions(output, e);
 437             return output;
 438         }
 439 
 440         checkMatches(output, extraMatches);
 441         return output;
 442     }
 443 
 444 
 445     public static OutputAnalyzer checkExecExpectError(OutputAnalyzer output,
 446                                              int expectedExitValue,
 447                                              String... extraMatches) throws Exception {
 448         if (isUnableToMap(output)) {
 449             System.out.println(UnableToMapMsg);
 450             return output;
 451         }
 452 
 453         output.shouldHaveExitValue(expectedExitValue);
 454         checkMatches(output, extraMatches);
 455         return output;
 456     }
 457 
 458     public static OutputAnalyzer checkMatches(OutputAnalyzer output,
 459                                               String... matches) throws Exception {
 460         for (String match : matches) {
 461             output.shouldContain(match);
 462         }
 463         return output;
 464     }
 465 
 466 
 467     // get the file object for the test artifact
 468     public static File getTestArtifact(String name, boolean checkExistence) {
 469         File dir = new File(System.getProperty("test.classes", "."));
 470         File file = new File(dir, name);




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.test.lib.cds;
  24 
  25 import java.io.IOException;
  26 import java.io.File;
  27 import java.io.FileOutputStream;
  28 import java.io.PrintStream;
  29 import java.text.SimpleDateFormat;
  30 import java.util.ArrayList;
  31 import java.util.Date;
  32 import jdk.test.lib.Utils;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 import jtreg.SkippedException;
  36 
  37 // This class contains common test utilities for testing CDS
  38 public class CDSTestUtils {
  39     public interface Checker {
  40         public void check(OutputAnalyzer output) throws Exception;
  41     }
  42 
  43     /*
  44      * INTRODUCTION
  45      *
  46      * When testing various CDS functionalities, we need to launch JVM processes
  47      * using a "launch method" (such as TestCommon.run), and analyze the results of these
  48      * processes.
  49      *
  50      * While typical jtreg tests would use OutputAnalyzer in such cases, due to the
  51      * complexity of CDS failure modes, we have added the CDSTestUtils.Result class
  52      * to make the analysis more convenient and less error prone.
  53      *
  54      * A Java process can end in one of the following 4 states:
  55      *


 206         public Result assertAbnormalExit(String... matches) throws Exception {
 207             if (!hasMappingFailure) {
 208                 checkMatches(output, matches);
 209                 output.shouldNotHaveExitValue(0);
 210             }
 211 
 212             return this;
 213         }
 214     }
 215 
 216     // Specify this property to copy sdandard output of the child test process to
 217     // the parent/main stdout of the test.
 218     // By default such output is logged into a file, and is copied into the main stdout.
 219     public static final boolean CopyChildStdoutToMainStdout =
 220         Boolean.valueOf(System.getProperty("test.cds.copy.child.stdout", "true"));
 221 
 222     // This property is passed to child test processes
 223     public static final String TestTimeoutFactor = System.getProperty("test.timeout.factor", "1.0");
 224 
 225     public static final String UnableToMapMsg =
 226         "Unable to map shared archive: test did not complete";
 227 
 228     // Create bootstrap CDS archive,
 229     // use extra JVM command line args as a prefix.
 230     // For CDS tests specifying prefix makes more sense than specifying suffix, since
 231     // normally there are no classes or arguments to classes, just "-version"
 232     // To specify suffix explicitly use CDSOptions.addSuffix()
 233     public static OutputAnalyzer createArchive(String... cliPrefix)
 234         throws Exception {
 235         return createArchive((new CDSOptions()).addPrefix(cliPrefix));
 236     }
 237 
 238     // Create bootstrap CDS archive
 239     public static OutputAnalyzer createArchive(CDSOptions opts)
 240         throws Exception {
 241 
 242         startNewArchiveName();
 243 
 244         ArrayList<String> cmd = new ArrayList<String>();
 245 
 246         for (String p : opts.prefix) cmd.add(p);


 297     // exceptions match. Pass null if you wish not to re-throw any exception.
 298     public static boolean checkCommonExecExceptions(OutputAnalyzer output, Exception e)
 299         throws Exception {
 300         if (output.getStdout().contains("http://bugreport.java.com/bugreport/crash.jsp")) {
 301             throw new RuntimeException("Hotspot crashed");
 302         }
 303         if (output.getStdout().contains("TEST FAILED")) {
 304             throw new RuntimeException("Test Failed");
 305         }
 306         if (output.getOutput().contains("shared class paths mismatch")) {
 307 //            throw new RuntimeException("shared class paths mismatch");
 308         }
 309         if (output.getOutput().contains("Unable to unmap shared space")) {
 310             throw new RuntimeException("Unable to unmap shared space");
 311         }
 312 
 313         // Special case -- sometimes Xshare:on fails because it failed to map
 314         // at given address. This behavior is platform-specific, machine config-specific
 315         // and can be random (see ASLR).
 316         if (isUnableToMap(output)) {
 317             throw new SkippedException(UnableToMapMsg);

 318         }
 319 
 320         if (e != null) {
 321             throw e;
 322         }
 323         return false;
 324     }
 325 
 326     public static boolean checkCommonExecExceptions(OutputAnalyzer output) throws Exception {
 327         return checkCommonExecExceptions(output, null);
 328     }
 329 
 330 
 331     // Check the output for indication that mapping of the archive failed.
 332     // Performance note: this check seems to be rather costly - searching the entire
 333     // output stream of a child process for multiple strings. However, it is necessary
 334     // to detect this condition, a failure to map an archive, since this is not a real
 335     // failure of the test or VM operation, and results in a test being "skipped".
 336     // Suggestions to improve:
 337     // 1. VM can designate a special exit code for such condition.


 428                                      String... extraMatches) throws Exception {
 429         try {
 430             if ("on".equals(opts.xShareMode)) {
 431                 output.shouldContain("sharing");
 432             }
 433             output.shouldHaveExitValue(0);
 434         } catch (RuntimeException e) {
 435             checkCommonExecExceptions(output, e);
 436             return output;
 437         }
 438 
 439         checkMatches(output, extraMatches);
 440         return output;
 441     }
 442 
 443 
 444     public static OutputAnalyzer checkExecExpectError(OutputAnalyzer output,
 445                                              int expectedExitValue,
 446                                              String... extraMatches) throws Exception {
 447         if (isUnableToMap(output)) {
 448             throw new SkippedException(UnableToMapMsg);

 449         }
 450 
 451         output.shouldHaveExitValue(expectedExitValue);
 452         checkMatches(output, extraMatches);
 453         return output;
 454     }
 455 
 456     public static OutputAnalyzer checkMatches(OutputAnalyzer output,
 457                                               String... matches) throws Exception {
 458         for (String match : matches) {
 459             output.shouldContain(match);
 460         }
 461         return output;
 462     }
 463 
 464 
 465     // get the file object for the test artifact
 466     public static File getTestArtifact(String name, boolean checkExistence) {
 467         File dir = new File(System.getProperty("test.classes", "."));
 468         File file = new File(dir, name);


< prev index next >