How to use checkIsNotEmpty method of org.assertj.core.internal.Strings class

Best Assertj code snippet using org.assertj.core.internal.Strings.checkIsNotEmpty

Source:Strings.java Github

copy

Full Screen

...209 */210 public void assertContains(AssertionInfo info, CharSequence actual, CharSequence... values) {211 assertNotNull(info, actual);212 checkIsNotNull(values);213 checkIsNotEmpty(values);214 checkCharSequenceArrayDoesNotHaveNullElements(values);215 Set<CharSequence> notFound = new LinkedHashSet<>();216 for (CharSequence value : values) {217 if (!stringContains(actual, value)) {218 notFound.add(value);219 }220 }221 if (notFound.isEmpty()) return;222 if (notFound.size() == 1 && values.length == 1) {223 throw failures.failure(info, shouldContain(actual, values[0], comparisonStrategy));224 }225 throw failures.failure(info, shouldContain(actual, values, notFound, comparisonStrategy));226 }227 /**228 * Verifies that the given {@code CharSequence} contains only digits.229 *230 * @param info contains information about the assertion.231 * @param actual the given {@code CharSequence}.232 * @throws NullPointerException if {@code actual} is {@code null}.233 * @throws AssertionError if {@code actual} contains non-digit characters or contains no digits at all.234 */235 public void assertContainsOnlyDigits(AssertionInfo info, CharSequence actual) {236 assertNotNull(info, actual);237 if (actual.length() == 0) throw failures.failure(info, shouldContainOnlyDigits(actual));238 for (int index = 0; index < actual.length(); index++) {239 char character = actual.charAt(index);240 if (!isDigit(character)) throw failures.failure(info, shouldContainOnlyDigits(actual, character, index));241 }242 }243 private void checkIsNotNull(CharSequence... values) {244 if (values == null) throw arrayOfValuesToLookForIsNull();245 }246 private void checkIsNotEmpty(CharSequence... values) {247 if (values.length == 0) throw arrayOfValuesToLookForIsEmpty();248 }249 /**250 * Delegates to {@link ComparisonStrategy#stringContains(String, String)}251 */252 private boolean stringContains(CharSequence actual, CharSequence sequence) {253 return comparisonStrategy.stringContains(actual.toString(), sequence.toString());254 }255 /**256 * Verifies that the given {@code CharSequence} contains the given sequence, ignoring case considerations.257 * 258 * @param info contains information about the assertion.259 * @param actual the actual {@code CharSequence}.260 * @param sequence the sequence to search for.261 * @throws NullPointerException if the given sequence is {@code null}.262 * @throws AssertionError if the given {@code CharSequence} is {@code null}.263 * @throws AssertionError if the actual {@code CharSequence} does not contain the given sequence.264 */265 public void assertContainsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence sequence) {266 checkCharSequenceIsNotNull(sequence);267 assertNotNull(info, actual);268 if (!actual.toString().toLowerCase().contains(sequence.toString().toLowerCase()))269 throw failures.failure(info, shouldContainIgnoringCase(actual, sequence));270 }271 /**272 * Verifies that the given {@code CharSequence} does not contain the given sequence.273 * 274 * @param info contains information about the assertion.275 * @param actual the actual {@code CharSequence}.276 * @param sequence the sequence to search for.277 * @throws NullPointerException if the given sequence is {@code null}.278 * @throws AssertionError if the given {@code CharSequence} is {@code null}.279 * @throws AssertionError if the actual {@code CharSequence} contains the given sequence.280 */281 public void assertDoesNotContain(AssertionInfo info, CharSequence actual, CharSequence sequence) {282 checkCharSequenceIsNotNull(sequence);283 assertNotNull(info, actual);284 if (stringContains(actual, sequence))285 throw failures.failure(info, shouldNotContain(actual, sequence, comparisonStrategy));286 }287 private void checkCharSequenceIsNotNull(CharSequence sequence) {288 checkNotNull(sequence, "The char sequence to look for should not be null");289 }290 /**291 * Verifies that two {@code CharSequence}s are equal, ignoring case considerations.292 * 293 * @param info contains information about the assertion.294 * @param actual the actual {@code CharSequence}.295 * @param expected the expected {@code CharSequence}.296 * @throws AssertionError if the given {@code CharSequence}s are not equal.297 */298 public void assertEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected) {299 if (!areEqualIgnoringCase(actual, expected)) throw failures.failure(info, shouldBeEqual(actual, expected));300 }301 /**302 * Verifies that two {@code CharSequence}s are not equal, ignoring case considerations.303 *304 * @param info contains information about the assertion.305 * @param actual the actual {@code CharSequence}.306 * @param expected the expected {@code CharSequence}.307 * @throws AssertionError if the given {@code CharSequence}s are equal ignoring case considerations.308 */309 public void assertNotEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected) {310 if (areEqualIgnoringCase(actual, expected))311 throw failures.failure(info, shouldNotBeEqualIgnoringCase(actual, expected));312 }313 private boolean areEqualIgnoringCase(CharSequence actual, CharSequence expected) {314 if (actual == null) return expected == null;315 if (expected == null) return false;316 return actual.toString().equalsIgnoreCase(expected.toString());317 }318 /**319 * Verifies that two {@code CharSequence}s are equal, ignoring any changes in whitespace.320 *321 * @param info contains information about the assertion.322 * @param actual the actual {@code CharSequence}.323 * @param expected the expected {@code CharSequence}.324 * @throws AssertionError if the given {@code CharSequence}s are not equal.325 */326 public void assertEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {327 if (!areEqualIgnoringWhitespace(actual, expected))328 throw failures.failure(info, shouldBeEqualIgnoringWhitespace(actual, expected));329 }330 /**331 * Verifies that two {@code CharSequence}s are not equal, ignoring any changes in whitespace.332 *333 * @param info contains information about the assertion.334 * @param actual the actual {@code CharSequence}.335 * @param expected the expected {@code CharSequence}.336 * @throws AssertionError if the given {@code CharSequence}s are equal.337 */338 public void assertNotEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {339 if (areEqualIgnoringWhitespace(actual, expected))340 throw failures.failure(info, shouldNotBeEqualIgnoringWhitespace(actual, expected));341 }342 private boolean areEqualIgnoringWhitespace(CharSequence actual, CharSequence expected) {343 if (actual == null) return expected == null;344 checkCharSequenceIsNotNull(expected);345 return removeAllWhitespaces(actual).equals(removeAllWhitespaces(expected));346 }347 // same implementation as Hamcrest's IsEqualIgnoringWhiteSpace348 private String removeAllWhitespaces(CharSequence toBeStripped) {349 final StringBuilder result = new StringBuilder();350 boolean lastWasSpace = true;351 for (int i = 0; i < toBeStripped.length(); i++) {352 char c = toBeStripped.charAt(i);353 if (isWhitespace(c)) {354 if (!lastWasSpace) result.append(' ');355 lastWasSpace = true;356 } else {357 result.append(c);358 lastWasSpace = false;359 }360 }361 return result.toString().trim();362 }363 /**364 * Verifies that actual {@code CharSequence}s contains only once the given sequence.365 * 366 * @param info contains information about the assertion.367 * @param actual the actual {@code CharSequence}.368 * @param sequence the given {@code CharSequence}.369 * @throws NullPointerException if the given sequence is {@code null}.370 * @throws AssertionError if the given {@code CharSequence} is {@code null}.371 * @throws AssertionError if the actual {@code CharSequence} does not contains <b>only once</b> the given372 * {@code CharSequence}.373 */374 public void assertContainsOnlyOnce(AssertionInfo info, CharSequence actual, CharSequence sequence) {375 checkCharSequenceIsNotNull(sequence);376 assertNotNull(info, actual);377 int sequenceOccurencesInActual = countOccurences(sequence, actual);378 if (sequenceOccurencesInActual == 1) return;379 throw failures.failure(info,380 shouldContainOnlyOnce(actual, sequence, sequenceOccurencesInActual, comparisonStrategy));381 }382 /**383 * Count occurrences of sequenceToSearch in actual {@link CharSequence}.384 * 385 * @param sequenceToSearch the sequence to search in in actual {@link CharSequence}.386 * @param actual the {@link CharSequence} to search occurrences in.387 * @return the number of occurrences of sequenceToSearch in actual {@link CharSequence}.388 */389 private int countOccurences(CharSequence sequenceToSearch, CharSequence actual) {390 String strToSearch = sequenceToSearch.toString();391 String strActual = actual.toString();392 int occurences = 0;393 for (int i = 0; i <= (strActual.length() - strToSearch.length()); i++) {394 if (comparisonStrategy.areEqual(strActual.substring(i, i + sequenceToSearch.length()), strToSearch)) {395 occurences++;396 }397 }398 return occurences;399 }400 /**401 * Verifies that the given {@code CharSequence} starts with the given prefix.402 * 403 * @param info contains information about the assertion.404 * @param actual the actual {@code CharSequence}.405 * @param prefix the given prefix.406 * @throws NullPointerException if the given sequence is {@code null}.407 * @throws AssertionError if the given {@code CharSequence} is {@code null}.408 * @throws AssertionError if the actual {@code CharSequence} does not start with the given prefix.409 */410 public void assertStartsWith(AssertionInfo info, CharSequence actual, CharSequence prefix) {411 failIfPrefixIsNull(prefix);412 assertNotNull(info, actual);413 if (!comparisonStrategy.stringStartsWith(actual.toString(), prefix.toString()))414 throw failures.failure(info, shouldStartWith(actual, prefix, comparisonStrategy));415 }416 /**417 * Verifies that the given {@code CharSequence} does not start with the given prefix.418 *419 * @param info contains information about the assertion.420 * @param actual the actual {@code CharSequence}.421 * @param prefix the given prefix.422 * @throws NullPointerException if the given sequence is {@code null}.423 * @throws AssertionError if the given {@code CharSequence} is {@code null}.424 * @throws AssertionError if the actual {@code CharSequence} starts with the given prefix.425 * @author Michal Kordas426 */427 public void assertDoesNotStartWith(AssertionInfo info, CharSequence actual, CharSequence prefix) {428 failIfPrefixIsNull(prefix);429 assertNotNull(info, actual);430 if (comparisonStrategy.stringStartsWith(actual.toString(), prefix.toString()))431 throw failures.failure(info, shouldNotStartWith(actual, prefix, comparisonStrategy));432 }433 private static void failIfPrefixIsNull(CharSequence prefix) {434 checkNotNull(prefix, "The given prefix should not be null");435 }436 /**437 * Verifies that the given {@code CharSequence} ends with the given suffix.438 * 439 * @param info contains information about the assertion.440 * @param actual the actual {@code CharSequence}.441 * @param suffix the given suffix.442 * @throws NullPointerException if the given sequence is {@code null}.443 * @throws AssertionError if the given {@code CharSequence} is {@code null}.444 * @throws AssertionError if the actual {@code CharSequence} does not end with the given suffix.445 */446 public void assertEndsWith(AssertionInfo info, CharSequence actual, CharSequence suffix) {447 failIfSuffixIsNull(suffix);448 assertNotNull(info, actual);449 if (!comparisonStrategy.stringEndsWith(actual.toString(), suffix.toString()))450 throw failures.failure(info, shouldEndWith(actual, suffix, comparisonStrategy));451 }452 /**453 * Verifies that the given {@code CharSequence} does not end with the given suffix.454 *455 * @param info contains information about the assertion.456 * @param actual the actual {@code CharSequence}.457 * @param suffix the given suffix.458 * @throws NullPointerException if the given sequence is {@code null}.459 * @throws AssertionError if the given {@code CharSequence} is {@code null}.460 * @throws AssertionError if the actual {@code CharSequence} ends with the given suffix.461 * @author Michal Kordas462 */463 public void assertDoesNotEndWith(AssertionInfo info, CharSequence actual, CharSequence suffix) {464 failIfSuffixIsNull(suffix);465 assertNotNull(info, actual);466 if (comparisonStrategy.stringEndsWith(actual.toString(), suffix.toString()))467 throw failures.failure(info, shouldNotEndWith(actual, suffix, comparisonStrategy));468 }469 private static void failIfSuffixIsNull(CharSequence suffix) {470 checkNotNull(suffix, "The given suffix should not be null");471 }472 /**473 * Verifies that the given {@code CharSequence} matches the given regular expression.474 * 475 * @param info contains information about the assertion.476 * @param actual the given {@code CharSequence}.477 * @param regex the regular expression to which the actual {@code CharSequence} is to be matched.478 * @throws NullPointerException if the given pattern is {@code null}.479 * @throws PatternSyntaxException if the regular expression's syntax is invalid.480 * @throws AssertionError if the given {@code CharSequence} is {@code null}.481 * @throws AssertionError if the actual {@code CharSequence} does not match the given regular expression.482 */483 public void assertMatches(AssertionInfo info, CharSequence actual, CharSequence regex) {484 checkRegexIsNotNull(regex);485 assertNotNull(info, actual);486 if (!Pattern.matches(regex.toString(), actual)) throw failures.failure(info, shouldMatch(actual, regex));487 }488 /**489 * Verifies that the given {@code CharSequence} does not match the given regular expression.490 * 491 * @param info contains information about the assertion.492 * @param actual the given {@code CharSequence}.493 * @param regex the regular expression to which the actual {@code CharSequence} is to be matched.494 * @throws NullPointerException if the given pattern is {@code null}.495 * @throws PatternSyntaxException if the regular expression's syntax is invalid.496 * @throws AssertionError if the given {@code CharSequence} is {@code null}.497 * @throws AssertionError if the actual {@code CharSequence} matches the given regular expression.498 */499 public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, CharSequence regex) {500 checkRegexIsNotNull(regex);501 assertNotNull(info, actual);502 if (Pattern.matches(regex.toString(), actual)) throw failures.failure(info, shouldNotMatch(actual, regex));503 }504 private void checkRegexIsNotNull(CharSequence regex) {505 if (regex == null) throw patternToMatchIsNull();506 }507 /**508 * Verifies that the given {@code CharSequence} matches the given regular expression.509 * 510 * @param info contains information about the assertion.511 * @param actual the given {@code CharSequence}.512 * @param pattern the regular expression to which the actual {@code CharSequence} is to be matched.513 * @throws NullPointerException if the given pattern is {@code null}.514 * @throws AssertionError if the given {@code CharSequence} is {@code null}.515 * @throws AssertionError if the given {@code CharSequence} does not match the given regular expression.516 */517 public void assertMatches(AssertionInfo info, CharSequence actual, Pattern pattern) {518 checkIsNotNull(pattern);519 assertNotNull(info, actual);520 if (!pattern.matcher(actual).matches()) throw failures.failure(info, shouldMatch(actual, pattern.pattern()));521 }522 /**523 * Verifies that the given {@code CharSequence} does not match the given regular expression.524 * 525 * @param info contains information about the assertion.526 * @param actual the given {@code CharSequence}.527 * @param pattern the regular expression to which the actual {@code CharSequence} is to be matched.528 * @throws NullPointerException if the given pattern is {@code null}.529 * @throws AssertionError if the given {@code CharSequence} matches the given regular expression.530 */531 public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, Pattern pattern) {532 checkIsNotNull(pattern);533 if (!(actual == null || !pattern.matcher(actual).matches()))534 throw failures.failure(info, shouldNotMatch(actual, pattern.pattern()));535 }536 private void checkIsNotNull(Pattern pattern) {537 if (pattern == null) throw patternToMatchIsNull();538 }539 private NullPointerException patternToMatchIsNull() {540 return new NullPointerException("The regular expression pattern to match should not be null");541 }542 private void assertNotNull(AssertionInfo info, CharSequence actual) {543 Objects.instance().assertNotNull(info, actual);544 }545 public void assertContainsSequence(AssertionInfo info, CharSequence actual, CharSequence[] sequence) {546 assertNotNull(info, actual);547 checkIsNotNull(sequence);548 checkIsNotEmpty(sequence);549 checkCharSequenceArrayDoesNotHaveNullElements(sequence);550 Set<CharSequence> notFound = new LinkedHashSet<>();551 for (CharSequence value : sequence) {552 if (!stringContains(actual, value)) notFound.add(value);553 }554 if (!notFound.isEmpty()) {555 // don't bother looking for a sequence, some of the sequence elements were not found !556 if (notFound.size() == 1 && sequence.length == 1) {557 throw failures.failure(info, shouldContain(actual, sequence[0], comparisonStrategy));558 }559 throw failures.failure(info, shouldContain(actual, sequence, notFound, comparisonStrategy));560 }561 // we have found all the given values but were they in the expected order ?562 if (sequence.length == 1) return; // no order check needed for a one element sequence...

Full Screen

Full Screen

checkIsNotEmpty

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.AbstractAssert;2import org.assertj.core.api.AbstractObjectAssert;3import org.assertj.core.api.Assertions;4import org.assertj.core.internal.Objects;5import org.assertj.core.internal.Strings;6public class MyAssert extends AbstractObjectAssert<MyAssert, String> {7 private Strings strings = Strings.instance();8 private Objects objects = Objects.instance();9 public MyAssert(String actual) {10 super(actual, MyAssert.class);11 }12 public static MyAssert assertThat(String actual) {13 return new MyAssert(actual);14 }15 public MyAssert isNotEmpty() {16 strings.assertNotEmpty(info, actual);17 return this;18 }19 public MyAssert isNotNull() {20 objects.assertNotNull(info, actual);21 return this;22 }23 public MyAssert isLowerCase() {24 isNotNull();25 isNotEmpty();26 if (!actual.equals(actual.toLowerCase())) {27 failWithMessage("%nExpecting:%n <%s>%nto be lower case", actual);28 }29 return this;30 }31}32public void test() {33 MyAssert.assertThat("abc").isLowerCase();34}35Assertions.assertThat("abc").isLowerCase();36MyAssert.assertThat("abc").isLowerCase();37Assertions.assertThat("abc").isLowerCase();38MyAssert.assertThat("abc").isLowerCase();39Assertions.assertThat("abc").isLowerCase();40MyAssert.assertThat("abc").isLowerCase();41Assertions.assertThat("abc").isLowerCase();42MyAssert.assertThat("abc").isLowerCase();43Assertions.assertThat("abc").isLowerCase();44MyAssert.assertThat("abc").isLowerCase();45Assertions.assertThat("abc").isLowerCase();46MyAssert.assertThat("abc").isLowerCase();47Assertions.assertThat("abc").isLowerCase();48MyAssert.assertThat("abc").isLowerCase();49Assertions.assertThat("abc").isLowerCase();50MyAssert.assertThat("abc").isLowerCase();51Assertions.assertThat("abc").isLowerCase();52MyAssert.assertThat("abc").isLowerCase();53Assertions.assertThat("abc").isLowerCase();54MyAssert.assertThat("abc").isLowerCase();55Assertions.assertThat("abc").isLowerCase();56MyAssert.assertThat("abc").isLowerCase();57Assertions.assertThat("abc").isLowerCase();

Full Screen

Full Screen

checkIsNotEmpty

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.internal.Strings;3import org.junit.Test;4public class StringsTest {5 public void test() {6 Strings strings = new Strings();7 Assertions.assertThat(strings.checkIsNotEmpty("abc")).isTrue();8 Assertions.assertThat(strings.checkIsNotEmpty("")).isFalse();9 Assertions.assertThat(strings.checkIsNotEmpty(null)).isFalse();10 }11}

Full Screen

Full Screen

checkIsNotEmpty

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.internal.Strings;2import org.assertj.core.api.Assertions;3public class AssertjExample {4 public static void main(String[] args) {5 Strings strings = new Strings();6 String str = "This is a non empty string";7 Assertions.assertThat(strings.checkIsNotEmpty(str)).isTrue();8 }9}

Full Screen

Full Screen

checkIsNotEmpty

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2public class AssertJTest {3 public void testAssertJ() {4 String str = "Hello World";5 assertThat(str).isNotEmpty();6 }7}8import static org.assertj.core.api.Assertions.assertThat;9public class AssertJTest {10 public void testAssertJ() {11 String str = "";12 assertThat(str).isEmpty();13 }14}15assertThat() method of the Assertions class also provides the isEqualToIgnoringCase() method to check if the String is equal to the given String ignoring the case. It returns true if the Strings are equal ignoring the case. Otherwise

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.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Strings

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful