1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   4  */
   5 
   6 import jdk.test.lib.process.OutputAnalyzer;
   7 
   8 
   9 // This class contains methods common to all transformation test cases
  10 public class TransformTestCommon {
  11 
  12     // Agent is the same for all test cases
  13     public static String prepareAgent(String[] agentClasses) throws Exception {
  14         return ClassFileInstaller.writeJar("TransformerAgent.jar",
  15             ClassFileInstaller.Manifest.fromSourceFile("../TransformerAgent.mf"),
  16                                         agentClasses);
  17     }
  18 
  19 
  20     // test case helper methods
  21     public static String
  22         getAgentParams(TestEntry entry, String parent, String child) {
  23 
  24         if(entry.transformParent && entry.transformChild)
  25             return parent + "," + child;
  26         if(entry.transformParent)
  27             return parent;
  28         if(entry.transformChild)
  29             return child;
  30 
  31         return "";
  32     }
  33 
  34 
  35     private static void checkTransformationResults(TestEntry entry, OutputAnalyzer out)
  36         throws Exception {
  37 
  38         if (entry.transformParent)
  39             out.shouldContain("parent-transform-check: this-has-been--transformed");
  40 
  41         if (entry.transformChild)
  42             out.shouldContain("child-transform-check: this-has-been--transformed");
  43     }
  44 
  45 
  46     private static void checkSharingByClass(TestEntry entry, OutputAnalyzer out,
  47                                             String parent, String child)
  48         throws Exception {
  49 
  50         String parentSharedMatch = parent + " source: shared objects file";
  51         String childSharedMatch =  child +  " source: shared objects file";
  52 
  53         if(entry.isParentExpectedShared)
  54             out.shouldContain(parentSharedMatch);
  55         else
  56             out.shouldNotContain(parentSharedMatch);
  57 
  58         if(entry.isChildExpectedShared)
  59             out.shouldContain(childSharedMatch);
  60         else
  61             out.shouldNotContain(childSharedMatch);
  62     }
  63 
  64 
  65     // Both parent and child classes should be passed to ClassFileTransformer.transform()
  66     // exactly once.
  67     private static void checkTransformationCounts(TestEntry entry, OutputAnalyzer out,
  68                                                   String parent, String child)
  69         throws Exception {
  70 
  71         out.shouldContain("TransformerAgent: SimpleTransformer called for: " + child + "@1");
  72         out.shouldContain("TransformerAgent: SimpleTransformer called for: " + parent + "@1");
  73 
  74         out.shouldNotContain("TransformerAgent: SimpleTransformer called for: " + child + "@2");
  75         out.shouldNotContain("TransformerAgent: SimpleTransformer called for: " + parent + "@2");
  76     }
  77 
  78 
  79     public static void checkResults(TestEntry entry, OutputAnalyzer out,
  80                                     String parent, String child)
  81         throws Exception {
  82 
  83         // If was not able to map an archive,
  84         // then do not check individual sharing, since
  85         // there was no sharing at all
  86         if (CDSTestUtils.isUnableToMap(out))
  87             return;
  88 
  89         CDSTestUtils.checkExec(out);
  90         checkTransformationCounts(entry, out, parent, child);
  91         checkTransformationResults(entry, out);
  92         checkSharingByClass(entry,out, parent, child);
  93     }
  94 }