1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.search.core.tests;
import java.io.ByteArrayInputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.search.internal.ui.SearchPlugin;
import org.eclipse.search.tests.SearchTestPlugin;
import org.eclipse.search2.internal.ui.text.PositionTracker;
import junit.framework.TestCase;
/**
*/
public class LineConversionTest extends TestCase {
private IFile fFile;
private static final String LINE_TWO= "This is the second line\n";
private static final String LINE_ONE= "This is the first line\n";
private static final String LINE_THREE= "This is the third line";
@Override
protected void setUp() throws Exception {
IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject("Test");
project.create(null);
project.open(null);
fFile= project.getFile("/test.txt");
fFile.create(new ByteArrayInputStream(getFileContents().getBytes()), true, null);
super.setUp();
}
@Override
protected void tearDown() throws Exception {
SearchPlugin.getActivePage().closeAllEditors(false);
fFile.getProject().delete(true, true, null);
super.tearDown();
}
private String getFileContents() {
return LINE_ONE+LINE_TWO+LINE_THREE;
}
public void testConvertToCharacter() throws Exception {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), fFile);
ITextFileBuffer fb= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
IDocument doc= fb.getDocument();
Position p1= new Position(2, 1);
Position p2= PositionTracker.convertToCharacterPosition(p1, doc);
//assertEquals(LINE_THREE, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1= new Position(0, 1);
p2= PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals(LINE_ONE, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1= new Position(1, 1);
p2= PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals(LINE_TWO, doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
p1= new Position(0, 0);
p2= PositionTracker.convertToCharacterPosition(p1, doc);
assertEquals("", doc.get(p2.getOffset(), p2.getLength()));
assertEquals(p1, PositionTracker.convertToLinePosition(p2, doc));
}
public void testBogusLines() throws Exception {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), fFile);
ITextFileBuffer fb= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
IDocument doc= fb.getDocument();
Position p1= new Position(2, 67);
try {
PositionTracker.convertToCharacterPosition(p1, doc);
assertTrue("shouldn't happen", false);
} catch (BadLocationException e) {
}
}
public void atestLineOffsets() throws Exception {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), fFile);
ITextFileBuffer fb= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE);
IDocument doc= fb.getDocument();
int offset= doc.getLineOffset(3);
int line= doc.getLineOfOffset(offset);
assertEquals(3, line);
}
}
|