mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
java: a test of mass destruction replaced by a more pointed one
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.psi;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.testFramework.PsiTestCase;
|
||||
import com.intellij.testFramework.SkipSlowTestLocally;
|
||||
import com.intellij.usageView.UsageViewLongNameLocation;
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@SkipSlowTestLocally
|
||||
public class ClsDuplicatesTest extends PsiTestCase {
|
||||
private Set<PsiNamedElement> myUnique = new THashSet<PsiNamedElement>(new TObjectHashingStrategy<PsiNamedElement>() {
|
||||
@Override
|
||||
public int computeHashCode(PsiNamedElement object) {
|
||||
String name = object.getName();
|
||||
return name == null ? 0 : name.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(PsiNamedElement o1, PsiNamedElement o2) {
|
||||
return o1.getParent() == o2.getParent() &&
|
||||
o1.getClass() == o2.getClass() &&
|
||||
StringUtil.equals(o1.getName(), o2.getName()) &&
|
||||
StringUtil.equals(o1.getText(), o2.getText());
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
myUnique = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Sdk getTestProjectJdk() {
|
||||
return JavaTestUtil.getTestJdk();
|
||||
}
|
||||
|
||||
public void testDuplicates() throws Exception {
|
||||
final PsiPackage rootPackage = JavaPsiFacade.getInstance(getProject()).findPackage("");
|
||||
assert rootPackage != null;
|
||||
|
||||
final GlobalSearchScope scope = GlobalSearchScope.allScope(getProject());
|
||||
JavaRecursiveElementVisitor visitor = new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitPackage(PsiPackage aPackage) {
|
||||
visit(aPackage);
|
||||
for (PsiPackage subPackage : aPackage.getSubPackages(scope)) {
|
||||
visitPackage(subPackage);
|
||||
}
|
||||
for (PsiClass aClass : aPackage.getClasses(scope)) {
|
||||
visitClass(aClass);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
if (element instanceof PsiNamedElement) {
|
||||
visit((PsiNamedElement)element);
|
||||
}
|
||||
super.visitElement(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) {
|
||||
super.visitClass(aClass);
|
||||
PsiElement parent = aClass.getParent();
|
||||
if (parent instanceof PsiFile) {
|
||||
myUnique.clear();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
rootPackage.accept(visitor);
|
||||
}
|
||||
|
||||
private void visit(PsiNamedElement element) {
|
||||
if (!myUnique.add(element)) {
|
||||
String description = ElementDescriptionUtil.getElementDescription(element, UsageViewLongNameLocation.INSTANCE);
|
||||
fail("Duplicate Element: " + description + ": " + element.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,15 +18,18 @@ package com.intellij.psi;
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.JarFileSystem;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.StandardFileSystems;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileSystem;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.impl.compiled.ClsFileImpl;
|
||||
import com.intellij.psi.impl.compiled.InnerClassSourceStrategy;
|
||||
import com.intellij.psi.impl.compiled.StubBuildingVisitor;
|
||||
import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl;
|
||||
import com.intellij.testFramework.LightIdeaTestCase;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import org.jetbrains.org.objectweb.asm.ClassReader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -75,6 +78,35 @@ public class ClsMirrorBuildingTest extends LightIdeaTestCase {
|
||||
doTest(clsPath, txtPath);
|
||||
}
|
||||
|
||||
public void testStrayInnersFiltering() throws IOException {
|
||||
String path = JavaTestUtil.getJavaTestDataPath() + "/../../mockJDK-1.8/jre/lib/rt.jar!/java/lang/Class.class";
|
||||
VirtualFile file = StandardFileSystems.jar().findFileByPath(path);
|
||||
assertNotNull(path, file);
|
||||
|
||||
InnerClassSourceStrategy<VirtualFile> strategy = new InnerClassSourceStrategy<VirtualFile>() {
|
||||
@Override
|
||||
public VirtualFile findInnerClass(String innerName, VirtualFile outerClass) {
|
||||
String baseName = outerClass.getNameWithoutExtension();
|
||||
VirtualFile child = outerClass.getParent().findChild(baseName + "$" + innerName + ".class");
|
||||
// stray inner classes should be filtered out
|
||||
assert child != null : innerName + " is not an inner class of " + outerClass;
|
||||
return child;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(VirtualFile innerClass, StubBuildingVisitor<VirtualFile> visitor) {
|
||||
try {
|
||||
byte[] bytes = innerClass.contentsToByteArray();
|
||||
new ClassReader(bytes).accept(visitor, ClassReader.SKIP_FRAMES);
|
||||
}
|
||||
catch (IOException ignored) { }
|
||||
}
|
||||
};
|
||||
PsiJavaFileStubImpl stub = new PsiJavaFileStubImpl("do.not.know.yet", true);
|
||||
StubBuildingVisitor<VirtualFile> visitor = new StubBuildingVisitor<VirtualFile>(file, strategy, stub, 0, null);
|
||||
new ClassReader(file.contentsToByteArray()).accept(visitor, ClassReader.SKIP_FRAMES);
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
doTest(getTestName(false));
|
||||
}
|
||||
@@ -85,7 +117,7 @@ public class ClsMirrorBuildingTest extends LightIdeaTestCase {
|
||||
}
|
||||
|
||||
private static void doTest(String clsPath, String txtPath) {
|
||||
VirtualFileSystem fs = clsPath.contains("!/") ? JarFileSystem.getInstance() : LocalFileSystem.getInstance();
|
||||
VirtualFileSystem fs = clsPath.contains("!/") ? StandardFileSystems.jar() : StandardFileSystems.local();
|
||||
VirtualFile file = fs.findFileByPath(clsPath);
|
||||
assertNotNull(clsPath, file);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user