langtools/test/tools/javac/classfiles/attributes/SourceFile/SourceFileTestBase.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
parent 25845 14935053bb07
child 36778 e04318f39f92
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
     1
/*
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
     2
 * Copyright (c) 2014, 2015, 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
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    34
/**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    35
 * 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
    36
 * 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
    37
 * 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
    38
 *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    39
 * @see #test
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    40
 * @see #compileAndTest
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    41
 */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    42
public class SourceFileTestBase extends TestBase {
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    43
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    44
     * 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
    45
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    46
     * @param classToTest class to check its SourceFile attribute
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    47
     * @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
    48
     */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    49
    protected void test(Class<?> classToTest, String fileName) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    50
        assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    51
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    52
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    53
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    54
     * 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
    55
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    56
     * @param classToTest class name to check its SourceFile attribute
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    57
     * @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
    58
     */
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    59
    protected void test(String classToTest, String fileName) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    60
        assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    61
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    62
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    63
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    64
     * Checks expected fileName for the specified class in the SourceFile attribute.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    65
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    66
     * @param classToTest path of class to check its SourceFile attribute
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    67
     * @param fileName    expected name of the file from which the test file is compiled.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    68
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    69
    protected void test(Path classToTest, String fileName) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    70
        assertAttributePresent(ClassFile.read(classToTest), fileName);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    71
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    72
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25845
diff changeset
    73
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    74
     * 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
    75
     * The file name is extracted from source code.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    76
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    77
     * @param sourceCode    source code to compile
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    78
     * @param classesToTest class names to check their SourceFile attribute.
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    79
     */
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    80
    protected void compileAndTest(String sourceCode, String... classesToTest) throws Exception {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    81
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24294
diff changeset
    82
        Map<String, ? extends JavaFileObject> classes = compile(sourceCode).getClasses();
24294
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    83
        String fileName = ToolBox.getJavaFileNameFromSource(sourceCode);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    84
        for (String className : classesToTest) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    85
            assertAttributePresent(ClassFile.read(classes.get(className).openInputStream()), fileName);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    86
        }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    87
    }
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
    private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception {
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
        //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
    92
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    93
        List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>();
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    94
        for (Attribute a : classFile.attributes.attrs) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    95
            if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) {
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    96
                sourceFileAttributes.add((SourceFile_attribute) a);
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    97
            }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
    98
        }
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
        assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute");
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
        SourceFile_attribute attribute = sourceFileAttributes.get(0);
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
        assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value,
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   105
                Attribute.SourceFile, "Incorrect attribute name");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   106
        assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName,
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   107
                "Incorrect source file name");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   108
        assertEquals(attribute.attribute_length, 2, "Incorrect attribute length");
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   109
    }
1599838bcaba 8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff changeset
   110
}