Skip to content

Commit 931b592

Browse files
Syemonjoel-costigliola
authored andcommitted
Add hasNoExtension to Path assertions
1 parent cd2ce82 commit 931b592

File tree

6 files changed

+218
-2
lines changed

6 files changed

+218
-2
lines changed

src/main/java/org/assertj/core/api/AbstractPathAssert.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1910,10 +1910,10 @@ private String readPath(Charset charset) {
19101910
* Example:
19111911
* <pre><code class='java'> Path path = Paths.get(&quot;file.java&quot;);
19121912
*
1913-
* // assertion succeeds:
1913+
* // assertion succeeds
19141914
* assertThat(path).hasExtension(&quot;java&quot;);
19151915
*
1916-
* // assertion fails:
1916+
* // assertion fails
19171917
* assertThat(path).hasExtension(&quot;png&quot;);</code></pre>
19181918
*
19191919
* @param expectedExtension the expected extension, without the {@code '.'}.
@@ -1929,4 +1929,25 @@ public SELF hasExtension(String expectedExtension) {
19291929
return myself;
19301930
}
19311931

1932+
/**
1933+
* Verifies that the actual {@code Path} has no extension.
1934+
*
1935+
* <p>
1936+
* Example:
1937+
* <pre><code class='java'> // assertion succeeds
1938+
* assertThat(Paths.get(&quot;file&quot;)).hasNoExtension();
1939+
*
1940+
* // assertion fails
1941+
* assertThat(Paths.get(&quot;file.txt&quot;)).hasNoExtension();</code></pre>
1942+
*
1943+
* @return {@code this} assertion object.
1944+
* @throws AssertionError if the actual {@code Path} is {@code null}.
1945+
* @throws AssertionError if the actual {@code Path} is not a regular file.
1946+
* @throws AssertionError if the actual {@code Path} does have an extension.
1947+
*/
1948+
public SELF hasNoExtension() {
1949+
paths.assertHasNoExtension(info, actual);
1950+
return myself;
1951+
}
1952+
19321953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2021 the original author or authors.
12+
*/
13+
package org.assertj.core.error;
14+
15+
import java.nio.file.Path;
16+
17+
/**
18+
* Creates an error message indicating that a {@code Path} should have no extension.
19+
*/
20+
public class ShouldHaveNoExtension extends BasicErrorMessageFactory {
21+
22+
private static final String PATH_HAS_EXTENSION = "%nExpected actual path:%n %s%n not to have an extension, but extension was: %s";
23+
24+
public static ShouldHaveNoExtension shouldHaveNoExtension(Path actual, String extension) {
25+
return new ShouldHaveNoExtension(actual, extension);
26+
}
27+
28+
private ShouldHaveNoExtension(Path actual, String extension) {
29+
super(PATH_HAS_EXTENSION, actual, extension);
30+
}
31+
}

src/main/java/org/assertj/core/internal/Paths.java

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest;
4141
import static org.assertj.core.error.ShouldHaveExtension.shouldHaveExtension;
4242
import static org.assertj.core.error.ShouldHaveName.shouldHaveName;
43+
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
4344
import static org.assertj.core.error.ShouldHaveNoParent.shouldHaveNoParent;
4445
import static org.assertj.core.error.ShouldHaveParent.shouldHaveParent;
4546
import static org.assertj.core.error.ShouldHaveSameContent.shouldHaveSameContent;
@@ -468,6 +469,12 @@ public void assertHasExtension(AssertionInfo info, Path actual, String expected)
468469
if (!expected.equals(extension)) throw failures.failure(info, shouldHaveExtension(actual, extension, expected));
469470
}
470471

472+
public void assertHasNoExtension(AssertionInfo info, Path actual) {
473+
assertIsRegularFile(info, actual);
474+
Optional<String> extension = getExtension(actual);
475+
if (extension.isPresent()) throw failures.failure(info, shouldHaveNoExtension(actual, extension.get()));
476+
}
477+
471478
private static Optional<String> getExtension(Path path) {
472479
String fileName = path.getFileName().toString();
473480
int dotAt = fileName.lastIndexOf('.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2021 the original author or authors.
12+
*/
13+
package org.assertj.core.api.path;
14+
15+
import static org.mockito.Mockito.verify;
16+
17+
import org.assertj.core.api.PathAssert;
18+
import org.assertj.core.api.PathAssertBaseTest;
19+
20+
class PathAssert_hasNoExtension_Test extends PathAssertBaseTest {
21+
22+
@Override
23+
protected PathAssert invoke_api_method() {
24+
return assertions.hasNoExtension();
25+
}
26+
27+
@Override
28+
protected void verify_internal_effects() {
29+
verify(paths).assertHasNoExtension(getInfo(assertions), getActual(assertions));
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2021 the original author or authors.
12+
*/
13+
package org.assertj.core.error;
14+
15+
import static java.lang.String.format;
16+
import static org.assertj.core.api.BDDAssertions.then;
17+
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
18+
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
19+
import static org.mockito.Mockito.mock;
20+
21+
import java.nio.file.Path;
22+
23+
import org.assertj.core.internal.TestDescription;
24+
import org.junit.jupiter.api.Test;
25+
26+
class ShouldHaveNoExtension_create_Test {
27+
28+
private static final TestDescription TEST_DESCRIPTION = new TestDescription("Test");
29+
30+
@Test
31+
void should_create_error_message() {
32+
// GIVEN
33+
final Path path = mock(Path.class);
34+
// WHEN
35+
String actualMessage = shouldHaveNoExtension(path, "java").create(TEST_DESCRIPTION, STANDARD_REPRESENTATION);
36+
// THEN
37+
then(actualMessage).isEqualTo(format("[Test] %n" +
38+
"Expected actual path:%n" +
39+
" %s%n" +
40+
" not to have an extension, but extension was: \"java\"", path));
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2021 the original author or authors.
12+
*/
13+
package org.assertj.core.internal.paths;
14+
15+
import static java.nio.file.Files.createDirectory;
16+
import static java.nio.file.Files.createFile;
17+
import static org.assertj.core.api.BDDAssertions.then;
18+
import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile;
19+
import static org.assertj.core.error.ShouldExist.shouldExist;
20+
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
21+
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
22+
import static org.assertj.core.util.FailureMessages.actualIsNull;
23+
24+
import java.io.IOException;
25+
import java.nio.file.Path;
26+
27+
import org.assertj.core.internal.PathsBaseTest;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.ValueSource;
31+
32+
class Paths_assertHasNoExtension_Test extends PathsBaseTest {
33+
34+
@Test
35+
void should_fail_if_actual_is_null() {
36+
// GIVEN
37+
Path actual = null;
38+
// WHEN
39+
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
40+
// THEN
41+
then(error).hasMessage(actualIsNull());
42+
}
43+
44+
@Test
45+
void should_fail_if_actual_does_not_exist() {
46+
// GIVEN
47+
Path actual = tempDir.resolve("non-existent");
48+
// WHEN
49+
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
50+
// THEN
51+
then(error).hasMessage(shouldExist(actual).create());
52+
}
53+
54+
@Test
55+
void should_fail_if_actual_is_not_a_regular_file() throws IOException {
56+
// GIVEN
57+
Path actual = createDirectory(tempDir.resolve("directory"));
58+
// WHEN
59+
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
60+
// THEN
61+
then(error).hasMessage(shouldBeRegularFile(actual).create());
62+
}
63+
64+
@Test
65+
void should_fail_if_actual_has_extension() throws IOException {
66+
// GIVEN
67+
Path actual = createFile(tempDir.resolve("file.txt"));
68+
// WHEN
69+
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
70+
// THEN
71+
then(error).hasMessage(shouldHaveNoExtension(actual, "txt").create());
72+
}
73+
74+
@ParameterizedTest
75+
@ValueSource(strings = { "file", "file." })
76+
void should_pass_if_actual_has_no_extension(String filename) throws IOException {
77+
// GIVEN
78+
Path actual = createFile(tempDir.resolve(filename));
79+
// WHEN/THEN
80+
paths.assertHasNoExtension(info, actual);
81+
}
82+
83+
}

0 commit comments

Comments
 (0)