Merge remote-tracking branch 'ebean/master' into fix-scalartype-problem2

This commit is contained in:
Jonas Pöhler committed 2022-01-05 13:34:29 +01:00
commit 3dc6c567a0
56 files changed
+769 -242

No files matched your search

@@ -5,25 +5,32 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BasicProfileLocationTest {
class BasicProfileLocationTest {
@Test
public void obtain() {
void obtain() {
DProfileLocation loc = new DTimedProfileLocation(12, "foo", MetricFactory.get().createTimedMetric("junk"));
String javaVersion = System.getProperty("java.version");
assertThat(loc.obtain()).isTrue();
assertThat(loc.fullLocation()).endsWith(":12)");
if (System.getProperty("java.version").startsWith("1.8")) {
if (javaVersion.startsWith("1.8")) {
assertThat(loc.fullLocation()).endsWith("invoke0(Native Method:12)");
assertThat(loc.location()).isEqualTo("sun.reflect.NativeMethodAccessorImpl.invoke0");
assertThat(loc.label()).isEqualTo("NativeMethodAccessorImpl.invoke0");
} else if (javaVersion.startsWith("18")){
assertThat(loc.fullLocation()).endsWith("jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)");
assertThat(loc.location()).isEqualTo("java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke");
assertThat(loc.label()).isEqualTo("DirectMethodHandleAccessor.invoke");
} else {
assertThat(loc.fullLocation()).endsWith("jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method:12)");
assertThat(loc.location()).isEqualTo("java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0");
assertThat(loc.label()).isEqualTo("NativeMethodAccessorImpl.invoke0");
}
assertThat(loc.label()).isEqualTo("NativeMethodAccessorImpl.invoke0");
}
@Test
public void basic_trimPackage() {
void basic_trimPackage() {
BasicProfileLocation loc = new BasicProfileLocation("com.foo.Bar.all");
assertThat(loc.obtain()).isFalse();
@@ -33,7 +40,7 @@ public class BasicProfileLocationTest {
}
@Test
public void basic_trimSinglePackage() {
void basic_trimSinglePackage() {
BasicProfileLocation loc = new BasicProfileLocation("foo.Bar.all");
assertThat(loc.obtain()).isFalse();
@@ -13,13 +13,12 @@ import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class TestLoadBeanCache extends BaseTestCase {
class TestLoadBeanCache extends BaseTestCase {
@Test
public void testLoad() {
void testLoad() {
ResetBasicData.reset();
@@ -35,32 +34,29 @@ public class TestLoadBeanCache extends BaseTestCase {
// this will hit the cache
Country nz = DB.find(Country.class, "NZ");
assertTrue(loadedNz == nz);
assertSame(loadedNz, nz);
}
@Test
public void testLoadWithFindMap() {
void testLoadWithFindMap() {
ResetBasicData.reset();
List<Object> ids = DB.find(Customer.class).findIds();
assertEquals(ids.size(), 4);
assertThat(ids).isNotEmpty();
DB.getDefault().pluginApi().cacheManager().clearAll();
// hit database
LoggedSql.start();
DB.find(Customer.class).where().idIn(ids).findMap();
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
// hit beanCache
LoggedSql.start();
DB.find(Customer.class).where().idIn(ids).findMap();
sql = LoggedSql.stop();
assertThat(sql).hasSize(0);
}
}
@@ -5,13 +5,16 @@ import io.ebean.BaseTestCase;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TWithPreInsert;
import org.tests.model.basic.TWithPreInsertChild;
import org.tests.model.basic.event.TWithPreInsertPersistAdapter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestPreInsertValidation extends BaseTestCase {
class TestPreInsertValidation extends BaseTestCase {
@Test
public void test() {
void test() {
TWithPreInsert e = new TWithPreInsert();
e.setTitle("Mister");
@@ -22,15 +25,56 @@ public class TestPreInsertValidation extends BaseTestCase {
// the save worked and name set in preInsert
assertNotNull(e.getId());
assertNotNull(e.getName());
assertThat(e.requestCascadeState()).isEqualTo(2);
TWithPreInsert e1 = DB.find(TWithPreInsert.class, e.getId());
assert e1 != null;
e1.setTitle("Missus");
DB.save(e1);
assertThat(e1.requestCascadeState()).isEqualTo(12);
}
@Test
public void testStatelessUpdate() {
void test_cascade() {
TWithPreInsert e = new TWithPreInsert();
e.setTitle("ParentCascading");
e.children().add(new TWithPreInsertChild("Child0"));
// the perInsert should populate the
// name with should not be null
DB.save(e);
// the save worked and name set in preInsert
assertNotNull(e.getId());
assertNotNull(e.getName());
assertThat(e.requestCascadeState()).isEqualTo(2);
assertThat(e.children().get(0).requestCascadeState()).isEqualTo(1);
TWithPreInsert e1 = DB.find(TWithPreInsert.class, e.getId());
assert e1 != null;
e1.setTitle("ParentCascading-changed");
TWithPreInsertChild childBean = e1.children().get(0);
childBean.setName("Child0-changed");
DB.save(e1);
assertThat(e1.requestCascadeState()).isEqualTo(12);
assertThat(childBean.requestCascadeState()).isEqualTo(11);
DB.delete(e1);
assertThat(e1.requestCascadeState()).isEqualTo(22);
// assert that isCascade() was true for the child bean
assertThat(TWithPreInsertPersistAdapter.cascadeDelete).hasSize(1);
String deleteCascade = TWithPreInsertPersistAdapter.cascadeDelete.get(0);
assertThat(deleteCascade).isEqualTo("class org.tests.model.basic.TWithPreInsertChild:1");
}
@Test
void testStatelessUpdate() {
TWithPreInsert e = new TWithPreInsert();
e.setName("BeanForUpdateTest");
@@ -14,6 +14,8 @@ import org.tests.idkeys.db.GenKeySeqB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@@ -24,6 +26,7 @@ public class TestGeneratedKeys extends BaseTestCase {
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testGenKeySeqA() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
try (Transaction tx = server.beginTransaction()) {
@@ -69,20 +72,34 @@ public class TestGeneratedKeys extends BaseTestCase {
}
private long readSequenceValue(Transaction tx, String sequence) throws SQLException {
Statement stm = null;
try {
stm = tx.connection().createStatement();
ResultSet rs = stm.executeQuery("select currval('" + sequence + "')");
rs.next();
String sql;
switch (spiEbeanServer().databasePlatform().getPlatform().base()) {
case H2 :
sql = "select currval('" + sequence + "')";
break;
case DB2 :
sql = "values previous value for " + sequence;
break;
case SQLSERVER :
sql = "select current_value from sys.sequences where name = '" + sequence + "'";
break;
case MARIADB :
throw new UnsupportedOperationException("reading sequence value outside of the current connection is not supported. "
+ "See https://mariadb.com/kb/en/previous-value-for-sequence_name/#description");
default :
throw new UnsupportedOperationException("reading sequence value from "
+ spiEbeanServer().databasePlatform().getPlatform()
+ " is not supported.");
}
try (Statement stm = tx.connection().createStatement()) {
ResultSet rs = stm.executeQuery(sql);
rs.next();
return rs.getLong(1);
} finally {
if (stm != null) {
try {
stm.close();
} catch (SQLException e) {
}
}
}
}
@@ -109,5 +126,25 @@ public class TestGeneratedKeys extends BaseTestCase {
assertNotNull(al.getId());
}
}
@Test
@ForPlatform({Platform.H2, Platform.MARIADB, Platform.SQLSERVER, Platform.DB2})
public void testGeneratedKeys() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
List<Long> idList = new ArrayList<>(52);
try (Transaction tx = server.beginTransaction()) {
// bigger than increment
for (int i = 1; i < 52; i++) {
GenKeySeqA gks = new GenKeySeqA();
gks.setDescription("my description " + i);
server.save(gks);
assertFalse(idList.contains(gks.getId()));
idList.add(gks.getId());
}
}
}
}
@@ -0,0 +1,63 @@
package org.tests.json;
import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.ValuePair;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.MutationDetection;
import io.ebean.annotation.Platform;
import io.ebean.config.DatabaseConfig;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.tests.model.json.EBasicJsonList;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
class TestJsonSourceDefault {
@Test
@ForPlatform(Platform.H2)
@Disabled
void testDirtyValues_diffSource() {
DatabaseConfig config = new DatabaseConfig();
config.getDataSourceConfig()
.setUsername("sa")
.setPassword("")
.setUrl("jdbc:h2:mem:testJsonSourceDirtyValues");
config.setName("jsonSource");
config.setDefaultServer(false);
config.setRegister(false);
config.setDdlGenerate(true);
config.setDdlRun(true);
config.setDdlExtra(false);
config.addClass(EBasicJsonList.class);
config.setJsonMutationDetection(MutationDetection.SOURCE);
Database db = DatabaseFactory.create(config);
try {
assertThat(db).isNotNull();
EBasicJsonList bean = new EBasicJsonList();
bean.getTags().add("aa");
bean.getTags().add("bb");
db.save(bean);
bean = db.find(EBasicJsonList.class, bean.getId());
bean.getTags().add("cc");
final Map<String, ValuePair> dirtyValues = db.beanState(bean).dirtyValues();
assertThat(dirtyValues).containsOnlyKeys("tags");
final ValuePair diff = dirtyValues.get("tags");
assertThat(diff.getOldValue()).isInstanceOf(List.class).asList().containsExactly("aa", "bb");
assertThat(diff.getNewValue()).isInstanceOf(List.class).asList().containsExactly("aa", "bb", "cc");
} finally {
if (db != null) {
db.shutdown();
}
}
}
}
@@ -2,10 +2,15 @@ package org.tests.model.basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import static javax.persistence.CascadeType.ALL;
@Entity
public class TWithPreInsert {
public class TWithPreInsert implements TWithPreInsertCommon {
@Id
private Integer id;
@@ -15,6 +20,14 @@ public class TWithPreInsert {
private String title;
@OneToMany(cascade = ALL)
private List<TWithPreInsertChild> children = new ArrayList<>();
/**
* For testing what happened in persist controller.
*/
transient int requestCascadeState;
public Integer getId() {
return id;
}
@@ -39,4 +52,15 @@ public class TWithPreInsert {
this.title = title;
}
public List<TWithPreInsertChild> children() {
return children;
}
public int requestCascadeState() {
return requestCascadeState;
}
public void requestCascadeState(int requestCascadeState) {
this.requestCascadeState = requestCascadeState;
}
}
@@ -0,0 +1,58 @@
package org.tests.model.basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class TWithPreInsertChild implements TWithPreInsertCommon {
@Id
private Integer id;
@NotNull
private String name;
private String title;
/**
* For testing what happened in persist controller.
*/
transient int requestCascadeState;
public TWithPreInsertChild(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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int requestCascadeState() {
return requestCascadeState;
}
public void requestCascadeState(int requestCascadeState) {
this.requestCascadeState = requestCascadeState;
}
}
@@ -0,0 +1,16 @@
package org.tests.model.basic;
public interface TWithPreInsertCommon {
Integer getId();
String getName();
void setName(String name);
String getTitle();
void setTitle(String title);
void requestCascadeState(int requestCascadeState);
}
@@ -2,37 +2,49 @@ package org.tests.model.basic.event;
import io.ebean.event.BeanPersistAdapter;
import io.ebean.event.BeanPersistRequest;
import org.tests.model.basic.TWithPreInsert;
import org.tests.model.basic.TWithPreInsertCommon;
import java.util.ArrayList;
import java.util.List;
public class TWithPreInsertPersistAdapter extends BeanPersistAdapter {
public static List<String> cascadeDelete = new ArrayList<>();
@Override
public boolean isRegisterFor(Class<?> cls) {
return TWithPreInsert.class.equals(cls);
return TWithPreInsertCommon.class.isAssignableFrom(cls);
}
@Override
public boolean preInsert(BeanPersistRequest<?> request) {
TWithPreInsert e = (TWithPreInsert) request.bean();
if (e.getName() == null) {
e.setName("set on preInsert");
TWithPreInsertCommon bean = (TWithPreInsertCommon) request.bean();
if (bean.getName() == null) {
bean.setName("set on preInsert");
}
bean.requestCascadeState(request.isCascade() ? 1 : 2);
return true;
}
@Override
public boolean preUpdate(BeanPersistRequest<?> request) {
TWithPreInsert b = (TWithPreInsert) request.bean();
System.out.println("preUpdate - title is: " + b.getTitle());
if (b.getTitle() == null) {
b.setTitle("set on preUpdate");
TWithPreInsertCommon bean = (TWithPreInsertCommon) request.bean();
System.out.println("preUpdate - title is: " + bean.getTitle());
if (bean.getTitle() == null) {
bean.setTitle("set on preUpdate");
}
// request.getEbeanServer().refresh(b);
// System.out.println("title is Mister:"+b.getTitle());
bean.requestCascadeState(request.isCascade() ? 11 : 12);
return super.preUpdate(request);
}
@Override
public boolean preDelete(BeanPersistRequest<?> request) {
TWithPreInsertCommon bean = (TWithPreInsertCommon) request.bean();
if (request.isCascade()) {
cascadeDelete.add(bean.getClass() + ":" + bean.getId());
} else {
bean.requestCascadeState(22);
}
return true;
}
}
@@ -10,6 +10,7 @@ import org.tests.model.basic.ResetBasicData;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.Locale;
public class TestCsvReader extends TransactionalTestCase {
@@ -32,7 +33,7 @@ public class TestCsvReader extends TransactionalTestCase {
// csvReader.addProperty("id");
csvReader.addProperty("status");
csvReader.addProperty("name");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy", Locale.ENGLISH);
csvReader.addProperty("billingAddress.line1");
csvReader.addProperty("billingAddress.city");
csvReader.addProperty("billingAddress.country.code");
@@ -10,6 +10,7 @@ import org.tests.model.basic.Customer;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,7 +33,7 @@ public class TestCsvReaderWithCallback extends TransactionalTestCase {
// csvReader.addProperty("id");
csvReader.addProperty("status");
csvReader.addProperty("name");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy");
csvReader.addDateTime("anniversary", "dd-MMM-yyyy", Locale.ENGLISH);
csvReader.addProperty("billingAddress.line1");
csvReader.addProperty("billingAddress.city");
// processor.addReference("billingAddress.country.code");