2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
5 package com
.intellij
.testFramework
.fixtures
.impl
;
7 import com
.intellij
.codeHighlighting
.HighlightDisplayLevel
;
8 import com
.intellij
.codeInsight
.TargetElementUtil
;
9 import com
.intellij
.codeInsight
.completion
.CodeCompletionHandler
;
10 import com
.intellij
.codeInsight
.completion
.LookupData
;
11 import com
.intellij
.codeInsight
.daemon
.HighlightDisplayKey
;
12 import com
.intellij
.codeInsight
.daemon
.impl
.GeneralHighlightingPass
;
13 import com
.intellij
.codeInsight
.daemon
.impl
.HighlightInfo
;
14 import com
.intellij
.codeInsight
.daemon
.impl
.LocalInspectionsPass
;
15 import com
.intellij
.codeInsight
.daemon
.impl
.PostHighlightingPass
;
16 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
17 import com
.intellij
.codeInsight
.intention
.IntentionManager
;
18 import com
.intellij
.codeInsight
.lookup
.Lookup
;
19 import com
.intellij
.codeInsight
.lookup
.LookupItem
;
20 import com
.intellij
.codeInsight
.lookup
.LookupManager
;
21 import com
.intellij
.codeInspection
.InspectionProfileEntry
;
22 import com
.intellij
.codeInspection
.InspectionToolProvider
;
23 import com
.intellij
.codeInspection
.LocalInspectionTool
;
24 import com
.intellij
.codeInspection
.ModifiableModel
;
25 import com
.intellij
.codeInspection
.ex
.InspectionProfileImpl
;
26 import com
.intellij
.codeInspection
.ex
.InspectionTool
;
27 import com
.intellij
.codeInspection
.ex
.LocalInspectionToolWrapper
;
28 import com
.intellij
.mock
.MockProgressIndicator
;
29 import com
.intellij
.openapi
.Disposable
;
30 import com
.intellij
.openapi
.application
.Result
;
31 import com
.intellij
.openapi
.application
.RunResult
;
32 import com
.intellij
.openapi
.command
.WriteCommandAction
;
33 import com
.intellij
.openapi
.editor
.Document
;
34 import com
.intellij
.openapi
.editor
.Editor
;
35 import com
.intellij
.openapi
.editor
.EditorFactory
;
36 import com
.intellij
.openapi
.editor
.RangeMarker
;
37 import com
.intellij
.openapi
.editor
.ex
.DocumentEx
;
38 import com
.intellij
.openapi
.editor
.markup
.GutterIconRenderer
;
39 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
40 import com
.intellij
.openapi
.fileEditor
.OpenFileDescriptor
;
41 import com
.intellij
.openapi
.fileTypes
.FileType
;
42 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
43 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
44 import com
.intellij
.openapi
.project
.Project
;
45 import com
.intellij
.openapi
.util
.Disposer
;
46 import com
.intellij
.openapi
.util
.Pair
;
47 import com
.intellij
.openapi
.util
.Ref
;
48 import com
.intellij
.openapi
.util
.TextRange
;
49 import com
.intellij
.openapi
.util
.io
.FileUtil
;
50 import com
.intellij
.openapi
.util
.text
.StringUtil
;
51 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
52 import com
.intellij
.openapi
.vfs
.VfsUtil
;
53 import com
.intellij
.openapi
.vfs
.VirtualFile
;
54 import com
.intellij
.openapi
.vfs
.VirtualFileFilter
;
55 import com
.intellij
.profile
.codeInspection
.InspectionProfileManager
;
56 import com
.intellij
.profile
.codeInspection
.InspectionProjectProfileManager
;
57 import com
.intellij
.psi
.*;
58 import com
.intellij
.psi
.impl
.PsiManagerImpl
;
59 import com
.intellij
.psi
.impl
.source
.PostprocessReformattingAspect
;
60 import com
.intellij
.psi
.impl
.source
.PsiFileImpl
;
61 import com
.intellij
.psi
.search
.GlobalSearchScope
;
62 import com
.intellij
.psi
.search
.UsageSearchContext
;
63 import com
.intellij
.refactoring
.rename
.RenameProcessor
;
64 import com
.intellij
.testFramework
.ExpectedHighlightingData
;
65 import com
.intellij
.testFramework
.UsefulTestCase
;
66 import com
.intellij
.testFramework
.fixtures
.CodeInsightTestFixture
;
67 import com
.intellij
.testFramework
.fixtures
.IdeaProjectTestFixture
;
68 import com
.intellij
.testFramework
.fixtures
.TempDirTestFixture
;
69 import com
.intellij
.util
.Function
;
70 import com
.intellij
.util
.containers
.ContainerUtil
;
71 import gnu
.trove
.THashMap
;
72 import junit
.framework
.TestCase
;
73 import org
.jetbrains
.annotations
.NonNls
;
74 import org
.jetbrains
.annotations
.NotNull
;
75 import org
.jetbrains
.annotations
.Nullable
;
78 import java
.io
.IOException
;
79 import java
.io
.OutputStream
;
83 * @author Dmitry Avdeev
85 public class CodeInsightTestFixtureImpl
extends BaseFixture
implements CodeInsightTestFixture
{
87 @NonNls private static final String PROFILE
= "Configurable";
89 private PsiManagerImpl myPsiManager
;
90 private PsiFile myFile
;
91 private Editor myEditor
;
92 private String myTestDataPath
;
94 private LocalInspectionTool
[] myInspections
;
95 private final Map
<String
, LocalInspectionTool
> myAvailableTools
= new THashMap
<String
, LocalInspectionTool
>();
96 private final Map
<String
, LocalInspectionToolWrapper
> myAvailableLocalTools
= new THashMap
<String
, LocalInspectionToolWrapper
>();
98 private final TempDirTestFixture myTempDirFixture
= new TempDirTextFixtureImpl();
99 private final IdeaProjectTestFixture myProjectFixture
;
100 @NonNls private static final String XXX
= "XXX";
102 public CodeInsightTestFixtureImpl(IdeaProjectTestFixture projectFixture
) {
103 myProjectFixture
= projectFixture
;
106 public void setTestDataPath(String dataPath
) {
107 myTestDataPath
= dataPath
;
110 public String
getTempDirPath() {
111 return myTempDirFixture
.getTempDirPath();
114 public TempDirTestFixture
getTempDirFixture() {
115 return myTempDirFixture
;
118 public void enableInspections(LocalInspectionTool
... inspections
) {
119 myInspections
= inspections
;
122 public void disableInspections(LocalInspectionTool
... inspections
) {
123 myAvailableTools
.clear();
124 myAvailableLocalTools
.clear();
125 final ArrayList
<LocalInspectionTool
> tools
= new ArrayList
<LocalInspectionTool
>(Arrays
.asList(myInspections
));
126 for (Iterator
<LocalInspectionTool
> i
= tools
.iterator(); i
.hasNext();) {
127 final LocalInspectionTool tool
= i
.next();
128 for (LocalInspectionTool toRemove
: inspections
) {
129 if (tool
.getShortName().equals(toRemove
.getShortName())) {
135 myInspections
= tools
.toArray(new LocalInspectionTool
[tools
.size()]);
136 configureInspections(myInspections
);
139 public void enableInspections(InspectionToolProvider
... providers
) {
140 final ArrayList
<LocalInspectionTool
> tools
= new ArrayList
<LocalInspectionTool
>();
141 for (InspectionToolProvider provider
: providers
) {
142 for (Class clazz
: provider
.getInspectionClasses()) {
144 LocalInspectionTool inspection
= (LocalInspectionTool
)clazz
.getConstructor().newInstance();
145 tools
.add(inspection
);
147 catch (Exception e
) {
148 throw new RuntimeException("Cannot instantiate " + clazz
);
152 myInspections
= tools
.toArray(new LocalInspectionTool
[tools
.size()]);
155 public long testHighlighting(final boolean checkWarnings
,
156 final boolean checkInfos
,
157 final boolean checkWeakWarnings
,
158 final String
... filePaths
) throws Throwable
{
160 final Ref
<Long
> duration
= new Ref
<Long
>();
161 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
163 protected void run() throws Throwable
{
164 configureByFiles(filePaths
);
165 collectAndCheckHighlightings(checkWarnings
, checkInfos
, checkWeakWarnings
, duration
);
167 }.execute().throwException();
168 return duration
.get().longValue();
171 public long testHighlighting(final String
... filePaths
) throws Throwable
{
172 return testHighlighting(true, true, true, filePaths
);
176 public PsiReference
getReferenceAtCaretPosition(final String filePath
) throws Throwable
{
177 final RunResult
<PsiReference
> runResult
= new WriteCommandAction
<PsiReference
>(myProjectFixture
.getProject()) {
178 protected void run(final Result
<PsiReference
> result
) throws Throwable
{
179 configureByFiles(filePath
);
180 final int offset
= myEditor
.getCaretModel().getOffset();
181 final PsiReference psiReference
= getFile().findReferenceAt(offset
);
182 result
.setResult(psiReference
);
185 runResult
.throwException();
186 return runResult
.getResultObject();
190 public PsiReference
getReferenceAtCaretPositionWithAssertion(final String filePath
) throws Throwable
{
191 final PsiReference reference
= getReferenceAtCaretPosition(filePath
);
192 assert reference
!= null: "no reference found at " + myEditor
.getCaretModel().getLogicalPosition();
197 public List
<IntentionAction
> getAvailableIntentions(final String
... filePaths
) throws Throwable
{
199 final Project project
= myProjectFixture
.getProject();
200 return new WriteCommandAction
<List
<IntentionAction
>>(project
) {
201 protected void run(final Result
<List
<IntentionAction
>> result
) throws Throwable
{
202 final int offset
= configureByFiles(filePaths
);
203 result
.setResult(getAvailableIntentions(project
, doHighlighting(), offset
, myEditor
, myFile
));
205 }.execute().getResultObject();
208 public void launchAction(@NotNull final IntentionAction action
) throws Throwable
{
209 new WriteCommandAction(myProjectFixture
.getProject()) {
210 protected void run(final Result result
) throws Throwable
{
211 action
.invoke(getProject(), getEditor(), getFile());
213 }.execute().throwException();
217 public void testCompletion(final String
[] filesBefore
, final String fileAfter
) throws Throwable
{
218 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
220 protected void run() throws Throwable
{
221 configureByFiles(filesBefore
);
222 new CodeCompletionHandler().invoke(getProject(), myEditor
, myFile
);
223 checkResultByFile(fileAfter
, myFile
, false);
225 }.execute().throwException();
228 public void testCompletion(String fileBefore
, String fileAfter
) throws Throwable
{
229 testCompletion(new String
[] { fileBefore
}, fileAfter
);
232 public void testCompletionVariants(final String fileBefore
, final String
... items
) throws Throwable
{
233 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
235 protected void run() throws Throwable
{
236 configureByFiles(fileBefore
);
237 final Ref
<LookupItem
[]> myItems
= Ref
.create(null);
238 new CodeCompletionHandler(){
239 protected Lookup
showLookup(Project project
,
243 LookupData data
, PsiFile file
) {
248 }.invoke(getProject(), myEditor
, myFile
);
249 final LookupItem
[] items1
= myItems
.get();
250 UsefulTestCase
.assertNotNull(items1
);
251 UsefulTestCase
.assertSameElements(ContainerUtil
.map(items1
, new Function
<LookupItem
, String
>() {
252 public String
fun(final LookupItem lookupItem
) {
253 return lookupItem
.getLookupString();
257 }.execute().throwException();
260 public void testRename(final String fileBefore
, final String fileAfter
, final String newName
) throws Throwable
{
261 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
262 protected void run() throws Throwable
{
263 configureByFiles(fileBefore
);
264 PsiElement element
= TargetElementUtil
.findTargetElement(myEditor
, TargetElementUtil
.REFERENCED_ELEMENT_ACCEPTED
);
265 assert element
!= null: "element not found at caret position, offset " + myEditor
.getCaretModel().getOffset();
266 new RenameProcessor(myProjectFixture
.getProject(), element
, newName
, false, false).run();
267 checkResultByFile(fileAfter
, myFile
, false);
269 }.execute().throwException();
273 public GutterIconRenderer
findGutter(final String filePath
) throws Throwable
{
274 final Project project
= myProjectFixture
.getProject();
275 final Ref
<GutterIconRenderer
> result
= new Ref
<GutterIconRenderer
>();
276 new WriteCommandAction
.Simple(project
) {
278 protected void run() throws Throwable
{
279 final int offset
= configureByFiles(filePath
);
281 final Collection
<HighlightInfo
> infos
= doHighlighting();
282 for (HighlightInfo info
:infos
) {
283 if (info
.endOffset
>= offset
&& info
.startOffset
<= offset
) {
284 final GutterIconRenderer renderer
= info
.getGutterIconRenderer();
285 if (renderer
!= null) {
286 result
.set(renderer
);
293 }.execute().throwException();
297 public void checkResultByFile(final String filePath
) throws Throwable
{
298 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
300 protected void run() throws Throwable
{
301 checkResultByFile(filePath
, myFile
, false);
303 }.execute().throwException();
306 public void checkResultByFile(final String filePath
, final String expectedFile
, final boolean ignoreWhitespaces
) throws Throwable
{
308 new WriteCommandAction
.Simple(myProjectFixture
.getProject()) {
310 protected void run() throws Throwable
{
311 String fullPath
= getTempDirPath() + "/" + filePath
;
312 final VirtualFile copy
= LocalFileSystem
.getInstance().refreshAndFindFileByPath(fullPath
.replace(File
.separatorChar
, '/'));
313 assert copy
!= null : "file not found: " + fullPath
;
314 final PsiFile psiFile
= myPsiManager
.findFile(copy
);
315 assert psiFile
!= null;
316 checkResultByFile(expectedFile
, psiFile
, ignoreWhitespaces
);
318 }.execute().throwException();
321 public void setUp() throws Exception
{
324 final String testDataPath
= getTestDataPath();
325 if (testDataPath
!= null) {
326 FileUtil
.copyDir(new File(testDataPath
), new File(getTempDirPath()), false);
328 myProjectFixture
.setUp();
329 myPsiManager
= (PsiManagerImpl
)PsiManager
.getInstance(getProject());
330 configureInspections(myInspections
== null ?
new LocalInspectionTool
[0] : myInspections
);
333 private void enableInspectionTool(LocalInspectionTool tool
){
334 final String shortName
= tool
.getShortName();
335 final HighlightDisplayKey key
= HighlightDisplayKey
.find(shortName
);
337 HighlightDisplayKey
.register(shortName
, tool
.getDisplayName(), tool
.getID());
339 myAvailableTools
.put(shortName
, tool
);
340 myAvailableLocalTools
.put(shortName
, new LocalInspectionToolWrapper(tool
));
343 private void configureInspections(final LocalInspectionTool
[] tools
) {
344 for (LocalInspectionTool tool
: tools
) {
345 enableInspectionTool(tool
);
348 final InspectionProfileImpl profile
= new InspectionProfileImpl(PROFILE
) {
349 public ModifiableModel
getModifiableModel() {
354 public InspectionProfileEntry
[] getInspectionTools() {
355 final Collection
<LocalInspectionToolWrapper
> tools
= myAvailableLocalTools
.values();
356 return tools
.toArray(new LocalInspectionToolWrapper
[tools
.size()]);
359 public boolean isToolEnabled(HighlightDisplayKey key
) {
360 return key
!= null && key
.toString() != null && myAvailableTools
!= null && myAvailableTools
.containsKey(key
.toString());
363 public HighlightDisplayLevel
getErrorLevel(HighlightDisplayKey key
) {
364 final LocalInspectionTool localInspectionTool
= myAvailableTools
.get(key
.toString());
365 return localInspectionTool
!= null ? localInspectionTool
.getDefaultLevel() : HighlightDisplayLevel
.WARNING
;
368 public InspectionTool
getInspectionTool(String shortName
) {
369 return myAvailableLocalTools
.get(shortName
);
372 final InspectionProfileManager inspectionProfileManager
= InspectionProfileManager
.getInstance();
373 inspectionProfileManager
.addProfile(profile
);
374 Disposer
.register(getProject(), new Disposable() {
375 public void dispose() {
376 inspectionProfileManager
.deleteProfile(PROFILE
);
379 inspectionProfileManager
.setRootProfile(profile
.getName());
380 InspectionProjectProfileManager
.getInstance(getProject()).updateProfile(profile
);
383 public void tearDown() throws Exception
{
384 LookupManager
.getInstance(getProject()).hideActiveLookup();
386 FileEditorManager editorManager
= FileEditorManager
.getInstance(getProject());
387 VirtualFile
[] openFiles
= editorManager
.getOpenFiles();
388 for (VirtualFile openFile
: openFiles
) {
389 editorManager
.closeFile(openFile
);
392 myProjectFixture
.tearDown();
393 myTempDirFixture
.tearDown();
398 private int configureByFiles(@NonNls String
... filePaths
) throws IOException
{
402 for (String filePath
: filePaths
) {
403 int fileOffset
= configureByFileInner(filePath
);
404 if (fileOffset
> 0) {
411 public void configureByFile(final String file
) throws IOException
{
412 new WriteCommandAction
.Simple(getProject()) {
413 protected void run() throws Throwable
{
414 configureByFileInner(file
);
422 * @return caret offset or -1 if caret marker does not present
423 * @throws IOException
425 private int configureByFileInner(@NonNls String filePath
) throws IOException
{
426 String fullPath
= getTempDirPath() + "/" + filePath
;
428 final VirtualFile copy
= LocalFileSystem
.getInstance().refreshAndFindFileByPath(fullPath
.replace(File
.separatorChar
, '/'));
429 assert copy
!= null: "file " + fullPath
+ " not found";
431 SelectionAndCaretMarkupLoader loader
= new SelectionAndCaretMarkupLoader(copy
.getPath());
433 final OutputStream outputStream
= copy
.getOutputStream(null, 0, 0);
434 outputStream
.write(loader
.newFileText
.getBytes());
435 outputStream
.close();
437 catch (IOException e
) {
438 throw new RuntimeException(e
);
440 if (myFile
== null) myFile
= myPsiManager
.findFile(copy
);
442 if (myEditor
== null) {
443 myEditor
= createEditor(copy
);
444 assert myEditor
!= null;
445 if (loader
.caretMarker
!= null) {
446 offset
= loader
.caretMarker
.getStartOffset();
447 myEditor
.getCaretModel().moveToOffset(offset
);
449 if (loader
.selStartMarker
!= null && loader
.selEndMarker
!= null) {
450 myEditor
.getSelectionModel().setSelection(loader
.selStartMarker
.getStartOffset(), loader
.selEndMarker
.getStartOffset());
457 private Editor
createEditor(VirtualFile file
) {
458 final Project project
= getProject();
459 final FileEditorManager instance
= FileEditorManager
.getInstance(project
);
460 if (file
.getFileType().isBinary()) {
463 return instance
.openTextEditor(new OpenFileDescriptor(project
, file
, 0), false);
466 private void collectAndCheckHighlightings(boolean checkWarnings
, boolean checkInfos
, boolean checkWeakWarnings
, Ref
<Long
> duration
)
468 final Project project
= getProject();
469 ExpectedHighlightingData data
= new ExpectedHighlightingData(myEditor
.getDocument(), checkWarnings
, checkWeakWarnings
, checkInfos
, myFile
);
471 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
473 ((PsiFileImpl
)myFile
).calcTreeElement(); //to load text
475 //to initialize caches
476 myPsiManager
.getCacheManager().getFilesWithWord(XXX
, UsageSearchContext
.IN_COMMENTS
, GlobalSearchScope
.allScope(project
), true);
477 VirtualFileFilter javaFilesFilter
= new VirtualFileFilter() {
478 public boolean accept(VirtualFile file
) {
479 FileType fileType
= FileTypeManager
.getInstance().getFileTypeByFile(file
);
480 return fileType
== StdFileTypes
.JAVA
|| fileType
== StdFileTypes
.CLASS
;
483 myPsiManager
.setAssertOnFileLoadingFilter(javaFilesFilter
); // check repository work
485 final long start
= System
.currentTimeMillis();
486 // ProfilingUtil.startCPUProfiling();
487 Collection
<HighlightInfo
> infos
= doHighlighting();
488 duration
.set(System
.currentTimeMillis() - start
);
489 // ProfilingUtil.captureCPUSnapshot("testing");
491 myPsiManager
.setAssertOnFileLoadingFilter(VirtualFileFilter
.NONE
);
493 data
.checkResult(infos
, myEditor
.getDocument().getText());
497 private Collection
<HighlightInfo
> doHighlighting() {
499 final Project project
= myProjectFixture
.getProject();
501 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
503 Document document
= myEditor
.getDocument();
504 GeneralHighlightingPass action1
= new GeneralHighlightingPass(project
, myFile
, document
, 0, myFile
.getTextLength(), true);
505 action1
.doCollectInformation(new MockProgressIndicator());
506 Collection
<HighlightInfo
> highlights1
= action1
.getHighlights();
508 PostHighlightingPass action2
= new PostHighlightingPass(project
, myFile
, myEditor
, 0, myFile
.getTextLength());
509 action2
.doCollectInformation(new MockProgressIndicator());
510 Collection
<HighlightInfo
> highlights2
= action2
.getHighlights();
512 Collection
<HighlightInfo
> highlights3
= null;
513 if (myAvailableTools
.size() > 0) {
514 LocalInspectionsPass inspectionsPass
= new LocalInspectionsPass(myFile
, myEditor
.getDocument(), 0, myFile
.getTextLength());
515 inspectionsPass
.doCollectInformation(new MockProgressIndicator());
516 highlights3
= inspectionsPass
.getHighlights();
519 ArrayList
<HighlightInfo
> list
= new ArrayList
<HighlightInfo
>();
520 for (HighlightInfo info
: highlights1
) {
524 for (HighlightInfo info
: highlights2
) {
528 if (highlights3
!= null) {
529 for (HighlightInfo info
: highlights3
) {
537 private String
getTestDataPath() {
538 return myTestDataPath
;
541 public Project
getProject() {
542 return myProjectFixture
.getProject();
545 public Editor
getEditor() {
549 public PsiFile
getFile() {
553 public static List
<IntentionAction
> getAvailableIntentions(final Project project
, final Collection
<HighlightInfo
> infos
, final int offset
,
554 final Editor editor
, final PsiFile file
) {
555 final List
<IntentionAction
> availableActions
= new ArrayList
<IntentionAction
>();
557 for (HighlightInfo info
:infos
) {
558 if (info
.quickFixActionRanges
!= null) {
559 for (Pair
<HighlightInfo
.IntentionActionDescriptor
, TextRange
> pair
: info
.quickFixActionRanges
) {
560 if (offset
> 0 && !pair
.getSecond().contains(offset
)) {
563 final HighlightInfo
.IntentionActionDescriptor actionDescriptor
= pair
.first
;
564 final IntentionAction action
= actionDescriptor
.getAction();
565 if (action
.isAvailable(project
, editor
, file
)) {
566 availableActions
.add(action
);
567 final List
<IntentionAction
> actions
= actionDescriptor
.getOptions(file
.findElementAt(editor
.getCaretModel().getOffset()));
568 if (actions
!= null) {
569 for (IntentionAction intentionAction
: actions
) {
570 if (intentionAction
.isAvailable(project
, editor
, file
)) {
571 availableActions
.add(intentionAction
);
580 for (IntentionAction intentionAction
: IntentionManager
.getInstance().getIntentionActions()) {
581 if (intentionAction
.isAvailable(project
, editor
, file
)) {
582 availableActions
.add(intentionAction
);
585 return availableActions
;
588 static class SelectionAndCaretMarkupLoader
{
589 final String newFileText
;
590 final RangeMarker caretMarker
;
591 final RangeMarker selStartMarker
;
592 final RangeMarker selEndMarker
;
594 SelectionAndCaretMarkupLoader(String fullPath
) throws IOException
{
595 final VirtualFile vFile
= LocalFileSystem
.getInstance().refreshAndFindFileByPath(fullPath
.replace(File
.separatorChar
, '/'));
596 assert vFile
!= null: "Cannot find file " + fullPath
;
597 vFile
.refresh(false, false);
598 String fileText
= StringUtil
.convertLineSeparators(VfsUtil
.loadText(vFile
), "\n");
599 Document document
= EditorFactory
.getInstance().createDocument(fileText
);
601 int caretIndex
= fileText
.indexOf(CARET_MARKER
);
602 int selStartIndex
= fileText
.indexOf(SELECTION_START_MARKER
);
603 int selEndIndex
= fileText
.indexOf(SELECTION_END_MARKER
);
605 caretMarker
= caretIndex
>= 0 ? document
.createRangeMarker(caretIndex
, caretIndex
) : null;
606 selStartMarker
= selStartIndex
>= 0 ? document
.createRangeMarker(selStartIndex
, selStartIndex
) : null;
607 selEndMarker
= selEndIndex
>= 0 ? document
.createRangeMarker(selEndIndex
, selEndIndex
) : null;
609 if (caretMarker
!= null) {
610 document
.deleteString(caretMarker
.getStartOffset(), caretMarker
.getStartOffset() + CARET_MARKER
.length());
612 if (selStartMarker
!= null) {
613 document
.deleteString(selStartMarker
.getStartOffset(), selStartMarker
.getStartOffset() + SELECTION_START_MARKER
.length());
615 if (selEndMarker
!= null) {
616 document
.deleteString(selEndMarker
.getStartOffset(), selEndMarker
.getStartOffset() + SELECTION_END_MARKER
.length());
619 newFileText
= document
.getText();
623 private void checkResultByFile(@NonNls String expectedFile
,
624 @NotNull PsiFile originalFile
,
625 boolean stripTrailingSpaces
) throws IOException
{
627 Project project
= myProjectFixture
.getProject();
629 project
.getComponent(PostprocessReformattingAspect
.class).doPostponedFormatting();
630 if (stripTrailingSpaces
) {
631 ((DocumentEx
)myEditor
.getDocument()).stripTrailingSpaces(false);
634 PsiDocumentManager
.getInstance(project
).commitAllDocuments();
636 SelectionAndCaretMarkupLoader loader
= new SelectionAndCaretMarkupLoader(getTestDataPath() + "/" + expectedFile
);
637 String newFileText1
= loader
.newFileText
;
638 if (stripTrailingSpaces
) {
639 Document document1
= EditorFactory
.getInstance().createDocument(loader
.newFileText
);
640 ((DocumentEx
)document1
).stripTrailingSpaces(false);
641 newFileText1
= document1
.getText();
644 String text
= originalFile
.getText();
645 text
= StringUtil
.convertLineSeparators(text
, "\n");
647 //noinspection HardCodedStringLiteral
648 TestCase
.assertEquals( "Text mismatch in file " + expectedFile
, newFileText1
, text
);
650 if (loader
.caretMarker
!= null) {
651 int caretLine
= StringUtil
.offsetToLineNumber(loader
.newFileText
, loader
.caretMarker
.getStartOffset());
652 int caretCol
= loader
.caretMarker
.getStartOffset() - StringUtil
.lineColToOffset(loader
.newFileText
, caretLine
, 0);
654 TestCase
.assertEquals("caretLine", caretLine
+ 1, myEditor
.getCaretModel().getLogicalPosition().line
+ 1);
655 TestCase
.assertEquals("caretColumn", caretCol
+ 1, myEditor
.getCaretModel().getLogicalPosition().column
+ 1);
658 if (loader
.selStartMarker
!= null && loader
.selEndMarker
!= null) {
659 int selStartLine
= StringUtil
.offsetToLineNumber(loader
.newFileText
, loader
.selStartMarker
.getStartOffset());
660 int selStartCol
= loader
.selStartMarker
.getStartOffset() - StringUtil
.lineColToOffset(loader
.newFileText
, selStartLine
, 0);
662 int selEndLine
= StringUtil
.offsetToLineNumber(loader
.newFileText
, loader
.selEndMarker
.getEndOffset());
663 int selEndCol
= loader
.selEndMarker
.getEndOffset() - StringUtil
.lineColToOffset(loader
.newFileText
, selEndLine
, 0);
665 TestCase
.assertEquals("selectionStartLine", selStartLine
+ 1,
666 StringUtil
.offsetToLineNumber(loader
.newFileText
, myEditor
.getSelectionModel().getSelectionStart()) + 1);
668 TestCase
.assertEquals("selectionStartCol", selStartCol
+ 1, myEditor
.getSelectionModel().getSelectionStart() -
669 StringUtil
.lineColToOffset(loader
.newFileText
, selStartLine
, 0) + 1);
671 TestCase
.assertEquals("selectionEndLine", selEndLine
+ 1,
672 StringUtil
.offsetToLineNumber(loader
.newFileText
, myEditor
.getSelectionModel().getSelectionEnd()) + 1);
674 TestCase
.assertEquals("selectionEndCol", selEndCol
+ 1,
675 myEditor
.getSelectionModel().getSelectionEnd() - StringUtil
.lineColToOffset(loader
.newFileText
, selEndLine
, 0) + 1);
678 TestCase
.assertTrue("has no selection", !myEditor
.getSelectionModel().hasSelection());