source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/annotations/JosmHome.java@ 18037

Last change on this file since 18037 was 18037, checked in by Don-vip, 3 years ago

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils.annotations;
3
4import java.io.File;
5import java.io.IOException;
6import java.lang.annotation.Documented;
7import java.lang.annotation.ElementType;
8import java.lang.annotation.Retention;
9import java.lang.annotation.RetentionPolicy;
10import java.lang.annotation.Target;
11import java.nio.file.FileVisitResult;
12import java.nio.file.Files;
13import java.nio.file.Path;
14import java.nio.file.SimpleFileVisitor;
15import java.nio.file.attribute.BasicFileAttributes;
16import java.util.UUID;
17
18import org.junit.jupiter.api.extension.AfterAllCallback;
19import org.junit.jupiter.api.extension.BeforeAllCallback;
20import org.junit.jupiter.api.extension.ExtendWith;
21import org.junit.jupiter.api.extension.ExtensionContext;
22import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
23import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25
26/**
27 * Use the JOSM home directory. See {@link JOSMTestRules}.
28 * Typically only used by {@link FullPreferences}.
29 *
30 * @author Taylor Smock
31 * @since 18037
32 */
33@Documented
34@Retention(RetentionPolicy.RUNTIME)
35@Target(ElementType.TYPE)
36@ExtendWith(JosmHome.JosmHomeExtension.class)
37public @interface JosmHome {
38 /**
39 * Create a JOSM home directory. Prefer using {@link JosmHome}.
40 * @author Taylor Smock
41 */
42 class JosmHomeExtension implements BeforeAllCallback, AfterAllCallback {
43 @Override
44 public void afterAll(ExtensionContext context) throws Exception {
45 Path tempDir = context.getStore(Namespace.create(JosmHome.class)).get("home", Path.class);
46 Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
47 @Override
48 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
49 Files.delete(dir);
50 return FileVisitResult.CONTINUE;
51 }
52
53 @Override
54 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
55 Files.delete(file);
56 return FileVisitResult.CONTINUE;
57 }
58 });
59 }
60
61 @Override
62 public void beforeAll(ExtensionContext context) throws Exception {
63 Path tempDir = Files.createTempDirectory(UUID.randomUUID().toString());
64 context.getStore(Namespace.create(JosmHome.class)).put("home", tempDir);
65 File home = tempDir.toFile();
66 System.setProperty("josm.home", home.getAbsolutePath());
67 JosmBaseDirectories.getInstance().clearMemos();
68 }
69 }
70}
Note: See TracBrowser for help on using the repository browser.