|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.aot; |
| 18 | + |
| 19 | +import org.springframework.aot.AotDetector; |
| 20 | +import org.springframework.lang.Nullable; |
| 21 | + |
| 22 | +/** |
| 23 | + * Holder for metadata specific to ahead-of-time (AOT) support in the <em>Spring |
| 24 | + * TestContext Framework</em>. |
| 25 | + * |
| 26 | + * <p>AOT test attributes are supported in two modes of operation: build-time |
| 27 | + * and run-time. At build time, test components can {@linkplain #setAttribute contribute} |
| 28 | + * attributes during the AOT processing phase. At run time, test components can |
| 29 | + * {@linkplain #getString(String) retrieve} attributes that were contributed at |
| 30 | + * build time. If {@link AotDetector#useGeneratedArtifacts()} returns {@code true}, |
| 31 | + * run-time mode applies. |
| 32 | + * |
| 33 | + * <p>For example, if a test component computes something at build time that |
| 34 | + * cannot be computed at run time, the result of the build-time computation can |
| 35 | + * be stored as an AOT attribute and retrieved at run time without repeating the |
| 36 | + * computation. |
| 37 | + * |
| 38 | + * <p>An {@link AotContextLoader} would typically contribute an attribute in |
| 39 | + * {@link AotContextLoader#loadContextForAotProcessing loadContextForAotProcessing()}; |
| 40 | + * whereas, an {@link AotTestExecutionListener} would typically contribute an attribute |
| 41 | + * in {@link AotTestExecutionListener#processAheadOfTime processAheadOfTime()}. |
| 42 | + * Any other test component — such as a |
| 43 | + * {@link org.springframework.test.context.TestContextBootstrapper TestContextBootstrapper} |
| 44 | + * — can choose to contribute an attribute at any point in time. Note that |
| 45 | + * contributing an attribute during standard JVM test execution will not have any |
| 46 | + * adverse side effect since AOT attributes will be ignored in that scenario. In |
| 47 | + * any case, you should use {@link AotDetector#useGeneratedArtifacts()} to determine |
| 48 | + * if invocations of {@link #setAttribute(String, String)} and |
| 49 | + * {@link #removeAttribute(String)} are permitted. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @since 6.0 |
| 53 | + */ |
| 54 | +public interface AotTestAttributes { |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the current instance of {@code AotTestAttributes} to use. |
| 58 | + * <p>See the class-level {@link AotTestAttributes Javadoc} for details on |
| 59 | + * the two supported modes. |
| 60 | + */ |
| 61 | + static AotTestAttributes getInstance() { |
| 62 | + return new DefaultAotTestAttributes(AotTestAttributesFactory.getAttributes()); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Set a {@code String} attribute for later retrieval during AOT run-time execution. |
| 68 | + * <p>In general, users should take care to prevent overlaps with other |
| 69 | + * metadata attributes by using fully-qualified names, perhaps using a |
| 70 | + * class or package name as a prefix. |
| 71 | + * @param name the unique attribute name |
| 72 | + * @param value the associated attribute value |
| 73 | + * @throws UnsupportedOperationException if invoked during |
| 74 | + * {@linkplain AotDetector#useGeneratedArtifacts() AOT run-time execution} |
| 75 | + * @throws IllegalArgumentException if the provided value is {@code null} or |
| 76 | + * if an attempt is made to override an existing attribute |
| 77 | + * @see #setAttribute(String, boolean) |
| 78 | + * @see #removeAttribute(String) |
| 79 | + * @see AotDetector#useGeneratedArtifacts() |
| 80 | + */ |
| 81 | + void setAttribute(String name, String value); |
| 82 | + |
| 83 | + /** |
| 84 | + * Set a {@code boolean} attribute for later retrieval during AOT run-time execution. |
| 85 | + * <p>In general, users should take care to prevent overlaps with other |
| 86 | + * metadata attributes by using fully-qualified names, perhaps using a |
| 87 | + * class or package name as a prefix. |
| 88 | + * @param name the unique attribute name |
| 89 | + * @param value the associated attribute value |
| 90 | + * @throws UnsupportedOperationException if invoked during |
| 91 | + * {@linkplain AotDetector#useGeneratedArtifacts() AOT run-time execution} |
| 92 | + * @throws IllegalArgumentException if an attempt is made to override an |
| 93 | + * existing attribute |
| 94 | + * @see #setAttribute(String, String) |
| 95 | + * @see #removeAttribute(String) |
| 96 | + * @see Boolean#toString(boolean) |
| 97 | + * @see AotDetector#useGeneratedArtifacts() |
| 98 | + */ |
| 99 | + default void setAttribute(String name, boolean value) { |
| 100 | + setAttribute(name, Boolean.toString(value)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Remove the attribute stored under the provided name. |
| 105 | + * @param name the unique attribute name |
| 106 | + * @throws UnsupportedOperationException if invoked during |
| 107 | + * {@linkplain AotDetector#useGeneratedArtifacts() AOT run-time execution} |
| 108 | + * @see AotDetector#useGeneratedArtifacts() |
| 109 | + * @see #setAttribute(String, String) |
| 110 | + */ |
| 111 | + void removeAttribute(String name); |
| 112 | + |
| 113 | + /** |
| 114 | + * Retrieve the attribute value for the given name as a {@link String}. |
| 115 | + * @param name the unique attribute name |
| 116 | + * @return the associated attribute value, or {@code null} if not found |
| 117 | + * @see #getBoolean(String) |
| 118 | + * @see #setAttribute(String, String) |
| 119 | + */ |
| 120 | + @Nullable |
| 121 | + String getString(String name); |
| 122 | + |
| 123 | + /** |
| 124 | + * Retrieve the attribute value for the given name as a {@code boolean}. |
| 125 | + * @param name the unique attribute name |
| 126 | + * @return {@code true} if the attribute is set to "true" (ignoring case), |
| 127 | + * {@code} false otherwise |
| 128 | + * @see #getString(String) |
| 129 | + * @see #setAttribute(String, String) |
| 130 | + * @see Boolean#parseBoolean(String) |
| 131 | + */ |
| 132 | + default boolean getBoolean(String name) { |
| 133 | + return Boolean.parseBoolean(getString(name)); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments