langtools/test/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     1
/*
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     4
 *
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     8
 *
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    13
 * accompanied this code).
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    14
 *
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    18
 *
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    21
 * questions.
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    22
 */
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    23
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    24
import com.sun.tools.classfile.Attribute;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    25
import com.sun.tools.classfile.ClassFile;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    26
import com.sun.tools.classfile.SourceFile_attribute;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    27
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    28
import java.nio.file.Path;
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    29
import java.util.ArrayList;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    30
import java.util.List;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    31
import java.util.Map;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    33
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    34
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    35
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    36
/**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    37
 * Base class for Source file attribute tests. Checks expected file name for specified classes in the SourceFile attribute.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    38
 * To add new tests you should extend the SourceFileTestBase class and invoke {@link #test} for static sources
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    39
 * or {@link #compileAndTest} for generated sources. For more information see corresponding methods.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    40
 *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    41
 * @see #test
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    42
 * @see #compileAndTest
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    43
 */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    44
public class SourceFileTestBase extends TestBase {
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    45
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    46
     * Checks expected fileName for the specified class in the SourceFile attribute.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    47
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    48
     * @param classToTest class to check its SourceFile attribute
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    49
     * @param fileName    expected name of the file from which the test file is compiled.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    50
     */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    51
    protected void test(Class<?> classToTest, String fileName) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    52
        assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    53
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    54
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    55
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    56
     * Checks expected fileName for the specified class in the SourceFile attribute.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    57
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    58
     * @param classToTest class name to check its SourceFile attribute
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    59
     * @param fileName    expected name of the file from which the test file is compiled.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    60
     */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    61
    protected void test(String classToTest, String fileName) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    62
        assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    63
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    64
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    65
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    66
     * Checks expected fileName for the specified class in the SourceFile attribute.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    67
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    68
     * @param classToTest path of class to check its SourceFile attribute
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    69
     * @param fileName    expected name of the file from which the test file is compiled.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    70
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    71
    protected void test(Path classToTest, String fileName) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    72
        assertAttributePresent(ClassFile.read(classToTest), fileName);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    73
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    74
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    75
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    76
     * Compiles sourceCode and for each specified class name checks the SourceFile attribute.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    77
     * The file name is extracted from source code.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    78
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    79
     * @param sourceCode    source code to compile
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    80
     * @param classesToTest class names to check their SourceFile attribute.
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    81
     */
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    82
    protected void compileAndTest(String sourceCode, String... classesToTest) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    83
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24294
diff changeset
    84
        Map<String, ? extends JavaFileObject> classes = compile(sourceCode).getClasses();
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    85
        String fileName = ToolBox.getJavaFileNameFromSource(sourceCode);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    86
        for (String className : classesToTest) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    87
            assertAttributePresent(ClassFile.read(classes.get(className).openInputStream()), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    88
        }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    89
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    90
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    91
    private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    92
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    93
        //We need to count attributes with the same names because there is no appropriate API in the ClassFile.
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    94
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    95
        List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>();
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    96
        for (Attribute a : classFile.attributes.attrs) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    97
            if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    98
                sourceFileAttributes.add((SourceFile_attribute) a);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    99
            }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   100
        }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   101
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   102
        assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   103
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   104
        SourceFile_attribute attribute = sourceFileAttributes.get(0);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   105
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   106
        assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value,
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   107
                Attribute.SourceFile, "Incorrect attribute name");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   108
        assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName,
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   109
                "Incorrect source file name");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   110
        assertEquals(attribute.attribute_length, 2, "Incorrect attribute length");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   111
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   112
}