Lines 24-32
Link Here
|
24 |
import org.eclipse.core.runtime.CoreException; |
24 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.IProgressMonitor; |
25 |
import org.eclipse.core.runtime.IProgressMonitor; |
26 |
import org.eclipse.core.runtime.NullProgressMonitor; |
26 |
import org.eclipse.core.runtime.NullProgressMonitor; |
|
|
27 |
import org.eclipse.core.runtime.Path; |
27 |
import org.eclipse.core.runtime.content.IContentDescription; |
28 |
import org.eclipse.core.runtime.content.IContentDescription; |
28 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
29 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
29 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
30 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
|
|
31 |
import org.eclipse.jdt.core.IClasspathEntry; |
30 |
import org.eclipse.jdt.core.ICompilationUnit; |
32 |
import org.eclipse.jdt.core.ICompilationUnit; |
31 |
import org.eclipse.jdt.core.IJavaProject; |
33 |
import org.eclipse.jdt.core.IJavaProject; |
32 |
import org.eclipse.jdt.core.IOpenable; |
34 |
import org.eclipse.jdt.core.IOpenable; |
Lines 929-935
Link Here
|
929 |
preferences.flush(); |
931 |
preferences.flush(); |
930 |
} |
932 |
} |
931 |
} |
933 |
} |
|
|
934 |
|
935 |
/** |
936 |
* Bug 303511: Allow to specify encoding for source attachments |
937 |
* Test whether the source mapper picks the right encoding for the source attachment as a ZIP in workspace. |
938 |
* The encoding could be explicitly set in the absence of which the inherited value from the project |
939 |
* is taken. |
940 |
* |
941 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
942 |
*/ |
943 |
public void testBug303511() throws JavaModelException, CoreException { |
944 |
|
945 |
// Set file encoding |
946 |
String encoding = "Shift-JIS"; |
947 |
if (wkspEncoding.equals(encoding)) |
948 |
getWorkspaceRoot().setDefaultCharset("UTF-8", null); |
949 |
IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$ |
950 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
951 |
|
952 |
assertNotNull("Cannot find class file!", zipFile); |
953 |
zipFile.setCharset(encoding, null); |
954 |
|
955 |
// Get class file and compare source (should not be the same as modify charset on zip file has no effect...) |
956 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
957 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
958 |
assertNotNull(sourceRef); |
959 |
String source = sourceRef.getSource(); |
960 |
assertNotNull(source); |
961 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
962 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
963 |
|
964 |
// Reset zip file encoding |
965 |
zipFile.setCharset(null, null); |
966 |
String oldEncoding = this.encodingProject.getDefaultCharset(); |
967 |
this.encodingProject.setDefaultCharset(encoding, null); |
968 |
|
969 |
root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
970 |
sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
971 |
assertNotNull(sourceRef); |
972 |
source = sourceRef.getSource(); |
973 |
assertNotNull(source); |
974 |
encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
975 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
976 |
|
977 |
this.encodingProject.setDefaultCharset(null, null); |
978 |
|
979 |
root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
980 |
sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
981 |
assertNotNull(sourceRef); |
982 |
source = sourceRef.getSource(); |
983 |
assertNotNull(source); |
984 |
encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
985 |
assertFalse("Sources should be decoded the same way", encodedContents.equals(source)); |
986 |
|
987 |
// Reset zip file encoding |
988 |
zipFile.setCharset(null, null); |
989 |
this.encodingProject.setDefaultCharset(oldEncoding, null); |
990 |
getWorkspaceRoot().setDefaultCharset(wkspEncoding, null); |
991 |
} |
992 |
|
993 |
/** |
994 |
* Bug 303511: Allow to specify encoding for source attachments |
995 |
* Test whether the source mapper picks the right encoding for an external source attachment |
996 |
* The attachment could be an external folder or external archive and have the encoding |
997 |
* explicitly set. In the absence of encoding for the source attachment resource, the default |
998 |
* encoding from the workspace is applied. |
999 |
* |
1000 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
1001 |
*/ |
1002 |
public void testBug303511a() throws JavaModelException, CoreException { |
1003 |
// Set file encoding |
1004 |
String encoding = "Shift-JIS"; |
1005 |
if (wkspEncoding.equals(encoding)) |
1006 |
getWorkspaceRoot().setDefaultCharset("UTF-8", null); |
1007 |
String externalPath = this.encodingProject.getLocation().toOSString() + File.separator + "testShiftJIS.zip"; |
1008 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
1009 |
getWorkspaceRoot().setDefaultCharset(encoding, null); |
1010 |
|
1011 |
IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath(); |
1012 |
IClasspathEntry oldEntry = null; |
1013 |
for (int index = 0; index < entries.length; index++) { |
1014 |
IClasspathEntry entry = entries[index]; |
1015 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1016 |
oldEntry = entry; |
1017 |
IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(externalPath), null); |
1018 |
entries[index] = newEntry; |
1019 |
} |
1020 |
} |
1021 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1022 |
this.encodingJavaProject.getResolvedClasspath(true); |
1023 |
|
1024 |
// Get class file and compare source (should not be the same as modify charset on zip file has no effect...) |
1025 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
1026 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1027 |
assertNotNull(sourceRef); |
1028 |
String source = sourceRef.getSource(); |
1029 |
assertNotNull(source); |
1030 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1031 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1032 |
|
1033 |
entries = this.encodingJavaProject.getRawClasspath(); |
1034 |
String sourcePath = this.encodingProject.getLocation().toOSString() + File.separator + "src"; |
1035 |
for (int index = 0; index < entries.length; index++) { |
1036 |
IClasspathEntry entry = entries[index]; |
1037 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1038 |
IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(sourcePath), null); |
1039 |
entries[index] = newEntry; |
1040 |
} |
1041 |
} |
1042 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1043 |
this.encodingJavaProject.getResolvedClasspath(true); |
1044 |
|
1045 |
entries = this.encodingJavaProject.getRawClasspath(); |
1046 |
for (int index = 0; index < entries.length; index++) { |
1047 |
IClasspathEntry entry = entries[index]; |
1048 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1049 |
entries[index] = oldEntry; |
1050 |
} |
1051 |
} |
1052 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1053 |
this.encodingJavaProject.getResolvedClasspath(true); |
1054 |
getWorkspaceRoot().setDefaultCharset(wkspEncoding, null); |
1055 |
} |
1056 |
|
1057 |
/** |
1058 |
* Bug 303511: Allow to specify encoding for source attachments |
1059 |
* Test that, for a source attachment in form of archives from another project (in the same workspace), the |
1060 |
* encoding of the archive (IResource), if set, is used. In the absence of explicit encoding, the encoding |
1061 |
* of the project that contains this archive is used. |
1062 |
* |
1063 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
1064 |
*/ |
1065 |
public void testBug303511b() throws Exception{ |
1066 |
try{ |
1067 |
String encoding = "Shift-JIS"; |
1068 |
if (wkspEncoding.equals(encoding)) |
1069 |
getWorkspaceRoot().setDefaultCharset("UTF-8", null); |
1070 |
IJavaProject project = this.createJavaProject("Encoding2", new String[] {""}, ""); |
1071 |
String oldEncoding = this.encodingProject.getDefaultCharset(); |
1072 |
IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$ |
1073 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
1074 |
assertNotNull("Cannot find class file!", zipFile); |
1075 |
|
1076 |
zipFile.setCharset(encoding, null); |
1077 |
|
1078 |
IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath(); |
1079 |
IClasspathEntry newEntry = null; |
1080 |
for (int index = 0; index < entries.length; index++) { |
1081 |
IClasspathEntry entry = entries[index]; |
1082 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1083 |
newEntry = entries[index]; |
1084 |
} |
1085 |
} |
1086 |
project.setRawClasspath(new IClasspathEntry[]{JavaCore.newLibraryEntry(newEntry.getPath(), new Path("/Encoding/testShiftJIS.zip"), null)}, null); |
932 |
|
1087 |
|
|
|
1088 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar"); |
1089 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1090 |
assertNotNull(sourceRef); |
1091 |
String source = sourceRef.getSource(); |
1092 |
assertNotNull(source); |
1093 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1094 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1095 |
|
1096 |
zipFile.setCharset(null, null); |
1097 |
this.encodingProject.setDefaultCharset(encoding, null); |
1098 |
|
1099 |
root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar"); |
1100 |
sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1101 |
assertNotNull(sourceRef); |
1102 |
source = sourceRef.getSource(); |
1103 |
assertNotNull(source); |
1104 |
encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1105 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1106 |
|
1107 |
this.encodingProject.setDefaultCharset(oldEncoding, null); |
1108 |
} |
1109 |
finally { |
1110 |
deleteProject("Encoding2"); |
1111 |
getWorkspaceRoot().setDefaultCharset(wkspEncoding, null); |
1112 |
} |
1113 |
} |
1114 |
/** |
1115 |
* Bug 303511: Allow to specify encoding for source attachments |
1116 |
* Test that, for a source attachment in form of folder from another project (in the same workspace), the |
1117 |
* encoding of the folder (IResource), if set, is used. In the absence of explicit encoding, the encoding |
1118 |
* of the project which contains this folder is used. |
1119 |
* |
1120 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
1121 |
*/ |
1122 |
public void testBug303511c() throws Exception{ |
1123 |
try{ |
1124 |
String encoding = "Shift-JIS"; |
1125 |
if (wkspEncoding.equals(encoding)) |
1126 |
getWorkspaceRoot().setDefaultCharset("UTF-8", null); |
1127 |
IJavaProject project = this.createJavaProject("Encoding2", new String[] {""}, ""); |
1128 |
String oldEncoding = this.encodingProject.getDefaultCharset(); |
1129 |
IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$ |
1130 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
1131 |
assertNotNull("Cannot find class file!", zipFile); |
1132 |
|
1133 |
IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath(); |
1134 |
IClasspathEntry newEntry = null; |
1135 |
for (int index = 0; index < entries.length; index++) { |
1136 |
IClasspathEntry entry = entries[index]; |
1137 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1138 |
newEntry = entries[index]; |
1139 |
} |
1140 |
} |
1141 |
|
1142 |
project.setRawClasspath(new IClasspathEntry[]{JavaCore.newLibraryEntry(newEntry.getPath(), new Path("/Encoding/src"), null)}, null); |
1143 |
this.encodingProject.setDefaultCharset(encoding, null); |
1144 |
sourceFile.setCharset(null, null); |
1145 |
|
1146 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar"); |
1147 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1148 |
assertNotNull(sourceRef); |
1149 |
String source = sourceRef.getSource(); |
1150 |
assertNotNull(source); |
1151 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1152 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1153 |
|
1154 |
this.encodingProject.setDefaultCharset(oldEncoding, null); |
1155 |
sourceFile.setCharset(encoding, null); |
1156 |
|
1157 |
root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar"); |
1158 |
sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1159 |
assertNotNull(sourceRef); |
1160 |
source = sourceRef.getSource(); |
1161 |
assertNotNull(source); |
1162 |
encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1163 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1164 |
|
1165 |
sourceFile.setCharset(null, null); |
1166 |
} |
1167 |
finally { |
1168 |
deleteProject("Encoding2"); |
1169 |
getWorkspaceRoot().setDefaultCharset(wkspEncoding, null); |
1170 |
} |
1171 |
} |
1172 |
|
933 |
private void verifyUtf8BOM(IFile file) throws CoreException { |
1173 |
private void verifyUtf8BOM(IFile file) throws CoreException { |
934 |
assertNull("File should not have any explicit charset", file.getCharset(false)); |
1174 |
assertNull("File should not have any explicit charset", file.getCharset(false)); |
935 |
IContentDescription contentDescription = file.getContentDescription(); |
1175 |
IContentDescription contentDescription = file.getContentDescription(); |