diff options
Diffstat (limited to 'platform/platform-tests/testSrc/com/intellij/openapi/components/impl')
3 files changed, 110 insertions, 35 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; } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/StateStorageManagerImplTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/StateStorageManagerImplTest.java index 76c673626b26..da54b2a65e35 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/StateStorageManagerImplTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/StateStorageManagerImplTest.java @@ -16,6 +16,7 @@ package com.intellij.openapi.components.impl; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.RoamingType; import com.intellij.openapi.components.StateStorage; import com.intellij.openapi.components.StateStorageException; import com.intellij.openapi.components.StateStorageOperation; @@ -55,7 +56,7 @@ public class StateStorageManagerImplTest extends LightPlatformLangTestCase { return null; } }; - myStateStorageManager.addMacro("MACRO1", "/temp/m1"); + myStateStorageManager.addMacro("$MACRO1$", "/temp/m1"); } @Override @@ -65,23 +66,23 @@ public class StateStorageManagerImplTest extends LightPlatformLangTestCase { } public void testCreateFileStateStorageMacroSubstituted() { - StateStorage data = myStateStorageManager.getFileStateStorage("$MACRO1$/test.xml"); + StateStorage data = myStateStorageManager.getStateStorage("$MACRO1$/test.xml", RoamingType.PER_USER); assertThat(data, is(notNullValue())); } public void testCreateStateStorageAssertionThrownWhenUnknownMacro() { try { - myStateStorageManager.getFileStateStorage("$UNKNOWN_MACRO$/test.xml"); + myStateStorageManager.getStateStorage("$UNKNOWN_MACRO$/test.xml", RoamingType.PER_USER); fail("Exception expected"); } catch (IllegalArgumentException e) { - assertEquals("Unknown macro: $UNKNOWN_MACRO$ in storage spec: $UNKNOWN_MACRO$/test.xml", e.getMessage()); + assertEquals("Unknown macro: $UNKNOWN_MACRO$ in storage file spec: $UNKNOWN_MACRO$/test.xml", e.getMessage()); } } public void testCreateFileStateStorageMacroSubstitutedWhenExpansionHas$() { - myStateStorageManager.addMacro("DOLLAR_MACRO", "/temp/d$"); - StateStorage data = myStateStorageManager.getFileStateStorage("$DOLLAR_MACRO$/test.xml"); + myStateStorageManager.addMacro("$DOLLAR_MACRO$", "/temp/d$"); + StateStorage data = myStateStorageManager.getStateStorage("$DOLLAR_MACRO$/test.xml", RoamingType.PER_USER); assertThat(data, is(notNullValue())); } } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/XmlElementStorageTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/XmlElementStorageTest.java index eeffb44915db..414843f69d96 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/XmlElementStorageTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/components/impl/XmlElementStorageTest.java @@ -16,26 +16,26 @@ package com.intellij.openapi.components.impl; import com.intellij.openapi.Disposable; +import com.intellij.openapi.components.RoamingType; import com.intellij.openapi.components.StateStorage; import com.intellij.openapi.components.StateStorageException; import com.intellij.openapi.components.TrackingPathMacroSubstitutor; -import com.intellij.openapi.components.impl.stores.ComponentRoamingManager; import com.intellij.openapi.components.impl.stores.ComponentVersionProvider; import com.intellij.openapi.components.impl.stores.XmlElementStorage; import com.intellij.openapi.util.Disposer; import com.intellij.testFramework.LightPlatformLangTestCase; -import com.intellij.util.io.fs.IFile; -import org.jdom.Document; import org.jdom.Element; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.File; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Set; -import static com.intellij.openapi.util.JDOMBuilder.*; +import static com.intellij.openapi.util.JDOMBuilder.attr; +import static com.intellij.openapi.util.JDOMBuilder.tag; /** * @author mike @@ -57,7 +57,7 @@ public class XmlElementStorageTest extends LightPlatformLangTestCase { public void testGetStateSucceeded() throws Exception { MyXmlElementStorage storage = - new MyXmlElementStorage(document(tag("root", tag("component", attr("name", "test"), tag("foo")))), myParentDisposable); + new MyXmlElementStorage(tag("root", tag("component", attr("name", "test"), tag("foo"))), myParentDisposable); Element state = storage.getState(this, "test", Element.class, null); assertNotNull(state); assertEquals("component", state.getName()); @@ -65,36 +65,36 @@ public class XmlElementStorageTest extends LightPlatformLangTestCase { } public void testGetStateNotSucceeded() throws Exception { - MyXmlElementStorage storage = new MyXmlElementStorage(document(tag("root")), myParentDisposable); + MyXmlElementStorage storage = new MyXmlElementStorage(tag("root"), myParentDisposable); Element state = storage.getState(this, "test", Element.class, null); assertNull(state); } public void testSetStateOverridesOldState() throws Exception { MyXmlElementStorage storage = - new MyXmlElementStorage(document(tag("root", tag("component", attr("name", "test"), tag("foo")))), myParentDisposable); + new MyXmlElementStorage(tag("root", tag("component", attr("name", "test"), tag("foo"))), myParentDisposable); Element newState = tag("component", attr("name", "test"), tag("bar")); StateStorage.ExternalizationSession externalizationSession = storage.startExternalization(); externalizationSession.setState(this, "test", newState, null); storage.startSave(externalizationSession).save(); - assertNotNull(storage.mySavedDocument); - assertNotNull(storage.mySavedDocument.getRootElement().getChild("component").getChild("bar")); - assertNull(storage.mySavedDocument.getRootElement().getChild("component").getChild("foo")); + assertNotNull(storage.mySavedElement); + assertNotNull(storage.mySavedElement.getChild("component").getChild("bar")); + assertNull(storage.mySavedElement.getChild("component").getChild("foo")); } private class MyXmlElementStorage extends XmlElementStorage { - private final Document myDocument; - private Document mySavedDocument; + private final Element myElement; + private Element mySavedElement; - public MyXmlElementStorage(final Document document, final Disposable parentDisposable) throws StateStorageException { - super(new MyPathMacroManager(), parentDisposable, "root", null, "", ComponentRoamingManager.getInstance(), ComponentVersionProvider.EMPTY); - myDocument = document; + public MyXmlElementStorage(Element element, final Disposable parentDisposable) throws StateStorageException { + super("", RoamingType.PER_USER, new MyPathMacroManager(), parentDisposable, "root", null, ComponentVersionProvider.EMPTY); + myElement = element; } @Override - protected Document loadDocument() throws StateStorageException { - return myDocument; + protected Element loadLocalData() { + return myElement; } @Override @@ -102,41 +102,42 @@ public class XmlElementStorageTest extends LightPlatformLangTestCase { return new MySaveSession(externalizationSession) { @Override protected void doSave() throws StateStorageException { - mySavedDocument = getDocumentToSave().clone(); + Element elementToSave = getElementToSave(); + mySavedElement = elementToSave == null ? null : elementToSave.clone(); } @NotNull @Override - public Collection<IFile> getStorageFilesToSave() throws StateStorageException { - return needsSave() ? getAllStorageFiles() : Collections.<IFile>emptyList(); + public Collection<File> getStorageFilesToSave() throws StateStorageException { + return needsSave() ? getAllStorageFiles() : Collections.<File>emptyList(); } @NotNull @Override - public List<IFile> getAllStorageFiles() { + public List<File> getAllStorageFiles() { throw new UnsupportedOperationException("Method getAllStorageFiles not implemented in " + getClass()); } - }; } } private static class MyPathMacroManager implements TrackingPathMacroSubstitutor { @Override - public void expandPaths(final Element element) { + public void expandPaths(@NotNull final Element element) { } @Override public void reset() { } + @NotNull @Override - public Collection<String> getComponents(Collection<String> macros) { + public Collection<String> getComponents(@NotNull Collection<String> macros) { return Collections.emptyList(); } @Override - public void collapsePaths(final Element element) { + public void collapsePaths(@NotNull final Element element) { } @Override @@ -149,17 +150,18 @@ public class XmlElementStorageTest extends LightPlatformLangTestCase { throw new UnsupportedOperationException("Method collapsePath not implemented in " + getClass()); } + @NotNull @Override public Collection<String> getUnknownMacros(final String componentName) { return Collections.emptySet(); } @Override - public void invalidateUnknownMacros(Set<String> macros) { + public void invalidateUnknownMacros(@NotNull Set<String> macros) { } @Override - public void addUnknownMacros(String componentName, Collection<String> unknownMacros) { + public void addUnknownMacros(@NotNull String componentName, @NotNull Collection<String> unknownMacros) { } } } |