From b7ca72f78f4837c634f1030ed95465a52dc5dc20 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 1 Sep 2014 12:02:17 +0200 Subject: [PATCH] =?UTF-8?q?ApplicationStoreTest=20=E2=80=94=20test=20strea?= =?UTF-8?q?m=20provider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/impl/ApplicationStoreTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java index cfbd5a12f146..e23084117625 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java @@ -7,13 +7,18 @@ import com.intellij.openapi.components.*; import com.intellij.openapi.components.impl.stores.*; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.testFramework.LightPlatformLangTestCase; import com.intellij.util.xmlb.XmlSerializerUtil; +import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.util.Map; public class ApplicationStoreTest extends LightPlatformLangTestCase { private File testAppConfig; @@ -53,10 +58,56 @@ public class ApplicationStoreTest extends LightPlatformLangTestCase { public void testStreamProviderSaveIfSeveralStoragesConfigured() throws Exception { SeveralStoragesConfigured component = new SeveralStoragesConfigured(); + + MyStreamProvider streamProvider = new MyStreamProvider(); + componentStore.getStateStorageManager().setStreamProvider(streamProvider); + componentStore.initComponent(component, false); StoreUtil.doSave(componentStore); } + private static class MyStreamProvider extends StreamProvider { + public final Map> data = new THashMap>(); + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public void saveContent(@NotNull String fileSpec, + @NotNull byte[] content, + int size, + @NotNull RoamingType roamingType, + boolean async) { + getMap(roamingType).put(fileSpec, new String(content, 0, size, CharsetToolkit.UTF8_CHARSET)); + } + + private Map getMap(@NotNull RoamingType roamingType) { + Map map = data.get(roamingType); + if (map == null) { + map = new THashMap(); + data.put(roamingType, map); + } + return map; + } + + @Nullable + @Override + public InputStream loadContent(@NotNull String fileSpec, @NotNull RoamingType roamingType) throws IOException { + String data = getMap(roamingType).get(fileSpec); + return data == null ? null : new ByteArrayInputStream(data.getBytes(CharsetToolkit.UTF8_CHARSET)); + } + + @Override + public void delete(@NotNull String fileSpec, @NotNull RoamingType roamingType) { + Map map = data.get(roamingType); + if (map != null) { + map.remove(fileSpec); + } + } + } + class MyComponentStore extends ComponentStoreImpl implements Disposable { private final StateStorageManager stateStorageManager;