xref: /aosp_15_r20/build/bazel/rules/java/java_xsd_config_library_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
16load(":java_xsd_config_library.bzl", "java_xsd_config_library")
17
18def _xsd_config_generates_java_outputs_test_impl(ctx):
19    env = analysistest.begin(ctx)
20
21    actions = analysistest.target_actions(env)
22
23    asserts.equals(
24        env,
25        2,  # xsdc and soong_zip
26        len(actions),
27        "Incorrect number of actions",
28    )
29
30    # soong_zip is the second action
31    soong_zip_action_outputs = actions[1].outputs.to_list()
32
33    asserts.equals(
34        env,
35        1,
36        len(soong_zip_action_outputs),
37        "Incorrect number of action outputs",
38    )
39
40    asserts.equals(
41        env,
42        ctx.attr.expected_output_filename,
43        soong_zip_action_outputs[0].basename,
44    )
45
46    return analysistest.end(env)
47
48_xsd_config_generates_java_outputs_test = analysistest.make(
49    _xsd_config_generates_java_outputs_test_impl,
50    attrs = {
51        "expected_output_filename": attr.string(),
52    },
53)
54
55def _xsd_config_generates_java_outputs():
56    subject_name = "xsd_config_generates_java_outputs"
57
58    # Test the internal _java_xsd_codegen created by java_xsd_config_library
59    test_subject_name = subject_name + "_gen"
60    test_name = subject_name + "_test"
61
62    java_xsd_config_library(
63        name = subject_name,
64        src = "foo.xsd",
65        package_name = "foo",
66        tags = ["manual"],
67    )
68    _xsd_config_generates_java_outputs_test(
69        name = test_name,
70        target_under_test = test_subject_name,
71        expected_output_filename = test_subject_name + ".srcjar",
72    )
73    return test_name
74
75def java_xsd_config_library_test_suite(name):
76    native.test_suite(
77        name = name,
78        tests = [
79            _xsd_config_generates_java_outputs(),
80        ],
81    )
82