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-934
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 or inherited from the project. |
939 |
* |
940 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
941 |
*/ |
942 |
public void testBug303511() throws JavaModelException, CoreException { |
943 |
|
944 |
// Set file encoding |
945 |
String encoding = "Shift-JIS"; |
946 |
IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$ |
947 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
948 |
|
949 |
assertNotNull("Cannot find class file!", zipFile); |
950 |
zipFile.setCharset(encoding, null); |
951 |
|
952 |
// Get class file and compare source (should not be the same as modify charset on zip file has no effect...) |
953 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
954 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
955 |
assertNotNull(sourceRef); |
956 |
String source = sourceRef.getSource(); |
957 |
assertNotNull(source); |
958 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
959 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
960 |
|
961 |
// Reset zip file encoding |
962 |
zipFile.setCharset(null, null); |
963 |
String oldEncoding = this.encodingProject.getDefaultCharset(); |
964 |
this.encodingProject.setDefaultCharset(encoding, null); |
965 |
|
966 |
root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
967 |
sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
968 |
assertNotNull(sourceRef); |
969 |
source = sourceRef.getSource(); |
970 |
assertNotNull(source); |
971 |
encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
972 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
973 |
|
974 |
// Reset zip file encoding |
975 |
zipFile.setCharset(null, null); |
976 |
this.encodingProject.setDefaultCharset(oldEncoding, null); |
977 |
} |
978 |
|
979 |
/** |
980 |
* Bug 303511: Allow to specify encoding for source attachments |
981 |
* Test whether the source mapper picks the right encoding for an external source attachment |
982 |
* The attachment could be an external folder or external archive and have the encoding |
983 |
* explicitly set or inherit from the project. |
984 |
* |
985 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511" |
986 |
*/ |
987 |
public void testBug303511a() throws JavaModelException, CoreException { |
988 |
// Set file encoding |
989 |
String encoding = "Shift-JIS"; |
990 |
String oldEncoding = this.encodingProject.getDefaultCharset(); |
991 |
String externalPath = this.encodingProject.getLocation().toOSString() + File.separator + "testShiftJIS.zip"; |
992 |
IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java"); |
993 |
|
994 |
this.encodingProject.setDefaultCharset(encoding, null); |
995 |
IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath(); |
996 |
IClasspathEntry oldEntry = null; |
997 |
for (int index = 0; index < entries.length; index++) { |
998 |
IClasspathEntry entry = entries[index]; |
999 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1000 |
oldEntry = entry; |
1001 |
IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(externalPath), null); |
1002 |
entries[index] = newEntry; |
1003 |
} |
1004 |
} |
1005 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1006 |
this.encodingJavaProject.getResolvedClasspath(true); |
1007 |
|
1008 |
// Get class file and compare source (should not be the same as modify charset on zip file has no effect...) |
1009 |
IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar"); |
1010 |
ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class"); |
1011 |
assertNotNull(sourceRef); |
1012 |
String source = sourceRef.getSource(); |
1013 |
assertNotNull(source); |
1014 |
String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding)); |
1015 |
assertTrue("Sources should be decoded the same way", encodedContents.equals(source)); |
1016 |
|
1017 |
entries = this.encodingJavaProject.getRawClasspath(); |
1018 |
String sourcePath = this.encodingProject.getLocation().toOSString() + File.separator + "src"; |
1019 |
for (int index = 0; index < entries.length; index++) { |
1020 |
IClasspathEntry entry = entries[index]; |
1021 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1022 |
IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(sourcePath), null); |
1023 |
entries[index] = newEntry; |
1024 |
} |
1025 |
} |
1026 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1027 |
this.encodingJavaProject.getResolvedClasspath(true); |
1028 |
|
1029 |
entries = this.encodingJavaProject.getRawClasspath(); |
1030 |
for (int index = 0; index < entries.length; index++) { |
1031 |
IClasspathEntry entry = entries[index]; |
1032 |
if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) { |
1033 |
entries[index] = oldEntry; |
1034 |
} |
1035 |
} |
1036 |
this.encodingJavaProject.setRawClasspath(entries, null); |
1037 |
this.encodingJavaProject.getResolvedClasspath(true); |
1038 |
refreshExternalArchives(this.encodingJavaProject); |
1039 |
this.encodingProject.setDefaultCharset(oldEncoding, null); |
1040 |
} |
932 |
|
1041 |
|
933 |
private void verifyUtf8BOM(IFile file) throws CoreException { |
1042 |
private void verifyUtf8BOM(IFile file) throws CoreException { |
934 |
assertNull("File should not have any explicit charset", file.getCharset(false)); |
1043 |
assertNull("File should not have any explicit charset", file.getCharset(false)); |