Skip to content

Commit 24d5cf8

Browse files
committed
test(writeObject): Добавлены тесты для записи метаданных в форматах EDT и Designer
- Созданы тесты MDOWriterDesignerTest и MDOWriterEdtTest для проверки записи объектов метаданных в формате Designer (.xml) и EDT (.mdo). - Тесты включают проверку корректности записанных данных и возможность чтения обратно записанных объектов. - Добавлены проверки на выброс исключений при неверных входных данных.
1 parent 1603f78 commit 24d5cf8

2 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.mdo.Catalog;
27+
import com.github._1c_syntax.bsl.mdo.Subsystem;
28+
import com.github._1c_syntax.bsl.types.MultiLanguageString;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
31+
32+
import java.nio.charset.StandardCharsets;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Тесты записи объектов метаданных в формате Конфигуратора (Designer .xml).
40+
*/
41+
class MDOWriterDesignerTest {
42+
43+
private static final String META_DATA_OBJECT = "MetaDataObject";
44+
45+
@Test
46+
void writeSubsystemDesignerXml(@TempDir Path tempDir) throws Exception {
47+
var subsystem = Subsystem.builder()
48+
.name("TestSubsystemDesigner")
49+
.uuid("3d00f7d6-e3b0-49cf-8093-e2e4f6ea2293")
50+
.synonym(MultiLanguageString.create("ru", "Подсистема для Конфигуратора"))
51+
.build();
52+
53+
var outFile = tempDir.resolve("Subsystems").resolve("TestSubsystemDesigner.xml");
54+
MDClasses.writeObject(outFile, subsystem);
55+
56+
assertThat(outFile).exists().isRegularFile();
57+
var content = Files.readString(outFile, StandardCharsets.UTF_8);
58+
assertThat(content).contains(META_DATA_OBJECT);
59+
assertThat(content).contains("<Subsystem uuid=\"3d00f7d6-e3b0-49cf-8093-e2e4f6ea2293\">");
60+
assertThat(content).contains("<Properties>");
61+
assertThat(content).contains("<Name>TestSubsystemDesigner</Name>");
62+
assertThat(content).contains("Подсистема для Конфигуратора");
63+
assertThat(content).contains("</Properties>");
64+
assertThat(content).doesNotContainPattern("<Subsystem[^>]*>\\s*<Subsystem");
65+
}
66+
67+
@Test
68+
void writeSubsystemWithChildrenHasChildObjects(@TempDir Path tempDir) throws Exception {
69+
var child = Subsystem.builder().name("ChildSubsystem").build();
70+
var subsystem = Subsystem.builder()
71+
.name("ParentSubsystem")
72+
.uuid("3d00f7d6-e3b0-49cf-8093-e2e4f6ea2293")
73+
.subsystem(child)
74+
.build();
75+
76+
var outFile = tempDir.resolve("Subsystems").resolve("ParentSubsystem.xml");
77+
MDClasses.writeObject(outFile, subsystem);
78+
79+
var content = Files.readString(outFile, StandardCharsets.UTF_8);
80+
assertThat(content).contains("<ChildObjects>");
81+
assertThat(content).contains("<Subsystem>ChildSubsystem</Subsystem>");
82+
assertThat(content).contains("</ChildObjects>");
83+
}
84+
85+
@Test
86+
void writeCatalogDesignerXml(@TempDir Path tempDir) throws Exception {
87+
var catalog = Catalog.builder()
88+
.name("TestCatalogDesigner")
89+
.uuid("eeef463d-d5e7-42f2-ae53-10279661f59d")
90+
.synonym(MultiLanguageString.create("ru", "Справочник для Конфигуратора"))
91+
.build();
92+
93+
var outFile = tempDir.resolve("Catalogs").resolve("TestCatalogDesigner.xml");
94+
MDClasses.writeObject(outFile, catalog);
95+
96+
assertThat(outFile).exists().isRegularFile();
97+
var content = Files.readString(outFile, StandardCharsets.UTF_8);
98+
assertThat(content).contains(META_DATA_OBJECT);
99+
assertThat(content).contains("<Catalog uuid=\"eeef463d-d5e7-42f2-ae53-10279661f59d\">");
100+
assertThat(content).contains("<Properties>");
101+
assertThat(content).contains("<Name>TestCatalogDesigner</Name>");
102+
assertThat(content).contains("Справочник для Конфигуратора");
103+
assertThat(content).doesNotContainPattern("<Catalog[^>]*>\\s*<Catalog");
104+
}
105+
106+
@Test
107+
void writeConfigurationDesignerXml(@TempDir Path tempDir) throws Exception {
108+
var config = Configuration.builder()
109+
.name("TestConfigDesigner")
110+
.uuid("afc7a6ad-095f-4fdc-8ba5-8c692defb671")
111+
.build();
112+
113+
var outFile = tempDir.resolve("Configuration.xml");
114+
MDClasses.writeObject(outFile, config);
115+
116+
assertThat(outFile).exists().isRegularFile();
117+
var content = Files.readString(outFile, StandardCharsets.UTF_8);
118+
assertThat(content).contains(META_DATA_OBJECT);
119+
assertThat(content).contains("<Configuration uuid=\"afc7a6ad-095f-4fdc-8ba5-8c692defb671\">");
120+
assertThat(content).contains("<Name>TestConfigDesigner</Name>");
121+
assertThat(content).contains("ChildObjects");
122+
assertThat(content).doesNotContainPattern("<Configuration[^>]*>\\s*<Configuration");
123+
}
124+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)