|
| 1 | +package org.jooby.jdbi; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.sql.PreparedStatement; |
| 8 | +import java.sql.Types; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import org.jooby.MockUnit; |
| 12 | +import org.junit.Test; |
| 13 | +import org.skife.jdbi.v2.StatementContext; |
| 14 | +import org.skife.jdbi.v2.tweak.Argument; |
| 15 | + |
| 16 | +public class OptionaArgumentFactoryTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void accepts() { |
| 20 | + assertTrue(new OptionalArgumentFactory().accepts(Optional.class, Optional.empty(), null)); |
| 21 | + assertFalse(new OptionalArgumentFactory().accepts(Optional.class, new Object(), null)); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void empty() throws Exception { |
| 26 | + new MockUnit(StatementContext.class, PreparedStatement.class) |
| 27 | + .expect(unit -> { |
| 28 | + PreparedStatement stmt = unit.get(PreparedStatement.class); |
| 29 | + stmt.setNull(1, Types.OTHER); |
| 30 | + }) |
| 31 | + .run(unit -> { |
| 32 | + Argument arg = new OptionalArgumentFactory() |
| 33 | + .build(Optional.class, Optional.empty(), unit.get(StatementContext.class)); |
| 34 | + assertNotNull(arg); |
| 35 | + arg.apply(1, unit.get(PreparedStatement.class), unit.get(StatementContext.class)); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void value() throws Exception { |
| 41 | + new MockUnit(StatementContext.class, PreparedStatement.class) |
| 42 | + .expect(unit -> { |
| 43 | + PreparedStatement stmt = unit.get(PreparedStatement.class); |
| 44 | + stmt.setObject(1, "x"); |
| 45 | + }) |
| 46 | + .run(unit -> { |
| 47 | + Argument arg = new OptionalArgumentFactory() |
| 48 | + .build(Optional.class, Optional.of("x"), unit.get(StatementContext.class)); |
| 49 | + assertNotNull(arg); |
| 50 | + arg.apply(1, unit.get(PreparedStatement.class), unit.get(StatementContext.class)); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments