mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
#2068 - Add ebean-test as module
This commit is contained in:
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.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user