diff options
Diffstat (limited to 'platform/platform-tests/testSrc/com/intellij/openapi')
4 files changed, 439 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) { } } } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/progress/util/ProgressIndicatorTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/progress/util/ProgressIndicatorTest.java new file mode 100644 index 000000000000..6e0201628a51 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/openapi/progress/util/ProgressIndicatorTest.java @@ -0,0 +1,329 @@ +/* + * 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.openapi.progress.util; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ModalityState; +import com.intellij.openapi.application.ex.ApplicationManagerEx; +import com.intellij.openapi.progress.*; +import com.intellij.openapi.progress.impl.ProgressManagerImpl; +import com.intellij.openapi.wm.ex.ProgressIndicatorEx; +import com.intellij.testFramework.LightPlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.util.Alarm; +import com.intellij.util.containers.DoubleArrayList; +import com.intellij.util.containers.Stack; +import com.intellij.util.ui.UIUtil; +import gnu.trove.TLongArrayList; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +/** + * @author yole + */ +public class ProgressIndicatorTest extends LightPlatformTestCase { + public void testCheckCanceledHasNoStackFrame() { + ProgressIndicatorBase pib = new ProgressIndicatorBase(); + pib.cancel(); + boolean hadException = false; + try { + pib.checkCanceled(); + } + catch(ProcessCanceledException ex) { + hadException = true; + assertTrue("Should have no stackframe", ex.getStackTrace().length == 0); + } + assertTrue("Please restore ProgressIndicatorBase.checkCanceled() check!", hadException); + } + + public void testProgressManagerCheckCanceledWorksRightAfterIndicatorBeenCanceled() { + for (int i=0; i<1000;i++) { + final ProgressIndicatorBase indicator = new ProgressIndicatorBase(); + ProgressManager.getInstance().runProcess(new Runnable() { + @Override + public void run() { + ProgressManager.checkCanceled(); + try { + indicator.cancel(); + ProgressManager.checkCanceled(); + fail("checkCanceled() must have caught just canceled indicator"); + } + catch (ProcessCanceledException ignored) { + } + } + }, indicator); + } + } + + private volatile long prevTime; + private volatile long now; + public void testCheckCanceledGranularity() throws InterruptedException { + prevTime = now = 0; + final long warmupEnd = System.currentTimeMillis() + 1000; + final TLongArrayList times = new TLongArrayList(); + final long end = warmupEnd + 1000; + + ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(new Runnable() { + @Override + public void run() { + final Alarm alarm = new Alarm(Alarm.ThreadToUse.OWN_THREAD, getTestRootDisposable()); + ProgressIndicatorEx indicator = (ProgressIndicatorEx)ProgressIndicatorProvider.getGlobalProgressIndicator(); + prevTime = System.currentTimeMillis(); + assert indicator != null; + indicator.addStateDelegate(new ProgressIndicatorStub() { + @Override + public void checkCanceled() throws ProcessCanceledException { + now = System.currentTimeMillis(); + if (now > warmupEnd) { + int delta = (int)(now - prevTime); + times.add(delta); + } + prevTime = now; + } + }); + while (System.currentTimeMillis() < end) { + ProgressManager.checkCanceled(); + } + alarm.cancelAllRequests(); + } + }, "", false, getProject(), null, ""); + long averageDelay = PlatformTestUtil.averageAmongMedians(times.toNativeArray(), 5); + System.out.println("averageDelay = " + averageDelay); + assertTrue(averageDelay < ProgressManagerImpl.CHECK_CANCELED_DELAY_MILLIS*3); + } + + public void testProgressIndicatorUtils() throws Throwable { + final AtomicBoolean run = new AtomicBoolean(true); + final AtomicBoolean insideReadAction = new AtomicBoolean(); + final AtomicReference<Throwable> exception = new AtomicReference<Throwable>(); + final ProgressIndicatorBase indicator = new ProgressIndicatorBase(); + ProgressIndicatorUtils.scheduleWithWriteActionPriority(indicator, new ReadTask() { + @Override + public void computeInReadAction(@NotNull ProgressIndicator indicator) { + insideReadAction.set(true); + while (run.get()) { + ProgressManager.checkCanceled(); + } + } + + @Override + public void onCanceled(@NotNull ProgressIndicator indicator) { + try { + assertTrue(run.get()); // cancel should happen early + run.set(false); + } + catch (Throwable e) { + exception.set(e); + } + } + }); + UIUtil.dispatchAllInvocationEvents(); + while (!insideReadAction.get()) { + ; + } + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + try { + assertTrue(indicator.isCanceled()); + } + catch (Throwable e) { + exception.set(e); + } + } + }); + assertFalse(run.get()); + if (exception.get() != null) throw exception.get(); + } + + private static class ProgressIndicatorStub implements ProgressIndicatorEx { + @Override + public void addStateDelegate(@NotNull ProgressIndicatorEx delegate) { + + } + + @Override + public boolean isModalityEntered() { + return false; + } + + @Override + public void finish(@NotNull TaskInfo task) { + + } + + @Override + public boolean isFinished(@NotNull TaskInfo task) { + return false; + } + + @Override + public boolean wasStarted() { + return false; + } + + @Override + public void processFinish() { + + } + + @Override + public void initStateFrom(@NotNull ProgressIndicator indicator) { + + } + + @NotNull + @Override + public Stack<String> getTextStack() { + return null; + } + + @NotNull + @Override + public DoubleArrayList getFractionStack() { + return null; + } + + @NotNull + @Override + public Stack<String> getText2Stack() { + return null; + } + + @Override + public int getNonCancelableCount() { + return 0; + } + + @Override + public void start() { + + } + + @Override + public void stop() { + + } + + @Override + public boolean isRunning() { + return false; + } + + @Override + public void cancel() { + + } + + @Override + public boolean isCanceled() { + return false; + } + + @Override + public void setText(String text) { + + } + + @Override + public String getText() { + return null; + } + + @Override + public void setText2(String text) { + + } + + @Override + public String getText2() { + return null; + } + + @Override + public double getFraction() { + return 0; + } + + @Override + public void setFraction(double fraction) { + + } + + @Override + public void pushState() { + + } + + @Override + public void popState() { + + } + + @Override + public void startNonCancelableSection() { + + } + + @Override + public void finishNonCancelableSection() { + + } + + @Override + public boolean isModal() { + return false; + } + + @NotNull + @Override + public ModalityState getModalityState() { + return null; + } + + @Override + public void setModalityProgress(ProgressIndicator modalityProgress) { + + } + + @Override + public boolean isIndeterminate() { + return false; + } + + @Override + public void setIndeterminate(boolean indeterminate) { + + } + + @Override + public void checkCanceled() throws ProcessCanceledException { + + } + + @Override + public boolean isPopupWasShown() { + return false; + } + + @Override + public boolean isShowing() { + return false; + } + } +} |