• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aas-core-works / aas-core-codegen / 23339141012

20 Mar 2026 10:37AM UTC coverage: 83.911% (+0.2%) from 83.702%
23339141012

push

github

web-flow
Add generation of test code for Java (#599)

We add the unit test generators for the Java SDK. This greatly simplifies our development pipeline.

435 of 450 new or added lines in 30 files covered. (96.67%)

4 existing lines in 1 file now uncovered.

31183 of 37162 relevant lines covered (83.91%)

3.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.1
/aas_core_codegen/java/main.py
1
"""Generate Java code based on the meta-model."""
2

3
import pathlib
4✔
4
from typing import TextIO, Sequence, Tuple, Callable, Optional, List
4✔
5

6
from aas_core_codegen import specific_implementations, run, java, intermediate
4✔
7
from aas_core_codegen.common import Error
4✔
8
from aas_core_codegen.java import (
4✔
9
    common as java_common,
10
    lib as java_lib,
11
    tests as java_tests,
12
)
13

14

15
def execute(context: run.Context, stdout: TextIO, stderr: TextIO) -> int:
4✔
16
    """Generate the code."""
17

18
    verified_ir_table, errors = java_lib.verify_for_types(
4✔
19
        symbol_table=context.symbol_table
20
    )
21

22
    if errors is not None:
4✔
23
        run.write_error_report(
×
24
            message=f"Failed to verify the intermediate symbol table "
25
            f"for generation of Java code"
26
            f"based on {context.model_path}",
27
            errors=[context.lineno_columner.error_message(error) for error in errors],
28
            stderr=stderr,
29
        )
30
        return 1
×
31

32
    assert verified_ir_table is not None
4✔
33

34
    unsupported_contracts_errors = (
4✔
35
        intermediate.errors_if_contracts_for_functions_or_methods_defined(
36
            verified_ir_table
37
        )
38
    )
39
    if unsupported_contracts_errors is not None:
4✔
UNCOV
40
        run.write_error_report(
×
41
            message=f"We do not support pre and post-conditions and snapshots "
42
            f"at the moment. Please notify the developers if you need this "
43
            f"feature (based on meta-model {context.model_path})",
44
            errors=[
45
                context.lineno_columner.error_message(error)
46
                for error in unsupported_contracts_errors
47
            ],
48
            stderr=stderr,
49
        )
50
        return 1
×
51

52
    unsupported_methods_errors = (
4✔
53
        intermediate.errors_if_non_implementation_specific_methods(verified_ir_table)
54
    )
55
    if unsupported_methods_errors is not None:
4✔
UNCOV
56
        run.write_error_report(
×
57
            message=f"We added some support for understood methods already and keep "
58
            f"maintaining it as it is only a matter of time when we will "
59
            f"introduce their transpilation. Introducing them after the fact "
60
            f"would have been much more difficult.\n"
61
            f"\n"
62
            f"At the given moment, however, we deliberately focus only on "
63
            f"implementation-specific methods. "
64
            f"(based on meta-model {context.model_path})",
65
            errors=[
66
                context.lineno_columner.error_message(error)
67
                for error in unsupported_methods_errors
68
            ],
69
            stderr=stderr,
70
        )
71
        return 1
×
72

73
    package_key = specific_implementations.ImplementationKey("package.txt")
4✔
74
    package_text = context.spec_impls.get(package_key, None)
4✔
75
    if package_text is None:
4✔
NEW
76
        stderr.write(f"The package snippet is missing: {package_key}\n")
×
UNCOV
77
        return 1
×
78

79
    if not java_common.PACKAGE_IDENTIFIER_RE.fullmatch(package_text):
4✔
NEW
80
        stderr.write(
×
81
            f"The text from the snippet {package_key} "
82
            f"is not a valid package identifier: {package_text!r}\n"
83
        )
84
        return 1
×
85

86
    package = java_common.PackageIdentifier(package_text)
4✔
87

88
    verify_errors = java_lib.verify_for_verification(
4✔
89
        spec_impls=context.spec_impls,
90
        verification_functions=verified_ir_table.verification_functions,
91
    )
92

93
    if verify_errors is not None:
4✔
94
        run.write_error_report(
×
95
            message="Failed to verify the verification functions for code generation",
96
            errors=verify_errors,
97
            stderr=stderr,
98
        )
99
        return 1
×
100

101
    project_rel_path = pathlib.Path("src") / "main" / "java" / package.replace(".", "/")
4✔
102
    assert not project_rel_path.is_absolute()
4✔
103

104
    tests_rel_path = pathlib.Path("src") / "test" / "java"
4✔
105
    assert not tests_rel_path.is_absolute()
4✔
106

107
    rel_paths_generators: Sequence[
4✔
108
        Tuple[
109
            pathlib.Path,
110
            Callable[
111
                [], Tuple[Optional[List[java_common.JavaFile]], Optional[List[Error]]]
112
            ],
113
        ]
114
    ] = [
115
        (
116
            project_rel_path / "constants",
117
            lambda: java_lib.generate_constants(
118
                symbol_table=context.symbol_table, package=package
119
            ),
120
        ),
121
        (
122
            project_rel_path / "copying",
123
            lambda: java_lib.generate_copying(
124
                symbol_table=context.symbol_table,
125
                package=package,
126
                spec_impls=context.spec_impls,
127
            ),
128
        ),
129
        (
130
            project_rel_path / "enhancing",
131
            lambda: java_lib.generate_enhancing(
132
                symbol_table=context.symbol_table,
133
                package=package,
134
                spec_impls=context.spec_impls,
135
            ),
136
        ),
137
        (
138
            project_rel_path / "generation",
139
            lambda: java_lib.generate_generation(
140
                symbol_table=context.symbol_table,
141
                package=package,
142
                spec_impls=context.spec_impls,
143
            ),
144
        ),
145
        (
146
            project_rel_path / "jsonization",
147
            lambda: java_lib.generate_jsonization(
148
                symbol_table=context.symbol_table,
149
                package=package,
150
                spec_impls=context.spec_impls,
151
            ),
152
        ),
153
        (
154
            project_rel_path / "reporting",
155
            lambda: (java_lib.generate_reporting(package=package), None),
156
        ),
157
        (
158
            project_rel_path / "stringification",
159
            lambda: java_lib.generate_stringification(
160
                symbol_table=context.symbol_table, package=package
161
            ),
162
        ),
163
        (
164
            project_rel_path / "types",
165
            lambda: java_lib.generate_types(
166
                symbol_table=verified_ir_table,
167
                package=package,
168
                spec_impls=context.spec_impls,
169
            ),
170
        ),
171
        (
172
            project_rel_path / "verification",
173
            lambda: java_lib.generate_verification(
174
                symbol_table=verified_ir_table,
175
                package=package,
176
                spec_impls=context.spec_impls,
177
            ),
178
        ),
179
        (
180
            project_rel_path / "visitation",
181
            lambda: java_lib.generate_visitation(
182
                symbol_table=context.symbol_table, package=package
183
            ),
184
        ),
185
        (
186
            project_rel_path / "xmlization",
187
            lambda: java_lib.generate_xmlization(
188
                symbol_table=context.symbol_table,
189
                package=package,
190
                spec_impls=context.spec_impls,
191
            ),
192
        ),
193
        (
194
            tests_rel_path,
195
            lambda: (java_tests.generate_common(package=package), None),
196
        ),
197
        (
198
            tests_rel_path,
199
            lambda: (java_tests.generate_common_json(package=package), None),
200
        ),
201
        (
202
            tests_rel_path,
203
            lambda: (
204
                java_tests.generate_common_jsonization(
205
                    package=package, symbol_table=context.symbol_table
206
                ),
207
                None,
208
            ),
209
        ),
210
        (
211
            tests_rel_path,
212
            lambda: (
213
                java_tests.generate_test_copying(
214
                    package=package, symbol_table=context.symbol_table
215
                ),
216
                None,
217
            ),
218
        ),
219
        (
220
            tests_rel_path,
221
            lambda: (
222
                java_tests.generate_test_descend_and_visitor_through(
223
                    package=package, symbol_table=context.symbol_table
224
                ),
225
                None,
226
            ),
227
        ),
228
        (
229
            tests_rel_path,
230
            lambda: (
231
                java_tests.generate_test_descend_once(
232
                    package=package, symbol_table=context.symbol_table
233
                ),
234
                None,
235
            ),
236
        ),
237
        (
238
            tests_rel_path,
239
            lambda: (
240
                java_tests.generate_test_enhancing(
241
                    package=package, symbol_table=context.symbol_table
242
                ),
243
                None,
244
            ),
245
        ),
246
        (
247
            tests_rel_path,
248
            lambda: (
249
                java_tests.generate_test_jsonization_of_concrete_classes(
250
                    package=package, symbol_table=context.symbol_table
251
                ),
252
                None,
253
            ),
254
        ),
255
        (
256
            tests_rel_path,
257
            lambda: (
258
                java_tests.generate_test_jsonization_of_enums(
259
                    package=package, symbol_table=context.symbol_table
260
                ),
261
                None,
262
            ),
263
        ),
264
        (
265
            tests_rel_path,
266
            lambda: (
267
                java_tests.generate_test_jsonization_of_interfaces(
268
                    package=package, symbol_table=context.symbol_table
269
                ),
270
                None,
271
            ),
272
        ),
273
        (
274
            tests_rel_path,
275
            lambda: (
276
                java_tests.generate_test_over_x_or_empty(
277
                    package=package, symbol_table=context.symbol_table
278
                ),
279
                None,
280
            ),
281
        ),
282
        (
283
            tests_rel_path,
284
            lambda: (
285
                java_tests.generate_test_verification_of_enums(
286
                    package=package, symbol_table=context.symbol_table
287
                ),
288
                None,
289
            ),
290
        ),
291
        (
292
            tests_rel_path,
293
            lambda: (
294
                java_tests.generate_test_x_or_default(
295
                    package=package, symbol_table=context.symbol_table
296
                ),
297
                None,
298
            ),
299
        ),
300
        (
301
            tests_rel_path,
302
            lambda: (
303
                java_tests.generate_test_xmlization_of_concrete_classes(
304
                    package=package, symbol_table=context.symbol_table
305
                ),
306
                None,
307
            ),
308
        ),
309
        (
310
            tests_rel_path,
311
            lambda: (
312
                java_tests.generate_test_xmlization_of_interfaces(
313
                    package=package, symbol_table=context.symbol_table
314
                ),
315
                None,
316
            ),
317
        ),
318
    ]
319

320
    for rel_path, generator_func in rel_paths_generators:
4✔
321
        assert not rel_path.is_absolute()
4✔
322

323
        java_files, errors = generator_func()
4✔
324

325
        if errors is not None:
4✔
UNCOV
326
            run.write_error_report(
×
327
                message=f"Failed to generate {rel_path} "
328
                f"based on {context.model_path}",
329
                errors=[
330
                    context.lineno_columner.error_message(error) for error in errors
331
                ],
332
                stderr=stderr,
333
            )
334
            return 1
×
335

336
        assert java_files is not None
4✔
337

338
        for java_file in java_files:
4✔
339
            pth = context.output_dir / rel_path / java_file.name
4✔
340

341
            try:
4✔
342
                pth.parent.mkdir(parents=True, exist_ok=True)
4✔
NEW
343
            except Exception as exception:
×
NEW
344
                run.write_error_report(
×
345
                    message=f"Failed to create the directory {pth.parent}",
346
                    errors=[str(exception)],
347
                    stderr=stderr,
348
                )
NEW
349
                return 1
×
350

351
            try:
4✔
352
                pth.write_text(java_file.content, encoding="utf-8")
4✔
NEW
353
            except Exception as exception:
×
NEW
354
                run.write_error_report(
×
355
                    message=f"Failed to write to {pth}",
356
                    errors=[str(exception)],
357
                    stderr=stderr,
358
                )
NEW
359
                return 1
×
360

361
    stdout.write(f"Code generated to: {context.output_dir}\n")
4✔
362
    return 0
4✔
363

364

365
assert java.__doc__ == __doc__
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc