Skip to content

Commit

Permalink
Add hasNoExtension to Path assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Syemon authored and joel-costigliola committed Sep 17, 2021
1 parent cd2ce82 commit 931b592
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/main/java/org/assertj/core/api/AbstractPathAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -1910,10 +1910,10 @@ private String readPath(Charset charset) {
* Example:
* <pre><code class='java'> Path path = Paths.get(&quot;file.java&quot;);
*
* // assertion succeeds:
* // assertion succeeds
* assertThat(path).hasExtension(&quot;java&quot;);
*
* // assertion fails:
* // assertion fails
* assertThat(path).hasExtension(&quot;png&quot;);</code></pre>
*
* @param expectedExtension the expected extension, without the {@code '.'}.
Expand All @@ -1929,4 +1929,25 @@ public SELF hasExtension(String expectedExtension) {
return myself;
}

/**
* Verifies that the actual {@code Path} has no extension.
*
* <p>
* Example:
* <pre><code class='java'> // assertion succeeds
* assertThat(Paths.get(&quot;file&quot;)).hasNoExtension();
*
* // assertion fails
* assertThat(Paths.get(&quot;file.txt&quot;)).hasNoExtension();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code Path} is {@code null}.
* @throws AssertionError if the actual {@code Path} is not a regular file.
* @throws AssertionError if the actual {@code Path} does have an extension.
*/
public SELF hasNoExtension() {
paths.assertHasNoExtension(info, actual);
return myself;
}

}
31 changes: 31 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldHaveNoExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2021 the original author or authors.
*/
package org.assertj.core.error;

import java.nio.file.Path;

/**
* Creates an error message indicating that a {@code Path} should have no extension.
*/
public class ShouldHaveNoExtension extends BasicErrorMessageFactory {

private static final String PATH_HAS_EXTENSION = "%nExpected actual path:%n %s%n not to have an extension, but extension was: %s";

public static ShouldHaveNoExtension shouldHaveNoExtension(Path actual, String extension) {
return new ShouldHaveNoExtension(actual, extension);
}

private ShouldHaveNoExtension(Path actual, String extension) {
super(PATH_HAS_EXTENSION, actual, extension);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/assertj/core/internal/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.assertj.core.error.ShouldHaveDigest.shouldHaveDigest;
import static org.assertj.core.error.ShouldHaveExtension.shouldHaveExtension;
import static org.assertj.core.error.ShouldHaveName.shouldHaveName;
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
import static org.assertj.core.error.ShouldHaveNoParent.shouldHaveNoParent;
import static org.assertj.core.error.ShouldHaveParent.shouldHaveParent;
import static org.assertj.core.error.ShouldHaveSameContent.shouldHaveSameContent;
Expand Down Expand Up @@ -468,6 +469,12 @@ public void assertHasExtension(AssertionInfo info, Path actual, String expected)
if (!expected.equals(extension)) throw failures.failure(info, shouldHaveExtension(actual, extension, expected));
}

public void assertHasNoExtension(AssertionInfo info, Path actual) {
assertIsRegularFile(info, actual);
Optional<String> extension = getExtension(actual);
if (extension.isPresent()) throw failures.failure(info, shouldHaveNoExtension(actual, extension.get()));
}

private static Optional<String> getExtension(Path path) {
String fileName = path.getFileName().toString();
int dotAt = fileName.lastIndexOf('.');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2021 the original author or authors.
*/
package org.assertj.core.api.path;

import static org.mockito.Mockito.verify;

import org.assertj.core.api.PathAssert;
import org.assertj.core.api.PathAssertBaseTest;

class PathAssert_hasNoExtension_Test extends PathAssertBaseTest {

@Override
protected PathAssert invoke_api_method() {
return assertions.hasNoExtension();
}

@Override
protected void verify_internal_effects() {
verify(paths).assertHasNoExtension(getInfo(assertions), getActual(assertions));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2021 the original author or authors.
*/
package org.assertj.core.error;

import static java.lang.String.format;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static org.mockito.Mockito.mock;

import java.nio.file.Path;

import org.assertj.core.internal.TestDescription;
import org.junit.jupiter.api.Test;

class ShouldHaveNoExtension_create_Test {

private static final TestDescription TEST_DESCRIPTION = new TestDescription("Test");

@Test
void should_create_error_message() {
// GIVEN
final Path path = mock(Path.class);
// WHEN
String actualMessage = shouldHaveNoExtension(path, "java").create(TEST_DESCRIPTION, STANDARD_REPRESENTATION);
// THEN
then(actualMessage).isEqualTo(format("[Test] %n" +
"Expected actual path:%n" +
" %s%n" +
" not to have an extension, but extension was: \"java\"", path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2021 the original author or authors.
*/
package org.assertj.core.internal.paths;

import static java.nio.file.Files.createDirectory;
import static java.nio.file.Files.createFile;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeRegularFile.shouldBeRegularFile;
import static org.assertj.core.error.ShouldExist.shouldExist;
import static org.assertj.core.error.ShouldHaveNoExtension.shouldHaveNoExtension;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.FailureMessages.actualIsNull;

import java.io.IOException;
import java.nio.file.Path;

import org.assertj.core.internal.PathsBaseTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class Paths_assertHasNoExtension_Test extends PathsBaseTest {

@Test
void should_fail_if_actual_is_null() {
// GIVEN
Path actual = null;
// WHEN
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
// THEN
then(error).hasMessage(actualIsNull());
}

@Test
void should_fail_if_actual_does_not_exist() {
// GIVEN
Path actual = tempDir.resolve("non-existent");
// WHEN
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
// THEN
then(error).hasMessage(shouldExist(actual).create());
}

@Test
void should_fail_if_actual_is_not_a_regular_file() throws IOException {
// GIVEN
Path actual = createDirectory(tempDir.resolve("directory"));
// WHEN
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
// THEN
then(error).hasMessage(shouldBeRegularFile(actual).create());
}

@Test
void should_fail_if_actual_has_extension() throws IOException {
// GIVEN
Path actual = createFile(tempDir.resolve("file.txt"));
// WHEN
AssertionError error = expectAssertionError(() -> paths.assertHasNoExtension(info, actual));
// THEN
then(error).hasMessage(shouldHaveNoExtension(actual, "txt").create());
}

@ParameterizedTest
@ValueSource(strings = { "file", "file." })
void should_pass_if_actual_has_no_extension(String filename) throws IOException {
// GIVEN
Path actual = createFile(tempDir.resolve(filename));
// WHEN/THEN
paths.assertHasNoExtension(info, actual);
}

}

0 comments on commit 931b592

Please sign in to comment.