From 5c99d5a1d203f00de47590b110060d02856da4a1 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 9 Feb 2017 17:32:41 +0100 Subject: [PATCH] =?UTF-8?q?serialization:=20kotlin=20data=20class=20full?= =?UTF-8?q?=20support=20(initial,=20enabled=20only=20in=20tests=20for=20no?= =?UTF-8?q?w)=20=E2=80=94=20part=204=20Vladimir=20Krivosheev=2009/02/2017,?= =?UTF-8?q?=2017:32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/java-tests/java-tests.iml | 2 +- .../ex/ProjectInspectionManagerTest.kt | 2 -- .../testSrc/ProjectStoreTest.kt | 29 --------------- .../testSrc/StorageManagerTest.kt | 7 ---- .../xml/XmlSerializerCollectionTest.kt | 4 +-- .../testSrc/xml/XmlSerializerTest.kt | 10 +++--- .../test/MasterPasswordMigrationTest.kt | 6 ++-- .../configurationStore/xmlSerializer.kt | 12 +++++++ .../src/com/intellij/util/jdom.kt | 11 ++---- .../com/intellij/testFramework/FixtureRule.kt | 36 ++++++++++++++++++- .../com/intellij/util/xmlb/XmlSerializer.java | 1 - 11 files changed, 59 insertions(+), 61 deletions(-) diff --git a/java/java-tests/java-tests.iml b/java/java-tests/java-tests.iml index ac00daab2c16..973e84156b30 100644 --- a/java/java-tests/java-tests.iml +++ b/java/java-tests/java-tests.iml @@ -53,8 +53,8 @@ - + \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt index 17d027602b8e..44274146a2a8 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt @@ -18,8 +18,6 @@ package com.intellij.codeInspection.ex import com.intellij.codeHighlighting.HighlightDisplayLevel import com.intellij.configurationStore.PROJECT_CONFIG_DIR import com.intellij.configurationStore.StoreAwareProjectManager -import com.intellij.configurationStore.loadAndUseProject -import com.intellij.configurationStore.saveStore import com.intellij.ide.highlighter.ProjectFileType import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectManager diff --git a/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt b/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt index 458178b7fb4e..69c6bbb29a32 100644 --- a/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt +++ b/platform/configuration-store-impl/testSrc/ProjectStoreTest.kt @@ -16,21 +16,17 @@ package com.intellij.configurationStore import com.intellij.ide.highlighter.ProjectFileType -import com.intellij.openapi.application.runWriteAction import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.State import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectManager import com.intellij.openapi.project.ex.ProjectEx -import com.intellij.openapi.project.ex.ProjectManagerEx import com.intellij.openapi.project.impl.ProjectImpl -import com.intellij.openapi.vfs.VirtualFile import com.intellij.project.stateStore import com.intellij.testFramework.* import com.intellij.testFramework.assertions.Assertions.assertThat import com.intellij.util.PathUtil import com.intellij.util.io.readText -import com.intellij.util.io.systemIndependentPath import com.intellij.util.io.write import org.intellij.lang.annotations.Language import org.junit.ClassRule @@ -38,31 +34,6 @@ import org.junit.Rule import org.junit.Test import java.nio.file.Paths -fun createProjectAndUseInLoadComponentStateMode(tempDirManager: TemporaryDirectory, directoryBased: Boolean = false, task: (Project) -> Unit) { - createOrLoadProject(tempDirManager, task, directoryBased = directoryBased) -} - -fun loadAndUseProject(tempDirManager: TemporaryDirectory, projectCreator: ((VirtualFile) -> String), task: (Project) -> Unit) { - createOrLoadProject(tempDirManager, task, projectCreator, false) -} - -private fun createOrLoadProject(tempDirManager: TemporaryDirectory, task: (Project) -> Unit, projectCreator: ((VirtualFile) -> String)? = null, directoryBased: Boolean) { - runInEdtAndWait { - val filePath: String - if (projectCreator == null) { - filePath = tempDirManager.newPath("test${if (directoryBased) "" else ProjectFileType.DOT_DEFAULT_EXTENSION}").systemIndependentPath - } - else { - filePath = runWriteAction { projectCreator(tempDirManager.newVirtualDirectory()) } - } - - val project = if (projectCreator == null) createHeavyProject(filePath, true) else ProjectManagerEx.getInstanceEx().loadProject(filePath)!! - project.runInLoadComponentStateMode { - project.use(task) - } - } -} - internal class ProjectStoreTest { companion object { @JvmField diff --git a/platform/configuration-store-impl/testSrc/StorageManagerTest.kt b/platform/configuration-store-impl/testSrc/StorageManagerTest.kt index 29cbf7319ef6..5d2e4d630e68 100644 --- a/platform/configuration-store-impl/testSrc/StorageManagerTest.kt +++ b/platform/configuration-store-impl/testSrc/StorageManagerTest.kt @@ -15,10 +15,7 @@ */ package com.intellij.configurationStore -import com.intellij.openapi.components.ComponentManager -import com.intellij.openapi.components.stateStore import com.intellij.testFramework.ProjectRule -import com.intellij.util.SmartList import junit.framework.TestCase import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -71,8 +68,4 @@ internal class StorageManagerTest { storageManager.addMacro("\$DOLLAR_MACRO$", "/temp/d$") assertThat(storageManager.getOrCreateStorage("\$DOLLAR_MACRO$/test.xml")).isNotNull() } -} - -fun ComponentManager.saveStore() { - stateStore.save(SmartList()) } \ No newline at end of file diff --git a/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt b/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt index 0b2b0255fe86..ae55ed522167 100644 --- a/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt +++ b/platform/configuration-store-impl/testSrc/xml/XmlSerializerCollectionTest.kt @@ -2,11 +2,11 @@ package com.intellij.configurationStore.xml +import com.intellij.configurationStore.deserialize import com.intellij.openapi.util.JDOMExternalizableStringList import com.intellij.util.SmartList import com.intellij.util.xmlb.SkipDefaultsSerializationFilter import com.intellij.util.xmlb.XmlSerializationException -import com.intellij.util.xmlb.XmlSerializer import com.intellij.util.xmlb.annotations.AbstractCollection import com.intellij.util.xmlb.annotations.CollectionBean import com.intellij.util.xmlb.annotations.Tag @@ -41,7 +41,7 @@ internal class XmlSerializerCollectionTest { val value = Element("value") list.writeExternal(value) - val o = XmlSerializer.deserialize(Element("state").addContent(Element("option").setAttribute("name", "myList").addContent(value)), Bean4::class.java) + val o = Element("state").addContent(Element("option").setAttribute("name", "myList").addContent(value)).deserialize() assertSerializer(o, "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "", SkipDefaultsSerializationFilter()) } diff --git a/platform/configuration-store-impl/testSrc/xml/XmlSerializerTest.kt b/platform/configuration-store-impl/testSrc/xml/XmlSerializerTest.kt index a99dbc760887..a4a81ebbef38 100644 --- a/platform/configuration-store-impl/testSrc/xml/XmlSerializerTest.kt +++ b/platform/configuration-store-impl/testSrc/xml/XmlSerializerTest.kt @@ -453,8 +453,7 @@ internal class XmlSerializerTest { @Test fun jdomElementArrayField() { val text = "\n" + " " - val bean = XmlSerializer.deserialize(JDOMUtil.loadDocument(text).rootElement, BeanWithJDOMElementArray::class.java) - + val bean = loadElement(text).deserialize() TestCase.assertEquals("bye", bean.STRING_V) TestCase.assertNotNull(bean.actions) @@ -618,17 +617,16 @@ internal class XmlSerializerTest { @Tag("bean") data class Bean(@Tag var description: String? = null) - var bean = XmlSerializer.deserialize(loadElement(""" + var bean = loadElement(""" Node.js integration ]]> -""".reader()), Bean::class.java) +""").deserialize() assertThat(bean.description).isEqualToIgnoringWhitespace("

Node.js integration

") - bean = XmlSerializer.deserialize( - loadElement("""Node.js integration]]>""".reader()), Bean::class.java) + bean = loadElement("""Node.js integration]]>""").deserialize() assertThat(bean.description).isEqualTo("

Node.js integration

") } diff --git a/platform/credential-store/test/MasterPasswordMigrationTest.kt b/platform/credential-store/test/MasterPasswordMigrationTest.kt index 769b143f57fb..d29119fffa35 100644 --- a/platform/credential-store/test/MasterPasswordMigrationTest.kt +++ b/platform/credential-store/test/MasterPasswordMigrationTest.kt @@ -15,11 +15,11 @@ */ package com.intellij.credentialStore +import com.intellij.configurationStore.deserializeInto import com.intellij.ide.passwordSafe.impl.providers.masterKey.MasterKeyPasswordSafeTest -import com.intellij.openapi.util.JDOMUtil import com.intellij.testFramework.ApplicationRule import com.intellij.testFramework.runInEdtAndWait -import com.intellij.util.xmlb.XmlSerializer +import com.intellij.util.loadElement import org.assertj.core.api.Assertions.assertThat import org.junit.ClassRule import org.junit.Test @@ -93,7 +93,7 @@ internal class MasterPasswordMigrationTest { private fun getDb(data: String): PasswordDatabase { val passwordDatabase = PasswordDatabase() val state = PasswordDatabase.State() - XmlSerializer.deserializeInto(state, JDOMUtil.load(data.reader())) + loadElement(data).deserializeInto(state) passwordDatabase.loadState(state) return passwordDatabase } diff --git a/platform/projectModel-impl/src/com/intellij/configurationStore/xmlSerializer.kt b/platform/projectModel-impl/src/com/intellij/configurationStore/xmlSerializer.kt index 3eba05055bc0..3be433b770a4 100644 --- a/platform/projectModel-impl/src/com/intellij/configurationStore/xmlSerializer.kt +++ b/platform/projectModel-impl/src/com/intellij/configurationStore/xmlSerializer.kt @@ -67,6 +67,18 @@ fun deserialize(url: URL, aClass: Class): T { } } +fun Element.deserializeInto(bean: Any) { + try { + (getBinding(bean.javaClass) as BeanBinding).deserializeInto(bean, this) + } + catch (e: XmlSerializationException) { + throw e + } + catch (e: Exception) { + throw XmlSerializationException(e) + } +} + private var _bindingCache: SoftReference>? = null private val bindingCache: MutableMap diff --git a/platform/projectModel-impl/src/com/intellij/util/jdom.kt b/platform/projectModel-impl/src/com/intellij/util/jdom.kt index ceebf94833ae..1e33e22ae541 100644 --- a/platform/projectModel-impl/src/com/intellij/util/jdom.kt +++ b/platform/projectModel-impl/src/com/intellij/util/jdom.kt @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -65,14 +65,7 @@ fun loadElement(stream: InputStream): Element = loadDocument(stream.reader()).de fun loadElement(path: Path): Element = loadDocument(Files.newInputStream(path).bufferedReader()).detachRootElement() -private fun loadDocument(reader: Reader): Document { - try { - return getSaxBuilder().build(reader) - } - finally { - reader.close() - } -} +private fun loadDocument(reader: Reader): Document = reader.use { getSaxBuilder().build(it) } fun Element?.isEmpty() = this == null || JDOMUtil.isEmpty(this) diff --git a/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt b/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt index 3ce521a69634..afc331482bfa 100644 --- a/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt +++ b/platform/testFramework/src/com/intellij/testFramework/FixtureRule.kt @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -19,7 +19,10 @@ import com.intellij.ide.highlighter.ProjectFileType import com.intellij.idea.IdeaTestApplication import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.runWriteAction +import com.intellij.openapi.components.ComponentManager import com.intellij.openapi.components.impl.stores.IProjectStore +import com.intellij.openapi.components.stateStore import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.module.Module import com.intellij.openapi.module.ModuleManager @@ -29,11 +32,13 @@ import com.intellij.openapi.project.ex.ProjectEx import com.intellij.openapi.project.ex.ProjectManagerEx import com.intellij.openapi.project.impl.ProjectManagerImpl import com.intellij.openapi.util.Disposer +import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS import com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager import com.intellij.project.stateStore +import com.intellij.util.SmartList import com.intellij.util.containers.forEachGuaranteed import com.intellij.util.io.systemIndependentPath import org.junit.rules.ExternalResource @@ -283,3 +288,32 @@ class WrapRule(private val before: () -> () -> Unit) : TestRule { } } } + +fun createProjectAndUseInLoadComponentStateMode(tempDirManager: TemporaryDirectory, directoryBased: Boolean = false, task: (Project) -> Unit) { + createOrLoadProject(tempDirManager, task, directoryBased = directoryBased) +} + +fun loadAndUseProject(tempDirManager: TemporaryDirectory, projectCreator: ((VirtualFile) -> String), task: (Project) -> Unit) { + createOrLoadProject(tempDirManager, task, projectCreator, false) +} + +private fun createOrLoadProject(tempDirManager: TemporaryDirectory, task: (Project) -> Unit, projectCreator: ((VirtualFile) -> String)? = null, directoryBased: Boolean) { + runInEdtAndWait { + val filePath: String + if (projectCreator == null) { + filePath = tempDirManager.newPath("test${if (directoryBased) "" else ProjectFileType.DOT_DEFAULT_EXTENSION}").systemIndependentPath + } + else { + filePath = runWriteAction { projectCreator(tempDirManager.newVirtualDirectory()) } + } + + val project = if (projectCreator == null) createHeavyProject(filePath, true) else ProjectManagerEx.getInstanceEx().loadProject(filePath)!! + project.runInLoadComponentStateMode { + project.use(task) + } + } +} + +fun ComponentManager.saveStore() { + stateStore.save(SmartList()) +} \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/xmlb/XmlSerializer.java b/platform/util/src/com/intellij/util/xmlb/XmlSerializer.java index 8fbadcabe2e5..a32ea6995eec 100644 --- a/platform/util/src/com/intellij/util/xmlb/XmlSerializer.java +++ b/platform/util/src/com/intellij/util/xmlb/XmlSerializer.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.util.xmlb; import com.intellij.openapi.util.JDOMUtil;