mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix tests
This commit is contained in:
@@ -605,9 +605,7 @@ internal fun assertSerializer(bean: Any, expected: String, filter: Serialization
|
||||
return element
|
||||
}
|
||||
|
||||
// no filter by default
|
||||
// ? = SerializationFilter { _, _ -> true }
|
||||
internal fun <T: Any> testSerializer(@Language("XML") expectedText: String, bean: T, filter: SerializationFilter? = SerializationFilter { _, _ -> true }): T {
|
||||
internal fun <T: Any> testSerializer(@Language("XML") expectedText: String, bean: T, filter: SerializationFilter? = null): T {
|
||||
val expectedTrimmed = expectedText.trimIndent()
|
||||
val element = assertSerializer(bean, expectedTrimmed, filter)
|
||||
|
||||
|
||||
+4
-11
@@ -1,11 +1,10 @@
|
||||
package com.intellij.configurationStore.xml;
|
||||
|
||||
import com.intellij.configurationStore.XmlSerializer;
|
||||
import com.intellij.openapi.util.JDOMUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.xmlb.annotations.Transient;
|
||||
import junit.framework.TestCase;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static com.intellij.testFramework.assertions.Assertions.assertThat;
|
||||
|
||||
@@ -16,17 +15,11 @@ public class XmlSerializerWithDefaultJDOMExternalizerCompatibilityTest extends T
|
||||
}
|
||||
|
||||
private static void assertCompatibleSerialization(final Object data) {
|
||||
assertThat(serializeWithJDom(data)).isEqualTo(serializeWithXmlSerializer(data));
|
||||
assertThat(serializeWithJDom(data)).isEqualTo(serialize(data));
|
||||
}
|
||||
|
||||
private static String serializeWithXmlSerializer(final Object data) {
|
||||
Element element = serialize(data);
|
||||
return StringUtil.trimStart(JDOMUtil.writeElement(element), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>").trim();
|
||||
}
|
||||
|
||||
private static String serializeWithJDom(final Object data) {
|
||||
private static Element serializeWithJDom(@NotNull Object data) {
|
||||
final Element jDomRoot = new Element("MyBean");
|
||||
|
||||
if (data instanceof com.intellij.openapi.util.JDOMExternalizable) {
|
||||
((com.intellij.openapi.util.JDOMExternalizable)data).writeExternal(jDomRoot);
|
||||
}
|
||||
@@ -34,7 +27,7 @@ public class XmlSerializerWithDefaultJDOMExternalizerCompatibilityTest extends T
|
||||
com.intellij.openapi.util.DefaultJDOMExternalizer.writeExternal(data, jDomRoot);
|
||||
}
|
||||
|
||||
return JDOMUtil.writeElement(jDomRoot).trim();
|
||||
return jDomRoot;
|
||||
}
|
||||
|
||||
private static Element serialize(Object bean) {
|
||||
|
||||
@@ -21,33 +21,37 @@ import kotlin.concurrent.write
|
||||
import kotlin.reflect.full.primaryConstructor
|
||||
import kotlin.reflect.jvm.isAccessible
|
||||
|
||||
private val skipDefaultsSerializationFilter = ThreadLocal<SoftReference<SkipDefaultsSerializationFilter>>()
|
||||
private val skipDefaultsSerializationFilter = ThreadLocal<SoftReference<SerializationFilter>>()
|
||||
|
||||
private fun getDefaultSerializationFilter(): SkipDefaultsSerializationFilter {
|
||||
private fun getDefaultSerializationFilter(): SerializationFilter {
|
||||
var result = SoftReference.dereference(skipDefaultsSerializationFilter.get())
|
||||
if (result == null) {
|
||||
result = SkipDefaultsSerializationFilter()
|
||||
result = object : SkipDefaultsSerializationFilter() {
|
||||
override fun accepts(accessor: Accessor, bean: Any): Boolean {
|
||||
return if (bean is BaseState) {
|
||||
bean.accepts(accessor, bean)
|
||||
}
|
||||
else {
|
||||
super.accepts(accessor, bean)
|
||||
}
|
||||
}
|
||||
}
|
||||
skipDefaultsSerializationFilter.set(SoftReference(result))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun <T : Any> T.serialize(filter: SerializationFilter? = null, createElementIfEmpty: Boolean = false): Element? {
|
||||
fun <T : Any> T.serialize(filter: SerializationFilter? = getDefaultSerializationFilter(), createElementIfEmpty: Boolean = false): Element? {
|
||||
try {
|
||||
val clazz = javaClass
|
||||
val binding = serializer.getClassBinding(clazz)
|
||||
return if (binding is BeanBinding) {
|
||||
// top level expects not null (null indicates error, empty element will be omitted)
|
||||
val effectiveFilter = when {
|
||||
filter != null -> filter
|
||||
this !is BaseState -> getDefaultSerializationFilter()
|
||||
else -> null /* BaseState implements SerializationFilter filter */
|
||||
}
|
||||
binding.serialize(this, createElementIfEmpty, effectiveFilter)
|
||||
binding.serialize(this, createElementIfEmpty, filter)
|
||||
}
|
||||
else {
|
||||
binding.serialize(this, null, filter ?: getDefaultSerializationFilter()) as Element
|
||||
binding.serialize(this, null, filter) as Element
|
||||
}
|
||||
}
|
||||
catch (e: XmlSerializationException) {
|
||||
|
||||
+7
-15
@@ -1,17 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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.
|
||||
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
*/
|
||||
package com.intellij.testFramework.assertions
|
||||
|
||||
@@ -51,11 +39,15 @@ class JdomAssert(actual: Element?) : AbstractAssert<JdomAssert, Element?>(actual
|
||||
return this
|
||||
}
|
||||
|
||||
fun isEqualTo(element: Element): JdomAssert {
|
||||
fun isEqualTo(element: Element?): JdomAssert {
|
||||
if (actual == element) {
|
||||
return this
|
||||
}
|
||||
|
||||
isNotNull
|
||||
|
||||
if (!JDOMUtil.areElementsEqual(actual, element)) {
|
||||
isEqualTo(JDOMUtil.writeElement(element))
|
||||
isEqualTo(JDOMUtil.writeElement(element!!))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -1,17 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 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.
|
||||
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
*/
|
||||
package com.intellij.util.xmlb;
|
||||
|
||||
@@ -26,7 +14,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* If class doesn't provide "equals" implementation, will be compared by serializable members.
|
||||
*/
|
||||
public final class SkipDefaultsSerializationFilter extends SkipDefaultValuesSerializationFilters {
|
||||
public class SkipDefaultsSerializationFilter extends SkipDefaultValuesSerializationFilters {
|
||||
boolean equal(@NotNull Binding binding, @NotNull Object bean) {
|
||||
Accessor accessor = binding.getAccessor();
|
||||
return equal(binding, accessor.read(bean), accessor.read(getDefaultBean(bean)));
|
||||
@@ -36,32 +24,31 @@ public final class SkipDefaultsSerializationFilter extends SkipDefaultValuesSeri
|
||||
if (defaultValue instanceof Element && currentValue instanceof Element) {
|
||||
return JDOMUtil.areElementsEqual((Element)currentValue, (Element)defaultValue);
|
||||
}
|
||||
else {
|
||||
if (currentValue == defaultValue) {
|
||||
return true;
|
||||
}
|
||||
if (currentValue == null || defaultValue == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (binding instanceof BasePrimitiveBinding) {
|
||||
Binding referencedBinding = ((BasePrimitiveBinding)binding).myBinding;
|
||||
if (referencedBinding instanceof BeanBinding) {
|
||||
BeanBinding classBinding = (BeanBinding)referencedBinding;
|
||||
ThreeState compareByFields = classBinding.compareByFields;
|
||||
if (compareByFields == ThreeState.UNSURE) {
|
||||
compareByFields = ReflectionUtil.getDeclaredMethod(classBinding.myBeanClass, "equals", Object.class) == null ? ThreeState.YES : ThreeState.NO;
|
||||
if (currentValue == defaultValue) {
|
||||
return true;
|
||||
}
|
||||
if (currentValue == null || defaultValue == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
classBinding.compareByFields = compareByFields;
|
||||
}
|
||||
if (binding instanceof BasePrimitiveBinding) {
|
||||
Binding referencedBinding = ((BasePrimitiveBinding)binding).myBinding;
|
||||
if (referencedBinding instanceof BeanBinding) {
|
||||
BeanBinding classBinding = (BeanBinding)referencedBinding;
|
||||
ThreeState compareByFields = classBinding.compareByFields;
|
||||
if (compareByFields == ThreeState.UNSURE) {
|
||||
compareByFields = ReflectionUtil.getDeclaredMethod(classBinding.myBeanClass, "equals", Object.class) == null ? ThreeState.YES : ThreeState.NO;
|
||||
|
||||
if (compareByFields == ThreeState.YES) {
|
||||
return classBinding.equalByFields(currentValue, defaultValue, this);
|
||||
}
|
||||
classBinding.compareByFields = compareByFields;
|
||||
}
|
||||
|
||||
if (compareByFields == ThreeState.YES) {
|
||||
return classBinding.equalByFields(currentValue, defaultValue, this);
|
||||
}
|
||||
}
|
||||
|
||||
return Comparing.equal(currentValue, defaultValue);
|
||||
}
|
||||
|
||||
return Comparing.equal(currentValue, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user