mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#2068 - Add ebean-test as module
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user