mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
move properties file tests to community
This commit is contained in:
+121
@@ -0,0 +1,121 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.lang.properties;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightTestCase;
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import com.intellij.testFramework.PsiTestUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author cdr
|
||||
*/
|
||||
public class PropertiesPerformanceTest extends CodeInsightTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ReferenceProvidersRegistry.getInstance();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Module createMainModule() throws IOException {
|
||||
String root = PluginPathManager.getPluginHomePath("java-i18n") + "/testData/performance/" + getTestName(true);
|
||||
VirtualFile tempProjectRootDir = PsiTestUtil.createTestProjectStructure(myProject, null, root, myFilesToDelete, false);
|
||||
Module module = loadAllModulesUnder(tempProjectRootDir);
|
||||
return module != null ? module : super.createMainModule();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/performance/";
|
||||
}
|
||||
|
||||
public void testTypingInBigFile() throws Exception {
|
||||
configureByFile(getTestName(true) + "/File1.properties");
|
||||
PlatformTestUtil.startPerformanceTest(getTestName(false), 100, () -> {
|
||||
type(' ');
|
||||
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
|
||||
backspace();
|
||||
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
|
||||
}).assertTiming();
|
||||
}
|
||||
|
||||
public void testResolveManyLiterals() throws Exception {
|
||||
final PsiClass aClass = generateTestFiles();
|
||||
assertNotNull(aClass);
|
||||
PlatformTestUtil.startPerformanceTest(getTestName(false), 2000, () -> aClass.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitLiteralExpression(PsiLiteralExpression expression) {
|
||||
PsiReference[] references = expression.getReferences();
|
||||
for (PsiReference reference : references) {
|
||||
reference.resolve();
|
||||
}
|
||||
}
|
||||
})).useLegacyScaling().assertTiming();
|
||||
}
|
||||
|
||||
private PsiClass generateTestFiles() throws IOException {
|
||||
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(myModule).getSourceRoots();
|
||||
assertTrue(sourceRoots.length > 0);
|
||||
final String src = sourceRoots[0].getPath();
|
||||
String className = "PropRef";
|
||||
|
||||
FileWriter classWriter = new FileWriter(new File(src, className + ".java"));
|
||||
try {
|
||||
classWriter.write("class " + className + "{");
|
||||
for (int f = 0; f < 100; f++) {
|
||||
FileWriter writer = new FileWriter(new File(src, "prop" + f + ".properties"));
|
||||
try {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
String key = "prop." + f + ".number." + i;
|
||||
writer.write(key + "=" + key + "\n");
|
||||
classWriter.write("String s_" + f + "_" + i + "=\"" + key + "\";\n");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
classWriter.write("}");
|
||||
}
|
||||
finally {
|
||||
classWriter.close();
|
||||
}
|
||||
|
||||
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(src);
|
||||
assertNotNull(src, virtualFile);
|
||||
virtualFile.refresh(false, true);
|
||||
|
||||
final PsiClass aClass = myJavaFacade.findClass(className, GlobalSearchScope.allScope(myProject));
|
||||
assert aClass != null;
|
||||
PsiDocumentManager.getInstance(getProject()).getDocument(aClass.getContainingFile());
|
||||
|
||||
aClass.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitLiteralExpression(PsiLiteralExpression expression) {
|
||||
expression.getNode();
|
||||
}
|
||||
});
|
||||
for (int f = 0; f < 100; f++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
String key = "prop." + f + ".number." + i;
|
||||
PropertiesImplUtil.findPropertiesByKey(getProject(), key);
|
||||
}
|
||||
}
|
||||
|
||||
return aClass;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.lang.properties;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightTestCase;
|
||||
import com.intellij.codeInsight.TargetElementUtil;
|
||||
import com.intellij.codeInsight.navigation.actions.GotoDeclarationAction;
|
||||
import com.intellij.ide.startup.impl.StartupManagerImpl;
|
||||
import com.intellij.lang.properties.psi.PropertiesFile;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.openapi.startup.StartupManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiPolyVariantReference;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.testFramework.PlatformTestCase;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
/**
|
||||
* @author cdr
|
||||
*/
|
||||
@PlatformTestCase.WrapInCommand
|
||||
public class PropertiesResolveTest extends CodeInsightTestCase{
|
||||
private static final String BASE_PATH = "testData/resolve/";
|
||||
|
||||
public void testJavaStringLiteral() throws Exception{
|
||||
configure("C.java");
|
||||
checkNavigation();
|
||||
}
|
||||
public void testJavaStringLiteralEscaped() throws Exception{
|
||||
configure("Escaped.java");
|
||||
checkNavigation();
|
||||
}
|
||||
|
||||
public void testPropertiesUpdating() throws Exception{
|
||||
configure("C.java");
|
||||
|
||||
final int offset = myEditor.getCaretModel().getOffset();
|
||||
final PsiReference findReference = TargetElementUtil.findReference(myEditor, offset);
|
||||
final PsiPolyVariantReference reference = assertInstanceOf(findReference, PsiPolyVariantReference.class);
|
||||
assertSize(1, reference.multiResolve(false));
|
||||
|
||||
final PropertiesFile propertiesFile = findPropertiesFile();
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
propertiesFile.addProperty("new", "property");
|
||||
});
|
||||
|
||||
assertSize(1, reference.multiResolve(false));
|
||||
}
|
||||
|
||||
private void checkNavigation() {
|
||||
final int offset = myEditor.getCaretModel().getOffset();
|
||||
final PsiElement elementAtCaret = myFile.findElementAt(offset);
|
||||
assertNotNull(elementAtCaret);
|
||||
final PsiElement targetElement = GotoDeclarationAction.findTargetElement(myProject, myEditor, offset);
|
||||
final PropertiesFile propertiesFile = findPropertiesFile();
|
||||
final IProperty property = assertInstanceOf(targetElement, IProperty.class);
|
||||
assertEquals(property.getPropertiesFile(), propertiesFile);
|
||||
|
||||
final String elementText = elementAtCaret.getText();
|
||||
final String unescapedKey = property.getUnescapedKey();
|
||||
assertNotNull(unescapedKey);
|
||||
assertTrue(elementText.contains(unescapedKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginPathManager.getPluginHomePath("java-i18n") + "/";
|
||||
}
|
||||
|
||||
private void configure(@NonNls final String fileName) throws Exception {
|
||||
configureByFile(BASE_PATH + fileName, BASE_PATH);
|
||||
((StartupManagerImpl)StartupManager.getInstance(myProject)).runPostStartupActivities();
|
||||
}
|
||||
|
||||
private PropertiesFile findPropertiesFile() {
|
||||
return (PropertiesFile)myFile.getContainingDirectory().findFile("myprops.properties");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.lang.properties;
|
||||
|
||||
import com.intellij.ide.startup.impl.StartupManagerImpl;
|
||||
import com.intellij.lang.properties.psi.PropertiesFile;
|
||||
import com.intellij.lang.properties.psi.Property;
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.startup.StartupManager;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor;
|
||||
import com.intellij.refactoring.MultiFileTestCase;
|
||||
import com.intellij.refactoring.rename.RenameProcessor;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class RenamePropertyTest extends MultiFileTestCase {
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
((StartupManagerImpl)StartupManager.getInstance(myProject)).runPostStartupActivities();
|
||||
}
|
||||
|
||||
public void testBundle() {
|
||||
doTest("xxx.properties","a.b","c.d");
|
||||
}
|
||||
|
||||
public void testNamesConflict() {
|
||||
try {
|
||||
doTest("xxx.properties","a.b","c.d");
|
||||
fail("Conflict was not detected");
|
||||
}
|
||||
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
|
||||
Assert.assertEquals("New property name 'c.d' hides existing property", e.getMessage());
|
||||
return;
|
||||
}
|
||||
fail("Conflict was not detected");
|
||||
}
|
||||
|
||||
|
||||
private void doTest(final String fileName, final String key, final String newName) {
|
||||
doTest(new PerformAction() {
|
||||
@Override
|
||||
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) {
|
||||
RenamePropertyTest.this.performAction(rootDir, fileName, key, newName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void performAction(final VirtualFile rootDir, String fileName, String key, String newName) {
|
||||
VirtualFile child = rootDir.findChild(fileName);
|
||||
|
||||
PropertiesFile file = (PropertiesFile)myPsiManager.findFile(child);
|
||||
Property property = (Property)file.findPropertyByKey(key);
|
||||
|
||||
new RenameProcessor(myProject, property, newName, true, true).run();
|
||||
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginPathManager.getPluginHomePath("java-i18n") + "/";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestRoot() {
|
||||
return "/testData/rename/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user