1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils.annotations;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.lang.annotation.Documented;
|
---|
7 | import java.lang.annotation.ElementType;
|
---|
8 | import java.lang.annotation.Retention;
|
---|
9 | import java.lang.annotation.RetentionPolicy;
|
---|
10 | import java.lang.annotation.Target;
|
---|
11 | import java.nio.file.FileVisitResult;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.Path;
|
---|
14 | import java.nio.file.SimpleFileVisitor;
|
---|
15 | import java.nio.file.attribute.BasicFileAttributes;
|
---|
16 | import java.util.UUID;
|
---|
17 |
|
---|
18 | import org.junit.jupiter.api.extension.AfterAllCallback;
|
---|
19 | import org.junit.jupiter.api.extension.BeforeAllCallback;
|
---|
20 | import org.junit.jupiter.api.extension.ExtendWith;
|
---|
21 | import org.junit.jupiter.api.extension.ExtensionContext;
|
---|
22 | import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
---|
23 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
|
---|
24 | import 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)
|
---|
37 | public @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 | }
|
---|