How to use failIfEmptySinceActualIsNotEmpty method of org.assertj.core.internal.Arrays class

Best Assertj code snippet using org.assertj.core.internal.Arrays.failIfEmptySinceActualIsNotEmpty

Source:AbstractLogFileAssert.java Github

copy

Full Screen

...16import static java.lang.System.lineSeparator;17import static org.apache.commons.lang3.StringUtils.isNotBlank;18import static org.assertj.core.api.Assertions.fail;19import static org.assertj.core.internal.CommonValidations.checkIsNotNull;20import static org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty;21import java.io.File;22import java.io.IOException;23import java.io.UncheckedIOException;24import java.nio.charset.Charset;25import java.util.ArrayList;26import java.util.Arrays;27import java.util.Collections;28import java.util.List;29import java.util.stream.Collectors;30import org.apache.commons.io.FileUtils;31import org.assertj.core.api.AbstractAssert;32import org.assertj.core.api.AssertionInfo;33import org.assertj.core.internal.Files;34import org.assertj.core.internal.Objects;35public abstract class AbstractLogFileAssert<SELF extends AbstractLogFileAssert<SELF>>36 extends AbstractAssert<SELF, File> {37 private final Files files = Files.instance();38 private final Charset charset = Charset.defaultCharset();39 public AbstractLogFileAssert(File actual, Class<?> selfType) {40 super(actual, selfType);41 }42 public SELF exists() {43 files.assertExists(info, actual);44 return myself;45 }46 public SELF doesNotExist() {47 files.assertDoesNotExist(info, actual);48 return myself;49 }50 public SELF contains(String... value) {51 assertContains(info, actual, charset, value);52 return myself;53 }54 public SELF doesNotContain(String... values) {55 assertDoesNotContain(info, actual, charset, values);56 return myself;57 }58 public SELF containsOnlyOnce(String... value) {59 assertContainsOnlyOnce(info, actual, charset, value);60 return myself;61 }62 private void assertContains(AssertionInfo info, File actual, Charset charset, String[] values) {63 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {64 return;65 }66 files.assertIsFile(info, actual);67 try {68 List<String> actualLines = FileUtils.readLines(actual, charset);69 List<String> expectedLines = nonBlankStrings(Arrays.asList(values));70 List<String> notFound = new ArrayList<>();71 for (String expectedLine : expectedLines) {72 if (!actualLinesContain(actualLines, expectedLine)) {73 notFound.add(expectedLine);74 }75 }76 if (!notFound.isEmpty()) {77 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +78 "to contain:" + lineSeparator() + " " + printLines(expectedLines) + lineSeparator() +79 "but could not find:" + lineSeparator() + " " + printLines(notFound));80 }81 } catch (IOException e) {82 String msg = String.format("Unable to verify text contents of file:<%s>", actual);83 throw new UncheckedIOException(msg, e);84 }85 }86 private void assertContainsOnlyOnce(AssertionInfo info, File actual, Charset charset,87 String[] values) {88 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {89 return;90 }91 files.assertIsFile(info, actual);92 try {93 List<String> actualLines = FileUtils.readLines(actual, charset);94 List<String> expectedLines = nonBlankStrings(Arrays.asList(values));95 List<String> notFound = new ArrayList<>();96 List<String> moreThanOnce = new ArrayList<>();97 for (String expectedLine : expectedLines) {98 if (actualLinesContain(actualLines, expectedLine)) {99 if (Collections.frequency(actualLines, expectedLine) > 1) {100 moreThanOnce.add(expectedLine);101 }102 } else {103 notFound.add(expectedLine);104 }105 }106 if (!notFound.isEmpty()) {107 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +108 "to contain:" + lineSeparator() + " " + printLines(expectedLines) + lineSeparator() +109 "but could not find:" + lineSeparator() + " " + printLines(notFound));110 }111 if (!moreThanOnce.isEmpty()) {112 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +113 "to contain only once:" + lineSeparator() + " " + printLines(expectedLines) +114 lineSeparator() + "but found more than once:" + lineSeparator() + " " +115 printLines(moreThanOnce));116 }117 } catch (IOException e) {118 String msg = String.format("Unable to verify text contents of file:<%s>", actual);119 throw new UncheckedIOException(msg, e);120 }121 }122 private void assertDoesNotContain(AssertionInfo info, File actual, Charset charset,123 String[] values) {124 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {125 return;126 }127 files.assertIsFile(info, actual);128 try {129 List<String> actualLines = FileUtils.readLines(actual, charset);130 List<String> unexpectedLines = nonBlankStrings(Arrays.asList(values));131 List<String> found = new ArrayList<>();132 for (String actualLine : actualLines) {133 for (String unexpectedLine : unexpectedLines) {134 if (actualLine.contains(unexpectedLine)) {135 found.add(actualLine);136 }137 }138 }139 if (!found.isEmpty()) {140 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +141 "to not contain:" + lineSeparator() + " " + printLines(unexpectedLines)142 + lineSeparator()143 + "but found:" + lineSeparator() + " " + printLines(found));144 }145 } catch (IOException e) {146 String msg = String.format("Unable to verify text contents of file:<%s>", actual);147 throw new UncheckedIOException(msg, e);148 }149 }150 private boolean commonCheckThatLogFileAssertionSucceeds(AssertionInfo info, File actual,151 Object[] sequence) {152 checkIsNotNull(sequence);153 assertNotNull(info, actual);154 files.assertIsFile(info, actual);155 files.assertExists(info, actual);156 // if both actual and values are empty, then assertion passes.157 if (FileUtils.sizeOf(actual) == 0 && sequence.length == 0) {158 return true;159 }160 failIfEmptySinceActualIsNotEmpty(sequence);161 return false;162 }163 private void assertNotNull(AssertionInfo info, File actual) {164 Objects.instance().assertNotNull(info, actual);165 }166 private boolean actualLinesContain(List<String> actualLines, String value) {167 for (String actualLine : actualLines) {168 if (actualLine.contains(value)) {169 return true;170 }171 }172 return false;173 }174 private String printLines(List<String> lines) {...

Full Screen

Full Screen

failIfEmptySinceActualIsNotEmpty

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.internal.Arrays;3import java.util.ArrayList;4public class ArraysTest {5 public static void main(String[] args) {6 Arrays arrays = new Arrays();7 ArrayList<String> list = new ArrayList<>();8 list.add("first");9 list.add("second");10 list.add("third");11 String[] array = list.toArray(new String[0]);12 Assertions.assertThat(array).isNotEmpty();13 arrays.failIfEmptySinceActualIsNotEmpty(array);14 }15}16Recommended Posts: How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Collections class in AssertJ?17How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Iterables class in AssertJ?18How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Maps class in AssertJ?19How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.ObjectArrays class in AssertJ?20How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Objects class in AssertJ?21How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Strings class in AssertJ?22How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.CharSequences class in AssertJ?23How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Files class in AssertJ?24How to use failIfEmptySinceActualIsNotEmpty() method of org.assertj.core.internal.Files class in AssertJ?

Full Screen

Full Screen

failIfEmptySinceActualIsNotEmpty

Using AI Code Generation

copy

Full Screen

1 public void shouldFailIfEmptySinceActualIsNotEmpty() {2 final int[] actual = {1, 2, 3};3 final int[] expected = {};4 try {5 arrays.failIfEmptySinceActualIsNotEmpty(info, actual, expected);6 } catch (AssertionError e) {7 verify(failures).failure(info, shouldBeEmpty(actual));8 return;9 }10 failBecauseExpectedAssertionErrorWasNotThrown();11 }12 public void shouldPassIfEmptySinceActualIsEmpty() {13 final int[] actual = {};14 final int[] expected = {};15 arrays.failIfEmptySinceActualIsNotEmpty(info, actual, expected);16 }17 public void shouldPassIfEmptySinceActualIsNotEmpty() {18 final int[] actual = {1, 2, 3};19 final int[] expected = {1, 2, 3};20 arrays.failIfEmptySinceActualIsNotEmpty(info, actual, expected);21 }22 public void shouldPassIfEmptySinceActualIsEmptyAndExpectedIsNotEmpty() {23 final int[] actual = {};24 final int[] expected = {1, 2, 3};25 arrays.failIfEmptySinceActualIsNotEmpty(info, actual, expected);26 }27 public void shouldPassIfEmptySinceActualIsNullAndExpectedIsNotEmpty() {28 final int[] actual = null;29 final int[] expected = {1, 2, 3};30 arrays.failIfEmptySinceActualIsNotEmpty(info, actual, expected);31 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful