1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
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
24import jdk.test.lib.process.OutputAnalyzer;
25
26
27// This class contains methods common to all transformation test cases
28public class TransformTestCommon {
29
30    // get parameters to an agent depending on the test case
31    // these parameters will instruct the agent which classes should be
32    // transformed
33    public static String getAgentParams(TestEntry entry,
34                                        String parent, String child) {
35
36        if (entry.transformParent && entry.transformChild)
37            return parent + "," + child;
38        if (entry.transformParent)
39            return parent;
40        if (entry.transformChild)
41            return child;
42
43        return "";
44    }
45
46
47    private static void checkTransformationResults(TestEntry entry,
48                                                   OutputAnalyzer out)
49        throws Exception {
50
51        if (entry.transformParent)
52            out.shouldContain(TransformUtil.ParentCheckPattern +
53                              TransformUtil.AfterPattern);
54
55        if (entry.transformChild)
56            out.shouldContain(TransformUtil.ChildCheckPattern +
57                              TransformUtil.AfterPattern);
58    }
59
60
61    private static void checkSharingByClass(TestEntry entry, OutputAnalyzer out,
62                                            String parent, String child)
63        throws Exception {
64
65        String parentSharedMatch = parent + " source: shared objects file";
66        String childSharedMatch =  child +  " source: shared objects file";
67
68        if (entry.isParentExpectedShared)
69            out.shouldContain(parentSharedMatch);
70        else
71            out.shouldNotContain(parentSharedMatch);
72
73        if (entry.isChildExpectedShared)
74            out.shouldContain(childSharedMatch);
75        else
76            out.shouldNotContain(childSharedMatch);
77    }
78
79
80    // Both parent and child classes should be passed to ClassFileTransformer.transform()
81    // exactly once.
82    private static void checkTransformationCounts(TestEntry entry, OutputAnalyzer out,
83                                                  String parent, String child)
84        throws Exception {
85
86        String patternBase = "TransformerAgent: SimpleTransformer called for: ";
87
88        out.shouldContain(patternBase + child + "@1");
89        out.shouldContain(patternBase + parent + "@1");
90
91        out.shouldNotContain(patternBase + child + "@2");
92        out.shouldNotContain(patternBase + parent + "@2");
93    }
94
95
96    public static void checkResults(TestEntry entry, OutputAnalyzer out,
97                                    String parent, String child)
98        throws Exception {
99
100        // If we were not able to map an archive,
101        // then do not perform other checks, since
102        // there was no sharing at all
103        if (CDSTestUtils.isUnableToMap(out))
104            return;
105
106        String childVmName = child.replace('.', '/');
107        String parentVmName = parent.replace('.', '/');
108
109        CDSTestUtils.checkExec(out);
110        checkTransformationCounts(entry, out, parentVmName, childVmName);
111        checkTransformationResults(entry, out);
112        checkSharingByClass(entry, out, parent, child);
113    }
114}
115