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