mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
added test for MapIndexStorage with custom equality policy
This commit is contained in:
@@ -59,20 +59,7 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
public void testUpdate() throws StorageException, IOException {
|
||||
final File storageFile = FileUtil.createTempFile("indextest", "storage");
|
||||
final File metaIndexFile = FileUtil.createTempFile("indextest_inputs", "storage");
|
||||
final MapIndexStorage indexStorage = new MapIndexStorage(storageFile, new EnumeratorStringDescriptor(), new EnumeratorStringDescriptor(), 16 * 1024);
|
||||
final StringIndex index = new StringIndex(indexStorage, new Factory<PersistentHashMap<Integer, Collection<String>>>() {
|
||||
@Override
|
||||
public PersistentHashMap<Integer, Collection<String>> create() {
|
||||
try {
|
||||
return createMetaIndex(metaIndexFile);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
StringIndex index = createIndex(new EnumeratorStringDescriptor())
|
||||
|
||||
try {
|
||||
// build index
|
||||
@@ -119,6 +106,44 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testUpdateWithCustomEqualityPolicy() {
|
||||
def index = createIndex(new CaseInsensitiveEnumeratorStringDescriptor())
|
||||
try {
|
||||
index.update("a.java", "x", null)
|
||||
assertDataEquals(index.getFilesByWord("x"), "a.java")
|
||||
index.flush() //todo: this should not be required but the following line will fail without it
|
||||
assertDataEquals(index.getFilesByWord("X"), "a.java")
|
||||
|
||||
index.update("b.java", "y", null)
|
||||
assertDataEquals(index.getFilesByWord("y"), "b.java")
|
||||
index.update("c.java", "Y", null)
|
||||
index.flush() //todo: this should not be required but the following line will fail without it
|
||||
assertDataEquals(index.getFilesByWord("y"), "b.java", "c.java")
|
||||
}
|
||||
finally {
|
||||
index.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
private static StringIndex createIndex(EnumeratorStringDescriptor keyDescriptor) {
|
||||
final File storageFile = FileUtil.createTempFile("indextest", "storage");
|
||||
final File metaIndexFile = FileUtil.createTempFile("indextest_inputs", "storage");
|
||||
final MapIndexStorage indexStorage = new MapIndexStorage(storageFile, keyDescriptor, new EnumeratorStringDescriptor(), 16 * 1024);
|
||||
final StringIndex index = new StringIndex(indexStorage, new Factory<PersistentHashMap<Integer, Collection<String>>>() {
|
||||
@Override
|
||||
public PersistentHashMap<Integer, Collection<String>> create() {
|
||||
try {
|
||||
return createMetaIndex(metaIndexFile);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
index
|
||||
}
|
||||
|
||||
private static PersistentHashMap<Integer, Collection<String>> createMetaIndex(File metaIndexFile) throws IOException {
|
||||
return new PersistentHashMap<Integer, Collection<String>>(metaIndexFile, new EnumeratorIntegerDescriptor(), new DataExternalizer<Collection<String>>() {
|
||||
@Override
|
||||
@@ -142,7 +167,7 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
private static <T> void assertDataEquals(List<T> actual, T... expected) {
|
||||
assertTrue(new HashSet<T>(Arrays.asList(expected)).equals(new HashSet<T>(actual)));
|
||||
assertSameElements(actual, expected);
|
||||
}
|
||||
|
||||
public void testCollectedPsiWithChangedDocument() throws IOException {
|
||||
|
||||
@@ -51,6 +51,10 @@ public class StringIndex {
|
||||
myIndex.update(Math.abs(path.hashCode()), toInput(path, content)).compute();
|
||||
}
|
||||
|
||||
public void flush() throws StorageException {
|
||||
myIndex.flush();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
myIndex.dispose();
|
||||
}
|
||||
|
||||
+2
-15
@@ -16,10 +16,9 @@
|
||||
package org.jetbrains.jps.builders.java.dependencyView;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import com.intellij.util.io.CaseInsensitiveEnumeratorStringDescriptor;
|
||||
import com.intellij.util.io.DataExternalizer;
|
||||
import com.intellij.util.io.EnumeratorStringDescriptor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
@@ -38,7 +37,7 @@ public class ObjectObjectPersistentMultiMapletTest extends UsefulTestCase {
|
||||
public void testReplaceWithEqualButNotSameKey() throws IOException {
|
||||
File file = FileUtil.createTempFile(getTestDirectoryName(), null);
|
||||
ObjectObjectPersistentMultiMaplet<String, IntValueStreamable> maplet =
|
||||
new ObjectObjectPersistentMultiMaplet<String, IntValueStreamable>(file, new CaseInsensitiveStringDescriptor(),
|
||||
new ObjectObjectPersistentMultiMaplet<String, IntValueStreamable>(file, new CaseInsensitiveEnumeratorStringDescriptor(),
|
||||
new IntValueExternalizer(),
|
||||
COLLECTION_FACTORY);
|
||||
maplet.put("a", new IntValueStreamable(1));
|
||||
@@ -47,18 +46,6 @@ public class ObjectObjectPersistentMultiMapletTest extends UsefulTestCase {
|
||||
assertEquals(2, assertOneElement(maplet.get("a")).value);
|
||||
}
|
||||
|
||||
private static class CaseInsensitiveStringDescriptor extends EnumeratorStringDescriptor {
|
||||
@Override
|
||||
public int getHashCode(String value) {
|
||||
return StringUtil.stringHashCodeInsensitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(String val1, String val2) {
|
||||
return val1.equalsIgnoreCase(val2);
|
||||
}
|
||||
}
|
||||
|
||||
private static class IntValueStreamable implements Streamable {
|
||||
public int value;
|
||||
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2000-2015 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.util.io;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class CaseInsensitiveEnumeratorStringDescriptor extends EnumeratorStringDescriptor {
|
||||
@Override
|
||||
public int getHashCode(String value) {
|
||||
return StringUtil.stringHashCodeInsensitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(String val1, String val2) {
|
||||
return val1.equalsIgnoreCase(val2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user