Skip to content

Commit

Permalink
gh-2625: Improve assertions readability with assertJ (#3014)
Browse files Browse the repository at this point in the history
* gh-2625: Improve assertions readability with assertJ

* Further examples of changes for gh-2625

* Fix changes merged in from develop branch

* Copyright Header Update

---------

Co-authored-by: GCHQDeveloper314 <94527357+GCHQDeveloper314@users.noreply.github.com>
Co-authored-by: t92549 <80890692+t92549@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 13, 2023
1 parent 92f3f8e commit 2dff1b6
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 297 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 Crown Copyright
* Copyright 2018-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,6 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand All @@ -51,8 +50,9 @@ public void shouldCacheSmallIterable(@Mock final Iterable<Integer> mockIterable)

final Iterable<Integer> cachingIterable = new CachingIterable<>(mockIterable, 5);

assertThat(SMALL_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(SMALL_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(cachingIterable)
.containsExactlyElementsOf(SMALL_LIST)
.containsExactlyElementsOf(SMALL_LIST);
verify(mockIterable, times(1)).iterator();
}

Expand All @@ -64,17 +64,17 @@ public void shouldNotCacheALargeIterable(@Mock final Iterable<Integer> mockItera

final Iterable<Integer> cachingIterable = new CachingIterable<>(mockIterable, 5);

assertThat(LARGE_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(LARGE_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(cachingIterable)
.containsExactlyElementsOf(LARGE_LIST)
.containsExactlyElementsOf(LARGE_LIST);
verify(mockIterable, times(2)).iterator();
}

@Test
public void shouldHandleNullIterable() {
Iterable<Integer> cachingIterable = new CachingIterable<>(null);

assertThat(Collections.emptyList()).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(Collections.emptyList()).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(cachingIterable).isEmpty();
}

@Test
Expand All @@ -98,7 +98,7 @@ public void shouldCloseTheIterableWhenFullyCached() throws IOException {

final Iterable<Integer> cachingIterable = new CachingIterable<>(iterable, 5);

assertThat(SMALL_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(cachingIterable).containsExactlyElementsOf(SMALL_LIST);
verify((Closeable) iterable).close();
}

Expand Down Expand Up @@ -130,15 +130,15 @@ public void shouldHandleMultipleIterators() throws IOException {
itr3.next();

final Iterator<Integer> itr4 = cachingIterable.iterator();
assertThat(SMALL_LIST).isEqualTo(Lists.newArrayList(itr4));
assertThat(Lists.newArrayList(itr4)).containsExactlyElementsOf(SMALL_LIST);

// should be cached now as it has been fully read.
verify((Closeable) iterable, times(3)).close();

itr3.next();

verify(iterable, times(4)).iterator();
assertThat(SMALL_LIST).isEqualTo(Lists.newArrayList(cachingIterable));
assertThat(cachingIterable).containsExactlyElementsOf(SMALL_LIST);

final Iterator<Integer> itr5 = cachingIterable.iterator();
assertThat(itr5.next()).isEqualTo((Integer) 0);
Expand Down
Loading

0 comments on commit 2dff1b6

Please sign in to comment.