cleanup (prefer to use loadElement)

This commit is contained in:
Vladimir Krivosheev
2017-02-14 16:41:38 +01:00
parent be2945cbdd
commit 03cc8a3a0f
6 changed files with 30 additions and 21 deletions
@@ -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)
}
}
@@ -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<JdomAssert, Element?>(actual, JdomAssert::class.java) {
fun isEmpty(): JdomAssert {
@@ -33,8 +37,14 @@ class JdomAssert(actual: Element?) : AbstractAssert<JdomAssert, Element?>(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 {
@@ -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<SAXBuilder> reference = ourSaxBuilder.get();
SAXBuilder saxBuilder = com.intellij.reference.SoftReference.dereference(reference);
@@ -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);
@@ -36,5 +36,6 @@
</library>
</orderEntry>
<orderEntry type="module" module-name="testFramework" scope="TEST" />
<orderEntry type="module" module-name="testExtensions" scope="TEST" />
</component>
</module>
@@ -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");
}
}