2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.execution
.testframework
.sm
;
18 import com
.intellij
.navigation
.ChooseByNameContributor
;
19 import com
.intellij
.navigation
.NavigationItem
;
20 import com
.intellij
.openapi
.application
.ApplicationManager
;
21 import com
.intellij
.openapi
.extensions
.Extensions
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
24 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
25 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
26 import com
.intellij
.openapi
.vfs
.VirtualFile
;
27 import com
.intellij
.openapi
.vfs
.ex
.temp
.TempFileSystem
;
28 import com
.intellij
.psi
.PsiFile
;
29 import org
.jetbrains
.annotations
.NonNls
;
30 import org
.jetbrains
.annotations
.NotNull
;
31 import org
.jetbrains
.annotations
.Nullable
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Collections
;
35 import java
.util
.List
;
38 * @author Roman Chernyatchik
40 public class TestsLocationProviderUtil
{
41 @NonNls private static final String PROTOCOL_SEPARATOR
= "://";
43 private TestsLocationProviderUtil() {
47 public static String
extractProtocol(@NotNull final String locationUrl
) {
48 final int index
= locationUrl
.indexOf(PROTOCOL_SEPARATOR
);
50 return locationUrl
.substring(0, index
);
56 public static String
extractPath(@NotNull final String locationUrl
) {
57 final int index
= locationUrl
.indexOf(PROTOCOL_SEPARATOR
);
59 return locationUrl
.substring(index
+ PROTOCOL_SEPARATOR
.length());
64 public static List
<VirtualFile
> findSuitableFilesFor(final String filePath
, final Project project
) {
65 final ProjectFileIndex index
= ProjectRootManager
.getInstance(project
).getFileIndex();
67 // at first let's try to find file as is, by it's real path
68 // and check that file belongs to current project
69 // this location provider designed for tests thus we will check only project content
70 // (we cannot check just sources or tests folders because RM doesn't use it
71 final VirtualFile file
= getByFullPath(filePath
);
72 final boolean inProjectContent
= file
!= null && (index
.isInContent(file
));
74 if (inProjectContent
) {
75 return Collections
.singletonList(file
);
78 //TODO: split file by "/" in parts
79 final List
<String
> folders
= new ArrayList
<String
>();
81 final String fileName
= "foo.feature";
83 //otherwise let's find all files with the same name and similar relative path
84 final List
<NavigationItem
> items
= new ArrayList
<NavigationItem
>();
85 final ChooseByNameContributor
[] contributors
= Extensions
.getExtensions(ChooseByNameContributor
.FILE_EP_NAME
);
86 for (ChooseByNameContributor contributor
: contributors
) {
87 // let's find files with same name in project and libraries
88 final NavigationItem
[] navigationItems
= contributor
.getItemsByName(fileName
, fileName
, project
, true);
89 for (NavigationItem navigationItem
: navigationItems
) {
90 if (navigationItem
instanceof PsiFile
) {
91 items
.add(navigationItem
);
95 //TODO let's filter psi files ...
97 //TODO let's iterate relative path components and determine which files are closer to our relative path
98 // let's extract the closest files to relative path. For this we will sort items by distance and
99 // we also assume that relative files and folders should have at least one common parent folder - just to remove false positives on some cases
100 for (String folder
: folders
) {
104 return Collections
.emptyList();
108 private static VirtualFile
getByFullPath(String filePath
) {
109 final VirtualFile fileByPath
= LocalFileSystem
.getInstance().findFileByPath(filePath
);
110 if (fileByPath
!= null) {
113 // if we are in UnitTest mode probably TempFileSystem is used instead of LocaFileSystem
114 if (ApplicationManager
.getApplication().isUnitTestMode()) {
115 final VirtualFile tempFileByPath
= TempFileSystem
.getInstance().findFileByPath(filePath
);
116 return tempFileByPath
;