Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuu-nkjm committed Mar 3, 2021
1 parent e2ee44a commit 2959a33
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/nkjmlab/sorm4j/util/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static <T, X extends RuntimeException> void runOrThrow(ThrowableRunnable
Function<Throwable, ? extends X> ex) throws X {
createRunnable(onTry, e -> {
throw ex.apply(e);
}).run();;
}).run();
}


Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/nkjmlab/sorm4j/TypedOrmConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -400,6 +401,27 @@ void testReadAllLazy() {
assertThat(map.get("NAME") != null ? map.get("NAME") : map.get("name"))
.isEqualTo(a.getName());
});

sorm.run(Player.class, m -> {
Map<String, Object> map =
m.readMapLazy("select * from players").stream().collect(Collectors.toList()).get(0);
assertThat(map.get("NAME") != null ? map.get("NAME") : map.get("name"))
.isEqualTo(a.getName());
});

sorm.run(Player.class, m -> {
LazyResultSet<Player> r = m.readLazy("select * from players");
Iterator<Player> it = r.iterator();
r.close();
try {
it.hasNext();
failBecauseExceptionWasNotThrown(Exception.class);
} catch (Exception e) {
assertThat(e.getMessage()).contains("already closed");
}
});


sorm.run(Player.class, m -> {
Map<String, Object> map = m.readMapList(SqlStatement.of("select * from players")).get(0);
assertThat(map.get("NAME") != null ? map.get("NAME") : map.get("name"))
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/nkjmlab/sorm4j/util/TryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,25 @@ void testCreateConsumer() {
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
}

@Test
void testCreateBiConsumer() {
try {
Try.createBiConsumer((con1, con2) -> {
throw new RuntimeException("try");
}, e -> {
}).accept("a", "b");
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
Try.createBiConsumer((con1, con2) -> {
}, e -> {
}).accept("a", "b");
}



@Test
void testCreateConsumerWithThrow() {
try {
Expand All @@ -64,6 +80,19 @@ void testCreateConsumerWithThrow() {
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
Try.createConsumerWithThrow(con -> {
}, OrmException::new).accept("a");
}

@Test
void testCreateBiConsumerWithThrow() {
try {
Try.createBiConsumerWithThrow((c1, c2) -> {
throw new RuntimeException("try");
}, OrmException::new).accept("a", "b");
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
}

@Test
Expand Down Expand Up @@ -106,6 +135,9 @@ void testGetOrThrow() {
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
Try.getOrThrow(() -> {
return null;
}, OrmException::new);
}

@Test
Expand All @@ -117,6 +149,8 @@ void testRunOrThrow() {
} catch (Exception e) {
assertThat(e.getMessage()).contains("try");
}
Try.runOrThrow(() -> {
}, OrmException::new);
}

}

0 comments on commit 2959a33

Please sign in to comment.