core/com.nokia.carbide.search.system.tests/src/com/nokia/carbide/search/system/tests/BasicTest.java
changeset 2159 db61d072b92b
equal deleted inserted replaced
2158:ac14826aa14b 2159:db61d072b92b
       
     1 /**
       
     2  * 
       
     3  */
       
     4 package com.nokia.carbide.search.system.tests;
       
     5 
       
     6 import java.io.File;
       
     7 import java.io.FileFilter;
       
     8 import java.util.ArrayList;
       
     9 import java.util.Collections;
       
    10 import java.util.List;
       
    11 import java.util.regex.Pattern;
       
    12 
       
    13 import org.eclipse.cdt.core.CCorePlugin;
       
    14 import org.eclipse.cdt.core.dom.IPDOMManager;
       
    15 import org.eclipse.core.resources.IProject;
       
    16 import org.eclipse.core.resources.IResource;
       
    17 import org.eclipse.core.resources.IWorkspace;
       
    18 import org.eclipse.core.resources.IWorkspaceDescription;
       
    19 import org.eclipse.core.resources.ResourcesPlugin;
       
    20 import org.eclipse.core.runtime.IPath;
       
    21 import org.eclipse.core.runtime.IProgressMonitor;
       
    22 import org.eclipse.core.runtime.IStatus;
       
    23 import org.eclipse.core.runtime.NullProgressMonitor;
       
    24 import org.eclipse.core.runtime.Path;
       
    25 import org.eclipse.core.runtime.Platform;
       
    26 import org.eclipse.core.runtime.content.IContentType;
       
    27 import org.eclipse.jface.resource.ImageDescriptor;
       
    28 import org.eclipse.ui.IWorkingSet;
       
    29 import org.junit.Test;
       
    30 
       
    31 import com.nokia.carbide.cpp.project.core.ProjectCorePlugin;
       
    32 import com.nokia.carbide.cpp.sdk.core.ISymbianBuildContext;
       
    33 import com.nokia.carbide.cpp.sdk.core.ISymbianBuilderID;
       
    34 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    35 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    36 import com.nokia.carbide.search.system.core.text.TextSearchEngine;
       
    37 import com.nokia.carbide.search.system.core.text.TextSearchScope;
       
    38 import com.nokia.carbide.search.system.internal.core.text.PatternConstructor;
       
    39 import com.nokia.carbide.search.system.internal.core.text.TextSearchEngineRegistry;
       
    40 import com.nokia.carbide.search.system.internal.ui.SearchMessages;
       
    41 import com.nokia.carbide.search.system.internal.ui.SearchPlugin;
       
    42 import com.nokia.carbide.search.system.internal.ui.text.BasicElementLabels;
       
    43 import com.nokia.carbide.search.system.internal.ui.text.FileSearchQuery;
       
    44 import com.nokia.carbide.search.system.internal.ui.text.FileSearchResult;
       
    45 import com.nokia.carbide.search.system.ui.ISearchQuery;
       
    46 import com.nokia.carbide.search.system.ui.ISearchResult;
       
    47 import com.nokia.carbide.search.system.ui.text.FileTextSearchScope;
       
    48 import com.nokia.carbide.search.system.ui.text.IEditorMatchAdapter;
       
    49 import com.nokia.carbide.search.system.ui.text.IFileMatchAdapter;
       
    50 import com.nokia.cpp.internal.api.utils.core.FileUtils;
       
    51 
       
    52 import junit.framework.TestCase;
       
    53 
       
    54 /**
       
    55  *
       
    56  */
       
    57 public class BasicTest extends TestCase {
       
    58 	static IProject carbideProject;
       
    59 	private static final String BASE_DIR = "data/TestProject/";
       
    60 	private static final String CARBIDE_PROJECT_NAME = "CarbideProject";
       
    61 	private static final String PROJECT_RELATIVE_BLDINF_PATH = "group/bld.inf";
       
    62 
       
    63 	@Override
       
    64 	protected void setUp() throws Exception {
       
    65 		// TODO Auto-generated method stub
       
    66 		super.setUp();
       
    67 	}
       
    68 
       
    69 	@Override
       
    70 	protected void tearDown() throws Exception {
       
    71 		// TODO Auto-generated method stub
       
    72 		super.tearDown();
       
    73 	}
       
    74 	
       
    75 	@Test
       
    76 	public void testTextSearchEngineRegistry() throws Exception {
       
    77 		TextSearchEngine engine = TextSearchEngine.create();
       
    78 		TextSearchEngineRegistry registry = SearchPlugin.getDefault().getTextSearchEngineRegistry();
       
    79 		String[][] availableEngines = registry.getAvailableEngines();
       
    80 	}
       
    81 
       
    82 	@Test
       
    83 	public void testBasicFileTextSearch() throws Exception {
       
    84 		String folderName = TestPlugin.getPluginPath("data");
       
    85 		String[] fileNamePatterns = {"*.txt"};
       
    86 		boolean includeSubFolders = true;
       
    87 		boolean includeHidden = true;
       
    88 		boolean includeArchives = true;
       
    89 		FileTextSearchScope scope = FileTextSearchScope.newSystemScope(folderName, fileNamePatterns, folderName, includeSubFolders, includeHidden, includeArchives);
       
    90 		assertNotNull("Search scope not created", scope);
       
    91 		assertNotNull("Scope description is null", scope.getDescription());
       
    92 		assertNull("Scope working set is not null", scope.getWorkingSets());
       
    93 		assertNull("Scope content types is not null", scope.getContentTypes());
       
    94 		assertNotNull("Scope fileNamePatterns is null", scope.getFileNamePatterns());
       
    95 		assertNotNull("Scope filterDescription is null", scope.getFilterDescription());
       
    96 		assertEquals("Scope includeSubFolders not expected", includeSubFolders, scope.includeSubfolders());
       
    97 		assertEquals("Scope isIncludeSubFolders not expected", includeSubFolders, scope.isIncludeSubfolders());
       
    98 		assertEquals("Scope includeHidden not expected", includeHidden, scope.includeHidden());
       
    99 		assertEquals("Scope isIncludeHidden not expected", includeHidden, scope.isIncludeHidden());
       
   100 		assertEquals("Scope includeArchives not expected", includeArchives, scope.includeArchives());
       
   101 		IResource[] res = scope.getRoots();
       
   102 		scope.getRootFolder();
       
   103 
       
   104 		String searchText = "the";
       
   105 		boolean isRegEx = false;
       
   106 		boolean isCaseSensitive = false;
       
   107 		FileSearchQuery query = new FileSearchQuery(searchText, isRegEx, isCaseSensitive, scope);
       
   108 		assertNotNull("Query not created", query);
       
   109 		assertEquals("Query isRegEx not expected", isRegEx, query.isRegexSearch());
       
   110 		assertEquals("Query isCaseSensitive not expected", isCaseSensitive, query.isCaseSensitive());
       
   111 		assertTrue("Query canRerun not true", query.canRerun());
       
   112 		assertTrue("Query canRunInBackground not true", query.canRunInBackground());
       
   113 		assertNotNull("Query scope is null", query.getSearchScope());
       
   114 		assertEquals("Query search label not expected", SearchMessages.FileSearchQuery_label, query.getLabel());
       
   115 		assertEquals("Query search text not expected", searchText, query.getSearchString());
       
   116 		
       
   117 		FileSearchResult result = (FileSearchResult)query.getSearchResult();
       
   118 		assertNotNull("Search results are null", result);
       
   119 
       
   120 		IProgressMonitor monitor = new NullProgressMonitor();
       
   121 		IStatus status = query.run(monitor);
       
   122 		assertEquals("Status from search query should be 0", 0, status.getCode());
       
   123 		result = (FileSearchResult)query.getSearchResult();
       
   124 		int matchCount = result.getMatchCount();
       
   125 		assertTrue("Expected number of matches not found", matchCount > 0);
       
   126 		Object[] elements = result.getElements();
       
   127 		assertNotNull("Result elements is null", elements);
       
   128 		String toolTip = result.getTooltip();
       
   129 		ImageDescriptor d = result.getImageDescriptor();
       
   130 		FileSearchQuery q = (FileSearchQuery)result.getQuery();
       
   131 		IFileMatchAdapter f = result.getFileMatchAdapter();
       
   132 		IEditorMatchAdapter e = result.getEditorMatchAdapter();
       
   133 		String l = result.getLabel();
       
   134 		String rl = q.getResultLabel(1);
       
   135 		TextSearchEngineRegistry registry = SearchPlugin.getDefault().getTextSearchEngineRegistry();
       
   136 		String[][] availableEngines = registry.getAvailableEngines();
       
   137 		
       
   138 		// file search only
       
   139 		query = new FileSearchQuery("", isRegEx, isCaseSensitive, scope);
       
   140 		status = query.run(monitor);
       
   141 		assertEquals("Status from search query should be 0", 0, status.getCode());
       
   142 		result = (FileSearchResult)query.getSearchResult();
       
   143 		matchCount = result.getMatchCount();
       
   144 		assertTrue("Expected number of matches not found", matchCount > 0);
       
   145 		toolTip = result.getTooltip();
       
   146 		
       
   147 	}
       
   148 	@Test
       
   149 	public void testPatternConstructor() throws Exception {
       
   150 		Pattern pattern = PatternConstructor.createPattern("word word", true, false, true, true);
       
   151 		pattern = PatternConstructor.createPattern("word word", false, false, true, true);
       
   152 		pattern = PatternConstructor.createPattern("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", true, false, true, true);
       
   153 	}
       
   154 	@Test
       
   155 	public void testBasicElementLabels() throws Exception {
       
   156 		BasicElementLabels.getFilePattern("file");
       
   157 		IPath p = new Path("/os");
       
   158 		BasicElementLabels.getPathLabel(p, true);
       
   159 		BasicElementLabels.getPathLabel(p, false);
       
   160 		BasicElementLabels.getResourceName("resource");
       
   161 		BasicElementLabels.getURLPart("http://www.x.xom/s.html#1");
       
   162 		BasicElementLabels.getVersionName("1.4.1");
       
   163 	}
       
   164 	
       
   165 	@Test
       
   166 	public void testResourceSearch() throws Exception {
       
   167 //		setUpProject();
       
   168 //		tearDownProject();
       
   169 	}
       
   170 
       
   171 	private void tearDownProject() throws Exception {
       
   172 		carbideProject.delete(true, true, null);
       
   173 	}
       
   174 
       
   175 	private void setUpProject() throws Exception {
       
   176 		if (carbideProject == null){
       
   177 			// turn off the indexer
       
   178 			CCorePlugin.getIndexManager().setDefaultIndexerId(IPDOMManager.ID_NO_INDEXER);
       
   179 
       
   180 			// turn off auto-building
       
   181 			IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
   182 			IWorkspaceDescription workspaceDesc = workspace.getDescription();
       
   183 			workspaceDesc.setAutoBuilding(false);
       
   184 			workspace.setDescription(workspaceDesc);
       
   185 
       
   186 			// create the test project
       
   187 			carbideProject = ProjectCorePlugin.createProject(CARBIDE_PROJECT_NAME, null);
       
   188 			assertNotNull(carbideProject);
       
   189 
       
   190 			// copy the project contents into the workspace
       
   191 			File baseDir;
       
   192 			if (Platform.isRunning()) {
       
   193 				baseDir = FileUtils.pluginRelativeFile(TestPlugin.getDefault(), BASE_DIR);
       
   194 			} else {
       
   195 				baseDir = new File(BASE_DIR).getAbsoluteFile();
       
   196 			}
       
   197 
       
   198 			FileUtils.copyTreeNoParent(baseDir, carbideProject.getLocation().toFile(), new FileFilter() {
       
   199 				public boolean accept(File arg0) {
       
   200 					return true;
       
   201 				}
       
   202 			});
       
   203 			
       
   204 			// refresh the workspace
       
   205 			ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
       
   206 
       
   207 			List<ISymbianBuildContext> configs = getUsableBuildConfigs();
       
   208 			
       
   209 			ProjectCorePlugin.postProjectCreatedActions(carbideProject, PROJECT_RELATIVE_BLDINF_PATH, configs, new ArrayList<String>(), "Debug MMP", null, new NullProgressMonitor());
       
   210 		}
       
   211 	}
       
   212 
       
   213 	/**
       
   214 	 * Get some build configurations for the first non-empty SDK we find.
       
   215 	 * @return a list of contexts, maximum 8
       
   216 	 */
       
   217 	private List<ISymbianBuildContext> getUsableBuildConfigs() {
       
   218 		for (ISymbianSDK sdk : SDKCorePlugin.getSDKManager().getSDKList()) {
       
   219 			if ((new File(sdk.getEPOCROOT()).exists())){
       
   220 				// TODO: Convert to SBSv2 test
       
   221 				List<ISymbianBuildContext> contexts = sdk.getBuildInfo(ISymbianBuilderID.SBSV1_BUILDER).getAllBuildConfigurations();
       
   222 				if (contexts.size() > 0) {
       
   223 					return contexts.subList(0, Math.min(contexts.size(), 8));
       
   224 				}
       
   225 			}
       
   226 		}
       
   227 		TestCase.fail("No installed SDKs provide build configurations");
       
   228 		return Collections.emptyList();
       
   229 	}
       
   230 }