#2068 - Add ebean-test as module

This commit is contained in:
rob bygrave committed 2020-10-08 23:48:19 +13:00
1 parent ab283efed7
commit bc055fea25
47 files changed
+2730

No files matched your search

@@ -0,0 +1,41 @@
package io.ebean.test;
import io.ebean.DB;
import org.junit.Test;
import org.test.BSimpleWithGen;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class DbJsonTest {
@Test
public void of() {
DB.find(BSimpleWithGen.class).delete();
BSimpleWithGen bean = new BSimpleWithGen("something");
DB.save(bean);
assertThat(bean.getVersion()).isEqualTo(1);
DbJson.of(bean)
.withPlaceholder("_")
.replace("id", "whenModified")
.assertContentMatches("/bean/example-bean.json");
BSimpleWithGen bean2 = new BSimpleWithGen("other");
DB.save(bean2);
final List<BSimpleWithGen> beans = DB.find(BSimpleWithGen.class).findList();
DbJson.of(beans)
//.withPlaceholder("_")
.replace("id", "whenModified")
.assertContentMatches("/bean/example-list.json");
}
}
@@ -0,0 +1,54 @@
package io.ebean.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class UserContextTest {
@Test
public void set() {
UserContext.set("u1", "t1");
assertEquals("u1", UserContext.currentUserId());
assertEquals("t1", UserContext.currentTenantId());
UserContext.set("u2", "t1");
assertEquals("u2", UserContext.currentUserId());
assertEquals("t1", UserContext.currentTenantId());
UserContext.set("u3", "t3");
assertEquals("u3", UserContext.currentUserId());
assertEquals("t3", UserContext.currentTenantId());
}
@Test
public void setUserId() {
UserContext.reset();
UserContext.setUserId("u1");
assertEquals("u1", UserContext.currentUserId());
assertNull(UserContext.currentTenantId());
UserContext.setUserId("u2");
assertEquals("u2", UserContext.currentUserId());
assertNull(UserContext.currentTenantId());
}
@Test
public void setTenantId() {
UserContext.reset();
UserContext.setTenantId("t1");
assertEquals("t1", UserContext.currentTenantId());
assertNull(UserContext.currentUserId());
UserContext.setTenantId("t2");
assertEquals("t2", UserContext.currentTenantId());
assertNull(UserContext.currentUserId());
}
}
@@ -0,0 +1,163 @@
package io.ebean.test.config.platform;
import io.ebean.config.ServerConfig;
import io.ebean.datasource.DataSourceConfig;
import org.junit.Test;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
public class ConfigTest {
@Test
public void trimExtensions() {
Config config = new Config("db", "db", "db", new ServerConfig());
assertThat(config.trimExtensions("a,b")).isEqualTo("a,b");
assertThat(config.trimExtensions(" a , b ")).isEqualTo("a,b");
assertThat(config.trimExtensions(" a , , b ")).isEqualTo("a,b");
}
@Test
public void extraDbProperties_basic() {
Properties p = new Properties();
p.setProperty("ebean.test.extraDb", "other");
ServerConfig serverConfig = new ServerConfig();
serverConfig.loadFromProperties(p);
Config config = new Config("other", "postgres", "other", serverConfig);
PostgresSetup postgresSetup = new PostgresSetup();
postgresSetup.setupExtraDbDataSource(config);
DataSourceConfig ds = serverConfig.getDataSourceConfig();
assertThat(ds.getUsername()).isEqualTo("other");
p = serverConfig.getProperties();
assertThat(p.getProperty("datasource.other.username")).isEqualTo("other");
assertThat(p.getProperty("datasource.other.password")).isEqualTo("test");
assertThat(p.getProperty("datasource.other.url")).isEqualTo("jdbc:postgresql://localhost:6432/other");
}
@Test
public void extraDbProperties_basic_extraDb_dbName() {
Properties p = new Properties();
p.setProperty("ebean.test.extraDb.dbName", "other");
ServerConfig serverConfig = new ServerConfig();
serverConfig.loadFromProperties(p);
Config config = new Config("other", "postgres", "other", serverConfig);
PostgresSetup postgresSetup = new PostgresSetup();
postgresSetup.setupExtraDbDataSource(config);
DataSourceConfig ds = serverConfig.getDataSourceConfig();
assertThat(ds.getUsername()).isEqualTo("other");
p = serverConfig.getProperties();
assertThat(p.getProperty("datasource.other.username")).isEqualTo("other");
assertThat(p.getProperty("datasource.other.password")).isEqualTo("test");
assertThat(p.getProperty("datasource.other.url")).isEqualTo("jdbc:postgresql://localhost:6432/other");
}
@Test
public void extraDbProperties_withOptions() {
Properties p = new Properties();
p.setProperty("ebean.test.extraDb", "other1");
p.setProperty("ebean.test.extraDb.dbName", "other_db_name");
p.setProperty("ebean.test.extraDb.username", "other_user");
p.setProperty("ebean.test.extraDb.password", "other_pwd");
p.setProperty("ebean.test.extraDb.url", "other_url");
ServerConfig serverConfig = new ServerConfig();
serverConfig.setName("scOther");
serverConfig.loadFromProperties(p);
Config config = new Config("other_db_name", "postgres", "other_db_name", serverConfig);
PostgresSetup postgresSetup = new PostgresSetup();
postgresSetup.setupExtraDbDataSource(config);
DataSourceConfig ds = serverConfig.getDataSourceConfig();
assertThat(ds.getUsername()).isEqualTo("other_user");
p = serverConfig.getProperties();
assertThat(p.getProperty("datasource.other_db_name.username")).isEqualTo("other_user");
assertThat(p.getProperty("datasource.other_db_name.password")).isEqualTo("other_pwd");
assertThat(p.getProperty("datasource.other_db_name.url")).isEqualTo("other_url");
}
@Test
public void extraDbProperties_withExtraDbOptions() {
Properties sourceProperties = new Properties();
sourceProperties.setProperty("ebean.test.dbName", "main");
sourceProperties.setProperty("ebean.test.extraDb.dbName", "central");
ServerConfig serverConfig = new ServerConfig();
serverConfig.setName("main");
serverConfig.loadFromProperties(sourceProperties);
Config config = new Config("main", "postgres", "main", serverConfig);
PostgresSetup postgresSetup = new PostgresSetup();
postgresSetup.setup(config);
Properties mainProps = serverConfig.getProperties();
assertThat(mainProps.getProperty("datasource.main.username")).isEqualTo("main");
ServerConfig centralConfig = new ServerConfig();
centralConfig.setName("central");
centralConfig.loadFromProperties(sourceProperties);
Config extraConfig = new Config("central", "postgres", "central", serverConfig);
postgresSetup = new PostgresSetup();
postgresSetup.setupExtraDbDataSource(extraConfig);
Properties centralProps = serverConfig.getProperties();
assertThat(centralProps.getProperty("datasource.central.username")).isEqualTo("central");
}
@Test
public void ignoreDockerShutdown() {
Properties sourceProperties = new Properties();
ServerConfig serverConfig = new ServerConfig();
serverConfig.loadFromProperties(sourceProperties);
Config config = new Config("main", "postgres", "main", serverConfig);
assertThat(config.ignoreDockerShutdown("./src/test/resources/logback-test.xml")).isTrue();
assertThat(config.ignoreDockerShutdown("./src/test/resources/file-does-not-exist")).isFalse();
assertThat(config.ignoreDockerShutdown("~/.ebean/ignore-docker-shutdown")).isTrue();
assertThat(config.ignoreDockerShutdown()).isTrue();
}
@Test
public void ignoreDockerShutdown_viaProperties() {
Properties sourceProperties = new Properties();
sourceProperties.setProperty("ebean.test.localDevelopment", "./src/test/resources/logback-test.xml");
ServerConfig serverConfig = new ServerConfig();
serverConfig.loadFromProperties(sourceProperties);
Config config = new Config("main", "postgres", "main", serverConfig);
assertThat(config.ignoreDockerShutdown()).isTrue();
}
}
@@ -0,0 +1,25 @@
package io.ebean.test.config.provider;
import io.ebean.config.ServerConfig;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ProviderAutoConfigTest {
private ProviderAutoConfig config = new ProviderAutoConfig(new ServerConfig());
@Test
public void msg() {
assertEquals(config.msg(1), "for testing purposes a current user provider has been configured. Use io.ebean.test.UserContext to set current user in tests.");
assertEquals(config.msg(2), "for testing purposes a current tenant provider has been configured. Use io.ebean.test.UserContext to set current tenant in tests.");
assertEquals(config.msg(3), "for testing purposes a current user and tenant provider has been configured. Use io.ebean.test.UserContext to set current user and tenant in tests.");
}
@Test
public void msgUnexpected() {
// rather than fail ...
assertEquals(config.msg(4), "for testing purposes [unexpected??] has been configured. Use io.ebean.test.UserContext to [unexpected??] in tests.");
}
}
@@ -0,0 +1,65 @@
package org.test;
import io.ebean.annotation.WhenModified;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.persistence.Version;
import java.time.Instant;
import java.util.List;
import java.util.Map;
@Entity
public class BSimpleWithGen {
@Id
private Integer id;
private String name;
@Transient
private Map<String, List<String>> someMap;
@WhenModified
private Instant whenModified;
@Version
private long version;
public BSimpleWithGen(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, List<String>> getSomeMap() {
return someMap;
}
public void setSomeMap(Map<String, List<String>> someMap) {
this.someMap = someMap;
}
public Instant getWhenModified() {
return whenModified;
}
public long getVersion() {
return version;
}
}
@@ -0,0 +1,49 @@
package org.test;
import io.ebean.DB;
import io.ebean.annotation.Transactional;
import io.ebean.test.ForTests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ForTestsBeforeAfterTest {
private ForTests.RollbackAll rollbackAll;
@Before
public void before() {
rollbackAll = ForTests.createRollbackAll();
}
@After
public void after() {
rollbackAll.close();
assertThat(getCount()).isEqualTo(0);
}
@Test
public void createRollbackAll() {
doInsert();
assertThat(getCount()).isEqualTo(1);
}
private int getCount() {
return DB.find(BSimpleWithGen.class)
.where().eq("name", "ForTestsBeforeAfterTest")
.findCount();
}
@Transactional
private void doInsert() {
BSimpleWithGen bean = new BSimpleWithGen("ForTestsBeforeAfterTest");
DB.save(bean);
}
}
@@ -0,0 +1,54 @@
package org.test;
import io.ebean.DB;
import io.ebean.Transaction;
import io.ebean.annotation.Transactional;
import io.ebean.test.ForTests;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ForTestsTest {
@Test
public void noTransactional_expect_noTransactionEnterExitCalled() {
ForTests.noTransactional(this::checkForTransactional);
}
@Test
public void enableTransactional() {
ForTests.enableTransactional(false);
checkForTransactional();
ForTests.enableTransactional(true);
}
@Transactional
private void checkForTransactional() {
final Transaction transaction = DB.currentTransaction();
assertThat(transaction).isNull();
}
@Test
public void rollbackTransactions() {
DB.find(BSimpleWithGen.class).delete();
ForTests.rollbackAll(this::doInsert);
final int count = DB.find(BSimpleWithGen.class).findCount();
assertThat(count).isEqualTo(0);
}
@Transactional
private void doInsert() {
BSimpleWithGen bean = new BSimpleWithGen("doInsert");
DB.save(bean);
}
}
@@ -0,0 +1,78 @@
datasource:
db:
username: sa
password:
databaseUrl: jdbc:h2:mem:tests
databaseDriver: org.h2.Driver
ebean:
# migration:
# run: false
#
# ddl:
# generate: true
# run: true
## createOnly: true
docstore:
url: http://127.0.0.1:9200
active: true
generateMapping: true
dropCreate: true
# useDocker: true
elasticVersion: 5.6
# create: true
test:
redis: latest
platform: mariadb #, postgres, mysql, mariadb, oracle, sqlserver, hana
# useDocker: false
# dockerMode: dropCreate
ddlMode: dropCreate # none | dropCreate | create | migrations
dbName: junk
postgres:
version: 9.6
extensions: pgcryto, hstore
username: asd
password: test
url: asd
driver: asd
docker:
postgres:
# port: 6432
version: 9.6
extensions: pgcryto, hstore
username: ${test_db}
password: test
databaseUrl: jdbc:postgresql://localhost:6432/${test_db}
databaseDriver: org.postgresql.Driver
mysql:
version: 5.6
username: ${test_db}
password: test
databaseUrl: jdbc:mysql://localhost:4306/${test_db}
databaseDriver: com.mysql.jdbc.Driver
sqlserver:
version: 2017-CE
username: ${test_db}
password: SqlS3rv#r
databaseUrl: jdbc:sqlserver://localhost:1433;databaseName=${test_db}
databaseDriver: com.microsoft.sqlserver.jdbc.SQLServerDriver
oracle:
username: ${test_db}
password: test
databaseUrl: jdbc:oracle:thin:@127.0.0.1:1521:XE
databaseDriver: oracle.jdbc.driver.OracleDriver
hana:
username: ${test_db}
password: HXEHana1
databaseUrl: jdbc:sap://localhost:39017/?databaseName=HXE
databaseDriver: com.sap.db.jdbc.Driver
@@ -0,0 +1,6 @@
{
"id": _,
"name": "something",
"whenModified": _,
"version": 1
}
@@ -0,0 +1,11 @@
[ {
"id": "*",
"name": "something",
"whenModified": "*",
"version": 1
}, {
"id": "*",
"name": "other",
"whenModified": "*",
"version": 1
} ]
@@ -0,0 +1,21 @@
postgres.version=9.6
postgres.dbName=test_db
postgres.dbUser=test_user
postgres.dbPassword=test
postgres.dbExtensions=hstore,pgcrypto
sqlserver.version=2017-CU2
#sqlserver.port=1433
#sqlserver.dbName=test_db
#sqlserver.dbUser=test_user
#sqlserver.dbPassword=SqlS3rv#r
hana.version=2.00.033.00.20180925.2
hana.port=39117
hana.instanceNumber=91
hana.passwordsUrl=file:///hana/mounts/passwords.json
hana.mountsDirectory=/data/dockermounts
# agree to the SAP license (https://www.sap.com/docs/download/cmp/2016/06/sap-hana-express-dev-agmt-and-exhibit.pdf)
hana.agreeToSapLicense=false
+2
View File
@@ -0,0 +1,2 @@
entity-packages: org.test
transactional-packages: org.test
@@ -0,0 +1,22 @@
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.avaje" level="TRACE"/>
<logger name="io.ebean" level="INFO"/>
<logger name="io.ebean.docker" level="TRACE"/>
<logger name="io.ebean.DDL" level="DEBUG"/>
<logger name="io.ebean.SQL" level="TRACE"/>
<logger name="io.ebean.TXN" level="TRACE"/>
<logger name="io.ebean.SUM" level="TRACE"/>
</configuration>