author | jlahoda |
Thu, 09 Oct 2014 10:08:52 +0200 | |
changeset 26993 | 513b2cae81c3 |
parent 25845 | 14935053bb07 |
child 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
1 |
/* |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
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 |
|
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
28 |
import java.util.ArrayList; |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
29 |
import java.util.List; |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
30 |
import java.util.Map; |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
31 |
import javax.tools.JavaFileObject; |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
32 |
|
25845
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
33 |
/** |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
34 |
* 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
|
35 |
* 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
|
36 |
* 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
|
37 |
* |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
38 |
* @see #test |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
39 |
* @see #compileAndTest |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
40 |
*/ |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
41 |
public class SourceFileTestBase extends TestBase { |
25845
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
42 |
/** |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
43 |
* 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
|
44 |
* |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
45 |
* @param classToTest class to check its SourceFile attribute |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
46 |
* @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
|
47 |
*/ |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
48 |
protected void test(Class<?> classToTest, String fileName) throws Exception { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
49 |
assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
50 |
} |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
51 |
|
25845
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
52 |
/** |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
53 |
* 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
|
54 |
* |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
55 |
* @param classToTest class name to check its SourceFile attribute |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
56 |
* @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
|
57 |
*/ |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
58 |
protected void test(String classToTest, String fileName) throws Exception { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
59 |
assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
60 |
} |
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 |
/** |
25845
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
63 |
* 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
|
64 |
* The file name is extracted from source code. |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
65 |
* |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
66 |
* @param sourceCode source code to compile |
14935053bb07
8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents:
25434
diff
changeset
|
67 |
* @param classesToTest class names to check their SourceFile attribute. |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
68 |
*/ |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
69 |
protected void compileAndTest(String sourceCode, String... classesToTest) throws Exception { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
70 |
|
25434
823512a72660
8042261: Implement classfile tests for Deprecated attribute.
jjg
parents:
24294
diff
changeset
|
71 |
Map<String, ? extends JavaFileObject> classes = compile(sourceCode).getClasses(); |
24294
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
72 |
String fileName = ToolBox.getJavaFileNameFromSource(sourceCode); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
73 |
for (String className : classesToTest) { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
74 |
assertAttributePresent(ClassFile.read(classes.get(className).openInputStream()), fileName); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
75 |
} |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
76 |
} |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
77 |
|
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
78 |
private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception { |
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 |
//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
|
81 |
|
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
82 |
List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>(); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
83 |
for (Attribute a : classFile.attributes.attrs) { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
84 |
if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) { |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
85 |
sourceFileAttributes.add((SourceFile_attribute) a); |
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 |
assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute"); |
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 |
SourceFile_attribute attribute = sourceFileAttributes.get(0); |
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 |
assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value, |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
94 |
Attribute.SourceFile, "Incorrect attribute name"); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
95 |
assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName, |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
96 |
"Incorrect source file name"); |
1599838bcaba
8040129: Implement classfile tests for SourceFile attribute.
anazarov
parents:
diff
changeset
|
97 |
assertEquals(attribute.attribute_length, 2, "Incorrect attribute length"); |
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 |
} |