Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
TestCase | 35 | 20 | 0% | 19 | 18 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.devacfr.testing.jupiter; | |
20 | ||
21 | import javax.annotation.Nonnull; | |
22 | import javax.annotation.Nullable; | |
23 | ||
24 | import java.nio.file.Path; | |
25 | import java.nio.file.Paths; | |
26 | import java.util.function.Function; | |
27 | ||
28 | import com.google.common.io.ByteSource; | |
29 | import com.google.common.io.Resources; | |
30 | import org.devacfr.testing.util.Approvals; | |
31 | import org.junit.jupiter.api.Assertions; | |
32 | import org.junit.jupiter.api.BeforeEach; | |
33 | import org.junit.jupiter.api.TestInfo; | |
34 | ||
35 | public class TestCase extends Assertions { | |
36 | ||
37 | private TestInfo testInfo; | |
38 | ||
39 | 66 | @BeforeEach |
40 | public final void beforeEachMethod(final TestInfo testInfo) { | |
41 | 66 | this.testInfo = testInfo; |
42 | } | |
43 | ||
44 | 0 | public String getName() { |
45 | 0 | return this.getClass().getSimpleName(); |
46 | } | |
47 | ||
48 | /** | |
49 | * Gets the file system path representation of this test class. | |
50 | * | |
51 | * @return Returns {@code String} representing the file system location path of this test class. | |
52 | */ | |
53 | 42 | @Nonnull |
54 | public final Path getPackagePath() { | |
55 | 42 | return Paths.get(getPackagePath(this.getClass())); |
56 | } | |
57 | ||
58 | 6 | public ByteSource getResource(final String filename) { |
59 | 6 | return Resources.asByteSource(Resources.getResource(getPackagePath().resolve(filename).toString())); |
60 | } | |
61 | ||
62 | /** | |
63 | * Gets the file system path representation of this test class. | |
64 | * | |
65 | * @param testClass | |
66 | * test class to use. | |
67 | * @return Returns {@code String} representing the file system location path of this test class. | |
68 | */ | |
69 | 42 | @Nonnull |
70 | public static final String getPackagePath(@Nonnull final Class<?> testClass) { | |
71 | 42 | return testClass.getPackage().getName().replace('.', '/'); |
72 | } | |
73 | ||
74 | 7 | public String getActualResource() { |
75 | 7 | return Approvals.getActualResource(getPackagePath(), this.getClass(), getMethodName(), null); |
76 | } | |
77 | ||
78 | 0 | public String getExpectedResource() { |
79 | 0 | return Approvals.getExpectedResource(getPackagePath(), this.getClass(), getMethodName(), null, null); |
80 | } | |
81 | ||
82 | 2 | public String getActualResource(final String extension) { |
83 | 2 | return Approvals.getActualResource(getPackagePath(), this.getClass(), getMethodName(), extension); |
84 | } | |
85 | ||
86 | 1 | public String getExpectedResource(final String extension) { |
87 | 1 | return Approvals.getExpectedResource(getPackagePath(), this.getClass(), getMethodName(), extension, null); |
88 | } | |
89 | ||
90 | 0 | public String getExpectedResource(final String extension, final Function<String, String> transformer) { |
91 | 0 | return Approvals |
92 | .getExpectedResource(getPackagePath(), this.getClass(), getMethodName(), extension, transformer); | |
93 | } | |
94 | ||
95 | 0 | public void verify() { |
96 | 0 | Approvals.verify(getPackagePath(), this.getClass(), getMethodName(), (Function<String, String>) null, null); |
97 | } | |
98 | ||
99 | /** | |
100 | * @param extension | |
101 | * the extension file. | |
102 | */ | |
103 | 0 | public void verify(final String extension) { |
104 | 0 | Approvals |
105 | .verify(getPackagePath(), this.getClass(), getMethodName(), (Function<String, String>) null, extension); | |
106 | } | |
107 | ||
108 | 0 | public void verify(final Path actualFile, final Path expectedFile) { |
109 | 0 | Approvals.verify(actualFile, expectedFile); |
110 | } | |
111 | ||
112 | 1 | public void verify(final Function<String, String> transform) { |
113 | 1 | Approvals.verify(getPackagePath(), this.getClass(), getMethodName(), transform, null); |
114 | } | |
115 | ||
116 | /** | |
117 | * @param transform | |
118 | * @param extension | |
119 | * the extension file. | |
120 | */ | |
121 | 19 | public void verify(final Function<String, String> transform, @Nullable final String extension) { |
122 | 19 | Approvals.verify(getPackagePath(), this.getClass(), getMethodName(), transform, extension); |
123 | } | |
124 | ||
125 | /** | |
126 | * @param actual | |
127 | * the actual value to test. | |
128 | * @param extension | |
129 | * the extension file. | |
130 | */ | |
131 | 0 | public void verify(@Nullable final String actual, @Nullable final String extension) { |
132 | 0 | Approvals.verify(getPackagePath(), this.getClass(), getMethodName(), actual, extension); |
133 | } | |
134 | ||
135 | /** | |
136 | * @param actualFile | |
137 | * the actual value stored in file to test. | |
138 | * @param extension | |
139 | * the extension file. | |
140 | */ | |
141 | 0 | public void verify(@Nonnull final Path actualFile, @Nullable final String extension) { |
142 | 0 | Approvals.verify(getPackagePath(), this.getClass(), getMethodName(), actualFile, extension); |
143 | } | |
144 | ||
145 | 30 | private String getMethodName() { |
146 | 30 | if (testInfo == null) { |
147 | 0 | return null; |
148 | } | |
149 | 30 | return this.testInfo.getTestMethod().get().getName(); |
150 | } | |
151 | } |