Best Assertj code snippet using org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty
Source:AbstractLogFileAssert.java
...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 contains(String... value) {47 assertContains(info, actual, charset, value);48 return myself;49 }50 public SELF doesNotContain(String... values) {51 assertDoesNotContain(info, actual, charset, values);52 return myself;53 }54 public SELF containsOnlyOnce(String... value) {55 assertContainsOnlyOnce(info, actual, charset, value);56 return myself;57 }58 private void assertContains(AssertionInfo info, File actual, Charset charset, String[] values) {59 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {60 return;61 }62 files.assertIsFile(info, actual);63 try {64 List<String> actualLines = FileUtils.readLines(actual, charset);65 List<String> expectedLines = nonBlankStrings(Arrays.asList(values));66 List<String> notFound = new ArrayList<>();67 for (String expectedLine : expectedLines) {68 if (!actualLinesContain(actualLines, expectedLine)) {69 notFound.add(expectedLine);70 }71 }72 if (!notFound.isEmpty()) {73 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +74 "to contain:" + lineSeparator() + " " + printLines(expectedLines) + lineSeparator() +75 "but could not find:" + lineSeparator() + " " + printLines(notFound));76 }77 } catch (IOException e) {78 String msg = String.format("Unable to verify text contents of file:<%s>", actual);79 throw new UncheckedIOException(msg, e);80 }81 }82 private void assertContainsOnlyOnce(AssertionInfo info, File actual, Charset charset,83 String[] values) {84 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {85 return;86 }87 files.assertIsFile(info, actual);88 try {89 List<String> actualLines = FileUtils.readLines(actual, charset);90 List<String> expectedLines = nonBlankStrings(Arrays.asList(values));91 List<String> notFound = new ArrayList<>();92 List<String> moreThanOnce = new ArrayList<>();93 for (String expectedLine : expectedLines) {94 if (actualLinesContain(actualLines, expectedLine)) {95 if (Collections.frequency(actualLines, expectedLine) > 1) {96 moreThanOnce.add(expectedLine);97 }98 } else {99 notFound.add(expectedLine);100 }101 }102 if (!notFound.isEmpty()) {103 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +104 "to contain:" + lineSeparator() + " " + printLines(expectedLines) + lineSeparator() +105 "but could not find:" + lineSeparator() + " " + printLines(notFound));106 }107 if (!moreThanOnce.isEmpty()) {108 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +109 "to contain only once:" + lineSeparator() + " " + printLines(expectedLines) +110 lineSeparator() + "but found more than once:" + lineSeparator() + " " +111 printLines(moreThanOnce));112 }113 } catch (IOException e) {114 String msg = String.format("Unable to verify text contents of file:<%s>", actual);115 throw new UncheckedIOException(msg, e);116 }117 }118 private void assertDoesNotContain(AssertionInfo info, File actual, Charset charset,119 String[] values) {120 if (commonCheckThatLogFileAssertionSucceeds(info, actual, values)) {121 return;122 }123 files.assertIsFile(info, actual);124 try {125 List<String> actualLines = FileUtils.readLines(actual, charset);126 List<String> unexpectedLines = nonBlankStrings(Arrays.asList(values));127 List<String> found = new ArrayList<>();128 for (String actualLine : actualLines) {129 for (String unexpectedLine : unexpectedLines) {130 if (actualLine.contains(unexpectedLine)) {131 found.add(actualLine);132 }133 }134 }135 if (!found.isEmpty()) {136 fail("Expecting:" + lineSeparator() + " " + printLines(actualLines) + lineSeparator() +137 "to not contain:" + lineSeparator() + " " + printLines(unexpectedLines)138 + lineSeparator()139 + "but found:" + lineSeparator() + " " + printLines(found));140 }141 } catch (IOException e) {142 String msg = String.format("Unable to verify text contents of file:<%s>", actual);143 throw new UncheckedIOException(msg, e);144 }145 }146 private boolean commonCheckThatLogFileAssertionSucceeds(AssertionInfo info, File actual,147 Object[] sequence) {148 checkIsNotNull(sequence);149 assertNotNull(info, actual);150 files.assertIsFile(info, actual);151 files.assertExists(info, actual);152 // if both actual and values are empty, then assertion passes.153 if (FileUtils.sizeOf(actual) == 0 && sequence.length == 0) {154 return true;155 }156 failIfEmptySinceActualIsNotEmpty(sequence);157 return false;158 }159 private void assertNotNull(AssertionInfo info, File actual) {160 Objects.instance().assertNotNull(info, actual);161 }162 private boolean actualLinesContain(List<String> actualLines, String value) {163 for (String actualLine : actualLines) {164 if (actualLine.contains(value)) {165 return true;166 }167 }168 return false;169 }170 private String printLines(List<String> lines) {...
failIfEmptySinceActualIsNotEmpty
Using AI Code Generation
1package org.assertj.core.internal;2import org.assertj.core.error.ShouldBeEmpty;3import org.assertj.core.error.ShouldNotBeEmpty;4import org.assertj.core.internal.CommonValidations;5import org.assertj.core.util.VisibleForTesting;6import org.assertj.core.util.introspection.IntrospectionError;7import org.assertj.core.util.introspection.PropertyOrFieldSupport;8import org.assertj.core.util.introspection.PropertyOrFieldSupport.ComparisonStrategy;9import org.assertj.core.util.introspection.PropertyOrFieldSupport.Extraction;10import org.assertj.core.util.introspection.PropertyOrFieldSupport.ExtractionResult;11import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldExtractor;12import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocation;13import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocations;14import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategy;15import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategyFactory;16import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationsFactory;17import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategyFactory;18import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationsFactory;19import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldExtractor;20import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocation;21import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocations;22import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategy;23import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategyFactory;24import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationsFactory;25import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategyFactory;26import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationsFactory;27import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldExtractor;28import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocation;29import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocations;30import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategy;31import org.assertj.core.util.introspection.PropertyOrFieldSupport.PropertyOrFieldLocationStrategyFactory
failIfEmptySinceActualIsNotEmpty
Using AI Code Generation
1package org.assertj.core.internal;2import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;3import static org.assertj.core.util.Preconditions.checkArgument;4import static org.assertj.core.util.Preconditions.checkNotNull;5import org.assertj.core.api.AssertionInfo;6import org.assertj.core.util.VisibleForTesting;7public class Strings {8 private static Strings instance;9 public static Strings instance() {10 if (instance == null) {11 instance = new Strings();12 }13 return instance;14 }15 Failures failures = Failures.instance();16 Strings() {17 }18 public void assertEmpty(AssertionInfo info, CharSequence actual) {19 checkNotNull(actual, "The given CharSequence should not be null");20 if (actual.length() != 0) throw failures.failure(info, shouldBeEmpty(actual));21 }22 public void assertNotEmpty(A
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!