@Test public void migrateIfNecessary() throws Exception { Assertions.assertThatThrownBy(() -> instance.migrateIfNecessary("<ScenarioSimulationModel version=\"9999999999.99999999999\" />")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Version 9999999999.99999999999 of the file is not supported. Current version is " + ScenarioSimulationXMLPersistence.getCurrentVersion()); String noMigrationNeeded = "<ScenarioSimulationModel version=\"" + currentVersion + "\" />"; String afterMigration = instance.migrateIfNecessary(noMigrationNeeded); Document document = DOMParserUtil.getDocument(afterMigration); commonCheckVersion(document, ScenarioSimulationXMLPersistence.getCurrentVersion()); }
public String migrateIfNecessary(String rawXml) throws Exception { String fileVersion = extractVersion(rawXml); ThrowingConsumer<Document> migrator = getMigrationStrategy().start(); boolean supported; switch (fileVersion) { case "1.0": migrator = migrator.andThen(getMigrationStrategy().from1_0to1_1()); case "1.1": migrator = migrator.andThen(getMigrationStrategy().from1_1to1_2()); case "1.2": migrator = migrator.andThen(getMigrationStrategy().from1_2to1_3()); case "1.3": migrator = migrator.andThen(getMigrationStrategy().from1_3to1_4()); case "1.4": migrator = migrator.andThen(getMigrationStrategy().from1_4to1_5()); case "1.5": migrator = migrator.andThen(getMigrationStrategy().from1_5to1_6()); case "1.6": migrator = migrator.andThen(getMigrationStrategy().from1_6to1_7()); case "1.7": migrator = migrator.andThen(getMigrationStrategy().from1_7to1_8()); supported = true; break; default: supported = CURRENT_VERSION.equals(fileVersion); break; } if (!supported) { throw new IllegalArgumentException(new StringBuilder().append("Version ").append(fileVersion) .append(" of the file is not supported. Current version is ") .append(CURRENT_VERSION).toString()); } migrator = migrator.andThen(getMigrationStrategy().end()); Document document = DOMParserUtil.getDocument(rawXml); migrator.accept(document); return DOMParserUtil.getString(document); }
ScenarioSimulationXMLPersistence { public String migrateIfNecessary(String rawXml) throws Exception { String fileVersion = extractVersion(rawXml); ThrowingConsumer<Document> migrator = getMigrationStrategy().start(); boolean supported; switch (fileVersion) { case "1.0": migrator = migrator.andThen(getMigrationStrategy().from1_0to1_1()); case "1.1": migrator = migrator.andThen(getMigrationStrategy().from1_1to1_2()); case "1.2": migrator = migrator.andThen(getMigrationStrategy().from1_2to1_3()); case "1.3": migrator = migrator.andThen(getMigrationStrategy().from1_3to1_4()); case "1.4": migrator = migrator.andThen(getMigrationStrategy().from1_4to1_5()); case "1.5": migrator = migrator.andThen(getMigrationStrategy().from1_5to1_6()); case "1.6": migrator = migrator.andThen(getMigrationStrategy().from1_6to1_7()); case "1.7": migrator = migrator.andThen(getMigrationStrategy().from1_7to1_8()); supported = true; break; default: supported = CURRENT_VERSION.equals(fileVersion); break; } if (!supported) { throw new IllegalArgumentException(new StringBuilder().append("Version ").append(fileVersion) .append(" of the file is not supported. Current version is ") .append(CURRENT_VERSION).toString()); } migrator = migrator.andThen(getMigrationStrategy().end()); Document document = DOMParserUtil.getDocument(rawXml); migrator.accept(document); return DOMParserUtil.getString(document); } }
ScenarioSimulationXMLPersistence { public String migrateIfNecessary(String rawXml) throws Exception { String fileVersion = extractVersion(rawXml); ThrowingConsumer<Document> migrator = getMigrationStrategy().start(); boolean supported; switch (fileVersion) { case "1.0": migrator = migrator.andThen(getMigrationStrategy().from1_0to1_1()); case "1.1": migrator = migrator.andThen(getMigrationStrategy().from1_1to1_2()); case "1.2": migrator = migrator.andThen(getMigrationStrategy().from1_2to1_3()); case "1.3": migrator = migrator.andThen(getMigrationStrategy().from1_3to1_4()); case "1.4": migrator = migrator.andThen(getMigrationStrategy().from1_4to1_5()); case "1.5": migrator = migrator.andThen(getMigrationStrategy().from1_5to1_6()); case "1.6": migrator = migrator.andThen(getMigrationStrategy().from1_6to1_7()); case "1.7": migrator = migrator.andThen(getMigrationStrategy().from1_7to1_8()); supported = true; break; default: supported = CURRENT_VERSION.equals(fileVersion); break; } if (!supported) { throw new IllegalArgumentException(new StringBuilder().append("Version ").append(fileVersion) .append(" of the file is not supported. Current version is ") .append(CURRENT_VERSION).toString()); } migrator = migrator.andThen(getMigrationStrategy().end()); Document document = DOMParserUtil.getDocument(rawXml); migrator.accept(document); return DOMParserUtil.getString(document); } private  ScenarioSimulationXMLPersistence(); }
ScenarioSimulationXMLPersistence { public String migrateIfNecessary(String rawXml) throws Exception { String fileVersion = extractVersion(rawXml); ThrowingConsumer<Document> migrator = getMigrationStrategy().start(); boolean supported; switch (fileVersion) { case "1.0": migrator = migrator.andThen(getMigrationStrategy().from1_0to1_1()); case "1.1": migrator = migrator.andThen(getMigrationStrategy().from1_1to1_2()); case "1.2": migrator = migrator.andThen(getMigrationStrategy().from1_2to1_3()); case "1.3": migrator = migrator.andThen(getMigrationStrategy().from1_3to1_4()); case "1.4": migrator = migrator.andThen(getMigrationStrategy().from1_4to1_5()); case "1.5": migrator = migrator.andThen(getMigrationStrategy().from1_5to1_6()); case "1.6": migrator = migrator.andThen(getMigrationStrategy().from1_6to1_7()); case "1.7": migrator = migrator.andThen(getMigrationStrategy().from1_7to1_8()); supported = true; break; default: supported = CURRENT_VERSION.equals(fileVersion); break; } if (!supported) { throw new IllegalArgumentException(new StringBuilder().append("Version ").append(fileVersion) .append(" of the file is not supported. Current version is ") .append(CURRENT_VERSION).toString()); } migrator = migrator.andThen(getMigrationStrategy().end()); Document document = DOMParserUtil.getDocument(rawXml); migrator.accept(document); return DOMParserUtil.getString(document); } private  ScenarioSimulationXMLPersistence(); static void configureXStreamMappings(XStream toConfigure); static ScenarioSimulationXMLPersistence getInstance(); static String getCurrentVersion(); static String cleanUpUnusedNodes(String input); static double getColumnWidth(String expressionIdentifierName); String marshal(final ScenarioSimulationModel sc); ScenarioSimulationModel unmarshal(final String rawXml); ScenarioSimulationModel unmarshal(final String rawXml, boolean migrate); String migrateIfNecessary(String rawXml); String extractVersion(String rawXml); MigrationStrategy getMigrationStrategy(); void setMigrationStrategy(MigrationStrategy migrationStrategy); }
ScenarioSimulationXMLPersistence { public String migrateIfNecessary(String rawXml) throws Exception { String fileVersion = extractVersion(rawXml); ThrowingConsumer<Document> migrator = getMigrationStrategy().start(); boolean supported; switch (fileVersion) { case "1.0": migrator = migrator.andThen(getMigrationStrategy().from1_0to1_1()); case "1.1": migrator = migrator.andThen(getMigrationStrategy().from1_1to1_2()); case "1.2": migrator = migrator.andThen(getMigrationStrategy().from1_2to1_3()); case "1.3": migrator = migrator.andThen(getMigrationStrategy().from1_3to1_4()); case "1.4": migrator = migrator.andThen(getMigrationStrategy().from1_4to1_5()); case "1.5": migrator = migrator.andThen(getMigrationStrategy().from1_5to1_6()); case "1.6": migrator = migrator.andThen(getMigrationStrategy().from1_6to1_7()); case "1.7": migrator = migrator.andThen(getMigrationStrategy().from1_7to1_8()); supported = true; break; default: supported = CURRENT_VERSION.equals(fileVersion); break; } if (!supported) { throw new IllegalArgumentException(new StringBuilder().append("Version ").append(fileVersion) .append(" of the file is not supported. Current version is ") .append(CURRENT_VERSION).toString()); } migrator = migrator.andThen(getMigrationStrategy().end()); Document document = DOMParserUtil.getDocument(rawXml); migrator.accept(document); return DOMParserUtil.getString(document); } private  ScenarioSimulationXMLPersistence(); static void configureXStreamMappings(XStream toConfigure); static ScenarioSimulationXMLPersistence getInstance(); static String getCurrentVersion(); static String cleanUpUnusedNodes(String input); static double getColumnWidth(String expressionIdentifierName); String marshal(final ScenarioSimulationModel sc); ScenarioSimulationModel unmarshal(final String rawXml); ScenarioSimulationModel unmarshal(final String rawXml, boolean migrate); String migrateIfNecessary(String rawXml); String extractVersion(String rawXml); MigrationStrategy getMigrationStrategy(); void setMigrationStrategy(MigrationStrategy migrationStrategy);  }