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;
025
026/**
027 * Annotation for a {@link Workspace} field in a test class. This will ensure it gets initialised
028 * and closed correctly between tests.
029 *
030 * <p>Use static-fields to keep a workspace object alive for the duration of all the tests
031 * (providing the same semantics as initialising and closing resources using the
032 * {@link org.junit.jupiter.api.BeforeAll} and {@link org.junit.jupiter.api.AfterAll} annotations).
033 *
034 * <p>You must extend your test class with the {@link JctExtension} extension for this annotation
035 * to be detected and handled.
036 *
037 * <p>Example usage:
038 *
039 * <pre><code>
040 * {@literal @ExtendWith(JctExtension.class)}
041 * class MyTest {
042 *   {@literal @Managed}
043 *   Workspace workspace;
044 *
045 *   {@literal @JavacCompilerTest}
046 *   void myTest(JctCompiler compiler) {
047 *     ...
048 *     var compilation = compiler.compile(workspace);
049 *     ...
050 *   }
051 * }
052 * </code></pre>
053 *
054 * @author Ashley Scopes
055 * @since 0.4.0
056 */
057@Documented
058@Retention(RetentionPolicy.RUNTIME)
059@Target(ElementType.FIELD)
060public @interface Managed {
061
062  /**
063   * Get the path strategy to use for the workspace.
064   *
065   * @return the path strategy to use.
066   */
067  PathStrategy pathStrategy() default PathStrategy.RAM_DIRECTORIES;
068}