Skip to content

Commit 1038d72

Browse files
steveorourkejoel-costigliola
authored andcommitted
Add isNotEmpty assertion for File
Fixes #1638.
1 parent b2eccd8 commit 1038d72

10 files changed

+391
-37
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,4 +990,55 @@ public SELF isNotEmptyDirectory() {
990990
files.assertIsNotEmptyDirectory(info, actual);
991991
return myself;
992992
}
993+
994+
/**
995+
* Verify that the actual {@code File} is empty (i.e. size is equal to zero bytes).
996+
* <p>
997+
* Example:
998+
* <pre><code class='java'> File file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
999+
*
1000+
* // assertion will pass
1001+
* assertThat(file).isEmpty();
1002+
*
1003+
* Files.write(file.toPath(), new byte[]{1, 1});
1004+
*
1005+
* // assertion will fail
1006+
* assertThat(file).isEmpty();</code></pre>
1007+
*
1008+
* @return {@code this} assertion object.
1009+
* @throws AssertionError if the actual {@code File} is {@code null}.
1010+
* @throws AssertionError if the actual {@code File} does not exist.
1011+
* @throws AssertionError if the actual {@code File} is not empty.
1012+
* @since 3.14.0
1013+
*/
1014+
public SELF isEmpty() {
1015+
files.assertIsEmptyFile(info, actual);
1016+
return myself;
1017+
}
1018+
1019+
/**
1020+
* Verify that the actual {@code File} is not empty (i.e. size is greater than zero bytes).
1021+
* <p>
1022+
* Example:
1023+
* <pre><code class='java'> File file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
1024+
* Files.write(file.toPath(), new byte[]{1, 1});
1025+
*
1026+
* // assertion will pass
1027+
* assertThat(file).isNotEmpty();
1028+
*
1029+
* file = File.createTempFile(&quot;tmp&quot;, &quot;txt&quot;);
1030+
*
1031+
* // assertion will fail
1032+
* assertThat(file).isNotEmpty();</code></pre>
1033+
*
1034+
* @return {@code this} assertion object.
1035+
* @throws AssertionError if the actual {@code File} is {@code null}.
1036+
* @throws AssertionError if the actual {@code File} does not exist.
1037+
* @throws AssertionError if the actual {@code File} is empty.
1038+
* @since 3.14.0
1039+
*/
1040+
public SELF isNotEmpty() {
1041+
files.assertIsNotEmptyFile(info, actual);
1042+
return myself;
1043+
}
9931044
}

src/main/java/org/assertj/core/error/ShouldBeEmpty.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
*/
1313
package org.assertj.core.error;
1414

15+
import java.io.File;
16+
1517
/**
1618
* Creates an error message indicating that an assertion that verifies a group of elements is empty failed. A group of elements
17-
* can be a collection, an array or a {@code String}.
19+
* can be a collection, an array, {@code String} or a {@code File}.
1820
*
1921
* @author Alex Ruiz
2022
*/
@@ -26,10 +28,19 @@ public class ShouldBeEmpty extends BasicErrorMessageFactory {
2628
* @return the created {@code ErrorMessageFactory}.
2729
*/
2830
public static ErrorMessageFactory shouldBeEmpty(Object actual) {
29-
return new ShouldBeEmpty(actual);
31+
return new ShouldBeEmpty("%nExpecting empty but was:<%s>", actual);
32+
}
33+
34+
/**
35+
* Creates a new <code>{@link ShouldBeEmpty}</code>.
36+
* @param actual the actual file in the failed assertion.
37+
* @return the created {@code ErrorMessageFactory}.
38+
*/
39+
public static ErrorMessageFactory shouldBeEmpty(File actual) {
40+
return new ShouldBeEmpty("%nExpecting file <%s> to be empty", actual);
3041
}
3142

32-
private ShouldBeEmpty(Object actual) {
33-
super("%nExpecting empty but was:<%s>", actual);
43+
private ShouldBeEmpty(String format, Object... arguments) {
44+
super(format, arguments);
3445
}
3546
}

src/main/java/org/assertj/core/error/ShouldNotBeEmpty.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
*/
1313
package org.assertj.core.error;
1414

15+
import java.io.File;
1516

1617
/**
1718
* Creates an error message indicating that an assertion that verifies a group of elements is not empty failed. A group of
18-
* elements can be a collection, an array or a {@code String}.
19+
* elements can be a collection, an array, {@code String} or a {@code File}.
1920
*
2021
* @author Alex Ruiz
2122
*/
2223
public class ShouldNotBeEmpty extends BasicErrorMessageFactory {
2324

24-
private static final ShouldNotBeEmpty INSTANCE = new ShouldNotBeEmpty();
25+
private static final ShouldNotBeEmpty INSTANCE = new ShouldNotBeEmpty("%nExpecting actual not to be empty");
2526

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

34-
private ShouldNotBeEmpty() {
35-
super("%nExpecting actual not to be empty");
35+
/**
36+
* Creates a new <code>{@link ShouldNotBeEmpty}</code>.
37+
* @param actual the actual file in the failed assertion.
38+
* @return the created {@code ErrorMessageFactory}.
39+
*/
40+
public static ErrorMessageFactory shouldNotBeEmpty(File actual) {
41+
return new ShouldNotBeEmpty("%nExpecting file <%s> not to be empty", actual);
3642
}
3743

44+
private ShouldNotBeEmpty(String format, Object... arguments) {
45+
super(format, arguments);
46+
}
3847
}

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static java.util.stream.Collectors.toList;
1818
import static org.assertj.core.error.ShouldBeAbsolutePath.shouldBeAbsolutePath;
1919
import static org.assertj.core.error.ShouldBeDirectory.shouldBeDirectory;
20+
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
2021
import static org.assertj.core.error.ShouldBeEmptyDirectory.shouldBeEmptyDirectory;
2122
import static org.assertj.core.error.ShouldBeFile.shouldBeFile;
2223
import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable;
@@ -275,18 +276,46 @@ public void assertDoesNotExist(AssertionInfo info, File actual) {
275276
}
276277

277278
/**
278-
* Asserts that the given file can be modified by the application.
279-
* @param info contains information about the assertion.
280-
* @param actual the given file.
281-
* @throws AssertionError if the given file is {@code null}.
282-
* @throws AssertionError if the given file can not be modified.
283-
*/
279+
* Asserts that the given file can be modified by the application.
280+
* @param info contains information about the assertion.
281+
* @param actual the given file.
282+
* @throws AssertionError if the given file is {@code null}.
283+
* @throws AssertionError if the given file can not be modified.
284+
*/
284285
public void assertCanWrite(AssertionInfo info, File actual) {
285286
assertNotNull(info, actual);
286287
if (actual.canWrite()) return;
287288
throw failures.failure(info, shouldBeWritable(actual));
288289
}
289290

291+
/**
292+
* Asserts that the given {@code File} is empty (i.e. size is equal to zero bytes).
293+
* @param info contains information about the assertion.
294+
* @param actual the given file.
295+
* @throws AssertionError if the given {@code File} is {@code null}.
296+
* @throws AssertionError if the given {@code File} does not exist.
297+
* @throws AssertionError if the given {@code File} is not empty.
298+
*/
299+
public void assertIsEmptyFile(AssertionInfo info, File actual) {
300+
assertIsFile(info, actual);
301+
if (actual.length() == 0) return;
302+
throw failures.failure(info, shouldBeEmpty(actual));
303+
}
304+
305+
/**
306+
* Asserts that the given {@code File} is not empty (i.e. size is greater than zero bytes).
307+
* @param info contains information about the assertion.
308+
* @param actual the given file.
309+
* @throws AssertionError if the given {@code File} is {@code null}.
310+
* @throws AssertionError if the given {@code File} does not exist.
311+
* @throws AssertionError if the given {@code File} is empty.
312+
*/
313+
public void assertIsNotEmptyFile(AssertionInfo info, File actual) {
314+
assertIsFile(info, actual);
315+
if (actual.length() > 0) return;
316+
throw failures.failure(info, shouldNotBeEmpty(actual));
317+
}
318+
290319
/**
291320
* Asserts that the given file can be read by the application.
292321
* @param info contains information about the assertion.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.api.file;
14+
15+
import org.assertj.core.api.FileAssert;
16+
import org.assertj.core.api.FileAssertBaseTest;
17+
import org.junit.jupiter.api.DisplayName;
18+
19+
import static org.mockito.Mockito.verify;
20+
21+
/**
22+
* Tests for <code>{@link FileAssert#isEmpty()}</code>.
23+
*/
24+
@DisplayName("FileAssert isEmptyFile")
25+
public class FileAssert_isEmptyFile_Test extends FileAssertBaseTest {
26+
27+
@Override
28+
protected FileAssert invoke_api_method() {
29+
return assertions.isEmpty();
30+
}
31+
32+
@Override
33+
protected void verify_internal_effects() {
34+
verify(files).assertIsEmptyFile(getInfo(assertions), getActual(assertions));
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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-2019 the original author or authors.
12+
*/
13+
package org.assertj.core.api.file;
14+
15+
import org.assertj.core.api.FileAssert;
16+
import org.assertj.core.api.FileAssertBaseTest;
17+
import org.junit.jupiter.api.DisplayName;
18+
19+
import static org.mockito.Mockito.verify;
20+
21+
/**
22+
* Tests for <code>{@link FileAssert#isNotEmpty()}</code>.
23+
*/
24+
@DisplayName("FileAssert isNotEmptyFile")
25+
public class FileAssert_isNotEmptyFile_Test extends FileAssertBaseTest {
26+
27+
@Override
28+
protected FileAssert invoke_api_method() {
29+
return assertions.isNotEmpty();
30+
}
31+
32+
@Override
33+
protected void verify_internal_effects() {
34+
verify(files).assertIsNotEmptyFile(getInfo(assertions), getActual(assertions));
35+
}
36+
}

src/test/java/org/assertj/core/error/ShouldBeEmpty_create_Test.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,44 @@
1212
*/
1313
package org.assertj.core.error;
1414

15-
import static org.assertj.core.api.Assertions.assertThat;
15+
import static java.lang.String.format;
16+
import static org.assertj.core.api.BDDAssertions.then;
1617
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
18+
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
1719
import static org.assertj.core.util.Lists.newArrayList;
1820

19-
import org.assertj.core.description.Description;
21+
import java.io.File;
22+
2023
import org.assertj.core.internal.TestDescription;
21-
import org.assertj.core.presentation.StandardRepresentation;
22-
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.DisplayName;
2325
import org.junit.jupiter.api.Test;
2426

2527
/**
26-
* Tests for <code>{@link ShouldBeEmpty#create(Description, org.assertj.core.presentation.Representation)}</code>.
27-
*
28+
* Tests for <code>{@link ShouldBeEmpty#create((org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
29+
*
2830
* @author Alex Ruiz
2931
* @author Yvonne Wang
3032
*/
31-
public class ShouldBeEmpty_create_Test {
33+
@DisplayName("ShouldBeEmpty create")
34+
class ShouldBeEmpty_create_Test {
3235

33-
private ErrorMessageFactory factory;
34-
35-
@BeforeEach
36-
public void setUp() {
37-
factory = shouldBeEmpty(newArrayList("Luke", "Yoda"));
36+
@Test
37+
void should_create_error_message() {
38+
// GIVEN
39+
ErrorMessageFactory factory = shouldBeEmpty(newArrayList("Luke", "Yoda"));
40+
// WHEN
41+
String message = factory.create(new TestDescription("Test"), STANDARD_REPRESENTATION);
42+
// THEN
43+
then(message).isEqualTo(format("[Test] %nExpecting empty but was:<[\"Luke\", \"Yoda\"]>"));
3844
}
3945

4046
@Test
41-
public void should_create_error_message() {
42-
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
43-
assertThat(message).isEqualTo(String.format("[Test] %nExpecting empty but was:<[\"Luke\", \"Yoda\"]>"));
47+
void should_create_error_message_for_File() {
48+
// GIVEN
49+
ErrorMessageFactory factory = shouldBeEmpty(new File("/te%st.txt"));
50+
// WHEN
51+
String message = factory.create(new TestDescription("Test"), STANDARD_REPRESENTATION);
52+
// THEN
53+
then(message).isEqualTo(format("[Test] %nExpecting file </te%%st.txt> to be empty"));
4454
}
4555
}

src/test/java/org/assertj/core/error/ShouldNotBeEmpty_create_Test.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,44 @@
1212
*/
1313
package org.assertj.core.error;
1414

15-
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.BDDAssertions.then;
1616
import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty;
1717

1818
import org.assertj.core.internal.TestDescription;
1919
import org.assertj.core.presentation.StandardRepresentation;
20-
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.DisplayName;
2121
import org.junit.jupiter.api.Test;
2222

23+
import java.io.File;
24+
2325
/**
2426
* Tests for <code>{@link ShouldNotBeEmpty#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>.
25-
*
27+
*
2628
* @author Alex Ruiz
2729
* @author Yvonne Wang
2830
*/
29-
public class ShouldNotBeEmpty_create_Test {
31+
@DisplayName("ShouldNotBeEmpty create")
32+
class ShouldNotBeEmpty_create_Test {
3033

3134
private ErrorMessageFactory factory;
3235

33-
@BeforeEach
34-
public void setUp() {
36+
@Test
37+
void should_create_error_message() {
38+
// GIVEN
3539
factory = shouldNotBeEmpty();
40+
// WHEN
41+
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
42+
// THEN
43+
then(message).isEqualTo(String.format("[Test] %nExpecting actual not to be empty"));
3644
}
3745

3846
@Test
39-
public void should_create_error_message() {
47+
void should_create_error_message_for_File() {
48+
// GIVEN
49+
factory = shouldNotBeEmpty(new File("/test.txt"));
50+
// WHEN
4051
String message = factory.create(new TestDescription("Test"), new StandardRepresentation());
41-
assertThat(message).isEqualTo(String.format("[Test] %nExpecting actual not to be empty"));
52+
// THEN
53+
then(message).isEqualTo(String.format("[Test] %nExpecting file </test.txt> not to be empty"));
4254
}
4355
}

0 commit comments

Comments
 (0)