001/*
002 * Copyright (C) 2022 - 2023, 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
030 * initialised 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}
035 * annotations).
036 *
037 * You must extend your test class with the {@link JctExtension} extension for this annotation
038 * to be detected and handled.
039 *
040 * <p>Example usage:
041 *
042 * <pre><code>
043 * {@literal @ExtendWith(JctExtension.class)}
044 * class MyTest {
045 *   {@literal @Managed}
046 *   Workspace workspace;
047 *
048 *   {@literal @JavacCompilerTest}
049 *   void myTest(JctCompiler compiler) {
050 *     ...
051 *     var compilation = compiler.compile(workspace);
052 *     ...
053 *   }
054 * }
055 * </code></pre>
056 *
057 * @author Ashley Scopes
058 * @since 0.4.0
059 */
060@API(since = "0.4.0", status = Status.STABLE)
061@Documented
062@Retention(RetentionPolicy.RUNTIME)
063@Target(ElementType.FIELD)
064public @interface Managed {
065
066  /**
067   * Get the path strategy to use for the workspace.
068   *
069   * @return the path strategy to use.
070   */
071  PathStrategy pathStrategy() default PathStrategy.RAM_DIRECTORIES;
072}