1
|
|
|
/* |
2
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
3
|
|
|
* contributor license agreements. See the NOTICE file distributed with |
4
|
|
|
* this work for additional information regarding copyright ownership. |
5
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6
|
|
|
* (the "License"); you may not use this file except in compliance with |
7
|
|
|
* the License. You may obtain a copy of the License at |
8
|
|
|
* |
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
* |
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
* See the License for the specific language governing permissions and |
15
|
|
|
* limitations under the License. |
16
|
|
|
*/ |
17
|
|
|
package io.github.glytching.junit.extension.folder; |
18
|
|
|
|
19
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext; |
20
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
21
|
|
|
import org.junit.jupiter.api.extension.ParameterContext; |
22
|
|
|
import org.junit.jupiter.api.extension.ParameterResolutionException; |
23
|
|
|
import org.junit.jupiter.api.extension.ParameterResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The temporary folder extension provides a test with access to temporary files and directories. |
27
|
|
|
* The temporary folder extension provides a {@link TemporaryFolder} which you can use to create a |
28
|
|
|
* temporary file or directory for use by your test. The {@link TemporaryFolder} can be injected |
29
|
|
|
* into your test or test case with any of the following approaches: |
30
|
|
|
* |
31
|
|
|
* <ul> |
32
|
|
|
* <li>Instance variable injection into a {@code @BeforeEach} method. The {@link TemporaryFolder} |
33
|
|
|
* will be destroyed during {@code @AfterEach} and no exception will be thrown in cases where |
34
|
|
|
* the deletion fails. For example: |
35
|
|
|
* <pre> |
36
|
|
|
* private TemporaryFolder temporaryFolder; |
37
|
|
|
* |
38
|
|
|
* @BeforeEach |
39
|
|
|
* public void setUp(TemporaryFolder temporaryFolder) { |
40
|
|
|
* this.temporaryFolder = temporaryFolder |
41
|
|
|
* // ... |
42
|
|
|
* } |
43
|
|
|
* </pre> |
44
|
|
|
* <li>Parameter injection into a {@code @Test} method. The {@link TemporaryFolder} will be |
45
|
|
|
* destroyed during {@code @AfterEach} and no exception will be thrown in cases where the |
46
|
|
|
* deletion fails. For example: |
47
|
|
|
* <pre> |
48
|
|
|
* @Test |
49
|
|
|
* public void testUsingTemporaryFolder(TemporaryFolder temporaryFolder) { |
50
|
|
|
* // ... |
51
|
|
|
* } |
52
|
|
|
* </pre> |
53
|
|
|
* <li>Class variable injection using a {@code @BeforeAll} method. Note: in this case <b>all</b> |
54
|
|
|
* tests in the test case will share the same instance of the {@code TemporaryFolder}. The |
55
|
|
|
* {@link TemporaryFolder} will be destroyed after any {@code @AfterAll} method completes and |
56
|
|
|
* no exception will be thrown in cases where the deletion fails. For example: |
57
|
|
|
* <pre> |
58
|
|
|
* private static TemporaryFolder TEMPORARY_FOLDER; |
59
|
|
|
* |
60
|
|
|
* @BeforeAll |
61
|
|
|
* public static void setUp(TemporaryFolder givenTemporaryFolder) { |
62
|
|
|
* TEMPORARY_FOLDER = givenTemporaryFolder |
63
|
|
|
* // ... |
64
|
|
|
* } |
65
|
|
|
* </pre> |
66
|
|
|
* </ul> |
67
|
|
|
* |
68
|
|
|
* <p>Usage examples: |
69
|
|
|
* |
70
|
|
|
* <p>Injecting a {@code TemporaryFolder} in a {@code @BeforeEach} method: |
71
|
|
|
* |
72
|
|
|
* <pre> |
73
|
|
|
* @ExtendWith(TemporaryFolderExtension.class) |
74
|
|
|
* public class MyTest { |
75
|
|
|
* |
76
|
|
|
* private TemporaryFolder temporaryFolder; |
77
|
|
|
* |
78
|
|
|
* @BeforeEach |
79
|
|
|
* public void setUp(TemporaryFolder temporaryFolder) { |
80
|
|
|
* this.temporaryFolder = temporaryFolder |
81
|
|
|
* // ... |
82
|
|
|
* } |
83
|
|
|
* |
84
|
|
|
* @Test |
85
|
|
|
* public void testUsingTemporaryFile() { |
86
|
|
|
* File file = temporaryFolder.createFile("foo.txt"); |
87
|
|
|
* // ... |
88
|
|
|
* } |
89
|
|
|
* |
90
|
|
|
* @Test |
91
|
|
|
* public void testUsingTemporaryDirectory() { |
92
|
|
|
* // use the temporary folder itself |
93
|
|
|
* File root = temporaryFolder.getRoot(); |
94
|
|
|
* |
95
|
|
|
* // create a sub directory within the temporary folder |
96
|
|
|
* File file = temporaryFolder.createDirectory("foo"); |
97
|
|
|
* // ... |
98
|
|
|
* } |
99
|
|
|
* } |
100
|
|
|
* </pre> |
101
|
|
|
* |
102
|
|
|
* <p>Injecting a {@code TemporaryFolder} in a {@code @Test} method: |
103
|
|
|
* |
104
|
|
|
* <pre> |
105
|
|
|
* public class MyTest { |
106
|
|
|
* |
107
|
|
|
* @Test |
108
|
|
|
* @ExtendWith(TemporaryFolderExtension.class) |
109
|
|
|
* public void testUsingTemporaryFile(TemporaryFolder temporaryFolder) { |
110
|
|
|
* File file = temporaryFolder.createFile("foo.txt"); |
111
|
|
|
* // ... |
112
|
|
|
* } |
113
|
|
|
* |
114
|
|
|
* @Test |
115
|
|
|
* @ExtendWith(TemporaryFolderExtension.class) |
116
|
|
|
* public void testUsingTemporaryDirectory(TemporaryFolder temporaryFolder) { |
117
|
|
|
* // use the temporary folder itself |
118
|
|
|
* File root = temporaryFolder.getRoot(); |
119
|
|
|
* |
120
|
|
|
* // create a sub directory within the temporary folder |
121
|
|
|
* File file = temporaryFolder.createDirectory("foo"); |
122
|
|
|
* // ... |
123
|
|
|
* } |
124
|
|
|
* } |
125
|
|
|
* </pre> |
126
|
|
|
* |
127
|
|
|
* @see <a href="https://github.com/junit-team/junit4/wiki/Rules#temporaryfolder-rule">JUnit 4 |
128
|
|
|
* TemporaryFolder Rule</a> |
129
|
|
|
* @since 1.0.0 |
130
|
|
|
*/ |
131
|
|
|
public class TemporaryFolderExtension implements ParameterResolver { |
132
|
|
|
|
133
|
|
|
private static final Namespace NAMESPACE = Namespace.create(TemporaryFolderExtension.class); |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Does this extension support injection for parameters of the type described by the given {@code |
137
|
|
|
* parameterContext}? |
138
|
|
|
* |
139
|
|
|
* @param parameterContext the context for the parameter for which an argument should be resolved |
140
|
|
|
* @param extensionContext the <em>context</em> in which the current test or container is being |
141
|
|
|
* executed |
142
|
|
|
* @return true if the given {@code parameterContext} describes a parameter of type: {@link |
143
|
|
|
* TemporaryFolder}, false otherwise |
144
|
|
|
* @throws ParameterResolutionException |
145
|
|
|
*/ |
146
|
|
|
@Override |
147
|
|
|
public boolean supportsParameter( |
148
|
|
|
ParameterContext parameterContext, ExtensionContext extensionContext) |
149
|
|
|
throws ParameterResolutionException { |
150
|
|
|
return appliesTo(parameterContext.getParameter().getType()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Provides a value for any parameter context which has passed the {@link |
155
|
|
|
* #supportsParameter(ParameterContext, ExtensionContext)} gate. |
156
|
|
|
* |
157
|
|
|
* @param parameterContext the context for the parameter for which an argument should be resolved |
158
|
|
|
* @param extensionContext the <em>context</em> in which the current test or container is being |
159
|
|
|
* executed |
160
|
|
|
* @return a new {@link TemporaryFolder} |
161
|
|
|
* @throws ParameterResolutionException |
162
|
|
|
*/ |
163
|
|
|
@Override |
164
|
|
|
public Object resolveParameter( |
165
|
|
|
ParameterContext parameterContext, ExtensionContext extensionContext) |
166
|
|
|
throws ParameterResolutionException { |
167
|
|
|
return extensionContext |
168
|
|
|
.getStore(NAMESPACE) |
169
|
|
|
.getOrComputeIfAbsent( |
170
|
|
|
parameterContext, key -> new TemporaryFolder(), TemporaryFolder.class); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private boolean appliesTo(Class<?> clazz) { |
174
|
|
|
return clazz == TemporaryFolder.class; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|