|
| 1 | +/* |
| 2 | + * This file is a part of MDClasses. |
| 3 | + * |
| 4 | + * Copyright (c) 2019 - 2026 |
| 5 | + * Tymko Oleg <[email protected]>, Maximov Valery <[email protected]> and contributors |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + * |
| 9 | + * MDClasses is free software; you can redistribute it and/or |
| 10 | + * modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either |
| 12 | + * version 3.0 of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * MDClasses is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with MDClasses. |
| 21 | + */ |
| 22 | +package com.github._1c_syntax.bsl.writer; |
| 23 | + |
| 24 | +import com.github._1c_syntax.bsl.mdclasses.Configuration; |
| 25 | +import com.github._1c_syntax.bsl.mdclasses.MDClasses; |
| 26 | +import com.github._1c_syntax.bsl.mdclasses.MDCReadSettings; |
| 27 | +import com.github._1c_syntax.bsl.mdo.Catalog; |
| 28 | +import com.github._1c_syntax.bsl.mdo.Subsystem; |
| 29 | +import com.github._1c_syntax.bsl.reader.MDOReader; |
| 30 | +import com.github._1c_syntax.bsl.reader.edt.EDTReader; |
| 31 | +import com.github._1c_syntax.bsl.types.MultiLanguageString; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.io.TempDir; |
| 34 | + |
| 35 | +import java.nio.charset.StandardCharsets; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * Тесты записи объектов метаданных в формате EDT (.mdo). |
| 43 | + */ |
| 44 | +class MDOWriterEdtTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + void writeSubsystemThenReadBack(@TempDir Path tempDir) throws Exception { |
| 48 | + var subsystem = Subsystem.builder() |
| 49 | + .name("TestSubsystem") |
| 50 | + .uuid("test-uuid-123") |
| 51 | + .synonym(MultiLanguageString.create("ru", "Тестовая подсистема")) |
| 52 | + .build(); |
| 53 | + |
| 54 | + var outFile = tempDir.resolve("Subsystems").resolve("TestSubsystem").resolve("TestSubsystem.mdo"); |
| 55 | + MDClasses.writeObject(outFile, subsystem); |
| 56 | + |
| 57 | + assertThat(outFile).exists(); |
| 58 | + assertThat(outFile).isRegularFile(); |
| 59 | + var content = Files.readString(outFile, StandardCharsets.UTF_8); |
| 60 | + assertThat(content).as("written file content").isNotEmpty(); |
| 61 | + assertThat(content).contains("mdclass:Subsystem"); |
| 62 | + assertThat(content).contains("<name>TestSubsystem</name>"); |
| 63 | + assertThat(content).contains("test-uuid-123"); |
| 64 | + assertThat(content).contains("Тестовая подсистема"); |
| 65 | + assertThat(content).contains("<includeHelpInContents>"); |
| 66 | + assertThat(content).contains("<includeInCommandInterface>"); |
| 67 | + int namePos = content.indexOf("<name>TestSubsystem</name>"); |
| 68 | + int includeHelpPos = content.indexOf("<includeHelpInContents>"); |
| 69 | + int includeCmdPos = content.indexOf("<includeInCommandInterface>"); |
| 70 | + assertThat(namePos).isLessThan(includeHelpPos); |
| 71 | + assertThat(includeHelpPos).isLessThan(includeCmdPos); |
| 72 | + |
| 73 | + var reader = new EDTReader(outFile, MDCReadSettings.SKIP_SUPPORT); |
| 74 | + var readBack = reader.read(outFile); |
| 75 | + assertThat(readBack).isNotNull().isInstanceOf(Subsystem.class); |
| 76 | + var readSubsystem = (Subsystem) readBack; |
| 77 | + assertThat(readSubsystem.getName()).isEqualTo(subsystem.getName()); |
| 78 | + assertThat(readSubsystem.getUuid()).isEqualTo(subsystem.getUuid()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void writeConfigurationThenReadBack(@TempDir Path tempDir) throws Exception { |
| 83 | + var config = Configuration.builder() |
| 84 | + .name("TestConfig") |
| 85 | + .uuid("test-config-uuid-001") |
| 86 | + .build(); |
| 87 | + |
| 88 | + var configurationMdo = tempDir.resolve("src").resolve("Configuration").resolve("Configuration.mdo"); |
| 89 | + MDClasses.writeObject(configurationMdo, config); |
| 90 | + |
| 91 | + assertThat(configurationMdo).exists().isRegularFile(); |
| 92 | + var content = Files.readString(configurationMdo, StandardCharsets.UTF_8); |
| 93 | + assertThat(content).contains("mdclass:Configuration"); |
| 94 | + assertThat(content).contains("<name>TestConfig</name>"); |
| 95 | + assertThat(content).contains("test-config-uuid-001"); |
| 96 | + |
| 97 | + var readBack = MDOReader.readConfiguration(configurationMdo, MDCReadSettings.SKIP_SUPPORT); |
| 98 | + assertThat(readBack).isNotNull().isInstanceOf(Configuration.class); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void writeCatalogThenReadBack(@TempDir Path tempDir) throws Exception { |
| 103 | + var catalog = Catalog.builder() |
| 104 | + .name("TestCatalog") |
| 105 | + .uuid("catalog-uuid-001") |
| 106 | + .synonym(MultiLanguageString.create("ru", "Тестовый справочник")) |
| 107 | + .checkUnique(true) |
| 108 | + .build(); |
| 109 | + |
| 110 | + var outFile = tempDir.resolve("Catalogs").resolve("TestCatalog").resolve("TestCatalog.mdo"); |
| 111 | + MDClasses.writeObject(outFile, catalog); |
| 112 | + |
| 113 | + assertThat(outFile).exists().isRegularFile(); |
| 114 | + var content = Files.readString(outFile, StandardCharsets.UTF_8); |
| 115 | + assertThat(content).contains("Catalog"); |
| 116 | + assertThat(content).contains("<name>TestCatalog</name>"); |
| 117 | + assertThat(content).contains("catalog-uuid-001"); |
| 118 | + assertThat(content).contains("Тестовый справочник"); |
| 119 | + |
| 120 | + var reader = new EDTReader(outFile, MDCReadSettings.SKIP_SUPPORT); |
| 121 | + var readBack = reader.read(outFile); |
| 122 | + assertThat(readBack).isNotNull().isInstanceOf(Catalog.class); |
| 123 | + var readCatalog = (Catalog) readBack; |
| 124 | + assertThat(readCatalog.getName()).isEqualTo(catalog.getName()); |
| 125 | + assertThat(readCatalog.getUuid()).isEqualTo(catalog.getUuid()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void writeObjectThrowsOnNullPath() { |
| 130 | + var subsystem = Subsystem.builder().name("Test").build(); |
| 131 | + try { |
| 132 | + MDClasses.writeObject((Path) null, subsystem); |
| 133 | + } catch (Exception e) { |
| 134 | + assertThat(e).isInstanceOf(IllegalArgumentException.class); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void writeObjectThrowsOnNullObject(@TempDir Path tempDir) { |
| 140 | + var path = tempDir.resolve("Test.mdo"); |
| 141 | + try { |
| 142 | + MDClasses.writeObject(path, null); |
| 143 | + } catch (Exception e) { |
| 144 | + assertThat(e).isInstanceOf(IllegalArgumentException.class); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void writeObjectThrowsOnUnsupportedFormat(@TempDir Path tempDir) { |
| 150 | + var subsystem = Subsystem.builder().name("Test").build(); |
| 151 | + var path = tempDir.resolve("Test.txt"); |
| 152 | + try { |
| 153 | + MDClasses.writeObject(path, subsystem); |
| 154 | + } catch (Exception e) { |
| 155 | + assertThat(e).isInstanceOf(UnsupportedOperationException.class); |
| 156 | + assertThat(e.getMessage()).contains(".mdo").contains(".xml"); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments