diff options
Diffstat (limited to 'platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java')
-rw-r--r-- | platform/platform-tests/testSrc/com/intellij/openapi/components/impl/ApplicationStoreTest.java | 76 |
1 files changed, 74 insertions, 2 deletions
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..719635a19a30 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,21 @@ 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; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; public class ApplicationStoreTest extends LightPlatformLangTestCase { private File testAppConfig; @@ -53,8 +61,73 @@ 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); + component.foo = "newValue"; StoreUtil.doSave(componentStore); + + assertThat(streamProvider.data.get(RoamingType.PER_USER).get(StoragePathMacros.APP_CONFIG + "/proxy.settings.xml"), equalTo("<application>\n" + + " <component name=\"HttpConfigurable\">\n" + + " <option name=\"foo\" value=\"newValue\" />\n" + + " </component>\n" + + "</application>")); + } + + public void testLoadFromStreamProvider() throws Exception { + SeveralStoragesConfigured component = new SeveralStoragesConfigured(); + + MyStreamProvider streamProvider = new MyStreamProvider(); + THashMap<String, String> map = new THashMap<String, String>(); + map.put(StoragePathMacros.APP_CONFIG + "/proxy.settings.xml", "<application>\n" + + " <component name=\"HttpConfigurable\">\n" + + " <option name=\"foo\" value=\"newValue\" />\n" + + " </component>\n" + + "</application>"); + streamProvider.data.put(RoamingType.PER_USER, map); + + componentStore.getStateStorageManager().setStreamProvider(streamProvider); + componentStore.initComponent(component, false); + assertThat(component.foo, equalTo("newValue")); + } + + private static class MyStreamProvider extends StreamProvider { + public final Map<RoamingType, Map<String, String>> data = new THashMap<RoamingType, Map<String, String>>(); + + @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<String, String> getMap(@NotNull RoamingType roamingType) { + Map<String, String> map = data.get(roamingType); + if (map == null) { + map = new THashMap<String, String>(); + 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<String, String> map = data.get(roamingType); + if (map != null) { + map.remove(fileSpec); + } + } } class MyComponentStore extends ComponentStoreImpl implements Disposable { @@ -88,7 +161,7 @@ public class ApplicationStoreTest extends LightPlatformLangTestCase { } }; - stateStorageManager.addMacro(StoragePathMacros.getMacroName(StoragePathMacros.APP_CONFIG), testAppConfigPath); + stateStorageManager.addMacro(StoragePathMacros.APP_CONFIG, testAppConfigPath); } @Override @@ -141,7 +214,6 @@ public class ApplicationStoreTest extends LightPlatformLangTestCase { @Nullable @Override public SeveralStoragesConfigured getState() { - foo = "newValue"; return this; } |