Skip to content

Commit

Permalink
Add hasNoExtension to File assertions.
Browse files Browse the repository at this point in the history
Commonize how to get a file/path extension.
  • Loading branch information
joel-costigliola committed Sep 18, 2021
1 parent 5bef9b2 commit a7ef096
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 46 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractFileAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,28 @@ public AbstractStringAssert<?> content(Charset charset) {
return internalContent(charset);
}

/**
* Verifies that the actual {@code File} has no extension.
*
* <p>
* Example:
* <pre><code class='java'> // assertions succeed
* assertThat(new File(&quot;file&quot;)).hasNoExtension();
* assertThat(new File(&quot;file.&quot;)).hasNoExtension();
*
* // assertion fails
* assertThat(new File(&quot;file.txt&quot;)).hasNoExtension();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code File} is {@code null}.
* @throws AssertionError if the actual {@code File} is not a file (ie a directory or does not exist).
* @throws AssertionError if the actual {@code File} does have an extension.
*/
public SELF hasNoExtension() {
files.assertHasNoExtension(info, actual);
return myself;
}

// this method was introduced to avoid to avoid double proxying in soft assertions for content()
private AbstractStringAssert<?> internalContent(Charset charset) {
files.assertCanRead(info, actual);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/assertj/core/api/AbstractPathAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -1934,8 +1934,9 @@ public SELF hasExtension(String expectedExtension) {
*
* <p>
* Example:
* <pre><code class='java'> // assertion succeeds
* <pre><code class='java'> // assertions succeed
* assertThat(Paths.get(&quot;file&quot;)).hasNoExtension();
* assertThat(Paths.get(&quot;file.&quot;)).hasNoExtension();
*
* // assertion fails
* assertThat(Paths.get(&quot;file.txt&quot;)).hasNoExtension();</code></pre>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldHaveExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static ShouldHaveExtension shouldHaveExtension(Path actual, String expect
return new ShouldHaveExtension(actual, expectedExtension);
}

public static ShouldHaveExtension shouldHaveExtension(File actual, String expectedExtension) {
return new ShouldHaveExtension(actual, expectedExtension);
}

private ShouldHaveExtension(Object actual, String expectedExtension) {
super("%nExpecting%n %s%nto have extension:%n %s%nbut had no extension.", actual, expectedExtension);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/assertj/core/error/ShouldHaveNoExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.assertj.core.error;

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

/**
Expand All @@ -20,12 +21,21 @@
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";
private static final String FILE_HAS_EXTENSION = "%nExpected actual file:%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);
}

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

private ShouldHaveNoExtension(Path actual, String extension) {
super(PATH_HAS_EXTENSION, actual, extension);
}

private ShouldHaveNoExtension(File actual, String extension) {
super(FILE_HAS_EXTENSION, actual, extension);
}
}
20 changes: 13 additions & 7 deletions src/main/java/org/assertj/core/internal/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 All @@ -41,6 +42,7 @@
import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain;
import static org.assertj.core.error.ShouldNotExist.shouldNotExist;
import static org.assertj.core.internal.Digests.digestDiff;
import static org.assertj.core.util.Files.getFileNameExtension;
import static org.assertj.core.util.Lists.list;
import static org.assertj.core.util.Preconditions.checkArgument;

Expand All @@ -56,6 +58,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -395,9 +398,14 @@ public void assertHasParent(AssertionInfo info, File actual, File expected) {
public void assertHasExtension(AssertionInfo info, File actual, String expected) {
requireNonNull(expected, "The expected extension should not be null.");
assertIsFile(info, actual);
String actualExtension = getFileExtension(actual);
if (expected.equals(actualExtension)) return;
throw failures.failure(info, shouldHaveExtension(actual, actualExtension, expected));
String extension = getFileExtension(actual).orElseThrow(() -> failures.failure(info, shouldHaveExtension(actual, expected)));
if (!expected.equals(extension)) throw failures.failure(info, shouldHaveExtension(actual, extension, expected));
}

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

/**
Expand Down Expand Up @@ -576,10 +584,8 @@ private static void assertNotNull(AssertionInfo info, File actual) {
Objects.instance().assertNotNull(info, actual);
}

private String getFileExtension(File file) {
String name = file.getName();
int dotAt = name.lastIndexOf('.');
return dotAt == -1 ? null : name.substring(dotAt + 1);
private Optional<String> getFileExtension(File file) {
return getFileNameExtension(file.getName());
}

private void verifyIsFile(File expected) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/assertj/core/internal/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.assertj.core.error.ShouldNotContain.directoryShouldNotContain;
import static org.assertj.core.error.ShouldNotExist.shouldNotExist;
import static org.assertj.core.error.ShouldStartWithPath.shouldStartWith;
import static org.assertj.core.util.Files.getFileNameExtension;
import static org.assertj.core.util.Preconditions.checkArgument;

import java.io.IOException;
Expand Down Expand Up @@ -477,10 +478,7 @@ public void assertHasNoExtension(AssertionInfo info, Path actual) {

private static Optional<String> getExtension(Path path) {
String fileName = path.getFileName().toString();
int dotAt = fileName.lastIndexOf('.');
if (dotAt == -1) return Optional.empty();
String extension = fileName.substring(dotAt + 1);
return extension.equals("") ? Optional.empty() : Optional.of(extension);
return getFileNameExtension(fileName);
}

}
11 changes: 10 additions & 1 deletion src/main/java/org/assertj/core/util/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
Expand All @@ -35,6 +36,9 @@
* @author Alex Ruiz
*/
public class Files {

private Files() {}

/**
* Returns the names of the files inside the specified directory.
*
Expand Down Expand Up @@ -311,6 +315,11 @@ private static void checkArgumentCharsetIsSupported(String charsetName) {
checkArgument(Charset.isSupported(charsetName), "Charset:<'%s'> is not supported on this system", charsetName);
}

private Files() {}
public static Optional<String> getFileNameExtension(String fileName) {
int dotAt = fileName.lastIndexOf('.');
if (dotAt == -1) return Optional.empty();
String extension = fileName.substring(dotAt + 1);
return extension.equals("") ? Optional.empty() : Optional.of(extension);
}

}
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.file;

import static org.mockito.Mockito.verify;

import org.assertj.core.api.FileAssert;
import org.assertj.core.api.FileAssertBaseTest;

class FileAssert_hasNoExtension_Test extends FileAssertBaseTest {

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static org.mockito.Mockito.mock;

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

import org.assertj.core.internal.TestDescription;
Expand All @@ -28,7 +29,7 @@ class ShouldHaveNoExtension_create_Test {
private static final TestDescription TEST_DESCRIPTION = new TestDescription("Test");

@Test
void should_create_error_message() {
void should_create_error_message_for_path() {
// GIVEN
final Path path = mock(Path.class);
// WHEN
Expand All @@ -37,6 +38,21 @@ void should_create_error_message() {
then(actualMessage).isEqualTo(format("[Test] %n" +
"Expected actual path:%n" +
" %s%n" +
" not to have an extension, but extension was: \"java\"", path));
" not to have an extension, but extension was: \"java\"",
STANDARD_REPRESENTATION.toStringOf(path)));
}

@Test
void should_create_error_message_for_file() {
// GIVEN
final File file = new File("foo.java");
// WHEN
String actualMessage = shouldHaveNoExtension(file, "java").create(TEST_DESCRIPTION, STANDARD_REPRESENTATION);
// THEN
then(actualMessage).isEqualTo(format("[Test] %n" +
"Expected actual file:%n" +
" %s%n" +
" not to have an extension, but extension was: \"java\"",
STANDARD_REPRESENTATION.toStringOf(file)));
}
}
4 changes: 4 additions & 0 deletions src/test/java/org/assertj/core/internal/FilesBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.assertj.core.api.AssertionInfo;
import org.assertj.core.util.diff.Delta;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;

/**
* Base class for testing <code>{@link Files}</code>, set up diff and failures attributes (which is why it is in
Expand All @@ -46,6 +47,8 @@ public class FilesBaseTest {

protected static final AssertionInfo INFO = someInfo();

@TempDir
protected File tempDir;
protected File actual;
protected Failures failures;
protected Files files;
Expand All @@ -55,6 +58,7 @@ public class FilesBaseTest {
protected BinaryDiff binaryDiff;
protected NioFilesWrapper nioFilesWrapper;

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() {
actual = mock(File.class);
Expand Down
Loading

0 comments on commit a7ef096

Please sign in to comment.