-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add row mapping annotation and auto discovery
- Loading branch information
1 parent
318546a
commit 775941e
Showing
7 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
sadu-mapper/src/main/java/de/chojo/sadu/mapper/annotation/MappingProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* Copyright (C) RainbowDashLabs and Contributor | ||
*/ | ||
|
||
package de.chojo.sadu.mapper.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) | ||
public @interface MappingProvider { | ||
String[] value(); | ||
} |
18 changes: 18 additions & 0 deletions
18
sadu-mapper/src/main/java/de/chojo/sadu/mapper/exceptions/InvalidMappingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* Copyright (C) RainbowDashLabs and Contributor | ||
*/ | ||
|
||
package de.chojo.sadu.mapper.exceptions; | ||
|
||
public class InvalidMappingException extends RuntimeException { | ||
|
||
public InvalidMappingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public InvalidMappingException(String message) { | ||
super(message); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
sadu-mapper/src/test/java/de/chojo/sadu/mapper/PostgresDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* Copyright (C) RainbowDashLabs and Contributor | ||
*/ | ||
|
||
package de.chojo.sadu.mapper; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
public class PostgresDatabase { | ||
public static GenericContainer<?> createContainer(String user, String pw) { | ||
GenericContainer<?> self = new GenericContainer<>(DockerImageName.parse("postgres:latest")) | ||
.withExposedPorts(5432) | ||
.withEnv("POSTGRES_USER", user) | ||
.withEnv("POSTGRES_PASSWORD", pw) | ||
.waitingFor(Wait.forLogMessage(".*database system is ready to accept connections.*", 2)); | ||
self.start(); | ||
return self; | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
sadu-mapper/src/test/java/de/chojo/sadu/mapper/annotation/MappingProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* Copyright (C) RainbowDashLabs and Contributor | ||
*/ | ||
|
||
package de.chojo.sadu.mapper.annotation; | ||
|
||
import de.chojo.sadu.datasource.DataSourceCreator; | ||
import de.chojo.sadu.mapper.RowMapperRegistry; | ||
import de.chojo.sadu.mapper.exceptions.InvalidMappingException; | ||
import de.chojo.sadu.mapper.rowmapper.RowMapping; | ||
import de.chojo.sadu.mapper.wrapper.Row; | ||
import de.chojo.sadu.postgresql.databases.PostgreSql; | ||
import de.chojo.sadu.postgresql.mapper.PostgresqlMapper; | ||
import de.chojo.sadu.queries.api.configuration.QueryConfiguration; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import javax.sql.DataSource; | ||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
|
||
import static de.chojo.sadu.mapper.PostgresDatabase.createContainer; | ||
import static de.chojo.sadu.queries.api.query.Query.query; | ||
|
||
public class MappingProviderTest { | ||
|
||
|
||
private static GenericContainer<?> pg; | ||
|
||
@BeforeAll | ||
static void beforeAll() throws IOException { | ||
pg = createContainer("postgres", "postgres"); | ||
DataSource dc = DataSourceCreator.create(PostgreSql.get()) | ||
.configure(c -> c.host(pg.getHost()).port(pg.getFirstMappedPort())).create() | ||
.usingPassword("postgres") | ||
.usingUsername("postgres") | ||
.build(); | ||
QueryConfiguration.setDefault(QueryConfiguration.builder(dc) | ||
.setRowMapperRegistry(new RowMapperRegistry().register(PostgresqlMapper.getDefaultMapper())) | ||
.build()); | ||
} | ||
|
||
@Test | ||
public void testMethodMapperRegistration() { | ||
MethodMappedClass methodMappedClass = query("Select 'test' as test") | ||
.single() | ||
.mapAs(MethodMappedClass.class) | ||
.first() | ||
.get(); | ||
Assertions.assertEquals(methodMappedClass.test, "test"); | ||
} | ||
|
||
@Test | ||
public void testConstructorMapperRegistration() { | ||
ConstructorMappedClass methodMappedClass = query("Select 'test' as test") | ||
.single() | ||
.mapAs(ConstructorMappedClass.class) | ||
.first() | ||
.get(); | ||
Assertions.assertEquals(methodMappedClass.test, "test"); | ||
} | ||
|
||
@Test | ||
public void testInvalidRegistration() { | ||
Assertions.assertThrows(InvalidMappingException.class, () -> { | ||
query("Select 'test' as test") | ||
.single() | ||
.mapAs(InvalidClass.class) | ||
.first(); | ||
}); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() throws IOException { | ||
pg.close(); | ||
} | ||
|
||
public static class MethodMappedClass { | ||
|
||
String test; | ||
|
||
public MethodMappedClass(String test) { | ||
this.test = test; | ||
} | ||
|
||
@MappingProvider({"test"}) | ||
public static RowMapping<MethodMappedClass> map() { | ||
return row -> new MethodMappedClass(row.getString(1)); | ||
} | ||
} | ||
|
||
public static class ConstructorMappedClass { | ||
|
||
String test; | ||
|
||
public ConstructorMappedClass(String test) { | ||
this.test = test; | ||
} | ||
|
||
@MappingProvider({"test"}) | ||
public ConstructorMappedClass(Row row) throws SQLException { | ||
this.test = row.getString("test"); | ||
} | ||
|
||
} | ||
|
||
public static class InvalidClass { | ||
|
||
String test; | ||
|
||
@MappingProvider({"test"}) | ||
public InvalidClass(String test) { | ||
this.test = test; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters