001/*
002 * Copyright (C) 2022 - 2024, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.junit;
017
018import io.github.ascopes.jct.workspaces.PathStrategy;
019import io.github.ascopes.jct.workspaces.Workspace;
020import java.lang.annotation.Documented;
021import java.lang.annotation.ElementType;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.lang.annotation.Target;
025import org.apiguardian.api.API;
026import org.apiguardian.api.API.Status;
027
028/**
029 * Annotation for a {@link Workspace} field in a test class. This will ensure it gets initialised
030 * and closed correctly between tests.
031 *
032 * <p>Use static-fields to keep a workspace object alive for the duration of all the tests
033 * (providing the same semantics as initialising and closing resources using the
034 * {@link org.junit.jupiter.api.BeforeAll} and {@link org.junit.jupiter.api.AfterAll} annotations).
035 *
036 * <p>You must extend your test class with the {@link JctExtension} extension for this annotation
037 * to be detected and handled.
038 *
039 * <p>Example usage:
040 *
041 * <pre><code>
042 * {@literal @ExtendWith(JctExtension.class)}
043 * class MyTest {
044 *   {@literal @Managed}
045 *   Workspace workspace;
046 *
047 *   {@literal @JavacCompilerTest}
048 *   void myTest(JctCompiler compiler) {
049 *     ...
050 *     var compilation = compiler.compile(workspace);
051 *     ...
052 *   }
053 * }
054 * </code></pre>
055 *
056 * @author Ashley Scopes
057 * @since 0.4.0
058 */
059@API(since = "0.4.0", status = Status.STABLE)
060@Documented
061@Retention(RetentionPolicy.RUNTIME)
062@Target(ElementType.FIELD)
063public @interface Managed {
064
065  /**
066   * Get the path strategy to use for the workspace.
067   *
068   * @return the path strategy to use.
069   */
070  PathStrategy pathStrategy() default PathStrategy.RAM_DIRECTORIES;
071}