Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buckelieg committed Jul 16, 2024
1 parent 8e75877 commit c80534f
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Stream<User> users = db.select("SELECT * FROM HUGE_TABLE")
.execute((batch, session) -> {
// list of mapped rows (to resulting type) with size not more than 1000
// session maps to currently used connection
Map<Long, UserAttr> attrs = session.select("SELECT * FROM USER_ATTR WHERE id IN (:ids)", batch.stream().map(User::getId).collect(Collectors.toList()))
Map<Long, UserAttr> attrs = session.select("SELECT * FROM USER_ATTR WHERE id IN (:ids)", entry("ids", batch.stream().map(User::getId).collect(Collectors.toList())))
.execute(/* map to collection of domain objects that represents a user attribute */)
.groupingBy(UserAttr::userId, Function::identity);
batch.forEach(user -> user.addAttrs(attrs.getOrDefault(user.getId, Collections.emptyList())));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/buckelieg/jdbc/DefaultConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import javax.annotation.Nullable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -46,7 +46,7 @@ final class DefaultConnectionManager implements ConnectionManager {
this.connectionSupplier = connectionSupplier;
this.maxConnections = maxConnections;
this.pool = new ArrayBlockingQueue<>(maxConnections);
this.obtainedConnections = new ArrayList<>(maxConnections);
this.obtainedConnections = new CopyOnWriteArrayList<>();
}

@Nonnull
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/buckelieg/jdbc/fn/TryPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ default TryPredicate<I, E> or(TryPredicate<? super I, E> other) throws E {
* @throws E an arbitrary exception
*/
static <I, E extends Throwable> TryPredicate<I, E> isEqual(Object targetRef) throws E {
return ref -> Objects.equals(ref, targetRef);
return (null == targetRef) ? Objects::isNull : ref -> Objects.equals(ref, targetRef);
}

/**
Expand Down
142 changes: 142 additions & 0 deletions src/test/java/buckelieg/jdbc/FNTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package buckelieg.jdbc;

import buckelieg.jdbc.fn.*;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import static org.junit.jupiter.api.Assertions.*;

public class FNTests {

@Test
public void testTryFunction() throws Exception {
TryFunction<Integer, Integer, Exception> fn = TryFunction.of(TryFunction.identity());
assertEquals(1, fn.apply(1));
assertEquals(2, fn.andThen(i -> i * 2).apply(1));
assertEquals(0, fn.<Integer>compose(i -> i - 1).apply(1));
assertThrows(NullPointerException.class, () -> fn.andThen(null).apply(1));
assertThrows(NullPointerException.class, () -> fn.compose(null).apply(1));
}

@Test
public void testTryBiFunction() throws Exception {
TryBiFunction<Integer, Integer, Integer, Exception> fn = TryBiFunction.of(Integer::sum);
assertEquals(3, fn.apply(1, 2));
assertEquals(6, fn.andThen(sum -> sum * 2).apply(1, 2));
assertThrows(NullPointerException.class, () -> fn.andThen(null).apply(1, 2));
}

@Test
public void testTryTriFunction() throws Exception {
TryTriFunction<Integer, Integer, Integer, Integer, Exception> fn = TryTriFunction.of((i1, i2, i3) -> i1 + i2 + i3);
assertEquals(6, fn.apply(1, 2, 3));
assertEquals(18, fn.andThen(sum -> sum * 3).apply(1, 2, 3));
assertThrows(NullPointerException.class, () -> fn.andThen(null).apply(1, 2, 3));
}

@Test
public void testTryQuadFunction() throws Exception {
TryQuadFunction<Integer, Integer, Integer, Integer, Integer, Exception> fn = TryQuadFunction.of((i1, i2, i3, i4) -> i1 + i2 + i3 + i4);
assertEquals(10, fn.apply(1, 2, 3, 4));
assertEquals(40, fn.andThen(sum -> sum * 4).apply(1, 2, 3, 4));
assertThrows(NullPointerException.class, () -> fn.andThen(null).apply(1, 2, 3, 4));
}

@Test
public void testTryRunnable() throws Exception {
TryRunnable<Exception> fn = TryRunnable.of(() -> assertInstanceOf(TryRunnable.class, this));
assertThrows(AssertionFailedError.class, fn::run);
assertEquals(TryRunnable.NOOP, TryRunnable.NOOP());
}

@Test
public void testTrySupplier() throws Exception {
TrySupplier<Integer, Exception> fn = TrySupplier.of(() -> 5);
assertEquals(5, fn.get());
}

@Test
public void testTryConsumer() throws Exception {
TryConsumer<Integer, Exception> fn = TryConsumer.<Integer, Exception>of(TryConsumer.NOOP())
.compose(integer -> assertEquals(integer, 1))
.andThen(integer -> assertEquals(integer, 1));
fn.accept(1);
assertThrows(NullPointerException.class, () -> fn.andThen(null).accept(1));
assertThrows(NullPointerException.class, () -> fn.compose(null).accept(1));
assertEquals(TryConsumer.NOOP, TryConsumer.NOOP());
}

@Test
public void testTryBiConsumer() throws Exception {
TryBiConsumer<Integer, Integer, Exception> fn = TryBiConsumer.of(TryBiConsumer.NOOP());
fn.andThen((i1, i2) -> {
assertEquals(i1, 1);
assertEquals(i2, 2);
}).accept(1, 2);
assertThrows(NullPointerException.class, () -> fn.andThen(null).accept(1, 2));
assertEquals(TryBiConsumer.NOOP, TryBiConsumer.NOOP());
}

@Test
public void testTryTriConsumer() throws Exception {
TryTriConsumer<Integer, Integer, Integer, Exception> fn = TryTriConsumer.of((i1, i2, i3) -> {
assertEquals(i1, 1);
assertEquals(i2, 2);
assertEquals(i3, 3);
});
fn.accept(1, 2, 3);
assertEquals(TryTriConsumer.NOOP, TryTriConsumer.NOOP());
}

@Test
public void testTryQuadConsumer() throws Exception {
TryQuadConsumer<Integer, Integer, Integer, Integer, Exception> fn = TryQuadConsumer.of((i1, i2, i3, i4) -> {
assertEquals(i1, 1);
assertEquals(i2, 2);
assertEquals(i3, 3);
assertEquals(i4, 4);
});
fn.accept(1, 2, 3, 4);
assertEquals(TryQuadConsumer.NOOP, TryQuadConsumer.NOOP());
}

@Test
public void testTryPredicate() throws Exception {
TryPredicate<Integer, Exception> fn = TryPredicate.of(TryPredicate.FALSE());
assertTrue(fn.negate().test(1));
assertFalse(fn.test(1));
assertEquals(TryPredicate.TRUE, TryPredicate.TRUE());
assertEquals(TryPredicate.FALSE, TryPredicate.FALSE());
assertTrue(TryPredicate.<Integer, Exception>TRUE().or(TryPredicate.TRUE()).test(1));
assertFalse(TryPredicate.<Integer, Exception>FALSE().or(TryPredicate.FALSE()).test(1));
assertFalse(TryPredicate.<Integer, Exception>TRUE().and(TryPredicate.FALSE()).test(1));
assertFalse(fn.and(TryPredicate.TRUE()).test(1));
assertTrue(TryPredicate.<Integer, Exception>not(TryPredicate.FALSE()).test(1));
assertFalse(TryPredicate.<Integer, Exception>not(TryPredicate.TRUE()).test(1));
assertTrue(TryPredicate.isEqual(fn).test(fn));
assertFalse(TryPredicate.isEqual(fn).test(TryPredicate.TRUE()));
assertFalse(TryPredicate.isEqual(null).test(fn));
assertThrows(NullPointerException.class, () -> TryPredicate.not(null).test(1));
assertThrows(NullPointerException.class, () -> TryPredicate.of(null).test(1));
assertThrows(NullPointerException.class, () -> fn.or(null).test(1));
assertThrows(NullPointerException.class, () -> fn.and(null).test(1));
}

@Test
public void testTryBiPredicate() throws Exception {
TryBiPredicate<Integer, Integer, Exception> fn = TryBiPredicate.of(TryBiPredicate.FALSE());
assertTrue(fn.negate().test(1, 2));
assertFalse(fn.test(1, 2));
assertEquals(TryPredicate.TRUE, TryPredicate.TRUE());
assertEquals(TryPredicate.FALSE, TryPredicate.FALSE());
assertTrue(TryBiPredicate.<Integer, Integer, Exception>TRUE().or(TryBiPredicate.TRUE()).test(1, 2));
assertFalse(TryBiPredicate.<Integer, Integer, Exception>FALSE().or(TryBiPredicate.FALSE()).test(1, 2));
assertFalse(TryBiPredicate.<Integer, Integer, Exception>TRUE().and(TryBiPredicate.FALSE()).test(1, 2));
assertFalse(fn.and(TryBiPredicate.TRUE()).test(1, 2));
assertThrows(NullPointerException.class, () -> TryPredicate.not(null).test(1));
assertThrows(NullPointerException.class, () -> TryPredicate.of(null).test(1));
assertThrows(NullPointerException.class, () -> fn.or(null).test(1, 1));
assertThrows(NullPointerException.class, () -> fn.and(null).test(1, 2));
}

}

0 comments on commit c80534f

Please sign in to comment.