View | Details | Raw Unified | Return to bug 66898 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/EncodingTests.java (-24 / +93 lines)
Lines 16-23 Link Here
16
16
17
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.resources.IProject;
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IWorkspaceRunnable;
19
import org.eclipse.core.resources.ResourcesPlugin;
20
import org.eclipse.core.resources.ResourcesPlugin;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.NullProgressMonitor;
23
import org.eclipse.core.runtime.NullProgressMonitor;
22
import org.eclipse.core.runtime.Preferences;
24
import org.eclipse.core.runtime.Preferences;
23
import org.eclipse.jdt.core.ICompilationUnit;
25
import org.eclipse.jdt.core.ICompilationUnit;
Lines 26-31 Link Here
26
import org.eclipse.jdt.core.IPackageFragment;
28
import org.eclipse.jdt.core.IPackageFragment;
27
import org.eclipse.jdt.core.IPackageFragmentRoot;
29
import org.eclipse.jdt.core.IPackageFragmentRoot;
28
import org.eclipse.jdt.core.ISourceReference;
30
import org.eclipse.jdt.core.ISourceReference;
31
import org.eclipse.jdt.core.JavaCore;
29
import org.eclipse.jdt.core.JavaModelException;
32
import org.eclipse.jdt.core.JavaModelException;
30
import org.eclipse.jdt.internal.core.util.Util;
33
import org.eclipse.jdt.internal.core.util.Util;
31
34
Lines 48-54 Link Here
48
	// All specified tests which do not belong to the class are skipped...
51
	// All specified tests which do not belong to the class are skipped...
49
	static {
52
	static {
50
		// Names of tests to run: can be "testBugXXXX" or "BugXXXX")
53
		// Names of tests to run: can be "testBugXXXX" or "BugXXXX")
51
//		testsNames = new String[] { "testBug66898" };
54
//		testsNames = new String[] { "testBug66898", "testBug66898b" };
52
		// Numbers of tests to run: "test<number>" will be run for each number of this array
55
		// Numbers of tests to run: "test<number>" will be run for each number of this array
53
//		testsNumbers = new int[] { 2, 12 };
56
//		testsNumbers = new int[] { 2, 12 };
54
		// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last }
57
		// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last }
Lines 587-615 Link Here
587
		// Set file encoding
590
		// Set file encoding
588
		String encoding = "UTF-8".equals(vmEncoding) ? "Cp1252" : "UTF-8";
591
		String encoding = "UTF-8".equals(vmEncoding) ? "Cp1252" : "UTF-8";
589
		this.utf8File.setCharset(encoding);
592
		this.utf8File.setCharset(encoding);
590
		
591
		// Move file
592
		String fileName = this.utf8File.getName();
593
		String fileName = this.utf8File.getName();
593
		ICompilationUnit cu = getCompilationUnit(this.utf8File.getFullPath().toString());
594
		ICompilationUnit cu = getCompilationUnit(this.utf8File.getFullPath().toString());
594
		createFolder("/Encoding/src/tmp");
595
		createFolder("/Encoding/src/tmp");
595
		IPackageFragment packFrag = getPackageFragment("Encoding", "src", "tmp");
596
		IPackageFragment packFrag = getPackageFragment("Encoding", "src", "tmp");
597
		
598
		// Move file
596
		cu.move(packFrag, null, null, false, null);
599
		cu.move(packFrag, null, null, false, null);
597
		ICompilationUnit destSource = packFrag.getCompilationUnit(fileName);
600
		ICompilationUnit destSource = packFrag.getCompilationUnit(fileName);
598
		IFile destFile = (IFile) destSource.getUnderlyingResource();
601
		IFile destFile = (IFile) destSource.getUnderlyingResource();
599
		assertEquals("Moved file should keep encoding", encoding, destFile.getCharset());
602
		assertEquals("Moved file should keep encoding", encoding, destFile.getCharset());
600
603
601
		// Get source and compare with file contents
604
		// Get source and compare with file contents
602
		String source = destSource.getSource();
605
		compareContents(destSource, encoding);
603
		String systemSource = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(source);
604
		String encodedContents = new String (Util.getResourceContentsAsCharArray(destFile));
605
		encodedContents = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(encodedContents);
606
		assertEquals("Encoded UTF-8 source should have been decoded the same way!", encodedContents, systemSource);
607
		byte[] sourceBytes = source.getBytes(encoding);
608
		byte[] encodedBytes = Util.getResourceContentsAsByteArray(destFile);
609
		assertEquals("Wrong size of encoded string", encodedBytes.length, sourceBytes.length);
610
		for (int i = 0, max = sourceBytes.length; i < max; i++) {
611
			assertTrue("Wrong size of encoded character at " + i, sourceBytes[i] == encodedBytes[i]);
612
		}
613
		
606
		
614
		// Rename file
607
		// Rename file
615
		destSource.rename("TestUTF8.java", false, null);
608
		destSource.rename("TestUTF8.java", false, null);
Lines 618-637 Link Here
618
		assertEquals("Moved file should keep encoding", encoding, renamedFile.getCharset());
611
		assertEquals("Moved file should keep encoding", encoding, renamedFile.getCharset());
619
		
612
		
620
		// Compare contents again
613
		// Compare contents again
621
		String sourceRenamed = renamedSource.getSource();
614
		compareContents(renamedSource, encoding);
622
		String systemSourceRenamed = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(sourceRenamed);
615
		
623
		String renamedContents = new String (Util.getResourceContentsAsCharArray(renamedFile));
616
		// Move back 
617
		renamedFile.move(this.utf8File.getFullPath(), false, null);
618
		assertEquals("Moved file should keep encoding", encoding, this.utf8File.getCharset());
619
		deleteFolder("/Encoding/src/tmp");
620
	}	
621
	public void testBug66898b() throws JavaModelException, CoreException {
622
623
		// Set file encoding
624
		final String encoding = "UTF-8".equals(vmEncoding) ? "Cp1252" : "UTF-8";
625
		this.utf8File.setCharset(encoding);
626
		final String fileName = utf8File.getName();
627
		final IPackageFragment srcFolder = getPackageFragment("Encoding", "src", "testUTF8");
628
		createFolder("/Encoding/src/tmp");
629
		final IPackageFragment tmpFolder = getPackageFragment("Encoding", "src", "tmp");
630
631
		// Copy file
632
		IWorkspaceRunnable copy = new IWorkspaceRunnable() {
633
			public void run(IProgressMonitor monitor) throws CoreException {
634
				ICompilationUnit cu = getCompilationUnit(utf8File.getFullPath().toString());
635
				cu.copy(tmpFolder, null, null, true, null);
636
				cu.close(); // purge buffer contents from cache
637
				ICompilationUnit dest = tmpFolder.getCompilationUnit(fileName);
638
				IFile destFile = (IFile) dest.getUnderlyingResource();
639
				assertEquals("Copied file should keep encoding", encoding, destFile.getCharset());
640
		
641
				// Get source and compare with file contents
642
				compareContents(dest, encoding);
643
			}
644
		};
645
		JavaCore.run(copy, null);
646
647
		// Rename file
648
		IWorkspaceRunnable rename = new IWorkspaceRunnable() {
649
			public void run(IProgressMonitor monitor) throws CoreException {
650
				ICompilationUnit cu = tmpFolder.getCompilationUnit(fileName);
651
				cu.rename("Renamed.java", true, null);
652
				cu.close(); // purge buffer contents from cache
653
				ICompilationUnit ren = tmpFolder.getCompilationUnit("Renamed.java");
654
				IFile renFile = (IFile) ren.getUnderlyingResource();
655
				assertEquals("Renamed file should keep encoding", encoding, renFile.getCharset());
656
		
657
				// Get source and compare with file contents
658
				compareContents(ren, encoding);
659
			}
660
		};
661
		JavaCore.run(rename, null);
662
663
		// Move file
664
		IWorkspaceRunnable move = new IWorkspaceRunnable() {
665
			public void run(IProgressMonitor monitor) throws CoreException {
666
				ICompilationUnit cu = tmpFolder.getCompilationUnit("Renamed.java");
667
				cu.move(srcFolder, null, null, true, null);
668
				cu.close(); // purge buffer contents from cache
669
				ICompilationUnit moved = srcFolder.getCompilationUnit("Renamed.java");
670
				IFile movedFile = (IFile) moved.getUnderlyingResource();
671
				assertEquals("Renamed file should keep encoding", encoding, movedFile.getCharset());
672
		
673
				// Get source and compare with file contents
674
				compareContents(moved, encoding);
675
			}
676
		};
677
		JavaCore.run(move, null);
678
		
679
		// Delete file
680
		ICompilationUnit cu = srcFolder.getCompilationUnit("Renamed.java");
681
		cu.delete(true, null);
682
		deleteFolder("/Encoding/src/tmp");
683
	}	
684
	void compareContents(ICompilationUnit cu, String encoding) throws JavaModelException {
685
		// Compare source strings
686
		String source = cu.getSource();
687
		String systemSourceRenamed = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(source);
688
		IFile file = (IFile) cu.getUnderlyingResource();
689
		String renamedContents = new String (Util.getResourceContentsAsCharArray(file));
624
		renamedContents = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(renamedContents);
690
		renamedContents = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(renamedContents);
625
		assertEquals("Encoded UTF-8 source should have been decoded the same way!", renamedContents, systemSourceRenamed);
691
		assertEquals("Encoded UTF-8 source should have been decoded the same way!", renamedContents, systemSourceRenamed);
626
		byte[] renamedSourceBytes = sourceRenamed.getBytes(encoding);
692
		// Compare bytes array
627
		byte[] renamedEncodedBytes = Util.getResourceContentsAsByteArray(renamedFile);
693
		byte[] renamedSourceBytes = null;
694
		try {
695
			renamedSourceBytes = source.getBytes(encoding);
696
		}
697
		catch (UnsupportedEncodingException uue) {
698
		}
699
		assertNotNull("Unsupported encoding: "+encoding, renamedSourceBytes);
700
		byte[] renamedEncodedBytes = Util.getResourceContentsAsByteArray(file);
628
		assertEquals("Wrong size of encoded string", renamedEncodedBytes.length, renamedSourceBytes.length);
701
		assertEquals("Wrong size of encoded string", renamedEncodedBytes.length, renamedSourceBytes.length);
629
		for (int i = 0, max = renamedSourceBytes.length; i < max; i++) {
702
		for (int i = 0, max = renamedSourceBytes.length; i < max; i++) {
630
			assertTrue("Wrong size of encoded character at " + i, renamedSourceBytes[i] == renamedEncodedBytes[i]);
703
			assertTrue("Wrong size of encoded character at " + i, renamedSourceBytes[i] == renamedEncodedBytes[i]);
631
		}
704
		}
632
		
705
	}
633
		// Move back 
634
		renamedFile.move(this.utf8File.getFullPath(), false, null);
635
		assertEquals("Moved file should keep encoding", encoding, this.utf8File.getCharset());
636
	}	
637
}
706
}

Return to bug 66898