001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.search;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014:
015: import junit.framework.Assert;
016:
017: import org.eclipse.core.runtime.CoreException;
018:
019: import org.eclipse.core.resources.IFile;
020:
021: import org.eclipse.search.ui.NewSearchUI;
022: import org.eclipse.search.ui.text.Match;
023:
024: import org.eclipse.jdt.core.ICompilationUnit;
025: import org.eclipse.jdt.core.IType;
026: import org.eclipse.jdt.core.search.SearchEngine;
027:
028: import org.eclipse.jdt.internal.ui.refactoring.nls.search.CompilationUnitEntry;
029: import org.eclipse.jdt.internal.ui.refactoring.nls.search.FileEntry;
030: import org.eclipse.jdt.internal.ui.refactoring.nls.search.NLSSearchQuery;
031: import org.eclipse.jdt.internal.ui.refactoring.nls.search.NLSSearchResult;
032: import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
033:
034: public class NLSSearchTestHelper {
035:
036: public static void assertNumberOfProblems(
037: ICompilationUnit accessor, IFile propertiesFile,
038: int expected) {
039: assertNumberResults(searchProblems(accessor, propertiesFile),
040: expected);
041: }
042:
043: public static void assertHasUndefinedKey(ICompilationUnit accessor,
044: IFile propertiesFile, String key, IFile file)
045: throws IOException, CoreException {
046: assertResultHasUndefinedKey(key, file, searchProblems(accessor,
047: propertiesFile));
048: }
049:
050: public static void assertHasUnusedKey(ICompilationUnit accessor,
051: IFile propertiesFile, String key, IFile file)
052: throws IOException, CoreException {
053: assertResultHasUnusedKey(key, file, searchProblems(accessor,
054: propertiesFile));
055: }
056:
057: public static void assertHasDuplicateKey(ICompilationUnit accessor,
058: IFile propertiesFile, String key, IFile file)
059: throws CoreException, IOException {
060: assertResultHasDuplicateKey(key, file, searchProblems(accessor,
061: propertiesFile));
062: }
063:
064: private static NLSSearchResult searchProblems(
065: ICompilationUnit accessor, IFile propertiesFile) {
066: IType type = accessor.getType("Accessor");
067: NLSSearchQuery query = new NLSSearchQuery(
068: (new IType[] { type }),
069: (new IFile[] { propertiesFile }), SearchEngine
070: .createWorkspaceScope(), ""); //$NON-NLS-1$
071: NewSearchUI.runQueryInForeground(
072: new BusyIndicatorRunnableContext(), query);
073: NLSSearchResult result = (NLSSearchResult) query
074: .getSearchResult();
075: return result;
076: }
077:
078: private static void assertNumberResults(NLSSearchResult result,
079: int expected) {
080: int is = result.getElements().length;
081: Assert.assertTrue("Expected number of results is " + expected
082: + " but was " + is, is == expected);
083: }
084:
085: private static void assertResultHasUndefinedKey(String key,
086: IFile file, NLSSearchResult result) throws IOException,
087: CoreException {
088: Match[] matches = result.getFileMatchAdapter()
089: .computeContainedMatches(result, file);
090:
091: for (int i = 0; i < matches.length; i++) {
092: Match match = matches[i];
093: if (match.getElement() instanceof ICompilationUnit) {
094: ICompilationUnit unit = (ICompilationUnit) match
095: .getElement();
096: String field = unit.getSource().substring(
097: match.getOffset(),
098: match.getOffset() + match.getLength());
099: if (field.indexOf(key) != -1)
100: return;
101: }
102: }
103:
104: Assert.assertTrue("No undefined key problem found for " + key
105: + " in " + file.getName(), false);
106: }
107:
108: private static void assertResultHasUnusedKey(String key,
109: IFile file, NLSSearchResult result) throws IOException,
110: CoreException {
111: Match[] matches = result.getFileMatchAdapter()
112: .computeContainedMatches(result, file);
113:
114: for (int i = 0; i < matches.length; i++) {
115: Match match = matches[i];
116: if (match.getElement() instanceof CompilationUnitEntry) {
117: ICompilationUnit unit = ((CompilationUnitEntry) match
118: .getElement()).getCompilationUnit();
119: String field = unit.getSource().substring(
120: match.getOffset(),
121: match.getOffset() + match.getLength());
122: if (field.indexOf(key) != -1)
123: return;
124: } else if (match.getElement() instanceof FileEntry) {
125: FileEntry entry = (FileEntry) match.getElement();
126: String content = getContent(entry.getPropertiesFile());
127: String propkey = content.substring(match.getOffset(),
128: match.getOffset() + match.getLength());
129: if (propkey.indexOf(key) != -1)
130: return;
131: }
132: }
133:
134: Assert.assertTrue("No unused key problem found for " + key
135: + " in " + file.getName(), false);
136: }
137:
138: private static String getContent(IFile entry) throws CoreException,
139: IOException {
140: StringBuffer buf = new StringBuffer();
141: InputStream contents = entry.getContents();
142: try {
143: char ch = (char) contents.read();
144: int avilable = contents.available();
145: while (avilable > 0 && ch != -1) {
146: buf.append(ch);
147: ch = (char) contents.read();
148: avilable--;
149: }
150: return buf.toString();
151: } finally {
152: contents.close();
153: }
154: }
155:
156: private static void assertResultHasDuplicateKey(String key,
157: IFile file, NLSSearchResult result) throws CoreException,
158: IOException {
159: Match[] matches = result.getFileMatchAdapter()
160: .computeContainedMatches(result, file);
161:
162: for (int i = 0; i < matches.length; i++) {
163: Match match = matches[i];
164: if (match.getElement() instanceof FileEntry) {
165: FileEntry entry = (FileEntry) match.getElement();
166: String content = getContent(entry.getPropertiesFile());
167: int firstIndex = content.indexOf(key);
168: if (firstIndex != -1
169: && content.indexOf(key, firstIndex + 1) != -1)
170: return;
171: }
172: }
173:
174: Assert.assertTrue("No duplicate key problem found for " + key
175: + " in " + file.getName(), false);
176: }
177: }
|