From 03cc8a3a0f56406154eccb0f1f3d1c0c61747b5a Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 14 Feb 2017 14:53:49 +0100 Subject: [PATCH] cleanup (prefer to use loadElement) --- .../src/com/intellij/util/jdom.kt | 4 ++-- .../testFramework/assertions/JdomAssert.kt | 18 ++++++++++++---- .../com/intellij/openapi/util/JDOMUtil.java | 5 ----- .../edu/learning/StudySerializationUtils.java | 2 ++ python/educational-core/student/student.iml | 1 + .../edu/learning/StudyMigrationTest.java | 21 ++++++++++--------- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/platform/projectModel-impl/src/com/intellij/util/jdom.kt b/platform/projectModel-impl/src/com/intellij/util/jdom.kt index 4512e75e29a2..bf1b8bea2a06 100644 --- a/platform/projectModel-impl/src/com/intellij/util/jdom.kt +++ b/platform/projectModel-impl/src/com/intellij/util/jdom.kt @@ -60,9 +60,9 @@ private fun getSaxBuilder(): SAXBuilder { @JvmOverloads @Throws(IOException::class) -fun write(element: Parent, file: Path, lineSeparator: String = "\n") { +fun Parent.write(file: Path, lineSeparator: String = "\n") { BufferedOutputStream(file.outputStream()).use { - JDOMUtil.write(element, it, lineSeparator) + JDOMUtil.write(this, it, lineSeparator) } } diff --git a/platform/testFramework/extensions/src/com/intellij/testFramework/assertions/JdomAssert.kt b/platform/testFramework/extensions/src/com/intellij/testFramework/assertions/JdomAssert.kt index 8ed846eadb7e..57d0c3555463 100644 --- a/platform/testFramework/extensions/src/com/intellij/testFramework/assertions/JdomAssert.kt +++ b/platform/testFramework/extensions/src/com/intellij/testFramework/assertions/JdomAssert.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. @@ -16,11 +16,15 @@ package com.intellij.testFramework.assertions import com.intellij.openapi.util.JDOMUtil +import com.intellij.openapi.util.text.StringUtilRt +import com.intellij.rt.execution.junit.FileComparisonFailure +import com.intellij.util.io.readText import com.intellij.util.isEmpty +import com.intellij.util.loadElement import org.assertj.core.api.AbstractAssert import org.assertj.core.internal.Objects import org.jdom.Element -import java.io.File +import java.nio.file.Path class JdomAssert(actual: Element?) : AbstractAssert(actual, JdomAssert::class.java) { fun isEmpty(): JdomAssert { @@ -33,8 +37,14 @@ class JdomAssert(actual: Element?) : AbstractAssert(actual return this } - fun isEqualTo(file: File): JdomAssert { - return isEqualTo(file.readText()) + fun isEqualTo(file: Path): JdomAssert { + isNotNull + + val expected = loadElement(file) + if (!JDOMUtil.areElementsEqual(actual, expected)) { + throw FileComparisonFailure(null, StringUtilRt.convertLineSeparators(file.readText()), JDOMUtil.writeElement(actual!!), file.toString()) + } + return this } fun isEqualTo(element: Element): JdomAssert { diff --git a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java index f6abf74ce2cc..30eba329bd41 100644 --- a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java +++ b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java @@ -257,11 +257,6 @@ public class JDOMUtil { return a1.getName().equals(a2.getName()) && a1.getValue().equals(a2.getValue()); } - @NotNull - public static Document loadDocument(char[] chars, int length) throws IOException, JDOMException { - return getSaxBuilder().build(new CharArrayReader(chars, 0, length)); - } - private static SAXBuilder getSaxBuilder() { SoftReference reference = ourSaxBuilder.get(); SAXBuilder saxBuilder = com.intellij.reference.SoftReference.dereference(reference); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java index 0163a3b895a4..6701b6bd4f6b 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java @@ -20,6 +20,7 @@ import com.jetbrains.edu.learning.stepic.StepicWrappers; import org.jdom.Attribute; import org.jdom.Element; import org.jdom.output.XMLOutputter; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.lang.reflect.Type; @@ -208,6 +209,7 @@ public class StudySerializationUtils { return state; } + @NotNull public static Element convertToForthVersion(Element state) throws StudyUnrecognizedFormatException { Element taskManagerElement = state.getChild(MAIN_ELEMENT); Element courseElement = getChildWithName(taskManagerElement, COURSE).getChild(COURSE_TITLED); diff --git a/python/educational-core/student/student.iml b/python/educational-core/student/student.iml index 74bb142082e8..054fd550f453 100644 --- a/python/educational-core/student/student.iml +++ b/python/educational-core/student/student.iml @@ -36,5 +36,6 @@ + \ No newline at end of file diff --git a/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java b/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java index 2d21fd58ed8d..34f7846b804d 100644 --- a/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java +++ b/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java @@ -1,26 +1,27 @@ package com.jetbrains.edu.learning; -import com.intellij.openapi.util.JDOMUtil; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.util.JdomKt; import org.jdom.Element; import org.jdom.JDOMException; +import org.jetbrains.annotations.NotNull; import org.junit.Test; -import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static com.intellij.testFramework.assertions.Assertions.assertThat; public class StudyMigrationTest { - @Test public void testFromThirdToForth() throws JDOMException, IOException, StudySerializationUtils.StudyUnrecognizedFormatException { - Element element = JDOMUtil.load(new File(FileUtil.join(getTestDataPath(), "3.xml"))); - Element actual = StudySerializationUtils.Xml.convertToForthVersion(element); - Element expected = JDOMUtil.load(new File(FileUtil.join(getTestDataPath()), "4.xml")); - PlatformTestUtil.assertElementsEqual(expected, actual); + Element element = JdomKt.loadElement(getTestDataPath().resolve("3.xml")); + assertThat(StudySerializationUtils.Xml.convertToForthVersion(element)).isEqualTo(getTestDataPath().resolve("4.xml")); } - protected String getTestDataPath() { - return FileUtil.join(PlatformTestUtil.getCommunityPath(), "python/educational-core/student/testData/migration"); + @NotNull + protected Path getTestDataPath() { + return Paths.get(PlatformTestUtil.getCommunityPath(), "python/educational-core/student/testData/migration"); } }