Skip to content

Commit

Permalink
Add isNotEmpty assertion for File
Browse files Browse the repository at this point in the history
Fixes #1638.
  • Loading branch information
steveorourke authored and joel-costigliola committed Oct 24, 2019
1 parent b2eccd8 commit 1038d72
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 37 deletions.
51 changes: 51 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractFileAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,55 @@ public SELF isNotEmptyDirectory() {
files.assertIsNotEmptyDirectory(info, actual);
return myself;
}

/**
* Verify that the actual {@code File} is empty (i.e. size is equal to zero bytes).
* <p>
* Example:
* <pre><code class='java'> File file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
*
* // assertion will pass
* assertThat(file).isEmpty();
*
* Files.write(file.toPath(), new byte[]{1, 1});
*
* // assertion will fail
* assertThat(file).isEmpty();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code File} is {@code null}.
* @throws AssertionError if the actual {@code File} does not exist.
* @throws AssertionError if the actual {@code File} is not empty.
* @since 3.14.0
*/
public SELF isEmpty() {
files.assertIsEmptyFile(info, actual);
return myself;
}

/**
* Verify that the actual {@code File} is not empty (i.e. size is greater than zero bytes).
* <p>
* Example:
* <pre><code class='java'> File file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
* Files.write(file.toPath(), new byte[]{1, 1});
*
* // assertion will pass
* assertThat(file).isNotEmpty();
*
* file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
*
* // assertion will fail
* assertThat(file).isNotEmpty();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual {@code File} is {@code null}.
* @throws AssertionError if the actual {@code File} does not exist.
* @throws AssertionError if the actual {@code File} is empty.
* @since 3.14.0
*/
public SELF isNotEmpty() {
files.assertIsNotEmptyFile(info, actual);
return myself;
}
}
19 changes: 15 additions & 4 deletions src/main/java/org/assertj/core/error/ShouldBeEmpty.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/
package org.assertj.core.error;

import java.io.File;

/**
* Creates an error message indicating that an assertion that verifies a group of elements is empty failed. A group of elements
* can be a collection, an array or a {@code String}.
* can be a collection, an array, {@code String} or a {@code File}.
*
* @author Alex Ruiz
*/
Expand All @@ -26,10 +28,19 @@ public class ShouldBeEmpty extends BasicErrorMessageFactory {
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeEmpty(Object actual) {
return new ShouldBeEmpty(actual);
return new ShouldBeEmpty("%nExpecting empty but was:<%s>", actual);
}

/**
* Creates a new <code>{@link ShouldBeEmpty}</code>.
* @param actual the actual file in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeEmpty(File actual) {
return new ShouldBeEmpty("%nExpecting file <%s> to be empty", actual);
}

private ShouldBeEmpty(Object actual) {
super("%nExpecting empty but was:<%s>", actual);
private ShouldBeEmpty(String format, Object... arguments) {
super(format, arguments);
}
}
17 changes: 13 additions & 4 deletions src/main/java/org/assertj/core/error/ShouldNotBeEmpty.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
*/
package org.assertj.core.error;

import java.io.File;

/**
* Creates an error message indicating that an assertion that verifies a group of elements is not empty failed. A group of
* elements can be a collection, an array or a {@code String}.
* elements can be a collection, an array, {@code String} or a {@code File}.
*
* @author Alex Ruiz
*/
public class ShouldNotBeEmpty extends BasicErrorMessageFactory {

private static final ShouldNotBeEmpty INSTANCE = new ShouldNotBeEmpty();
private static final ShouldNotBeEmpty INSTANCE = new ShouldNotBeEmpty("%nExpecting actual not to be empty");

/**
* Returns the singleton instance of this class.
Expand All @@ -31,8 +32,16 @@ public static ErrorMessageFactory shouldNotBeEmpty() {
return INSTANCE;
}

private ShouldNotBeEmpty() {
super("%nExpecting actual not to be empty");
/**
* Creates a new <code>{@link ShouldNotBeEmpty}</code>.
* @param actual the actual file in the failed assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldNotBeEmpty(File actual) {
return new ShouldNotBeEmpty("%nExpecting file <%s> not to be empty", actual);
}

private ShouldNotBeEmpty(String format, Object... arguments) {
super(format, arguments);
}
}
41 changes: 35 additions & 6 deletions src/main/java/org/assertj/core/internal/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static java.util.stream.Collectors.toList;
import static org.assertj.core.error.ShouldBeAbsolutePath.shouldBeAbsolutePath;
import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory;
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.ShouldBeEmptyDirectory.shouldBeEmptyDirectory;
import static org.assertj.core.error.ShouldBeFile.shouldBeFile;
import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable;
Expand Down Expand Up @@ -275,18 +276,46 @@ public void assertDoesNotExist(AssertionInfo info, File actual) {
}

/**
* Asserts that the given file can be modified by the application.
* @param info contains information about the assertion.
* @param actual the given file.
* @throws AssertionError if the given file is {@code null}.
* @throws AssertionError if the given file can not be modified.
*/
* Asserts that the given file can be modified by the application.
* @param info contains information about the assertion.
* @param actual the given file.
* @throws AssertionError if the given file is {@code null}.
* @throws AssertionError if the given file can not be modified.
*/
public void assertCanWrite(AssertionInfo info, File actual) {
assertNotNull(info, actual);
if (actual.canWrite()) return;
throw failures.failure(info, shouldBeWritable(actual));
}

/**
* Asserts that the given {@code File} is empty (i.e. size is equal to zero bytes).
* @param info contains information about the assertion.
* @param actual the given file.
* @throws AssertionError if the given {@code File} is {@code null}.
* @throws AssertionError if the given {@code File} does not exist.
* @throws AssertionError if the given {@code File} is not empty.
*/
public void assertIsEmptyFile(AssertionInfo info, File actual) {
assertIsFile(info, actual);
if (actual.length() == 0) return;
throw failures.failure(info, shouldBeEmpty(actual));
}

/**
* Asserts that the given {@code File} is not empty (i.e. size is greater than zero bytes).
* @param info contains information about the assertion.
* @param actual the given file.
* @throws AssertionError if the given {@code File} is {@code null}.
* @throws AssertionError if the given {@code File} does not exist.
* @throws AssertionError if the given {@code File} is empty.
*/
public void assertIsNotEmptyFile(AssertionInfo info, File actual) {
assertIsFile(info, actual);
if (actual.length() > 0) return;
throw failures.failure(info, shouldNotBeEmpty(actual));
}

/**
* Asserts that the given file can be read by the application.
* @param info contains information about the assertion.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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-2019 the original author or authors.
*/
package org.assertj.core.api.file;

import org.assertj.core.api.FileAssert;
import org.assertj.core.api.FileAssertBaseTest;
import org.junit.jupiter.api.DisplayName;

import static org.mockito.Mockito.verify;

/**
* Tests for <code>{@link FileAssert#isEmpty()}</code>.
*/
@DisplayName("FileAssert isEmptyFile")
public class FileAssert_isEmptyFile_Test extends FileAssertBaseTest {

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

@Override
protected void verify_internal_effects() {
verify(files).assertIsEmptyFile(getInfo(assertions), getActual(assertions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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-2019 the original author or authors.
*/
package org.assertj.core.api.file;

import org.assertj.core.api.FileAssert;
import org.assertj.core.api.FileAssertBaseTest;
import org.junit.jupiter.api.DisplayName;

import static org.mockito.Mockito.verify;

/**
* Tests for <code>{@link FileAssert#isNotEmpty()}</code>.
*/
@DisplayName("FileAssert isNotEmptyFile")
public class FileAssert_isNotEmptyFile_Test extends FileAssertBaseTest {

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

@Override
protected void verify_internal_effects() {
verify(files).assertIsNotEmptyFile(getInfo(assertions), getActual(assertions));
}
}
40 changes: 25 additions & 15 deletions src/test/java/org/assertj/core/error/ShouldBeEmpty_create_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,44 @@
*/
package org.assertj.core.error;

import static org.assertj.core.api.Assertions.assertThat;
import static java.lang.String.format;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static org.assertj.core.util.Lists.newArrayList;

import org.assertj.core.description.Description;
import java.io.File;

import org.assertj.core.internal.TestDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link ShouldBeEmpty#create(Description, org.assertj.core.presentation.Representation)}</code>.
*
* Tests for <code>{@link ShouldBeEmpty#create((org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
*
* @author Alex Ruiz
* @author Yvonne Wang
*/
public class ShouldBeEmpty_create_Test {
@DisplayName("ShouldBeEmpty create")
class ShouldBeEmpty_create_Test {

private ErrorMessageFactory factory;

@BeforeEach
public void setUp() {
factory = shouldBeEmpty(newArrayList("Luke", "Yoda"));
@Test
void should_create_error_message() {
// GIVEN
ErrorMessageFactory factory = shouldBeEmpty(newArrayList("Luke", "Yoda"));
// WHEN
String message = factory.create(new TestDescription("Test"), STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[Test] %nExpecting empty but was:<[\"Luke\", \"Yoda\"]>"));
}

@Test
public void should_create_error_message() {
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
assertThat(message).isEqualTo(String.format("[Test] %nExpecting empty but was:<[\"Luke\", \"Yoda\"]>"));
void should_create_error_message_for_File() {
// GIVEN
ErrorMessageFactory factory = shouldBeEmpty(new File("/te%st.txt"));
// WHEN
String message = factory.create(new TestDescription("Test"), STANDARD_REPRESENTATION);
// THEN
then(message).isEqualTo(format("[Test] %nExpecting file </te%%st.txt> to be empty"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,44 @@
*/
package org.assertj.core.error;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty;

import org.assertj.core.internal.TestDescription;
import org.assertj.core.presentation.StandardRepresentation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;

/**
* Tests for <code>{@link ShouldNotBeEmpty#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
*
*
* @author Alex Ruiz
* @author Yvonne Wang
*/
public class ShouldNotBeEmpty_create_Test {
@DisplayName("ShouldNotBeEmpty create")
class ShouldNotBeEmpty_create_Test {

private ErrorMessageFactory factory;

@BeforeEach
public void setUp() {
@Test
void should_create_error_message() {
// GIVEN
factory = shouldNotBeEmpty();
// WHEN
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(String.format("[Test] %nExpecting actual not to be empty"));
}

@Test
public void should_create_error_message() {
void should_create_error_message_for_File() {
// GIVEN
factory = shouldNotBeEmpty(new File("/test.txt"));
// WHEN
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
assertThat(message).isEqualTo(String.format("[Test] %nExpecting actual not to be empty"));
// THEN
then(message).isEqualTo(String.format("[Test] %nExpecting file </test.txt> not to be empty"));
}
}
Loading

0 comments on commit 1038d72

Please sign in to comment.