mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
initial add of EbeanORM server based on v2.8.1
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestLogLevelOrdinalValue extends TestCase {
|
||||
|
||||
public void testValues() {
|
||||
|
||||
Assert.assertEquals(0,LogLevel.NONE.ordinal());
|
||||
Assert.assertEquals(1,LogLevel.SUMMARY.ordinal());
|
||||
Assert.assertEquals(2,LogLevel.SQL.ordinal());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.RawSql.Sql;
|
||||
|
||||
public class TestRawSqlBuilder extends TestCase {
|
||||
|
||||
public void testSimple() {
|
||||
|
||||
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust");
|
||||
Sql sql = r.getSql();
|
||||
Assert.assertEquals("id", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust", sql.getPreWhere());
|
||||
Assert.assertEquals("", sql.getPreHaving());
|
||||
Assert.assertNull(sql.getOrderBy());
|
||||
|
||||
}
|
||||
|
||||
public void testWithWhere() {
|
||||
|
||||
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust where id > ?");
|
||||
Sql sql = r.getSql();
|
||||
Assert.assertEquals("id", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust where id > ?", sql.getPreWhere());
|
||||
Assert.assertEquals("", sql.getPreHaving());
|
||||
Assert.assertNull(sql.getOrderBy());
|
||||
}
|
||||
|
||||
public void testWithOrder() {
|
||||
|
||||
RawSqlBuilder r = RawSqlBuilder.parse("select id from t_cust where id > ? order by id desc");
|
||||
Sql sql = r.getSql();
|
||||
Assert.assertEquals("id", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust where id > ?", sql.getPreWhere());
|
||||
Assert.assertEquals("", sql.getPreHaving());
|
||||
Assert.assertEquals("id desc", sql.getOrderBy());
|
||||
|
||||
r = RawSqlBuilder.parse("select id from t_cust order by id desc");
|
||||
sql = r.getSql();
|
||||
Assert.assertEquals("id", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust", sql.getPreWhere());
|
||||
Assert.assertEquals("", sql.getPreHaving());
|
||||
Assert.assertEquals("id desc", sql.getOrderBy());
|
||||
|
||||
r = RawSqlBuilder.parse("select id, sum(x) from t_cust where id > ? group by id order by id desc");
|
||||
sql = r.getSql();
|
||||
Assert.assertEquals("id, sum(x)", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust where id > ?", sql.getPreWhere());
|
||||
Assert.assertEquals("group by id", sql.getPreHaving());
|
||||
Assert.assertEquals("id desc", sql.getOrderBy());
|
||||
}
|
||||
|
||||
public void testWithHaving() {
|
||||
|
||||
RawSqlBuilder r = RawSqlBuilder.parse("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ? order by id desc");
|
||||
Sql sql = r.getSql();
|
||||
Assert.assertEquals("id, sum(x)", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust where id > ?", sql.getPreWhere());
|
||||
Assert.assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
|
||||
Assert.assertEquals("id desc", sql.getOrderBy());
|
||||
|
||||
// no where
|
||||
r = RawSqlBuilder.parse("select id, sum(x) from t_cust group by id having sum(x) > ? order by id desc");
|
||||
sql = r.getSql();
|
||||
Assert.assertEquals("id, sum(x)", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust", sql.getPreWhere());
|
||||
Assert.assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
|
||||
Assert.assertEquals("id desc", sql.getOrderBy());
|
||||
|
||||
// no where, no order by
|
||||
r = RawSqlBuilder.parse("select id, sum(x) from t_cust group by id having sum(x) > ?");
|
||||
sql = r.getSql();
|
||||
Assert.assertEquals("id, sum(x)", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust", sql.getPreWhere());
|
||||
Assert.assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
|
||||
Assert.assertNull(sql.getOrderBy());
|
||||
|
||||
// no order by
|
||||
r = RawSqlBuilder.parse("select id, sum(x) from t_cust where id > ? group by id having sum(x) > ?");
|
||||
sql = r.getSql();
|
||||
Assert.assertEquals("id, sum(x)", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust where id > ?", sql.getPreWhere());
|
||||
Assert.assertEquals("group by id having sum(x) > ?", sql.getPreHaving());
|
||||
Assert.assertNull(sql.getOrderBy());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.RawSql.Sql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestRawSqlBuilderDistinct extends TestCase {
|
||||
|
||||
public void testDistinct() {
|
||||
|
||||
RawSqlBuilder r = RawSqlBuilder.parse("select distinct id, name from t_cust");
|
||||
Sql sql = r.getSql();
|
||||
Assert.assertEquals("id, name", sql.getPreFrom());
|
||||
Assert.assertEquals("from t_cust", sql.getPreWhere());
|
||||
Assert.assertEquals("", sql.getPreHaving());
|
||||
Assert.assertNull(sql.getOrderBy());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.avaje.ebean;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebean.RawSql.ColumnMapping;
|
||||
import com.avaje.ebean.RawSql.ColumnMapping.Column;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestRawSqlColumnParsing extends TestCase {
|
||||
|
||||
public void test_simple() {
|
||||
|
||||
ColumnMapping columnMapping = DRawSqlColumnsParser.parse("a,b,c");
|
||||
Map<String, Column> mapping = columnMapping.mapping();
|
||||
Column c = mapping.get("a");
|
||||
|
||||
assertEquals("a",c.getDbColumn());
|
||||
assertEquals(0, c.getIndexPos());
|
||||
assertEquals("a",c.getPropertyName());
|
||||
|
||||
c = mapping.get("b");
|
||||
assertEquals("b",c.getDbColumn());
|
||||
assertEquals(1, c.getIndexPos());
|
||||
assertEquals("b",c.getPropertyName());
|
||||
|
||||
c = mapping.get("c");
|
||||
assertEquals("c",c.getDbColumn());
|
||||
assertEquals(2, c.getIndexPos());
|
||||
assertEquals("c",c.getPropertyName());
|
||||
|
||||
}
|
||||
|
||||
public void test_simpleWithSpacing() {
|
||||
|
||||
ColumnMapping columnMapping = DRawSqlColumnsParser.parse(" a , b , c ");
|
||||
Map<String, Column> mapping = columnMapping.mapping();
|
||||
Column c = mapping.get("a");
|
||||
|
||||
assertEquals("a",c.getDbColumn());
|
||||
assertEquals(0, c.getIndexPos());
|
||||
assertEquals("a", c.getPropertyName());
|
||||
|
||||
c = mapping.get("b");
|
||||
assertEquals("b",c.getDbColumn());
|
||||
assertEquals(1, c.getIndexPos());
|
||||
assertEquals("b",c.getPropertyName());
|
||||
|
||||
c = mapping.get("c");
|
||||
assertEquals("c",c.getDbColumn());
|
||||
assertEquals(2, c.getIndexPos());
|
||||
assertEquals("c",c.getPropertyName());
|
||||
|
||||
}
|
||||
|
||||
public void test_withAlias() {
|
||||
|
||||
ColumnMapping columnMapping = DRawSqlColumnsParser.parse("a a0,b b1, c c2 , d d3 , e e4 ");
|
||||
Map<String, Column> mapping = columnMapping.mapping();
|
||||
|
||||
assertEquals(5, mapping.size());
|
||||
|
||||
Column c = mapping.get("a");
|
||||
|
||||
assertEquals("a",c.getDbColumn());
|
||||
assertEquals(0, c.getIndexPos());
|
||||
assertEquals("a0",c.getPropertyName());
|
||||
|
||||
c = mapping.get("b");
|
||||
assertEquals("b",c.getDbColumn());
|
||||
assertEquals(1, c.getIndexPos());
|
||||
assertEquals("b1",c.getPropertyName());
|
||||
|
||||
c = mapping.get("c");
|
||||
assertEquals("c",c.getDbColumn());
|
||||
assertEquals(2, c.getIndexPos());
|
||||
assertEquals("c2",c.getPropertyName());
|
||||
|
||||
c = mapping.get("d");
|
||||
assertEquals("d",c.getDbColumn());
|
||||
assertEquals(3, c.getIndexPos());
|
||||
assertEquals("d3",c.getPropertyName());
|
||||
|
||||
|
||||
c = mapping.get("e");
|
||||
assertEquals("e",c.getDbColumn());
|
||||
assertEquals(4, c.getIndexPos());
|
||||
assertEquals("e4",c.getPropertyName());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void test_withAsAlias() {
|
||||
|
||||
ColumnMapping columnMapping = DRawSqlColumnsParser.parse("a as a0,'b' b1, \"c(blah)\" as c2 , d as d3 , e as e4 ");
|
||||
Map<String, Column> mapping = columnMapping.mapping();
|
||||
|
||||
assertEquals(5, mapping.size());
|
||||
|
||||
Column c = mapping.get("a");
|
||||
|
||||
assertEquals("a",c.getDbColumn());
|
||||
assertEquals(0, c.getIndexPos());
|
||||
assertEquals("a0",c.getPropertyName());
|
||||
|
||||
c = mapping.get("'b'");
|
||||
assertEquals("'b'",c.getDbColumn());
|
||||
assertEquals(1, c.getIndexPos());
|
||||
assertEquals("b1",c.getPropertyName());
|
||||
|
||||
c = mapping.get("\"c(blah)\"");
|
||||
assertEquals("\"c(blah)\"",c.getDbColumn());
|
||||
assertEquals(2, c.getIndexPos());
|
||||
assertEquals("c2",c.getPropertyName());
|
||||
|
||||
c = mapping.get("d");
|
||||
assertEquals("d",c.getDbColumn());
|
||||
assertEquals(3, c.getIndexPos());
|
||||
assertEquals("d3",c.getPropertyName());
|
||||
|
||||
|
||||
c = mapping.get("e");
|
||||
assertEquals("e",c.getDbColumn());
|
||||
assertEquals(4, c.getIndexPos());
|
||||
assertEquals("e4",c.getPropertyName());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.avaje.ebean.enhance.agent;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestTransformConstruct extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
Transformer t = new Transformer("", "");
|
||||
Assert.assertNotNull(t);
|
||||
|
||||
t = new Transformer("d", "");
|
||||
Assert.assertNotNull(t);
|
||||
|
||||
t = new Transformer("dd", "");
|
||||
Assert.assertNotNull(t);
|
||||
|
||||
t = new Transformer((String)null, null);
|
||||
Assert.assertNotNull(t);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Copyright (C) 2009 Authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebean.server.type;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.H2Platform;
|
||||
import com.avaje.ebeaninternal.server.core.BootupClasses;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundType;
|
||||
import com.avaje.ebeaninternal.server.type.DefaultTypeManager;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarDataReader;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
import com.avaje.ebeaninternal.server.type.reflect.CheckImmutableResponse;
|
||||
import com.avaje.tests.model.ivo.CMoney;
|
||||
import com.avaje.tests.model.ivo.ExhangeCMoneyRate;
|
||||
import com.avaje.tests.model.ivo.Money;
|
||||
|
||||
public class TestTypeManager extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
serverConfig.setDatabasePlatform(new H2Platform());
|
||||
|
||||
BootupClasses bootupClasses = new BootupClasses();
|
||||
|
||||
DefaultTypeManager typeManager = new DefaultTypeManager(serverConfig, bootupClasses);
|
||||
|
||||
CheckImmutableResponse checkImmutable = typeManager.checkImmutable(Money.class);
|
||||
Assert.assertTrue(checkImmutable.isImmutable());
|
||||
|
||||
checkImmutable = typeManager.checkImmutable(CMoney.class);
|
||||
Assert.assertTrue(checkImmutable.isImmutable());
|
||||
|
||||
ScalarDataReader<?> dataReader = typeManager.recursiveCreateScalarDataReader(ExhangeCMoneyRate.class);
|
||||
Assert.assertTrue(dataReader instanceof CtCompoundType<?>);
|
||||
|
||||
dataReader = typeManager.recursiveCreateScalarDataReader(CMoney.class);
|
||||
Assert.assertTrue(dataReader instanceof CtCompoundType<?>);
|
||||
|
||||
ScalarType<?> scalarType = typeManager.recursiveCreateScalarTypes(Money.class);
|
||||
Assert.assertTrue(scalarType.getJdbcType() == Types.DECIMAL);
|
||||
Assert.assertTrue(!scalarType.isJdbcNative());
|
||||
Assert.assertEquals(Money.class, scalarType.getType());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.avaje.ebean.text;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
|
||||
public class TestPathPropertiesParse extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
PathProperties s0 = PathProperties.parse("(id,name)");
|
||||
|
||||
assertEquals(1,s0.getPaths().size());
|
||||
assertTrue(s0.get(null).contains("id"));
|
||||
assertTrue(s0.get(null).contains("name"));
|
||||
assertFalse(s0.get(null).contains("status"));
|
||||
|
||||
PathProperties s1 = PathProperties.parse(":(id,name,shipAddr(*))");
|
||||
assertEquals(2,s1.getPaths().size());
|
||||
assertEquals(3,s1.get(null).size());
|
||||
assertTrue(s1.get(null).contains("id"));
|
||||
assertTrue(s1.get(null).contains("name"));
|
||||
assertTrue(s1.get(null).contains("shipAddr"));
|
||||
assertTrue(s1.get("shipAddr").contains("*"));
|
||||
assertEquals(1,s1.get("shipAddr").size());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.avaje.ebeaninternal.server.cluster.mcast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebeaninternal.server.cluster.mcast.IncomingPacketsProcessed.GotAllPoint;
|
||||
|
||||
public class TestMcastMemberPackets extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
GotAllPoint member = new GotAllPoint("129.12.23.12:9089",3);
|
||||
|
||||
assertTrue(member.processPacket(1234));
|
||||
assertTrue(member.processPacket(1235));
|
||||
assertTrue(member.processPacket(1236));
|
||||
|
||||
assertEquals(1236l, member.getGotAllPoint());
|
||||
assertEquals(0, member.getMissingPackets().size());
|
||||
|
||||
assertFalse(member.processPacket(1234));
|
||||
|
||||
assertTrue(member.processPacket(1239));
|
||||
List<Long> missingPackets = member.getMissingPackets();
|
||||
assertEquals(2, missingPackets.size());
|
||||
|
||||
assertTrue(missingPackets.contains(1237l));
|
||||
assertTrue(missingPackets.contains(1238l));
|
||||
assertFalse(missingPackets.contains(1239l));
|
||||
assertFalse(missingPackets.contains(1236l));
|
||||
|
||||
missingPackets = member.getMissingPackets();
|
||||
assertEquals(2, missingPackets.size());
|
||||
assertTrue(missingPackets.contains(1237l));
|
||||
assertTrue(missingPackets.contains(1238l));
|
||||
|
||||
assertEquals(1236l, member.getGotAllPoint());
|
||||
|
||||
// get a missing packet
|
||||
assertTrue(member.processPacket(1237));
|
||||
assertEquals(1237l, member.getGotAllPoint());
|
||||
|
||||
missingPackets = member.getMissingPackets();
|
||||
assertEquals(1, missingPackets.size());
|
||||
assertTrue(missingPackets.contains(1238l));
|
||||
|
||||
|
||||
// but we now hit maxResendIncoming
|
||||
missingPackets = member.getMissingPackets();
|
||||
assertEquals(0, missingPackets.size());
|
||||
// gave up on 1238 ..
|
||||
assertEquals(1239l, member.getGotAllPoint());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.avaje.ebeaninternal.server.cluster.mcast;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestPacketsAcked extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
OutgoingPacketsAcked packetsAcked = new OutgoingPacketsAcked();
|
||||
|
||||
assertEquals(0l,packetsAcked.getMinimumGotAllPacketId());
|
||||
|
||||
long receivedAck = packetsAcked.receivedAck("A", new MessageAck("A", 1020l));
|
||||
assertEquals(1020l,packetsAcked.getMinimumGotAllPacketId());
|
||||
assertEquals(1020l,receivedAck);
|
||||
|
||||
receivedAck = packetsAcked.receivedAck("B", new MessageAck("B", 1030l));
|
||||
assertEquals(1020l,packetsAcked.getMinimumGotAllPacketId());
|
||||
assertEquals(0l,receivedAck);
|
||||
|
||||
receivedAck = packetsAcked.receivedAck("C", new MessageAck("C", 1025l));
|
||||
assertEquals(0l,receivedAck);
|
||||
|
||||
receivedAck = packetsAcked.receivedAck("A", new MessageAck("A", 1040l));
|
||||
assertEquals(1025l,receivedAck);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestBusyBuffer extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
BusyConnectionBuffer b = new BusyConnectionBuffer(2,4);
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p2);
|
||||
Assert.assertEquals(6, b.getCapacity());
|
||||
b.add(p3);
|
||||
|
||||
Assert.assertEquals(0, p0.getSlotId());
|
||||
Assert.assertEquals(1, p1.getSlotId());
|
||||
Assert.assertEquals(2, p2.getSlotId());
|
||||
Assert.assertEquals(3, p3.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(4, p2.getSlotId());
|
||||
|
||||
b.remove(p0);
|
||||
b.add(p0);
|
||||
Assert.assertEquals(5, p0.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(0, p2.getSlotId());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void test_rotate() {
|
||||
|
||||
BusyConnectionBuffer b = new BusyConnectionBuffer(2,2);
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
Assert.assertEquals(2, b.getCapacity());
|
||||
b.add(p2);
|
||||
Assert.assertEquals(4, b.getCapacity());
|
||||
b.add(p3);
|
||||
Assert.assertEquals(4, b.getCapacity());
|
||||
|
||||
Assert.assertEquals(0, p0.getSlotId());
|
||||
Assert.assertEquals(1, p1.getSlotId());
|
||||
Assert.assertEquals(2, p2.getSlotId());
|
||||
Assert.assertEquals(3, p3.getSlotId());
|
||||
|
||||
|
||||
b.remove(p2);
|
||||
b.remove(p0);
|
||||
b.remove(p3);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(0, p2.getSlotId());
|
||||
|
||||
b.remove(p0);
|
||||
b.add(p0);
|
||||
// p1 is still in it's slot
|
||||
Assert.assertEquals(2, p0.getSlotId());
|
||||
|
||||
b.remove(p2);
|
||||
b.add(p2);
|
||||
Assert.assertEquals(3, p2.getSlotId());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebeaninternal.server.core.DefaultBackgroundExecutor;
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.Status;
|
||||
|
||||
public class TestDataSourceMax extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
boolean runThisManuallyNow = true;
|
||||
|
||||
if (!runThisManuallyNow){
|
||||
return;
|
||||
}
|
||||
|
||||
String name = "h2";
|
||||
|
||||
DataSourceConfig dsConfig = new DataSourceConfig();
|
||||
dsConfig.loadSettings(name);
|
||||
dsConfig.setMinConnections(3);
|
||||
dsConfig.setMaxConnections(3);
|
||||
dsConfig.setWaitTimeoutMillis(30000);
|
||||
|
||||
DataSourcePool pool = new DataSourcePool(null, name, dsConfig);
|
||||
|
||||
Assert.assertEquals(3, pool.getMaxSize());
|
||||
|
||||
DefaultBackgroundExecutor bg = new DefaultBackgroundExecutor(10, 2, 180, 30, "testDs");
|
||||
|
||||
try {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
//Thread.sleep(10*i);
|
||||
bg.execute(new ConnRunner(pool, 100));
|
||||
}
|
||||
|
||||
System.out.println("main thread sleep ... "+pool.getStatus(false));
|
||||
|
||||
Thread.sleep(1000);
|
||||
Status status = pool.getStatus(false);
|
||||
System.out.println(status);
|
||||
|
||||
// this dumpOrder was for 3 vectors used in PooledConnectionQueue
|
||||
// that logged the order of wait, notify and obtain events
|
||||
// I have remove that code.
|
||||
|
||||
// String s = pool.dumpOrder();
|
||||
// System.err.println(s);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static class ConnRunner implements Runnable {
|
||||
|
||||
final DataSourcePool pool;
|
||||
final long sleepMillis;
|
||||
|
||||
ConnRunner(DataSourcePool pool, long sleepMillis) {
|
||||
this.pool = pool;
|
||||
this.sleepMillis = sleepMillis;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Connection connection = pool.getConnection();
|
||||
Thread.sleep(sleepMillis);
|
||||
connection.close();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.avaje.ebeaninternal.server.lib.sql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestFreeBuffer extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
FreeConnectionBuffer b = new FreeConnectionBuffer(3);
|
||||
|
||||
PooledConnection p0 = new PooledConnection("0");
|
||||
PooledConnection p1 = new PooledConnection("1");
|
||||
PooledConnection p2 = new PooledConnection("2");
|
||||
//PooledConnection p3 = new PooledConnection("3");
|
||||
|
||||
Assert.assertEquals(3, b.getCapacity());
|
||||
Assert.assertEquals(0, b.size());
|
||||
Assert.assertEquals(true, b.isEmpty());
|
||||
|
||||
b.add(p0);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
Assert.assertEquals(false, b.isEmpty());
|
||||
|
||||
PooledConnection r0 = b.remove();
|
||||
Assert.assertTrue(p0 == r0);
|
||||
|
||||
Assert.assertEquals(0, b.size());
|
||||
Assert.assertEquals(true, b.isEmpty());
|
||||
|
||||
b.add(p0);
|
||||
b.add(p1);
|
||||
b.add(p2);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
PooledConnection r1 = b.remove();
|
||||
Assert.assertTrue(p0 == r1);
|
||||
PooledConnection r2 = b.remove();
|
||||
Assert.assertTrue(p1 == r2);
|
||||
|
||||
Assert.assertEquals(1, b.size());
|
||||
b.add(p0);
|
||||
Assert.assertEquals(2, b.size());
|
||||
PooledConnection r3 = b.remove();
|
||||
Assert.assertTrue(p2 == r3);
|
||||
Assert.assertEquals(1, b.size());
|
||||
PooledConnection r4 = b.remove();
|
||||
Assert.assertTrue(p0 == r4);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
b.add(p2);
|
||||
b.add(p1);
|
||||
b.add(p0);
|
||||
|
||||
Assert.assertEquals(3, b.size());
|
||||
|
||||
PooledConnection r5 = b.remove();
|
||||
Assert.assertTrue(p2 == r5);
|
||||
Assert.assertEquals(2, b.size());
|
||||
|
||||
PooledConnection r6 = b.remove();
|
||||
Assert.assertTrue(p1 == r6);
|
||||
Assert.assertEquals(1, b.size());
|
||||
|
||||
PooledConnection r7 = b.remove();
|
||||
Assert.assertTrue(p0 == r7);
|
||||
Assert.assertEquals(0, b.size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.avaje.ebeaninternal.server.loadcontext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestDLoadWeakList extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
String s0 = new String("zero");
|
||||
String s1 = new String("one");
|
||||
String s2 = new String("two");
|
||||
String s3 = new String("three");
|
||||
String s4 = new String("four");
|
||||
String s5 = new String("five");
|
||||
String s6 = new String("six");
|
||||
String s7 = new String("seven");
|
||||
String s8 = new String("eight");
|
||||
String s9 = new String("nine");
|
||||
String s10 = new String("ten");
|
||||
|
||||
DLoadWeakList<Object> list = new DLoadWeakList<Object>();
|
||||
|
||||
list.add(s0);
|
||||
list.add(s1);
|
||||
list.add(s2);
|
||||
list.add(s3);
|
||||
list.add(s4);
|
||||
list.add(s5);
|
||||
list.add(s6);
|
||||
list.add(s7);
|
||||
list.add(s8);
|
||||
list.add(s9);
|
||||
list.add(s10);
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
System.gc();
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// this is really only a HINT, so no guarantee
|
||||
// .. but the SUN JVM does do the business
|
||||
System.gc();
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
List<Object> b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("zero", b0.get(0));
|
||||
Assert.assertEquals("one", b0.get(1));
|
||||
|
||||
try {
|
||||
b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
b0 = list.getNextBatch(2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("two", b0.get(0));
|
||||
Assert.assertEquals("three", b0.get(1));
|
||||
|
||||
list.removeEntry(1);
|
||||
|
||||
b0 = list.getLoadBatch(7, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("seven", b0.get(0));
|
||||
Assert.assertEquals("eight", b0.get(1));
|
||||
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.avaje.ebeaninternal.server.loadcontext;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class TestDLoadWeakListWithGC extends TestCase {
|
||||
|
||||
|
||||
private void doStuffInMethodScope(DLoadWeakList<Object> list) {
|
||||
String s5 = new String("five");
|
||||
String s6 = new String("six");
|
||||
String s7 = new String("seven");
|
||||
String s8 = new String("eight");
|
||||
String s9 = new String("nine");
|
||||
String s10 = new String("ten");
|
||||
|
||||
list.add(s5);
|
||||
list.add(s6);
|
||||
list.add(s7);
|
||||
list.add(s8);
|
||||
list.add(s9);
|
||||
list.add(s10);
|
||||
}
|
||||
|
||||
public void test() {
|
||||
|
||||
String s0 = new String("zero");
|
||||
String s1 = new String("one");
|
||||
String s2 = new String("two");
|
||||
String s3 = new String("three");
|
||||
String s4 = new String("four");
|
||||
|
||||
DLoadWeakList<Object> list = new DLoadWeakList<Object>();
|
||||
|
||||
list.add(s0);
|
||||
list.add(s1);
|
||||
list.add(s2);
|
||||
list.add(s3);
|
||||
list.add(s4);
|
||||
|
||||
int initialSize = list.list.size();
|
||||
|
||||
doStuffInMethodScope(list);
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
System.gc();
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// this is really only a HINT, so no guarantee
|
||||
// .. but the SUN JVM does do the business
|
||||
System.gc();
|
||||
|
||||
Assert.assertEquals(11, list.list.size());
|
||||
|
||||
// these weak refs are still good as we still
|
||||
// have a hard reference to the objects in scope
|
||||
for (int i = 0; i < initialSize; i++) {
|
||||
WeakReference<Object> weakReference = list.list.get(i);
|
||||
Assert.assertNotNull(weakReference);
|
||||
Assert.assertNotNull(weakReference.get());
|
||||
}
|
||||
|
||||
// these weak refs are all null as we don't have
|
||||
// hard refs to the objects in scope ... and it just
|
||||
// so happens the System.gc() in sun jvm is aggressive here.
|
||||
for (int i = initialSize; i < list.list.size(); i++) {
|
||||
WeakReference<Object> weakReference = list.list.get(i);
|
||||
Assert.assertNotNull(weakReference);
|
||||
Assert.assertNull(weakReference.get());
|
||||
}
|
||||
|
||||
List<Object> b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("zero", b0.get(0));
|
||||
Assert.assertEquals("one", b0.get(1));
|
||||
|
||||
try {
|
||||
b0 = list.getLoadBatch(0, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
b0 = list.getNextBatch(2);
|
||||
Assert.assertEquals(2, b0.size());
|
||||
Assert.assertEquals("two", b0.get(0));
|
||||
Assert.assertEquals("three", b0.get(1));
|
||||
|
||||
list.removeEntry(1);
|
||||
|
||||
try {
|
||||
// past the gc blown area
|
||||
b0 = list.getLoadBatch(7, 2);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebeaninternal.server.expression.DefaultExpressionFactory;
|
||||
import com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetailParser;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryProperties;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
|
||||
public class TestQueryLanguage extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
|
||||
DefaultOrmQuery<Order> q = check("find order join customer (id, name)");
|
||||
OrmQueryDetail detail = q.getDetail();
|
||||
OrmQueryProperties chunk = detail.getChunk("customer", false);
|
||||
Set<String> props = chunk.getAllIncludedProperties();
|
||||
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
|
||||
q = check("find order join customer(id, name)");
|
||||
detail = q.getDetail();
|
||||
chunk = detail.getChunk("customer", false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
Assert.assertFalse(chunk.isCache());
|
||||
Assert.assertFalse(chunk.isReadOnly());
|
||||
|
||||
q = check("find order join customer(+cache +readonly, id, name)");
|
||||
detail = q.getDetail();
|
||||
chunk = detail.getChunk("customer", false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
Assert.assertTrue(chunk.isCache());
|
||||
Assert.assertTrue(chunk.isReadOnly());
|
||||
|
||||
q = check("find order join customer(+cache +readonly,id,name)");
|
||||
detail = q.getDetail();
|
||||
chunk = detail.getChunk("customer", false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
Assert.assertTrue(chunk.isCache());
|
||||
Assert.assertTrue(chunk.isReadOnly());
|
||||
|
||||
q = check("find order(id,status) join customer(+cache +readonly,id,name)");
|
||||
detail = q.getDetail();
|
||||
chunk = detail.getChunk("customer", false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
Assert.assertTrue(chunk.isCache());
|
||||
Assert.assertTrue(chunk.isReadOnly());
|
||||
|
||||
chunk = detail.getChunk(null, false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("status"));
|
||||
Assert.assertFalse(props.contains("orderDate"));
|
||||
|
||||
q = check("find order(id,status) join customer(+cache +readonly,id,name) where id > :minId order by status");
|
||||
detail = q.getDetail();
|
||||
chunk = detail.getChunk("customer", false);
|
||||
props = chunk.getAllIncludedProperties();
|
||||
Assert.assertTrue(props.contains("id"));
|
||||
Assert.assertTrue(props.contains("name"));
|
||||
Assert.assertTrue(chunk.isCache());
|
||||
Assert.assertTrue(chunk.isReadOnly());
|
||||
|
||||
String orderBy = q.getOrderBy().toStringFormat();
|
||||
Assert.assertEquals("status", orderBy);
|
||||
}
|
||||
|
||||
private DefaultOrmQuery<Order> check(String q) {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
OrmQueryDetailParser p = new OrmQueryDetailParser(q);
|
||||
p.parse();
|
||||
DefaultOrmQuery<Order> qry = new DefaultOrmQuery<Order>(Order.class, server, new DefaultExpressionFactory(), (String)null);
|
||||
p.assign(qry);
|
||||
|
||||
return qry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.avaje.ebeaninternal.server.rawsql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.RawSql;
|
||||
import com.avaje.ebean.RawSqlBuilder;
|
||||
import com.avaje.ebean.RawSql.Sql;
|
||||
|
||||
public class TestRawSqlParsing extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
String sql
|
||||
= " select order_id, sum(order_qty*unit_price) as totalAmount"
|
||||
+ " from o_order_detail "
|
||||
+ " group by order_id";
|
||||
|
||||
RawSql rawSql = RawSqlBuilder
|
||||
.parse(sql)
|
||||
.columnMapping("order_id","order.id")
|
||||
//.columnMapping("sum(order_qty*unit_price)","totalAmount")
|
||||
.create();
|
||||
|
||||
Sql rs = rawSql.getSql();
|
||||
|
||||
String s = rs.toString();
|
||||
System.out.println(s);
|
||||
assertTrue(s, s.contains("[order_id, sum"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.tests.autofetch;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.tests.model.basic.EBasicClob;
|
||||
|
||||
public class MainAutoFetchExcludeLazyLobs {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GlobalProperties.put("ebean.autofetch.queryTuning", "true");
|
||||
GlobalProperties.put("ebean.autofetch.profiling", "true");
|
||||
|
||||
|
||||
EBasicClob a = new EBasicClob();
|
||||
a.setName("name 1");
|
||||
a.setTitle("a title");
|
||||
a.setDescription("not that meaningful");
|
||||
|
||||
Ebean.save(a);
|
||||
|
||||
List<EBasicClob> list = Ebean.find(EBasicClob.class)
|
||||
.setAutofetch(true)
|
||||
.findList();
|
||||
|
||||
for (EBasicClob bean : list) {
|
||||
bean.getName();
|
||||
// although we read the description
|
||||
// autofetch will not include it later
|
||||
bean.getDescription();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.autofetch;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainAutoQueryTune1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//GlobalProperties.put("ebean.ddl.run", "false");
|
||||
//GlobalProperties.put("ebean.ddl.generate", "false");
|
||||
GlobalProperties.put("ebean.autofetch.queryTuning", "true");
|
||||
// GlobalProperties.put("ebean.autofetch.queryTuningAddVersion", "true");
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
MainAutoQueryTune1 me = new MainAutoQueryTune1();
|
||||
me.tuneJoin();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void tuneJoin()
|
||||
{
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.setAutofetch(true)
|
||||
.fetch("customer")
|
||||
.where()
|
||||
.eq("status", Order.Status.NEW)
|
||||
.eq("customer.name", "Rob")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
for (Order order : list)
|
||||
{
|
||||
System.out.println(order.getId() + " " + order.getOrderDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.PostgresPlatform;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
import com.avaje.tests.model.basic.TSDetail;
|
||||
import com.avaje.tests.model.basic.TSMaster;
|
||||
|
||||
/**
|
||||
* Used to run some tests manually on a specific Database type.
|
||||
*/
|
||||
public class MainDbBoolean {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
MainDbBoolean me = new MainDbBoolean();
|
||||
|
||||
EbeanServer server = me.createEbeanServer();
|
||||
me.simpleCheck(server);
|
||||
|
||||
EbeanServer oraServer = me.createOracleEbeanServer();
|
||||
me.simpleCheck(oraServer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a server for running small oracle specific tests manually.
|
||||
* DDL generation etc.
|
||||
*/
|
||||
private EbeanServer createOracleEbeanServer() {
|
||||
|
||||
ServerConfig c = new ServerConfig();
|
||||
c.setName("ora");
|
||||
|
||||
// requires oracle driver in class path
|
||||
DataSourceConfig oraDb = new DataSourceConfig();
|
||||
oraDb.setDriver("oracle.jdbc.driver.OracleDriver");
|
||||
oraDb.setUsername("junk");
|
||||
oraDb.setPassword("junk");
|
||||
oraDb.setUrl("jdbc:oracle:thin:junk/junk@localhost:1521:XE");
|
||||
oraDb.setHeartbeatSql("select count(*) from dual");
|
||||
|
||||
|
||||
c.loadFromProperties();
|
||||
c.setDdlGenerate(true);
|
||||
c.setDdlRun(true);
|
||||
c.setDefaultServer(false);
|
||||
c.setRegister(false);
|
||||
c.setDataSourceConfig(oraDb);
|
||||
|
||||
//c.setDatabaseBooleanTrue("1");
|
||||
//c.setDatabaseBooleanFalse("0");
|
||||
c.setDatabaseBooleanTrue("T");
|
||||
c.setDatabaseBooleanFalse("F");
|
||||
|
||||
c.addClass(TOne.class);
|
||||
c.addClass(TSMaster.class);
|
||||
c.addClass(TSDetail.class);
|
||||
|
||||
return EbeanServerFactory.create(c);
|
||||
|
||||
}
|
||||
|
||||
private EbeanServer createEbeanServer() {
|
||||
|
||||
ServerConfig c = new ServerConfig();
|
||||
c.setName("pgtest");
|
||||
|
||||
// requires postgres driver in class path
|
||||
DataSourceConfig postgresDb = new DataSourceConfig();
|
||||
postgresDb.setDriver("org.postgresql.Driver");
|
||||
postgresDb.setUsername("test");
|
||||
postgresDb.setPassword("test");
|
||||
postgresDb.setUrl("jdbc:postgresql://127.0.0.1:5432/test");
|
||||
postgresDb.setHeartbeatSql("select count(*) from t_one");
|
||||
|
||||
c.loadFromProperties();
|
||||
c.setDdlGenerate(true);
|
||||
c.setDdlRun(true);
|
||||
c.setDefaultServer(false);
|
||||
c.setRegister(false);
|
||||
c.setDataSourceConfig(postgresDb);
|
||||
|
||||
//c.setDatabaseBooleanTrue("1");
|
||||
//c.setDatabaseBooleanFalse("0");
|
||||
c.setDatabaseBooleanTrue("T");
|
||||
c.setDatabaseBooleanFalse("F");
|
||||
|
||||
c.setDatabasePlatform(new PostgresPlatform());
|
||||
|
||||
c.addClass(TOne.class);
|
||||
|
||||
return EbeanServerFactory.create(c);
|
||||
|
||||
}
|
||||
|
||||
private void simpleCheck(EbeanServer server) {
|
||||
|
||||
TOne o = new TOne();
|
||||
o.setName("banan");
|
||||
o.setDescription("this one is true");
|
||||
o.setActive(true);
|
||||
|
||||
server.save(o);
|
||||
|
||||
TOne o2 = new TOne();
|
||||
o2.setName("banan");
|
||||
o2.setDescription("this one is false");
|
||||
o2.setActive(false);
|
||||
|
||||
server.save(o2);
|
||||
|
||||
|
||||
List<TOne> list = server.find(TOne.class)
|
||||
.setAutofetch(false)
|
||||
.order("id")
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(list.size() == 2);
|
||||
Assert.assertTrue(list.get(0).isActive());
|
||||
Assert.assertFalse(!list.get(0).isActive());
|
||||
|
||||
String sql = "select id, name, active from t_oneb order by id";
|
||||
List<SqlRow> sqlRows = server.createSqlQuery(sql).findList();
|
||||
Assert.assertTrue(sqlRows.size() == 2);
|
||||
Object active0 = sqlRows.get(0).get("active");
|
||||
Object active1 = sqlRows.get(1).get("active");
|
||||
|
||||
Assert.assertTrue("T".equals(active0));
|
||||
Assert.assertTrue("F".equals(active1));
|
||||
|
||||
|
||||
|
||||
Query<TOne> query = server.find(TOne.class)
|
||||
.setAutofetch(false)
|
||||
.order("id");
|
||||
|
||||
int rc = query.findRowCount();
|
||||
Assert.assertTrue(rc > 0);
|
||||
|
||||
|
||||
System.out.println("done");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.EbeanServerFactory;
|
||||
import com.avaje.ebean.FutureList;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.SqlFutureList;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebean.config.DataSourceConfig;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
public class MainFutureList {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
checkFutureRowCount(true);
|
||||
//testSqlQueryFuture();
|
||||
//testOrmFuture();
|
||||
}
|
||||
|
||||
private static EbeanServer createEbeanServer(boolean primary) {
|
||||
|
||||
if (primary){
|
||||
return Ebean.getServer(null);
|
||||
}
|
||||
|
||||
ServerConfig c = new ServerConfig();
|
||||
c.setName("pgtest");
|
||||
|
||||
// // requires postgres driver in class path
|
||||
// DataSourceConfig postgresDb = new DataSourceConfig();
|
||||
// postgresDb.setDriver("org.postgresql.Driver");
|
||||
// postgresDb.setUsername("test");
|
||||
// postgresDb.setPassword("test");
|
||||
// postgresDb.setUrl("jdbc:postgresql://127.0.0.1:5432/test");
|
||||
// postgresDb.setHeartbeatSql("select count(*) from t_one");
|
||||
|
||||
// requires oracle driver in class path
|
||||
DataSourceConfig oraDb = new DataSourceConfig();
|
||||
oraDb.setDriver("oracle.jdbc.driver.OracleDriver");
|
||||
oraDb.setUsername("junk");
|
||||
oraDb.setPassword("junk");
|
||||
oraDb.setUrl("jdbc:oracle:thin:junk/junk@localhost:1521:XE");
|
||||
oraDb.setHeartbeatSql("select count(*) from dual");
|
||||
|
||||
|
||||
c.loadFromProperties();
|
||||
c.setDdlGenerate(true);
|
||||
c.setDdlRun(true);
|
||||
c.setDefaultServer(false);
|
||||
c.setRegister(false);
|
||||
// c.setDataSourceConfig(postgresDb);
|
||||
c.setDataSourceConfig(oraDb);
|
||||
|
||||
//c.setDatabaseBooleanTrue("1");
|
||||
//c.setDatabaseBooleanFalse("0");
|
||||
//c.setDatabaseBooleanTrue("T");
|
||||
//c.setDatabaseBooleanFalse("F");
|
||||
|
||||
//c.setDatabasePlatform(new Postgres83Platform());
|
||||
|
||||
c.addClass(TOne.class);
|
||||
|
||||
return EbeanServerFactory.create(c);
|
||||
|
||||
}
|
||||
|
||||
public static void checkFutureRowCount(boolean primay) throws Exception {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
EbeanServer server = createEbeanServer(primay);
|
||||
|
||||
Query<Order> query = server.find(Order.class);
|
||||
Future<Integer> futureRowCount = server.findFutureRowCount(query, null);
|
||||
boolean done = futureRowCount.isDone();
|
||||
|
||||
System.out.println("done: "+done);
|
||||
|
||||
Integer rowCount = futureRowCount.get();
|
||||
System.out.println("got rc:"+rowCount);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void checkSqlQueryFuture(boolean primay) throws Exception {
|
||||
|
||||
EbeanServer server = createEbeanServer(primay);
|
||||
|
||||
String sql = "select o.* from all_tables o";
|
||||
SqlQuery sqlQuery = server.createSqlQuery(sql);
|
||||
|
||||
SqlFutureList list = server.findFutureList(sqlQuery, null);
|
||||
System.out.println("start done:"+list.isDone());
|
||||
Thread.sleep(200);
|
||||
if (!list.isDone()){
|
||||
list.cancel(true);
|
||||
}
|
||||
|
||||
if (!list.isCancelled()){
|
||||
List<SqlRow> list2 = list.get();
|
||||
System.out.println("got "+list2.size());
|
||||
}
|
||||
|
||||
Thread.sleep(3000);
|
||||
System.out.println("done sleeping");
|
||||
}
|
||||
|
||||
public static void checkOrmFuture() throws Exception {
|
||||
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
//EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
|
||||
FutureList<Order> futureList = query.findFutureList();
|
||||
|
||||
Thread.sleep(3000);
|
||||
System.out.println("end of sleep");
|
||||
|
||||
if (!futureList.isDone()){
|
||||
futureList.cancel(true);
|
||||
}
|
||||
|
||||
System.out.println("and... done:"+futureList.isDone());
|
||||
|
||||
if (!futureList.isCancelled()){
|
||||
//List<Order> l0 = futureList.get(30, TimeUnit.SECONDS);
|
||||
List<Order> list = futureList.get();
|
||||
System.out.println("list:"+list);
|
||||
}
|
||||
|
||||
System.out.println("done");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebeaninternal.server.lib.sql.DataSourcePoolListener;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class MyTestDataSourcePoolListener implements DataSourcePoolListener
|
||||
{
|
||||
public static int SLEEP_AFTER_BORROW = 0;
|
||||
|
||||
public void onAfterBorrowConnection(Connection c)
|
||||
{
|
||||
if (SLEEP_AFTER_BORROW > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(SLEEP_AFTER_BORROW);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onBeforeReturnConnection(Connection c)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestAssocOneEqExpression extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setId(1);
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.where().eq("customer", c)
|
||||
.query();
|
||||
|
||||
query.findList();
|
||||
String sql = query.getGeneratedSql();
|
||||
Assert.assertTrue(sql.contains("where t0.kcustomer_id = ?"));
|
||||
|
||||
Address b = new Address();
|
||||
b.setId((short)1);
|
||||
|
||||
Query<Order> q2 = Ebean.find(Order.class)
|
||||
.where().eq("customer.billingAddress", b)
|
||||
.query();
|
||||
|
||||
q2.findList();
|
||||
sql = q2.getGeneratedSql();
|
||||
Assert.assertTrue(sql.contains("where t1.billing_address_id = ?"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestBackgroundFetchAfter extends TestCase {
|
||||
|
||||
public void testWrtJoin() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
boolean h2Db = "h2".equals(server.getDatabasePlatform().getName());
|
||||
|
||||
// limit not in sql as join to many
|
||||
Query<Order> q = Ebean.find(Order.class)
|
||||
.fetch("details")
|
||||
.setBackgroundFetchAfter(3)
|
||||
.setMaxRows(10);
|
||||
|
||||
q.findList();
|
||||
String sql = q.getGeneratedSql();
|
||||
|
||||
if (h2Db){
|
||||
Assert.assertTrue(sql.indexOf("limit") == -1);
|
||||
}
|
||||
|
||||
// allows limit use as no join to many
|
||||
q = Ebean.find(Order.class)
|
||||
.setBackgroundFetchAfter(3)
|
||||
.setMaxRows(10);
|
||||
|
||||
q.findList();
|
||||
sql = q.getGeneratedSql();
|
||||
|
||||
if (h2Db){
|
||||
Assert.assertTrue(sql.indexOf("limit") > -1);
|
||||
}
|
||||
|
||||
// allows limit use as join to one (not many)
|
||||
q = Ebean.find(Order.class)
|
||||
.fetch("customer")
|
||||
.setBackgroundFetchAfter(3)
|
||||
.setMaxRows(10);
|
||||
|
||||
q.findList();
|
||||
sql = q.getGeneratedSql();
|
||||
|
||||
if (h2Db){
|
||||
Assert.assertTrue(sql.indexOf("limit") > -1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.AdminAutofetch;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestBatchLazy extends TestCase {
|
||||
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class);
|
||||
List<Order> list = query.findList();
|
||||
|
||||
|
||||
for (Order order : list) {
|
||||
Customer customer = order.getCustomer();
|
||||
customer.getName();
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
for (OrderDetail orderDetail : details) {
|
||||
orderDetail.getProduct().getSku();
|
||||
}
|
||||
}
|
||||
|
||||
AdminAutofetch adminAutofetch = Ebean.getServer(null).getAdminAutofetch();
|
||||
adminAutofetch.collectUsageViaGC();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestBatchLazyMany extends TestCase {
|
||||
|
||||
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order2 = Ebean.getReference(Order.class, 1);
|
||||
order2.getOrderDate();
|
||||
System.out.println("done");
|
||||
|
||||
// List<Order> list = Ebean.find(Order.class)
|
||||
// //.join("details")
|
||||
// //.join("details", "+fetchquery")
|
||||
// .findList();
|
||||
//
|
||||
// Order order = list.get(0);
|
||||
// //List<OrderDetail> details = order.getDetails();
|
||||
// //details.size();
|
||||
//
|
||||
// Customer customer = order.getCustomer();
|
||||
// customer.getName();
|
||||
// System.out.println("done");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.basic.Order.Status;
|
||||
|
||||
public class TestBeanReferenceRefresh extends TestCase {
|
||||
|
||||
|
||||
public void testMe() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order = Ebean.getReference(Order.class, 1);
|
||||
|
||||
Assert.assertTrue("isReference",Ebean.getBeanState(order).isReference());
|
||||
|
||||
order.getOrderDate();
|
||||
|
||||
Assert.assertFalse(Ebean.getBeanState(order).isReference());
|
||||
Assert.assertNotNull(order.getStatus());
|
||||
Assert.assertNotNull(order.getDetails());
|
||||
Assert.assertNull(Ebean.getBeanState(order).getLoadedProps());
|
||||
|
||||
Status status = order.getStatus();
|
||||
Assert.assertTrue(status != Order.Status.SHIPPED);
|
||||
order.setStatus(Order.Status.SHIPPED);
|
||||
Ebean.refresh(order);
|
||||
|
||||
Status statusRefresh = order.getStatus();
|
||||
Assert.assertEquals(status,statusRefresh);
|
||||
|
||||
System.out.println("done");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestDeleteByIdCollection extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c0 = ResetBasicData.createCustomer("del1", "del1 ship", "del1 bill", 1);
|
||||
Customer c1 = ResetBasicData.createCustomer("del2", "del2 ship", "del2 bill", 2);
|
||||
|
||||
Ebean.save(c0);
|
||||
Ebean.save(c1);
|
||||
|
||||
Customer c0Back = Ebean.find(Customer.class, c0.getId());
|
||||
Customer c1Back = Ebean.find(Customer.class, ""+c1.getId());
|
||||
|
||||
assertNotNull(c0Back);
|
||||
assertNotNull(c1Back);
|
||||
|
||||
List<String> ids = new ArrayList<String>();
|
||||
// also test id type conversion
|
||||
ids.add(""+c0.getId());
|
||||
ids.add(""+c1.getId());
|
||||
|
||||
|
||||
Ebean.delete(Customer.class, ids);
|
||||
|
||||
c0Back = Ebean.find(Customer.class, c0.getId());
|
||||
c1Back = Ebean.find(Customer.class, ""+c1.getId());
|
||||
|
||||
assertNull(c0Back);
|
||||
assertNull(c1Back);
|
||||
}
|
||||
|
||||
public void testDelByStatement() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order0 = ResetBasicData.createOrderCustAndOrder("delBySql 0");
|
||||
Order order1 = ResetBasicData.createOrderCustAndOrder("delBySql 1");
|
||||
|
||||
Order o0Back = Ebean.find(Order.class, order0.getId());
|
||||
Order o1Back = Ebean.find(Order.class, order1.getId());
|
||||
|
||||
assertNotNull(o0Back);
|
||||
assertNotNull(o1Back);
|
||||
|
||||
|
||||
List<Object> ids = new ArrayList<Object>();
|
||||
// also test id type conversion
|
||||
ids.add(order0.getId());
|
||||
ids.add(order1.getId());
|
||||
|
||||
Ebean.delete(Order.class, ids);
|
||||
|
||||
o0Back = Ebean.find(Order.class, order0.getId());
|
||||
o1Back = Ebean.find(Order.class, order1.getId());
|
||||
|
||||
assertNull(o0Back);
|
||||
assertNull(o1Back);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.PFile;
|
||||
import com.avaje.tests.model.basic.PFileContent;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestDeleteImportedPartial extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
|
||||
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getFileContent().getId();
|
||||
|
||||
PFile partialPfile = Ebean.find(PFile.class)
|
||||
.select("id")
|
||||
.where().idEq(persistentFile.getId())
|
||||
.findUnique();
|
||||
|
||||
// should delete file and fileContent
|
||||
Ebean.delete(partialPfile);
|
||||
System.out.println("finished delete");
|
||||
|
||||
PFile file1 = Ebean.find(PFile.class, id);
|
||||
PFileContent content1 = Ebean.find(PFileContent.class, contentId);
|
||||
|
||||
Assert.assertNull(file1);
|
||||
Assert.assertNull(content1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.PersistentFile;
|
||||
import com.avaje.tests.model.basic.PersistentFileContent;
|
||||
|
||||
public class TestDeleteOneToOne extends TestCase {
|
||||
|
||||
public void testCreateDeletePersistentFile() {
|
||||
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt", new PersistentFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getPersistentFileContent().getId();
|
||||
|
||||
// should delete file and fileContent
|
||||
Ebean.delete(PersistentFile.class, id);
|
||||
|
||||
PersistentFile file1 = Ebean.find(PersistentFile.class, id);
|
||||
PersistentFileContent content1 = Ebean.find(PersistentFileContent.class, contentId);
|
||||
|
||||
Assert.assertNull(file1);
|
||||
Assert.assertNull(content1);
|
||||
|
||||
}
|
||||
|
||||
// public void testDeleteMany() {
|
||||
//
|
||||
// Customer c = new Customer();
|
||||
// c.setName("Fiona");
|
||||
// c.setStatus(Customer.Status.ACTIVE);
|
||||
// c.addContact(new Contact("Fiona", "Black"));
|
||||
// c.addContact(new Contact("Tracy", "Red"));
|
||||
//
|
||||
// Ebean.save(c);
|
||||
//
|
||||
// Ebean.delete(Customer.class, c.getId());
|
||||
//
|
||||
// Customer deletedCustomer = Ebean.find(Customer.class, c.getId());
|
||||
//
|
||||
// Assert.assertNull(deletedCustomer);
|
||||
//
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.PFile;
|
||||
import com.avaje.tests.model.basic.PFileContent;
|
||||
|
||||
public class TestDeleteOneToOneMultiple extends TestCase {
|
||||
|
||||
public void testCreateDeletePersistentFile() {
|
||||
|
||||
PFile persistentFile = new PFile("test.txt", new PFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
Integer id = persistentFile.getId();
|
||||
Integer contentId = persistentFile.getFileContent().getId();
|
||||
|
||||
// should delete file and fileContent
|
||||
Ebean.delete(PFile.class, id);
|
||||
System.out.println("finished delete");
|
||||
|
||||
PFile file1 = Ebean.find(PFile.class, id);
|
||||
PFileContent content1 = Ebean.find(PFileContent.class, contentId);
|
||||
|
||||
Assert.assertNull(file1);
|
||||
Assert.assertNull(content1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.TMapSuperEntity;
|
||||
|
||||
public class TestDeletePartialNoVersion extends TestCase {
|
||||
|
||||
public void testNoVersion() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babanaone");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = Ebean.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name")
|
||||
.findUnique();
|
||||
|
||||
Assert.assertNotNull(e2);
|
||||
|
||||
e2.setName("banaban2");
|
||||
Ebean.save(e2);
|
||||
|
||||
Ebean.delete(e2);
|
||||
}
|
||||
|
||||
|
||||
public void testWithVersion() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babanatwo");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = Ebean.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name, version")
|
||||
.findUnique();
|
||||
|
||||
Assert.assertNotNull(e2);
|
||||
|
||||
e2.setName("banaban2two");
|
||||
Ebean.save(e2);
|
||||
|
||||
Ebean.delete(e2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.LogLevel;
|
||||
import com.avaje.tests.model.embedded.EMain;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestDynamicUpdate extends TestCase {
|
||||
|
||||
public void testUpdate() {
|
||||
|
||||
// insert
|
||||
EMain b = new EMain();
|
||||
b.setName("aaa");
|
||||
b.getEmbeddable().setDescription("123");
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
server.save(b);
|
||||
|
||||
assertNotNull(b.getId());
|
||||
|
||||
// reload object und update the name
|
||||
EMain b2 = server.find(EMain.class, b.getId());
|
||||
|
||||
b2.getEmbeddable().setDescription("ABC");
|
||||
server.save(b2);
|
||||
|
||||
//server.getAdminLogging().setLogLevel(LogLevel.SQL);
|
||||
|
||||
server.beginTransaction().setLogLevel(LogLevel.SQL);
|
||||
try {
|
||||
EMain b3 = server.find(EMain.class, b.getId());
|
||||
assertEquals("ABC", b3.getEmbeddable().getDescription());
|
||||
} finally {
|
||||
server.endTransaction();
|
||||
}
|
||||
EMain b4 = server.find(EMain.class, b.getId());
|
||||
b4.setName("bbb");
|
||||
b4.getEmbeddable().setDescription("123");
|
||||
server.save(b4);
|
||||
|
||||
EMain b5 = server.find(EMain.class, b.getId());
|
||||
assertEquals("123", b5.getEmbeddable().getDescription());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
public class TestErrorBindLog extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
GlobalProperties.put("somethingelse", "d:/junk2");
|
||||
try {
|
||||
Ebean.find(Order.class)
|
||||
.where().gt("id", "JUNK")
|
||||
.findList();
|
||||
|
||||
} catch (PersistenceException e){
|
||||
String msg = e.getMessage();
|
||||
e.printStackTrace();
|
||||
Assert.assertTrue(msg.contains("Bind values:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Update;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
|
||||
public class TestExplicitInsert extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
//GlobalProperties.put("ebean.classes", ""+LDPerson.class.toString()+","+EBasic.class.toString());
|
||||
|
||||
EBasic b = new EBasic();
|
||||
b.setName("exp insert");
|
||||
b.setDescription("explicit insert");
|
||||
b.setStatus(EBasic.Status.ACTIVE);
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
server.insert(b);
|
||||
|
||||
assertNotNull(b.getId());
|
||||
|
||||
EBasic b2 = server.find(EBasic.class, b.getId());
|
||||
b2.setId(null);
|
||||
|
||||
b2.setName("force insert");
|
||||
server.insert(b2);
|
||||
|
||||
assertNotNull(b2.getId());
|
||||
assertTrue(!b.getId().equals(b2.getId()));
|
||||
|
||||
|
||||
List<EBasic> list = server.find(EBasic.class)
|
||||
.setMaxRows(10)
|
||||
.findList();
|
||||
|
||||
assertTrue(list.size() >= 2);
|
||||
|
||||
int firstRow = 1;
|
||||
List<EBasic> list2 = server.find(EBasic.class)
|
||||
.order().asc("id")
|
||||
.setFirstRow(firstRow)
|
||||
.setMaxRows(10)
|
||||
.findList();
|
||||
|
||||
int expectedCount = list.size() -firstRow;
|
||||
if (expectedCount > 0){
|
||||
assertEquals(expectedCount, list2.size());
|
||||
} else {
|
||||
assertTrue(list2.isEmpty());
|
||||
}
|
||||
|
||||
Update<EBasic> update = Ebean.createUpdate(EBasic.class, "update ebasic set description = 'test'");
|
||||
|
||||
int rows = update.execute();
|
||||
|
||||
assertTrue(rows > 0);
|
||||
Ebean.externalModification("e_basic", true, false, true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.FutureIds;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestFetchId extends TestCase {
|
||||
|
||||
|
||||
public void testFetchId() throws InterruptedException, ExecutionException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("details")
|
||||
.where().gt("id", 1)
|
||||
.gt("details.id", 0)
|
||||
.query();
|
||||
|
||||
List<Object> ids = Ebean.getServer(null).findIds(query, null);
|
||||
|
||||
FutureIds<Order> futureIds = Ebean.getServer(null).findFutureIds(query,null);
|
||||
|
||||
// this list is likely empty at this point and
|
||||
// will get populated in the background
|
||||
List<Object> partial = futureIds.getPartialIds();
|
||||
|
||||
// this is likely 0 or a small number
|
||||
System.out.println("partial: " + partial.size());
|
||||
|
||||
// wait for all the id's to be fetched
|
||||
List<Object> idList = futureIds.get();
|
||||
Assert.assertTrue("same instance", partial == idList);
|
||||
|
||||
Assert.assertTrue("sz > 0", ids.size() > 0);
|
||||
System.out.println("ids: " + partial);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.EBasicVer;
|
||||
|
||||
public class TestIUDVanilla extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
EBasicVer e0 = new EBasicVer();
|
||||
e0.setName("vanilla");
|
||||
|
||||
Ebean.save(e0);
|
||||
|
||||
// // only use the below test when not using enhancement
|
||||
// boolean entity = (e0 instanceof EntityBean);
|
||||
// Assert.assertTrue(!entity);
|
||||
|
||||
Assert.assertNotNull(e0.getId());
|
||||
Assert.assertNotNull(e0.getLastUpdate());
|
||||
|
||||
Timestamp lastUpdate0 = e0.getLastUpdate();
|
||||
|
||||
e0.setName("modified");
|
||||
Ebean.save(e0);
|
||||
|
||||
Timestamp lastUpdate1 = e0.getLastUpdate();
|
||||
Assert.assertNotNull(lastUpdate1);
|
||||
Assert.assertNotSame(lastUpdate0, lastUpdate1);
|
||||
|
||||
EBasicVer e2 = Ebean.getServer(null).createEntityBean(EBasicVer.class);
|
||||
|
||||
HashSet<String> loaded = new HashSet<String>();
|
||||
loaded.add("id");
|
||||
loaded.add("lastUpdate");
|
||||
loaded.add("name");
|
||||
|
||||
e2.setId(e0.getId());
|
||||
e2.setLastUpdate(lastUpdate1);
|
||||
|
||||
Ebean.getBeanState(e2).setLoaded(loaded);
|
||||
e2.setName("forcedUpdate");
|
||||
Ebean.save(e2);
|
||||
|
||||
EBasicVer e3 = new EBasicVer();
|
||||
e3.setId(e0.getId());
|
||||
e3.setName("ModNoOCC");
|
||||
// e3.setLastUpdate(e2.getLastUpdate());
|
||||
|
||||
Ebean.update(e3);
|
||||
|
||||
e3.setName("ModAgain");
|
||||
e3.setDescription("Banana");
|
||||
|
||||
|
||||
Set<String> updateProps = new HashSet<String>();
|
||||
updateProps.add("name");
|
||||
updateProps.add("description");
|
||||
|
||||
Ebean.update(e3, updateProps);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
|
||||
public class TestInEmpty extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.where()
|
||||
.gt("id", 0)
|
||||
.in("id",new Object[0])
|
||||
.findList();
|
||||
|
||||
Assert.assertEquals(0, list.size());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Car;
|
||||
import com.avaje.tests.model.basic.Truck;
|
||||
import com.avaje.tests.model.basic.Vehicle;
|
||||
|
||||
public class TestInheritRef extends TestCase {
|
||||
|
||||
|
||||
public void testAssocOne() {
|
||||
|
||||
Ebean.createUpdate(Vehicle.class, "delete from vehicle");
|
||||
|
||||
Car c = new Car();
|
||||
c.setLicenseNumber("C6788");
|
||||
c.setDriver("CarDriver");
|
||||
Ebean.save(c);
|
||||
|
||||
Truck t = new Truck();
|
||||
t.setLicenseNumber("T1098");
|
||||
t.setCapacity(20D);
|
||||
Ebean.save(t);
|
||||
|
||||
List<Vehicle> list = Ebean.find(Vehicle.class)
|
||||
.setAutofetch(false)
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
for (Vehicle vehicle : list) {
|
||||
if (vehicle instanceof Truck){
|
||||
Truck truck = (Truck)vehicle;
|
||||
Assert.assertTrue(truck.getLicenseNumber().equals("T1098"));
|
||||
Assert.assertTrue(truck.getCapacity() == 20D);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
import com.avaje.tests.model.basic.TJodaEntity;
|
||||
|
||||
public class TestJodaType extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
BeanDescriptor<TJodaEntity> beanDescriptor = server.getBeanDescriptor(TJodaEntity.class);
|
||||
BeanProperty beanProperty = beanDescriptor.getBeanProperty("localTime");
|
||||
ScalarType<?> scalarType = beanProperty.getScalarType();
|
||||
|
||||
Assert.assertNotNull(scalarType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Car;
|
||||
import com.avaje.tests.model.basic.Trip;
|
||||
import com.avaje.tests.model.basic.Vehicle;
|
||||
import com.avaje.tests.model.basic.VehicleDriver;
|
||||
|
||||
public class TestJoinInheritance extends TestCase {
|
||||
|
||||
|
||||
/**
|
||||
* Test join hierarchy assoc one.
|
||||
*
|
||||
* This test catches the problem where the discriminator column is select
|
||||
* but is not skipped if the parent bean is already in the presistence context.
|
||||
*
|
||||
* Test case: We have several trips with different addresses but always the same
|
||||
* driver and car (the cars could be different). So the driver will be loaded
|
||||
* when the first trip is loaded but when the second trip is loaded the driver is
|
||||
* found in the persistence context. So the bean is not read but the index in the
|
||||
* result set is forwarded by the number of properties - however, the vehicle type
|
||||
* column was not being skipped - but the query reads happily - only the data is
|
||||
* then wrong in the address object.
|
||||
*/
|
||||
public void testJoinHierarchyAssocOne() {
|
||||
|
||||
Ebean.createUpdate(Vehicle.class, "delete from vehicle");
|
||||
|
||||
|
||||
Address address = new Address();
|
||||
address.setLine1("Street");
|
||||
address.setLine2("Street");
|
||||
address.setCity("City");
|
||||
|
||||
address.setCretime(new Timestamp(new Date().getTime()));
|
||||
|
||||
Ebean.save(address);
|
||||
|
||||
Car c = new Car();
|
||||
c.setLicenseNumber("C6788");
|
||||
c.setDriver("CarDriver");
|
||||
c.setRegistrationDate(new Date());
|
||||
Ebean.save(c);
|
||||
|
||||
VehicleDriver driver = new VehicleDriver();
|
||||
|
||||
driver.setVehicle(c);
|
||||
driver.setAddress(address);
|
||||
driver.setLicenseIssuedOn(new Date());
|
||||
Ebean.save(driver);
|
||||
|
||||
final String line1 = "Street1";
|
||||
final String line2 = "Street2";
|
||||
final String city = "City";
|
||||
|
||||
int nrDrivers = 2;
|
||||
for (int i = 0; i < nrDrivers;i++){
|
||||
Address address1 = new Address();
|
||||
address1.setLine1(line1);
|
||||
address1.setLine2(line2);
|
||||
address1.setCity(city);
|
||||
Ebean.save(address1);
|
||||
|
||||
Trip trip = new Trip();
|
||||
trip.setVehicleDriver(driver);
|
||||
trip.setAddress(address1);
|
||||
trip.setCretime(new Timestamp(new Date().getTime()));
|
||||
Ebean.save(trip);
|
||||
}
|
||||
|
||||
Ebean.beginTransaction();
|
||||
|
||||
Query<Trip> q = Ebean.createQuery(Trip.class, "join address join vehicleDriver ");
|
||||
|
||||
List<Trip> trips = q.findList();
|
||||
|
||||
Assert.assertTrue(trips.size() == nrDrivers);
|
||||
|
||||
for (Trip t:trips){
|
||||
Address a = t.getAddress();
|
||||
Assert.assertTrue(line1.equals(a.getLine1()));
|
||||
Assert.assertTrue(line2.equals(a.getLine2()));
|
||||
Assert.assertTrue(city.equals(a.getCity()));
|
||||
}
|
||||
|
||||
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLazyLoadEmptyOneToMany extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setName("testll");
|
||||
|
||||
Ebean.save(c);
|
||||
|
||||
Customer c1 = Ebean.find(Customer.class)
|
||||
.setAutofetch(false)
|
||||
.select("id")
|
||||
.fetch("contacts", "id")
|
||||
.where().idEq(c.getId())
|
||||
.findUnique();
|
||||
|
||||
List<Contact> contacts = c1.getContacts();
|
||||
int sz = contacts.size();
|
||||
|
||||
Assert.assertTrue(sz == 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLazyLoadInCache extends TestCase {
|
||||
|
||||
|
||||
public void testLoadInCache() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<?, Customer> map = Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.setLoadBeanCache(true)
|
||||
//.setUseCache(true)
|
||||
.setReadOnly(true)
|
||||
.orderBy().asc("id")
|
||||
.findMap();
|
||||
|
||||
Assert.assertTrue(map.size() > 0);
|
||||
|
||||
Object id = map.keySet().iterator().next();
|
||||
|
||||
Customer cust1 = map.get(id);
|
||||
|
||||
Customer cust1B = Ebean.find(Customer.class)
|
||||
.setReadOnly(true)
|
||||
.setUseCache(true)
|
||||
.setId(id)
|
||||
.findUnique();
|
||||
|
||||
Assert.assertTrue(cust1 != cust1B);
|
||||
|
||||
Set<String> loadedProps = Ebean.getBeanState(cust1).getLoadedProps();
|
||||
|
||||
Assert.assertTrue(loadedProps.contains("name"));
|
||||
Assert.assertFalse(loadedProps.contains("status"));
|
||||
|
||||
cust1.getStatus();
|
||||
|
||||
// null after lazy load
|
||||
Assert.assertNull(Ebean.getBeanState(cust1).getLoadedProps());
|
||||
|
||||
// a readOnly reference
|
||||
Address billingAddress = cust1.getBillingAddress();
|
||||
BeanState billAddrState = Ebean.getBeanState(billingAddress);
|
||||
Assert.assertTrue(billAddrState.isReference());
|
||||
Assert.assertTrue(billAddrState.isReadOnly());
|
||||
|
||||
// lazy load .. no longer a reference
|
||||
billingAddress.getCity();
|
||||
Assert.assertFalse(billAddrState.isReference());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLimitQuery extends TestCase {
|
||||
|
||||
public void testNothing() {
|
||||
|
||||
}
|
||||
|
||||
public void testLimitWithMany() {
|
||||
rob();
|
||||
rob();
|
||||
}
|
||||
private void rob() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
boolean h2Db = "h2".equals(server.getDatabasePlatform().getName());
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("details")
|
||||
.where().gt("details.id", 0)
|
||||
.setMaxRows(10);
|
||||
//.findList();
|
||||
|
||||
List<Order> list = query.findList();
|
||||
|
||||
Assert.assertTrue("sz > 0", list.size() > 0);
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
boolean hasDetailsJoin = sql.indexOf("join o_order_detail") > -1;
|
||||
boolean hasLimit = sql.indexOf("limit 11") > -1;
|
||||
boolean hasSelectedDetails = sql.indexOf("od.id,") > -1;
|
||||
boolean hasDistinct = sql.indexOf("select distinct") > -1;
|
||||
|
||||
Assert.assertTrue(hasDetailsJoin);
|
||||
Assert.assertFalse(hasSelectedDetails);
|
||||
Assert.assertTrue(hasDistinct);
|
||||
if (h2Db){
|
||||
Assert.assertTrue(hasLimit);
|
||||
}
|
||||
|
||||
query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.fetch("details")
|
||||
.setMaxRows(10);
|
||||
|
||||
query.findList();
|
||||
|
||||
sql = query.getGeneratedSql();
|
||||
hasDetailsJoin = sql.indexOf("left outer join o_order_detail") > -1;
|
||||
hasLimit = sql.indexOf("limit 11") > -1;
|
||||
hasSelectedDetails = sql.indexOf("od.id") > -1;
|
||||
hasDistinct = sql.indexOf("select distinct") > -1;
|
||||
|
||||
Assert.assertFalse("no join with maxRows",hasDetailsJoin);
|
||||
Assert.assertFalse(hasSelectedDetails);
|
||||
Assert.assertFalse(hasDistinct);
|
||||
if (h2Db){
|
||||
Assert.assertTrue(hasLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Country;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLoadBeanCache extends TestCase {
|
||||
|
||||
public void testLoad() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Map<?, Country> map = Ebean.find(Country.class)
|
||||
.setLoadBeanCache(true)
|
||||
.setUseQueryCache(true)
|
||||
.setReadOnly(true)
|
||||
.order("name")
|
||||
.findMap();
|
||||
|
||||
Country loadedNz = map.get("NZ");
|
||||
|
||||
// this will hit the cache
|
||||
Country nz = Ebean.find(Country.class, "NZ");
|
||||
|
||||
Assert.assertTrue(loadedNz == nz);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.EBasicVer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestLogTransLogOnError extends TestCase {
|
||||
|
||||
public void testQueryError() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Transaction t = Ebean.beginTransaction();
|
||||
try {
|
||||
t.log("--- hello");
|
||||
Ebean.find(Customer.class).findList();
|
||||
Ebean.find(Order.class).where().gt("id", 1).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer();
|
||||
newBean.setDescription("something");
|
||||
newBean.setName("aName");
|
||||
|
||||
//Ebean.save(newBean);
|
||||
|
||||
t.log("--- next query should error");
|
||||
List<Customer> list = Ebean.find(Customer.class)
|
||||
.where().eq("id", "NotAnInt!!")
|
||||
.findList();
|
||||
|
||||
Assert.assertEquals(0, list.size());
|
||||
// Get here with mysql?
|
||||
//Assert.assertTrue(false);
|
||||
|
||||
} catch (RuntimeException e){
|
||||
//e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public void testPersistError() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Transaction t = Ebean.beginTransaction();
|
||||
try {
|
||||
t.log("--- hello testPersistError");
|
||||
Ebean.find(Customer.class).findList();
|
||||
|
||||
EBasicVer newBean = new EBasicVer();
|
||||
newBean.setDescription("something sdfjksdjflsjdflsjdflksjdfkjd fsjdfkjsdkfjsdkfjskdjfskjdf"
|
||||
+" sjdf sdjflksjdfkjsdlfkjsdkfjs ksjdfksjdlfjsldf something sdfjksdjflsjdflsjdflksjdfkjd"
|
||||
+"fsjdfkjsdkfjsdkfjskdjfskjdf sjdf sdjflksjdfkjsdlfkjsdkfjs ksjdfksjdlfjsldf something s"
|
||||
+"dfjksdjflsjdflsjdflksjdfkjd fsjdfkjsdkfjsdkfjskdjfskjdf sjdf sdjflksjdfkjsdlfkjsdkfjs ");
|
||||
newBean.setName("aName");
|
||||
|
||||
t.log("--- next insert should error");
|
||||
Ebean.save(newBean);
|
||||
|
||||
// never get here
|
||||
Assert.assertTrue(false);
|
||||
|
||||
} catch (RuntimeException e){
|
||||
//e.printStackTrace();
|
||||
Assert.assertTrue(true);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.common.BeanList;
|
||||
import com.avaje.tests.model.basic.MRole;
|
||||
import com.avaje.tests.model.basic.MUser;
|
||||
|
||||
public class TestM2MCascadeOne extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
MUser u = new MUser();
|
||||
u.setUserName("testM2M");
|
||||
|
||||
List<MRole> roles = u.getRoles();
|
||||
if (roles != null){
|
||||
if (roles instanceof BeanList<?>){
|
||||
System.out.println("enhancement checkNullManyFields=true successful");
|
||||
}
|
||||
}
|
||||
|
||||
Ebean.save(u);
|
||||
|
||||
MRole r0 = new MRole();
|
||||
r0.setRoleName("rol_0");
|
||||
Ebean.save(r0);
|
||||
|
||||
MRole r1 = new MRole();
|
||||
r1.setRoleName("rol_1");
|
||||
|
||||
MUser u1 = Ebean.find(MUser.class, u.getUserid());
|
||||
|
||||
u1.addRole(r0);
|
||||
u1.addRole(r1);
|
||||
|
||||
Ebean.save(u1);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.MRole;
|
||||
import com.avaje.tests.model.basic.MUser;
|
||||
|
||||
public class TestM2MVanilla extends TestCase {
|
||||
|
||||
public void testVanilla() {
|
||||
|
||||
MRole r1 = new MRole();
|
||||
r1.setRoleName("role1");
|
||||
Ebean.save(r1);
|
||||
|
||||
MRole r2 = new MRole();
|
||||
r2.setRoleName("role2");
|
||||
Ebean.save(r2);
|
||||
|
||||
MRole r3 = new MRole();
|
||||
r3.setRoleName("role3");
|
||||
Ebean.save(r3);
|
||||
|
||||
MUser u0 = new MUser();
|
||||
u0.setUserName("something");
|
||||
|
||||
Ebean.save(u0);
|
||||
|
||||
MUser user = Ebean.find(MUser.class, u0.getUserid());
|
||||
|
||||
List<MRole> roleList = new ArrayList<MRole>();
|
||||
roleList.add(r1);
|
||||
roleList.add(r2);
|
||||
|
||||
user.setRoles(roleList);
|
||||
|
||||
Ebean.save(user);
|
||||
//Ebean.saveManyToManyAssociations(user, "roles");
|
||||
|
||||
MUser checkUser = Ebean.find(MUser.class, u0.getUserid());
|
||||
List<MRole> checkRoles = checkUser.getRoles();
|
||||
Assert.assertNotNull(checkRoles);
|
||||
Assert.assertEquals(2, checkRoles.size());
|
||||
|
||||
checkRoles.add(r3);
|
||||
|
||||
Ebean.save(checkUser);
|
||||
//Ebean.saveManyToManyAssociations(checkUser, "roles");
|
||||
|
||||
MUser checkUser2 = Ebean.find(MUser.class, u0.getUserid());
|
||||
List<MRole> checkRoles2 = checkUser2.getRoles();
|
||||
Assert.assertNotNull(checkRoles2);
|
||||
Assert.assertEquals("added a role", 3, checkRoles2.size());
|
||||
|
||||
Query<MUser> rolesQuery0 = Ebean.find(MUser.class)
|
||||
.where().eq("roles", r1)
|
||||
.query();
|
||||
|
||||
rolesQuery0.findList();
|
||||
|
||||
Query<MUser> rolesQuery = Ebean.find(MUser.class)
|
||||
.where().in("roles", roleList)
|
||||
.query();
|
||||
|
||||
List<MUser> userInRolesList = rolesQuery.findList();
|
||||
Assert.assertTrue(userInRolesList.size() > 0);
|
||||
|
||||
List<MUser> list = Ebean.find(MUser.class)
|
||||
.where().in("roles", roleList)
|
||||
.filterMany("roles").eq("roleName", "role1")
|
||||
.findList();
|
||||
|
||||
MUser mUser = list.get(0);
|
||||
List<MRole> roles = mUser.getRoles();
|
||||
Assert.assertEquals(1, roles.size());
|
||||
|
||||
Ebean.refreshMany(mUser, "roles");
|
||||
Assert.assertEquals(1, mUser.getRoles().size());
|
||||
|
||||
|
||||
checkRoles2.remove(0);
|
||||
checkRoles2.remove(0);
|
||||
Ebean.saveManyToManyAssociations(checkUser2,"roles");
|
||||
|
||||
|
||||
checkUser2 = Ebean.find(MUser.class, u0.getUserid());
|
||||
checkRoles2 = checkUser2.getRoles();
|
||||
Assert.assertNotNull(checkRoles2);
|
||||
Assert.assertEquals("added a role", 1, checkRoles2.size());
|
||||
|
||||
|
||||
Ebean.delete(checkUser2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestManyLazyLoad extends TestCase {
|
||||
|
||||
|
||||
public void testLazyLoadRef() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class).order().asc("id").findList();
|
||||
Assert.assertTrue(list.size()+" > 0", list.size() > 0);
|
||||
|
||||
// just use the first one
|
||||
Order order = list.get(0);
|
||||
|
||||
// get it as a reference
|
||||
Order order1 = Ebean.getReference(Order.class, order.getId());
|
||||
Assert.assertNotNull(order1);
|
||||
|
||||
Date orderDate = order1.getOrderDate();
|
||||
Assert.assertNotNull(orderDate);
|
||||
|
||||
List<OrderDetail> details = order1.getDetails();
|
||||
|
||||
// lazy load the details
|
||||
int sz = details.size();
|
||||
Assert.assertTrue(sz+" > 0", sz > 0);
|
||||
|
||||
Order o = details.get(0).getOrder();
|
||||
Assert.assertTrue("same instance", o == order1);
|
||||
|
||||
|
||||
// change order... list before a scalar property
|
||||
Order order2 = Ebean.getReference(Order.class, order.getId());
|
||||
Assert.assertNotNull(order2);
|
||||
|
||||
List<OrderDetail> details2 = order2.getDetails();
|
||||
|
||||
// lazy load the details
|
||||
int sz2 = details2.size();
|
||||
Assert.assertTrue(sz2+" > 0", sz2 > 0);
|
||||
|
||||
Order o2 = details2.get(0).getOrder();
|
||||
Assert.assertTrue("same instance", o2 == order2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.TMapSuperEntity;
|
||||
|
||||
public class TestMappedSuper extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
TMapSuperEntity e = new TMapSuperEntity();
|
||||
e.setName("babana");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
// select includes a transient property
|
||||
TMapSuperEntity e2 = Ebean.find(TMapSuperEntity.class)
|
||||
.where().idEq(e.getId())
|
||||
.select("id, name, myint, someObject, bananan")
|
||||
.findUnique();
|
||||
|
||||
Assert.assertNotNull(e2);
|
||||
|
||||
// using a raw SQL query that populates a transient field
|
||||
Query<TMapSuperEntity> query = Ebean.createNamedQuery(TMapSuperEntity.class, "testTransient");
|
||||
|
||||
List<TMapSuperEntity> list = query.where()
|
||||
.gt("id", 0)
|
||||
.istartsWith("name", "bab")
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(list.size() >= 1);
|
||||
TMapSuperEntity e3 = list.get(0);
|
||||
Integer myint = e3.getMyint();
|
||||
Assert.assertEquals(Integer.valueOf(12), myint);
|
||||
|
||||
TMapSuperEntity eSaveDelete = new TMapSuperEntity();
|
||||
eSaveDelete.setName("babana");
|
||||
|
||||
Ebean.save(eSaveDelete);
|
||||
|
||||
Ebean.delete(eSaveDelete);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.OCar;
|
||||
import com.avaje.tests.model.basic.OEngine;
|
||||
import com.avaje.tests.model.basic.OGearBox;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestMultipleOneToOneIUD extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
OEngine engine = new OEngine();
|
||||
engine.setShortDesc("engine 1");
|
||||
|
||||
OGearBox gearBox = new OGearBox();
|
||||
gearBox.setBoxDesc("6 speed manual");
|
||||
gearBox.setSize(6);
|
||||
|
||||
OCar car = new OCar();
|
||||
car.setVin("xx4534");
|
||||
car.setName("test car");
|
||||
car.setEngine(engine);
|
||||
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
Ebean.save(gearBox);
|
||||
Ebean.save(car);
|
||||
|
||||
assertNotNull(car.getId());
|
||||
assertNotNull(engine.getEngineId());
|
||||
assertNotNull(gearBox.getId());
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
OCar c2 = Ebean.find(OCar.class, car.getId());
|
||||
assertNotNull(c2);
|
||||
assertNotNull(c2.getEngine());
|
||||
// gearBox not assigned yet
|
||||
assertNull(c2.getGearBox());
|
||||
|
||||
// ok, assign gearBox
|
||||
c2.setGearBox(gearBox);
|
||||
Ebean.save(c2);
|
||||
|
||||
// now all should be there...
|
||||
OCar c3 = Ebean.find(OCar.class, car.getId());
|
||||
assertNotNull(c3);
|
||||
assertNotNull(c3.getEngine());
|
||||
assertNotNull(c3.getGearBox());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.MNonUpdPropEntity;
|
||||
|
||||
public class TestNonUpdateProperty extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
MNonUpdPropEntity e = new MNonUpdPropEntity();
|
||||
e.setName("name");
|
||||
e.setNote("note");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
MNonUpdPropEntity e2 = Ebean.find(MNonUpdPropEntity.class, e.getId());
|
||||
|
||||
e2.setName("mod");
|
||||
Ebean.update(e2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestOrderByAnnotation extends TestCase {
|
||||
|
||||
public void testOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Customer custTest = ResetBasicData.createCustAndOrder("testOrderByAnn");
|
||||
|
||||
Customer customer = Ebean.find(Customer.class, custTest.getId());
|
||||
List<Order> orders = customer.getOrders();
|
||||
|
||||
Assert.assertTrue(orders.size() > 0);
|
||||
|
||||
|
||||
Query<Order> q1 = Ebean.find(Order.class)
|
||||
.fetch("details");
|
||||
|
||||
q1.findList();
|
||||
|
||||
String s1 = q1.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(s1.contains("order by t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderTotalAmountFormula extends TestCase {
|
||||
|
||||
public void testAsJoin() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> l0 = Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.fetch("orders", "status, totalAmount")
|
||||
.where()
|
||||
.eq("orders.details.product.name", "Desk")
|
||||
.like("contacts.firstName", "Ji%")
|
||||
|
||||
.findList();
|
||||
|
||||
for (Customer c0 : l0) {
|
||||
System.out.println("customer: "+c0.getId());
|
||||
List<Order> orders = c0.getOrders();
|
||||
for (Order order : orders) {
|
||||
System.out.println("... order:"+order);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.OrderAggregate;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderTotalAmountReportBean extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<OrderAggregate> l0 =
|
||||
Ebean.find(OrderAggregate.class)
|
||||
.findList();
|
||||
|
||||
for (OrderAggregate r0 : l0) {
|
||||
System.out.println(r0);
|
||||
}
|
||||
|
||||
List<OrderAggregate> list =
|
||||
Ebean.createNamedQuery(OrderAggregate.class,"total.amount")
|
||||
.where().gt("order.id", 0)
|
||||
.having().gt("totalAmount", 50)
|
||||
.findList();
|
||||
|
||||
for (OrderAggregate r1 : list) {
|
||||
System.out.println(r1);
|
||||
Assert.assertTrue(r1.getTotalAmount() > 20.50);
|
||||
// partial object query without totalItems
|
||||
// ... no lazy loading invoked on this type of bean
|
||||
Assert.assertTrue(r1.getTotalItems() == null);
|
||||
}
|
||||
|
||||
|
||||
List<OrderAggregate> l2 =
|
||||
Ebean.createQuery(OrderAggregate.class)
|
||||
.where().gt("order.id", 0)
|
||||
.having().lt("totalItems", 3).gt("totalAmount", 50)
|
||||
.findList();
|
||||
|
||||
for (OrderAggregate r2 : l2) {
|
||||
//System.out.println(r2);
|
||||
Assert.assertTrue(r2.getTotalItems() < 3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestOrderTotalAmountSql extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
String sql = "select order_id, sum(order_qty*unit_price) as total_amount from o_order_detail where order_qty > :minQty group by order_id";
|
||||
List<SqlRow> sqlRows =
|
||||
Ebean.createSqlQuery(sql)
|
||||
.setParameter("minQty",1)
|
||||
.findList();
|
||||
|
||||
for (SqlRow sqlRow : sqlRows) {
|
||||
Integer id = sqlRow.getInteger("order_id");
|
||||
Double amount = sqlRow.getDouble("total_amount");
|
||||
Assert.assertNotNull("sqlRows: "+sqlRows,id);
|
||||
Assert.assertNotNull("sqlRows: "+sqlRows,amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Page;
|
||||
import com.avaje.ebean.PagingList;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
public class TestPaging extends TestCase {
|
||||
|
||||
|
||||
private void loadData() {
|
||||
|
||||
int rowCount = Ebean.find(TOne.class).findRowCount();
|
||||
if (rowCount > 500){
|
||||
return;
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
TOne o = new TOne();
|
||||
|
||||
int rvalue = r.nextInt(100000);
|
||||
o.setName(rvalue+"name");
|
||||
o.setDescription(rvalue+"");
|
||||
Ebean.save(o);
|
||||
}
|
||||
Ebean.commitTransaction();
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
|
||||
loadData();
|
||||
//checkLastPage();
|
||||
//bgFetchOne();
|
||||
pagingOne();
|
||||
}
|
||||
|
||||
// private void checkLastPage() {
|
||||
//
|
||||
//
|
||||
// PagingList<TOne> pagingList =
|
||||
// Ebean.find(TOne.class)
|
||||
// .where().gt("name", "2")
|
||||
// .findPagingList(10);
|
||||
//
|
||||
//
|
||||
// pagingList.setFetchAhead(false);
|
||||
//
|
||||
// Page<TOne> lastPage = pagingList.getPage(pagingList.getTotalPageCount() - 1);
|
||||
// String displayLastPage = lastPage.getDisplayXtoYofZ(" to "," of ");
|
||||
// System.out.println("LASTPAGE: "+displayLastPage);
|
||||
//
|
||||
// List<TOne> list = lastPage.getList();
|
||||
// list.get(0);
|
||||
//
|
||||
// Assert.assertFalse(lastPage.hasNext());
|
||||
//
|
||||
// }
|
||||
|
||||
// @SuppressWarnings("unchecked")
|
||||
// private void bgFetchOne() {
|
||||
//
|
||||
//
|
||||
// Query<TOne> query = Ebean.find(TOne.class)
|
||||
// .setAutofetch(false)
|
||||
// .select("id")
|
||||
// .where().gt("name", "2")
|
||||
// .setBackgroundFetchAfter(10)
|
||||
// //.setMaxRows(20)
|
||||
// .orderBy("id");
|
||||
//
|
||||
// //query.findList();
|
||||
// //query.findIds();
|
||||
//
|
||||
//// long t1 = System.currentTimeMillis();
|
||||
//
|
||||
// List<TOne> ids = query.findList();
|
||||
// //List<Object> ids = query.findIds();
|
||||
//
|
||||
//// long t0 = System.currentTimeMillis();
|
||||
//// System.out.println("Got: "+ids.size());
|
||||
// BeanCollection<TOne> bc = (BeanCollection<TOne>)ids;
|
||||
// bc.backgroundFetchWait();
|
||||
//
|
||||
//// long ex0 = System.currentTimeMillis() - t0;
|
||||
//// long ex1 = System.currentTimeMillis() - t1;
|
||||
//// System.out.println("Got: "+ids.size());
|
||||
//// System.out.println("exetime t0:"+ex0+" t1:"+ex1);
|
||||
// //System.out.println("done "+bc.size());
|
||||
// }
|
||||
|
||||
private void pagingOne() throws InterruptedException {
|
||||
|
||||
loadData();
|
||||
|
||||
int pageSize = 10;
|
||||
|
||||
PagingList<TOne> pagingList =
|
||||
Ebean.find(TOne.class)
|
||||
.select("id")
|
||||
//.where().gt("name", "2")
|
||||
.findPagingList(10);
|
||||
|
||||
|
||||
// get the row count in the background...
|
||||
// ... otherwise it is fetched on demand
|
||||
// ... when getRowCount() or getPageCount()
|
||||
// ... is called
|
||||
pagingList.getFutureRowCount();
|
||||
|
||||
// get the first page
|
||||
Page<TOne> page = pagingList.getPage(0);
|
||||
//String display0 = page.getDisplayXtoYofZ(" to "," of ");
|
||||
//System.out.println("PAGE0: "+display0);
|
||||
|
||||
// get the beans from the page as a list
|
||||
List<TOne> list = page.getList();
|
||||
Assert.assertTrue("page size ",list.size() == pageSize);
|
||||
|
||||
int totalRows = pagingList.getTotalRowCount();
|
||||
Assert.assertTrue("page size ",totalRows >= list.size());
|
||||
|
||||
Thread.sleep(300);
|
||||
|
||||
Page<TOne> next = page.next();
|
||||
//String display1 = next.getDisplayXtoYofZ(" to "," of ");
|
||||
//System.out.println("PAGE1: "+display1);
|
||||
|
||||
List<TOne> list2 = next.getList();
|
||||
|
||||
Assert.assertTrue("page size ",list2.size() == pageSize);
|
||||
|
||||
if (page.hasNext()){
|
||||
Page<TOne> next3 = page.next();
|
||||
List<TOne> list3 = next3.getList();
|
||||
Assert.assertTrue("page size ",list3.size() == pageSize);
|
||||
}
|
||||
|
||||
|
||||
Page<TOne> lastPage = pagingList.getPage(pagingList.getTotalPageCount() - 1);
|
||||
//String displayLastPage = lastPage.getDisplayXtoYofZ(" to "," of ");
|
||||
//System.out.println("LASTPAGE: "+displayLastPage);
|
||||
|
||||
Assert.assertFalse(lastPage.hasNext());
|
||||
|
||||
checkForLoop();
|
||||
}
|
||||
|
||||
private void checkForLoop() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Query<TOne> query = server.find(TOne.class)
|
||||
.where().gt("name", "2")
|
||||
.query();
|
||||
|
||||
int pageSize = 10;
|
||||
|
||||
PagingList<TOne> pagingList = server.findPagingList(query, null, pageSize);
|
||||
|
||||
List<TOne> asList = pagingList.getAsList();
|
||||
for (int i = 0; i < asList.size(); i++) {
|
||||
if (i % 10 == 0){
|
||||
//System.out.println("here");
|
||||
}
|
||||
TOne tOne = asList.get(i);
|
||||
tOne.hashCode();
|
||||
//System.out.print(".");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestPersistenceContext extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
// implicit transaction with its own
|
||||
// persistence context
|
||||
Order oBefore = Ebean.find(Order.class, 1);
|
||||
|
||||
Order order = null;
|
||||
|
||||
// start a persistence context
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
|
||||
order = Ebean.find(Order.class, 1);
|
||||
|
||||
// not the same instance ...as a different
|
||||
// persistence context
|
||||
Assert.assertTrue(order != oBefore);
|
||||
|
||||
|
||||
// finds an existing bean in the persistence context
|
||||
// ... so doesn't even execute a query
|
||||
Order o2 = Ebean.find(Order.class, 1);
|
||||
Order o3 = Ebean.getReference(Order.class, 1);
|
||||
|
||||
// all the same instance
|
||||
Assert.assertTrue(order == o2);
|
||||
Assert.assertTrue(order == o3);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
|
||||
// implicit transaction with its own
|
||||
// persistence context
|
||||
Order oAfter = Ebean.find(Order.class, 1);
|
||||
|
||||
Assert.assertTrue(oAfter != oBefore);
|
||||
Assert.assertTrue(oAfter != order);
|
||||
|
||||
|
||||
Order testOrder = ResetBasicData.createOrderCustAndOrder("testPC");
|
||||
Integer id = testOrder.getCustomer().getId();
|
||||
Integer orderId = testOrder.getId();
|
||||
|
||||
// start a persistence context
|
||||
Ebean.beginTransaction();
|
||||
try {
|
||||
Customer customer = Ebean.find(Customer.class)
|
||||
.setUseCache(false)
|
||||
.setId(id)
|
||||
.findUnique();
|
||||
|
||||
System.gc();
|
||||
Order order2 = Ebean.find(Order.class, orderId);
|
||||
Customer customer2 = order2.getCustomer();
|
||||
|
||||
Assert.assertEquals(customer.getId(),customer2.getId());
|
||||
|
||||
Assert.assertTrue(customer == customer2);
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestQuery extends TestCase
|
||||
{
|
||||
|
||||
public void testCountOrderBy()
|
||||
{
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.order().asc("orderDate")
|
||||
.order().desc("id");
|
||||
//.orderBy("orderDate");
|
||||
|
||||
int rc = query.findList().size();
|
||||
//int rc = query.findRowCount();
|
||||
Assert.assertTrue(rc > 0);
|
||||
//String generatedSql = query.getGeneratedSql();
|
||||
//Assert.assertFalse(generatedSql.contains("order by"));
|
||||
|
||||
}
|
||||
|
||||
public void testForUpdate()
|
||||
{
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.setForUpdate(false)
|
||||
.setMaxRows(1)
|
||||
.order().asc("orderDate")
|
||||
.order().desc("id");
|
||||
|
||||
int rc = query.findList().size();
|
||||
Assert.assertTrue(rc > 0);
|
||||
assertTrue(query.getGeneratedSql().toLowerCase().indexOf("for update") < 0);
|
||||
|
||||
query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.setForUpdate(true)
|
||||
.setMaxRows(1)
|
||||
.order().asc("orderDate")
|
||||
.order().desc("id");
|
||||
|
||||
rc = query.findList().size();
|
||||
Assert.assertTrue(rc > 0);
|
||||
assertTrue(query.getGeneratedSql().toLowerCase().indexOf("for update") > -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.OrderBy;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryParse extends TestCase {
|
||||
|
||||
public void test() {
|
||||
//GlobalProperties.put("ebean.ddl.generate", "false");
|
||||
//GlobalProperties.put("ebean.ddl.run", "false");
|
||||
ResetBasicData.reset();
|
||||
|
||||
String oql = "where 1=1 order by customer.name desc, status";
|
||||
Query<Order> query = Ebean.createQuery(Order.class, oql);
|
||||
|
||||
OrderBy<Order> order = query.order();
|
||||
Assert.assertTrue(order.getProperties().size() == 2);
|
||||
Assert.assertEquals("customer.name",order.getProperties().get(0).getProperty());
|
||||
Assert.assertFalse(order.getProperties().get(0).isAscending());
|
||||
Assert.assertEquals("status",order.getProperties().get(1).getProperty());
|
||||
Assert.assertTrue(order.getProperties().get(1).isAscending());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryWhereBetween extends TestCase {
|
||||
|
||||
public void testCountOrderBy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Timestamp t = new Timestamp(System.currentTimeMillis());
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.where().betweenProperties("cretime","updtime", t)
|
||||
.order().asc("orderDate")
|
||||
.order().desc("id");
|
||||
|
||||
query.findList();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
Assert.assertTrue(sql.indexOf("between t0.cretime and t0.updtime") > -1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.cache.ServerCache;
|
||||
import com.avaje.ebean.cache.ServerCacheManager;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.core.CacheOptions;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.tests.model.basic.Country;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryWithCache extends TestCase {
|
||||
|
||||
|
||||
// public void testJoinCache() {
|
||||
//
|
||||
// ResetBasicData.reset();
|
||||
//
|
||||
// Ebean.getServer(null).runCacheWarming();
|
||||
//
|
||||
// Query<Order> query = Ebean.createQuery(Order.class)
|
||||
// .setAutofetch(false)
|
||||
// .fetch("customer","+cache +readonly")
|
||||
// .setId(1);
|
||||
//
|
||||
// Order order = query.findUnique();
|
||||
// Customer customer = order.getCustomer();
|
||||
// Assert.assertTrue(Ebean.getBeanState(customer).isReadOnly());
|
||||
//
|
||||
//// // invoke lazy loading
|
||||
//// customer.getName();
|
||||
////
|
||||
//// order = query.findUnique();
|
||||
//// customer = order.getCustomer();
|
||||
//// custState = Ebean.getBeanState(customer);
|
||||
//// Assert.assertFalse(custState.isReadOnly());
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public void testFindId() {
|
||||
//
|
||||
// ResetBasicData.reset();
|
||||
//
|
||||
// Order o = Ebean.find(Order.class)
|
||||
// .setUseCache(true)
|
||||
// .setReadOnly(true)
|
||||
// .setId(1)
|
||||
// .findUnique();
|
||||
//
|
||||
// BeanState beanState = Ebean.getBeanState(o);
|
||||
// Assert.assertTrue(beanState.isReadOnly());
|
||||
//
|
||||
// Order o2 = Ebean.find(Order.class)
|
||||
// .setUseCache(true)
|
||||
// .setReadOnly(true)
|
||||
// .setId(1)
|
||||
// .findUnique();
|
||||
//
|
||||
// BeanState beanState2 = Ebean.getBeanState(o2);
|
||||
//
|
||||
// // same instance as readOnly = true
|
||||
// Assert.assertTrue("not same instance", o != o2);
|
||||
// Assert.assertTrue(beanState2.isReadOnly());
|
||||
//
|
||||
// Order o3 = Ebean.find(Order.class)
|
||||
// .setUseCache(true)
|
||||
// .setReadOnly(false)
|
||||
// .setId(1)
|
||||
// .findUnique();
|
||||
//
|
||||
// // NOT the same instance as readOnly = false
|
||||
// Assert.assertTrue("not same instance", o != o3);
|
||||
// }
|
||||
|
||||
public void testCountryDeploy() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
BeanDescriptor<Country> beanDescriptor = server.getBeanDescriptor(Country.class);
|
||||
CacheOptions cacheOptions = beanDescriptor.getCacheOptions();
|
||||
|
||||
Assert.assertNotNull(cacheOptions);
|
||||
Assert.assertTrue(cacheOptions.isUseCache());
|
||||
Assert.assertTrue(cacheOptions.isReadOnly());
|
||||
Assert.assertTrue(beanDescriptor.isCacheSharableBeans());
|
||||
|
||||
ServerCacheManager serverCacheManager = server.getServerCacheManager();
|
||||
serverCacheManager.clear(Country.class);
|
||||
|
||||
ServerCache beanCache = serverCacheManager.getBeanCache(Country.class);
|
||||
Assert.assertEquals(0, beanCache.size());
|
||||
|
||||
Country nz1 = Ebean.getReference(Country.class, "NZ");
|
||||
Assert.assertEquals(0, beanCache.size());
|
||||
|
||||
// has the effect of loading the cache via lazy loading
|
||||
nz1.getName();
|
||||
Assert.assertEquals(1, beanCache.size());
|
||||
|
||||
Country nz2 = Ebean.getReference(Country.class, "NZ");
|
||||
Country nz2b = Ebean.getReference(Country.class, "NZ");
|
||||
|
||||
Country nz3 = Ebean.find(Country.class, "NZ");
|
||||
|
||||
Country nz4 = Ebean.find(Country.class)
|
||||
.setId("NZ")
|
||||
.setAutofetch(false)
|
||||
.setUseCache(false)
|
||||
.findUnique();
|
||||
|
||||
Assert.assertTrue(nz2 == nz2b);
|
||||
Assert.assertTrue(nz2 == nz3);
|
||||
Assert.assertTrue(nz3 != nz4);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.tests.model.basic.Address;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestReadOnlyPropagation extends TestCase {
|
||||
|
||||
public void testReadOnly() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.setReadOnly(true)
|
||||
.setId(1)
|
||||
.findUnique();
|
||||
|
||||
Assert.assertTrue(Ebean.getBeanState(order).isReadOnly());
|
||||
|
||||
|
||||
Customer customer = order.getCustomer();
|
||||
Assert.assertTrue(Ebean.getBeanState(customer).isReadOnly());
|
||||
|
||||
Address billingAddress = customer.getBillingAddress();
|
||||
Assert.assertTrue(Ebean.getBeanState(billingAddress).isReadOnly());
|
||||
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
BeanCollection<?> bc = (BeanCollection<?>)details;
|
||||
|
||||
Assert.assertTrue(bc.isReadOnly());
|
||||
Assert.assertTrue(!bc.isPopulated());
|
||||
|
||||
bc.size();
|
||||
Assert.assertTrue(bc.size() > 0);
|
||||
Assert.assertTrue(bc.isReadOnly());
|
||||
Assert.assertTrue(bc.isPopulated());
|
||||
try {
|
||||
details.add(new OrderDetail());
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
try {
|
||||
details.remove(0);
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
try {
|
||||
Iterator<OrderDetail> it = details.iterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
try {
|
||||
ListIterator<OrderDetail> it = details.listIterator();
|
||||
it.next();
|
||||
it.remove();
|
||||
Assert.assertTrue(false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
try {
|
||||
List<OrderDetail> subList = details.subList(0, 1);
|
||||
subList.remove(0);
|
||||
Assert.assertTrue(false);
|
||||
} catch (UnsupportedOperationException e){
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.PersistentFile;
|
||||
import com.avaje.tests.model.basic.PersistentFileContent;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestSaveDeleteOneToOne extends TestCase {
|
||||
|
||||
public void testCreateDeletePersistentFile() {
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt",
|
||||
new PersistentFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
Ebean.delete(persistentFile);
|
||||
}
|
||||
|
||||
public void testCreateLoadDeletePersistentFile() {
|
||||
PersistentFile persistentFile = new PersistentFile("test.txt",
|
||||
new PersistentFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
|
||||
persistentFile = Ebean.find(PersistentFile.class, persistentFile.getId());
|
||||
|
||||
PersistentFileContent persistentFileContent = persistentFile.getPersistentFileContent();
|
||||
|
||||
Assert.assertNotNull(persistentFileContent);
|
||||
|
||||
Assert.assertNotNull("load byte content", persistentFileContent.getContent());
|
||||
|
||||
Ebean.delete(persistentFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.PFile;
|
||||
import com.avaje.tests.model.basic.PFileContent;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestSaveDeleteOneToOneMultiple extends TestCase {
|
||||
|
||||
// public void testCreateDeletePFile() {
|
||||
// PFile persistentFile = new PFile("test.txt",
|
||||
// new PFileContent("test".getBytes()));
|
||||
//
|
||||
// Ebean.save(persistentFile);
|
||||
// Ebean.delete(persistentFile);
|
||||
// }
|
||||
|
||||
public void testCreateLoadDeletePFile() {
|
||||
PFile persistentFile = new PFile("test.txt",
|
||||
new PFileContent("test".getBytes()));
|
||||
|
||||
Ebean.save(persistentFile);
|
||||
|
||||
persistentFile = Ebean.find(PFile.class, persistentFile.getId());
|
||||
|
||||
PFileContent persistentFileContent = persistentFile.getFileContent();
|
||||
|
||||
Assert.assertNotNull(persistentFileContent);
|
||||
|
||||
Assert.assertNotNull("load byte content", persistentFileContent.getContent());
|
||||
|
||||
Ebean.delete(persistentFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.bean.SerializeControl;
|
||||
import com.avaje.ebean.common.BeanList;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.Order.Status;
|
||||
|
||||
public class TestSerialization extends TestCase {
|
||||
|
||||
public void testSerialization() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
Customer customer = server.getReference(Customer.class, 1);
|
||||
|
||||
Order o = server.createEntityBean(Order.class);
|
||||
o.setOrderDate(new Date(System.currentTimeMillis()));
|
||||
o.setStatus(Status.NEW);
|
||||
o.setCustomer(customer);
|
||||
|
||||
BeanList<OrderDetail> details = new BeanList<OrderDetail>();
|
||||
o.setDetails(details);
|
||||
|
||||
EntityBean eb = (EntityBean)o;
|
||||
|
||||
Order orderCopy = (Order)eb._ebean_createCopy();
|
||||
|
||||
Assert.assertNotNull(orderCopy.getDetails());
|
||||
Assert.assertNotNull(orderCopy.getCustomer());
|
||||
|
||||
EntityBeanIntercept ebi = eb._ebean_getIntercept();
|
||||
o.setStatus(Status.APPROVED);
|
||||
|
||||
ebi.setReadOnly(true);
|
||||
ebi.setLoaded();
|
||||
|
||||
try {
|
||||
o.setStatus(Status.COMPLETE);
|
||||
Assert.assertTrue("dont get here",false);
|
||||
} catch (IllegalStateException e){
|
||||
Assert.assertTrue("throws exception",true);
|
||||
}
|
||||
|
||||
SerializeControl.setVanilla(true);
|
||||
Assert.assertTrue(SerializeControl.isVanillaBeans());
|
||||
Assert.assertTrue(SerializeControl.isVanillaCollections());
|
||||
|
||||
Order testUsingSubclassing = new Order();
|
||||
if (testUsingSubclassing instanceof EntityBean){
|
||||
System.out.println("Need to run serialisation test with 'subclassing/proxies'");
|
||||
|
||||
} else {
|
||||
System.out.println("Testing serialisation of 'subclassing/proxies'");
|
||||
Object vanillaOrder = serialWriteRead(o, true);
|
||||
Assert.assertFalse("should be an EntityBean", (vanillaOrder instanceof EntityBean));
|
||||
Assert.assertTrue("should be an Order", (vanillaOrder instanceof Order));
|
||||
|
||||
Order vanOrder = (Order)vanillaOrder;
|
||||
Customer vanCustomer = vanOrder.getCustomer();
|
||||
List<OrderDetail> vanDetails = vanOrder.getDetails();
|
||||
|
||||
Assert.assertFalse("should NOT be an EntityBean", (vanCustomer instanceof EntityBean));
|
||||
Assert.assertFalse("should NOT be an BeanList", (vanDetails instanceof BeanList<?>));
|
||||
Assert.assertTrue("should be an ArrayList", (vanDetails instanceof ArrayList<?>));
|
||||
Assert.assertTrue("should be an Customer", (vanCustomer instanceof Customer));
|
||||
}
|
||||
|
||||
SerializeControl.setVanilla(false);
|
||||
|
||||
Object subclassOrder = serialWriteRead(o, false);
|
||||
Assert.assertTrue("should be an Order", (subclassOrder instanceof Order));
|
||||
Assert.assertTrue("should be an EntityBean", (subclassOrder instanceof EntityBean));
|
||||
|
||||
SerializeControl.setVanilla(true);
|
||||
|
||||
File serTestFile = new File("serTest");
|
||||
if (serTestFile.exists()){
|
||||
serTestFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private Object serialWriteRead(Object inputObject, boolean vanilla){
|
||||
|
||||
try {
|
||||
|
||||
|
||||
|
||||
File serTestFile = new File("serTest");
|
||||
FileOutputStream fout = new FileOutputStream(serTestFile);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fout);
|
||||
|
||||
oos.writeObject(inputObject);
|
||||
oos.close();
|
||||
|
||||
FileInputStream fin = new FileInputStream(serTestFile);
|
||||
|
||||
ObjectInputStream ois;
|
||||
if (vanilla){
|
||||
ois = new ObjectInputStream(fin);
|
||||
} else {
|
||||
ois = Ebean.getServer(null).createProxyObjectInputStream(fin);
|
||||
}
|
||||
Object readObject = ois.readObject();
|
||||
ois.close();
|
||||
return readObject;
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
Assert.assertTrue(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.Product;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestSharedInstancePropagation extends TestCase {
|
||||
|
||||
|
||||
/**
|
||||
* Test that the sharedInstance status is propagated on lazy loading.
|
||||
*/
|
||||
public void testSharedListNavigate() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Order order = Ebean.find(Order.class)
|
||||
.setAutofetch(false)
|
||||
.setUseCache(true)
|
||||
.setReadOnly(true)
|
||||
.setId(1)
|
||||
.findUnique();
|
||||
|
||||
|
||||
Assert.assertNotNull(order);
|
||||
Assert.assertTrue(Ebean.getBeanState(order).isReadOnly());
|
||||
|
||||
List<OrderDetail> details = order.getDetails();
|
||||
BeanCollection<?> bc = (BeanCollection<?>)details;
|
||||
Assert.assertTrue(bc.isReadOnly());
|
||||
Assert.assertFalse(bc.isPopulated());
|
||||
|
||||
// lazy load
|
||||
bc.size();
|
||||
|
||||
Assert.assertTrue(bc.isPopulated());
|
||||
Assert.assertTrue(bc.size() > 0);
|
||||
OrderDetail detail = details.get(0);
|
||||
|
||||
Assert.assertTrue(Ebean.getBeanState(detail).isReadOnly());
|
||||
Assert.assertFalse(Ebean.getBeanState(detail).isReference());
|
||||
|
||||
Product product = detail.getProduct();
|
||||
|
||||
Assert.assertTrue(Ebean.getBeanState(product).isReadOnly());
|
||||
|
||||
// lazy load
|
||||
product.getName();
|
||||
Assert.assertFalse(Ebean.getBeanState(product).isReference());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
|
||||
public class TestTransient extends TestCase {
|
||||
|
||||
public void testTransient() {
|
||||
|
||||
Customer cnew = new Customer();
|
||||
cnew.setName("testTrans");
|
||||
|
||||
Ebean.save(cnew);
|
||||
Integer custId = cnew.getId();
|
||||
|
||||
Customer c = Ebean.find(Customer.class).setAutofetch(false).setId(custId).findUnique();
|
||||
|
||||
Assert.assertNotNull(c);
|
||||
|
||||
BeanState beanState = Ebean.getBeanState(c);
|
||||
Assert.assertFalse("not new or dirty as transient", beanState.isNewOrDirty());
|
||||
|
||||
c.getLock().tryLock();
|
||||
try {
|
||||
c.setSelected(Boolean.TRUE);
|
||||
} finally {
|
||||
c.getLock().unlock();
|
||||
}
|
||||
|
||||
Boolean selected = c.getSelected();
|
||||
Assert.assertNotNull(selected);
|
||||
|
||||
Assert.assertFalse("not new or dirty as transient", beanState.isNewOrDirty());
|
||||
|
||||
Ebean.save(c);
|
||||
|
||||
selected = c.getSelected();
|
||||
Assert.assertNotNull(selected);
|
||||
|
||||
c.setName("Modified");
|
||||
Assert.assertTrue("dirty now", beanState.isNewOrDirty());
|
||||
|
||||
selected = c.getSelected();
|
||||
Assert.assertNotNull(selected);
|
||||
|
||||
Ebean.save(c);
|
||||
Assert.assertFalse("Not dirty after save", beanState.isNewOrDirty());
|
||||
|
||||
selected = c.getSelected();
|
||||
Assert.assertNotNull(selected);
|
||||
|
||||
String updateStmt = "update customer set name = 'Rob' where id = :id";
|
||||
int rows = Ebean.createUpdate(Customer.class, updateStmt).set("id", custId).execute();
|
||||
|
||||
Assert.assertTrue("changed name back", 1 == rows);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestTransientInternalFields extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> list = Ebean.find(Customer.class).findList();
|
||||
|
||||
Customer c = list.get(0);
|
||||
|
||||
Object back = serialWriteRead(c, false);
|
||||
|
||||
if (back instanceof EntityBean){
|
||||
EntityBean entityBean = (EntityBean)back;
|
||||
EntityBeanIntercept ebi = entityBean._ebean_getIntercept();
|
||||
if (ebi == null){
|
||||
ebi = entityBean._ebean_intercept();
|
||||
Assert.assertNotNull(ebi);
|
||||
}
|
||||
}
|
||||
|
||||
File serTestFile = new File("serTransTest");
|
||||
if (serTestFile.exists()){
|
||||
serTestFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
private Object serialWriteRead(Object inputObject, boolean vanilla){
|
||||
|
||||
try {
|
||||
|
||||
File serTestFile = new File("serTransTest");
|
||||
FileOutputStream fout = new FileOutputStream(serTestFile);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fout);
|
||||
|
||||
oos.writeObject(inputObject);
|
||||
oos.close();
|
||||
|
||||
FileInputStream fin = new FileInputStream(serTestFile);
|
||||
|
||||
ObjectInputStream ois;
|
||||
if (vanilla){
|
||||
ois = new ObjectInputStream(fin);
|
||||
} else {
|
||||
ois = Ebean.getServer(null).createProxyObjectInputStream(fin);
|
||||
}
|
||||
Object readObject = ois.readObject();
|
||||
ois.close();
|
||||
return readObject;
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
Assert.assertTrue(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestUpdateManyToOne extends TestCase{
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> custs = Ebean.find(Customer.class).findList();
|
||||
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class).setMaxRows(1).findList();
|
||||
|
||||
Order order = orders.get(0);
|
||||
Customer customer = order.getCustomer();
|
||||
|
||||
Customer changeCust = null;
|
||||
for (Customer c : custs) {
|
||||
if (!customer.getId().equals(c.getId())){
|
||||
changeCust = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
order.setCustomer(changeCust);
|
||||
Ebean.save(order);
|
||||
|
||||
order.setCustomer(customer);
|
||||
Ebean.save(order);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.TUuidEntity;
|
||||
|
||||
public class TestUuidInsert extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
TUuidEntity e = new TUuidEntity();
|
||||
e.setName("bana");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
TUuidEntity e2 = Ebean.find(TUuidEntity.class, e.getId());
|
||||
e2.setName("apple");
|
||||
|
||||
Ebean.save(e2);
|
||||
|
||||
Ebean.delete(e2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.UUOne;
|
||||
import com.avaje.tests.model.basic.UUTwo;
|
||||
|
||||
public class TestUuidInsertMasterDetail extends TestCase {
|
||||
|
||||
public void testInsert() {
|
||||
|
||||
UUTwo two = new UUTwo();
|
||||
two.setName("something");
|
||||
|
||||
ArrayList<UUTwo> list = new ArrayList<UUTwo>();
|
||||
list.add(two);
|
||||
|
||||
UUOne one = new UUOne();
|
||||
one.setName("some one");
|
||||
one.setComments(list);
|
||||
|
||||
Ebean.save(one);
|
||||
|
||||
UUOne oneB = Ebean.find(UUOne.class, one.getId());
|
||||
|
||||
UUTwo twoB = new UUTwo();
|
||||
twoB.setName("another something");
|
||||
oneB.getComments().add(twoB);
|
||||
|
||||
Ebean.save(oneB);
|
||||
}
|
||||
|
||||
public void testNullFK() {
|
||||
|
||||
UUTwo two = new UUTwo();
|
||||
two.setName("something");
|
||||
Ebean.save(two);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
|
||||
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebeaninternal.api.SpiTransaction;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestWeakPersistenceContext extends TestCase {
|
||||
|
||||
|
||||
public void testOne() {
|
||||
|
||||
PersistenceContext ctx = inner();
|
||||
|
||||
System.gc();
|
||||
|
||||
try {
|
||||
Thread.sleep(300);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// this is really only a HINT, so no guarantee
|
||||
// .. but the SUN JVM does do the business
|
||||
System.gc();
|
||||
|
||||
// Pass on the SUN JVM
|
||||
Object o3 = ctx.get(Order.class, 1);
|
||||
Assert.assertNull("Sun JVM should have GC'ed this bean",o3);
|
||||
|
||||
}
|
||||
|
||||
private PersistenceContext inner() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
SpiTransaction st = (SpiTransaction)transaction;
|
||||
PersistenceContext ctx = st.getPersistenceContext();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
//.select("id")
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
|
||||
Object o1 = ctx.get(Order.class, 1);
|
||||
|
||||
Assert.assertNotNull(o1);
|
||||
|
||||
Ebean.endTransaction();
|
||||
|
||||
Object o2 = ctx.get(Order.class, 1);
|
||||
Assert.assertNotNull(o2);
|
||||
|
||||
System.gc();
|
||||
|
||||
return ctx;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.avaje.tests.basic;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestWhereAnnotation extends TestCase {
|
||||
|
||||
public void testWhere() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
Customer custTest = ResetBasicData.createCustAndOrder("testWhereAnn");
|
||||
|
||||
Customer customer = Ebean.find(Customer.class, custTest.getId());
|
||||
List<Order> orders = customer.getOrders();
|
||||
|
||||
Assert.assertTrue(orders.size() > 0);
|
||||
|
||||
|
||||
Query<Customer> q1 = Ebean.find(Customer.class)
|
||||
.setUseCache(false)
|
||||
.fetch("orders")
|
||||
.where().idEq(1)
|
||||
.query();
|
||||
|
||||
q1.findUnique();
|
||||
String s1 = q1.getGeneratedSql();
|
||||
Assert.assertTrue(s1.contains("t1.order_date is not null"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.tests.basic.delete;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestDeleteByIdList extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
OrderDetail dummy = Ebean.getReference(OrderDetail.class, 1);
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
server.getBeanDescriptor(OrderDetail.class).cachePutBeanData(dummy);
|
||||
|
||||
Customer c0 = ResetBasicData.createCustAndOrder("DelIdList-0");
|
||||
assertNotNull(c0);
|
||||
|
||||
Customer c1 = ResetBasicData.createCustAndOrder("DelIdList-1");
|
||||
|
||||
List<Object> orderIds = Ebean.find(Order.class)
|
||||
.where().in("customer", c0,c1)
|
||||
.findIds();
|
||||
|
||||
assertEquals(2, orderIds.size());
|
||||
|
||||
|
||||
Ebean.delete(Order.class, orderIds);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.avaje.tests.basic.delete;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.OrderDetail;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestDeleteCascadeById extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
OrderDetail dummy = Ebean.getReference(OrderDetail.class, 1);
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
server.getBeanDescriptor(OrderDetail.class).cachePutBeanData(dummy);
|
||||
|
||||
Customer cust = ResetBasicData.createCustAndOrder("DelCas");
|
||||
assertNotNull(cust);
|
||||
|
||||
List<Order> orders = Ebean.find(Order.class)
|
||||
.where().eq("customer", cust)
|
||||
.findList();
|
||||
|
||||
assertEquals(1, orders.size());
|
||||
Order o = orders.get(0);
|
||||
assertNotNull(o);
|
||||
|
||||
Ebean.delete(o);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (C) 2009 Authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.tests.basic.encrypt;
|
||||
|
||||
import com.avaje.ebean.config.EncryptKey;
|
||||
|
||||
public class BasicEncryptKey implements EncryptKey {
|
||||
|
||||
private final String key;
|
||||
|
||||
public BasicEncryptKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getStringValue() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.avaje.tests.basic.encrypt;
|
||||
|
||||
import com.avaje.ebean.config.EncryptKey;
|
||||
import com.avaje.ebean.config.EncryptKeyManager;
|
||||
|
||||
public class BasicEncyptKeyManager implements EncryptKeyManager {
|
||||
|
||||
/**
|
||||
* Initialise the key manager.
|
||||
*/
|
||||
public void initialise() {
|
||||
|
||||
}
|
||||
|
||||
public EncryptKey getEncryptKey(String tableName, String columnName) {
|
||||
// Must be 16 Chars for Oracle function
|
||||
return new BasicEncryptKey("simple0123456789");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.avaje.tests.basic.encrypt;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebean.config.dbplatform.DbEncrypt;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.EBasicEncrypt;
|
||||
|
||||
public class TestEncrypt extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
EBasicEncrypt e = new EBasicEncrypt();
|
||||
e.setName("testname");
|
||||
e.setDescription("testdesc");
|
||||
e.setDob(new Date(System.currentTimeMillis()-100000));
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
Date earlyDob = new Date(System.currentTimeMillis()-500000);
|
||||
|
||||
List<EBasicEncrypt> qlList =
|
||||
Ebean.createQuery(EBasicEncrypt.class, "where description like :d and dob >= :dob")
|
||||
.setParameter("d", "testde%")
|
||||
.setParameter("dob", earlyDob)
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(qlList.size() > 0);
|
||||
|
||||
qlList =
|
||||
Ebean.createQuery(EBasicEncrypt.class, "find e (id, description) where description = :d")
|
||||
.setParameter("d", "testdesc")
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(qlList.size() == 1);
|
||||
|
||||
|
||||
|
||||
SqlQuery q = Ebean.createSqlQuery("select * from e_basicenc where id = :id");
|
||||
q.setParameter("id", e.getId());
|
||||
|
||||
SqlRow row = q.findUnique();
|
||||
String name = row.getString("name");
|
||||
Object desc = row.get("description");
|
||||
System.out.println("SqlRow: "+name+" "+desc);
|
||||
|
||||
EBasicEncrypt e1 = Ebean.find(EBasicEncrypt.class, e.getId());
|
||||
|
||||
String desc1 = e1.getDescription();
|
||||
System.out.println("Decrypted: "+desc1+" "+e1.getDob());
|
||||
|
||||
|
||||
e1.setName("testmod");
|
||||
e1.setDescription("moddesc");
|
||||
|
||||
Ebean.save(e1);
|
||||
|
||||
EBasicEncrypt e2 = Ebean.find(EBasicEncrypt.class, e.getId());
|
||||
|
||||
String desc2 = e2.getDescription();
|
||||
System.out.println("moddesc="+desc2);
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
DbEncrypt dbEncrypt = server.getDatabasePlatform().getDbEncrypt();
|
||||
|
||||
if (dbEncrypt == null){
|
||||
// can not test the where clause
|
||||
System.out.println("TestEncrypt: Not testing where clause as no DbEncrypt");
|
||||
|
||||
} else {
|
||||
|
||||
List<EBasicEncrypt> list =
|
||||
Ebean.find(EBasicEncrypt.class)
|
||||
.where().eq("description", "moddesc")
|
||||
.findList();
|
||||
|
||||
Assert.assertEquals(1, list.size());
|
||||
|
||||
list
|
||||
= Ebean.find(EBasicEncrypt.class)
|
||||
.where().startsWith("description", "modde")
|
||||
.findList();
|
||||
|
||||
Assert.assertEquals(1, list.size());
|
||||
|
||||
list =
|
||||
Ebean.createQuery(EBasicEncrypt.class, "where description like :d")
|
||||
.setParameter("d", "modde%")
|
||||
.findList();
|
||||
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.avaje.tests.basic.encrypt;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.tests.model.basic.EBasicEncryptBinary;
|
||||
|
||||
public class TestEncryptBinary extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Timestamp t0 = new Timestamp(System.currentTimeMillis());
|
||||
EBasicEncryptBinary e = new EBasicEncryptBinary();
|
||||
e.setName("test1");
|
||||
e.setDescription("testdesc1");
|
||||
e.setSomeTime(t0);
|
||||
e.setData("HelloWorld".getBytes());
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
|
||||
SqlQuery q = Ebean.createSqlQuery("select * from e_basicenc_bin where id = :id");
|
||||
q.setParameter("id", e.getId());
|
||||
|
||||
SqlRow row = q.findUnique();
|
||||
String name = row.getString("name");
|
||||
Object data = row.get("data");
|
||||
Object someTimeData = row.get("some_time");
|
||||
System.out.println("SqlRow name:"+name+" data:"+data+" someTime:"+someTimeData);
|
||||
|
||||
EBasicEncryptBinary e1 = Ebean.find(EBasicEncryptBinary.class, e.getId());
|
||||
|
||||
Timestamp t1 = e1.getSomeTime();
|
||||
byte[] data1 = e1.getData();
|
||||
String s = new String(data1);
|
||||
String desc1 = e1.getDescription();
|
||||
System.out.println("Decrypted data:"+s+" desc:"+desc1);
|
||||
|
||||
Assert.assertEquals(t0, t1);
|
||||
|
||||
e1.setName("testmod");
|
||||
e1.setDescription("moddesc");
|
||||
|
||||
Ebean.save(e1);
|
||||
|
||||
EBasicEncryptBinary e2 = Ebean.find(EBasicEncryptBinary.class, e.getId());
|
||||
|
||||
String desc2 = e2.getDescription();
|
||||
System.out.println("moddesc="+desc2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.ebean.event.TransactionEventListener;
|
||||
|
||||
public class MyTestTransactionEventListener implements TransactionEventListener {
|
||||
private volatile static boolean doTest = false;
|
||||
|
||||
private static Transaction lastCommitted;
|
||||
private static Transaction lastRollbacked;
|
||||
|
||||
public void postTransactionCommit(Transaction tx) {
|
||||
if (!doTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastCommitted = tx;
|
||||
}
|
||||
|
||||
public void postTransactionRollback(Transaction tx, Throwable cause) {
|
||||
if (!doTest) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastRollbacked = tx;
|
||||
}
|
||||
|
||||
public static void setDoTest(boolean doTest) {
|
||||
MyTestTransactionEventListener.doTest = doTest;
|
||||
|
||||
// reset what we've recorded so far
|
||||
lastCommitted = null;
|
||||
lastRollbacked = null;
|
||||
}
|
||||
|
||||
public static Transaction getLastCommitted() {
|
||||
return lastCommitted;
|
||||
}
|
||||
|
||||
public static Transaction getLastRollbacked() {
|
||||
return lastRollbacked;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.TWithPreInsert;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestPreInsertValidation extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("Mister");
|
||||
// the perInsert should populate the
|
||||
// name with should not be null
|
||||
Ebean.save(e);
|
||||
|
||||
// the save worked
|
||||
Assert.assertNotNull(e.getId());
|
||||
|
||||
TWithPreInsert e1 = Ebean.find(TWithPreInsert.class, e.getId());
|
||||
|
||||
e1.setTitle("Missus");
|
||||
Ebean.save(e1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
public class TestQueryAdapter extends TestCase {
|
||||
|
||||
public void testSimple() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
TOne o = new TOne();
|
||||
o.setName("something");
|
||||
|
||||
Ebean.save(o);
|
||||
|
||||
//Ebean.find(TOne.class, o.getId());
|
||||
|
||||
Query<TOne> queryFindId = Ebean.find(TOne.class)
|
||||
.setId(o.getId());
|
||||
|
||||
TOne one = queryFindId.findUnique();
|
||||
Assert.assertNotNull(one);
|
||||
Assert.assertEquals(one.getId(), o.getId());
|
||||
String generatedSql = queryFindId.getGeneratedSql();
|
||||
Assert.assertTrue(generatedSql.contains(" 1=1"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.avaje.tests.basic.event;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.basic.TWithPreInsert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestTransactionEvent extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
MyTestTransactionEventListener.setDoTest(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
MyTestTransactionEventListener.setDoTest(true);
|
||||
}
|
||||
|
||||
public void test() {
|
||||
|
||||
assertNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
final Object myUserObject = new Object();
|
||||
|
||||
Transaction tx = Ebean.beginTransaction();
|
||||
tx.putUserObject("myUserObject", myUserObject);
|
||||
|
||||
TWithPreInsert e = new TWithPreInsert();
|
||||
e.setTitle("Mister Transaction1");
|
||||
Ebean.save(e);
|
||||
|
||||
tx.commit();
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted(), tx);
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"));
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"), myUserObject);
|
||||
assertNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
Transaction tx2 = Ebean.beginTransaction();
|
||||
tx2.putUserObject("myUserObject2", myUserObject);
|
||||
|
||||
TWithPreInsert e2 = new TWithPreInsert();
|
||||
e2.setTitle("Mister Transaction2");
|
||||
Ebean.save(e2);
|
||||
|
||||
tx2.rollback();
|
||||
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted());
|
||||
assertNotNull(MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
assertNotSame(MyTestTransactionEventListener.getLastCommitted(), MyTestTransactionEventListener.getLastRollbacked());
|
||||
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted(), tx);
|
||||
assertNotNull(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"));
|
||||
assertSame(MyTestTransactionEventListener.getLastCommitted().getUserObject("myUserObject"), myUserObject);
|
||||
|
||||
assertSame(MyTestTransactionEventListener.getLastRollbacked(), tx2);
|
||||
assertNotNull(MyTestTransactionEventListener.getLastRollbacked().getUserObject("myUserObject2"));
|
||||
assertSame(MyTestTransactionEventListener.getLastRollbacked().getUserObject("myUserObject2"), myUserObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.avaje.tests.basic.join;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.avaje.tests.model.basic.Order.Status;
|
||||
|
||||
public class TestSecondaryJoin extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
//.select("*")
|
||||
//.join("customer")
|
||||
.findList();
|
||||
|
||||
Order o0 = list.get(0);
|
||||
o0.setCustomerName("Banan");
|
||||
o0.setStatus(Status.APPROVED);
|
||||
|
||||
Ebean.save(o0);
|
||||
|
||||
System.out.println("done");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.avaje.tests.basic.lob;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.tests.model.basic.EBasicClobNoVer;
|
||||
|
||||
public class TestBasicClobNoVer extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
EBasicClobNoVer entity = new EBasicClobNoVer();
|
||||
entity.setName("test");
|
||||
entity.setDescription("This is a test");
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
server.save(entity);
|
||||
|
||||
Ebean.find(EBasicClobNoVer.class).select("*").findList();
|
||||
|
||||
server.refresh(entity);
|
||||
System.out.println("description=" + entity.getDescription());
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.avaje.tests.basic.lob;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.TBytesOnly;
|
||||
|
||||
public class TestByteOnly extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
byte[] content = new byte[]{1,1};
|
||||
TBytesOnly e = new TBytesOnly();
|
||||
e.setContent(content);
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
byte[] content2 = new byte[]{1,1};
|
||||
|
||||
TBytesOnly e2 = Ebean.find(TBytesOnly.class, e.getId());
|
||||
e2.setContent(content2);
|
||||
|
||||
Ebean.save(e2);
|
||||
|
||||
//Ebean.getServer(null).getAdminAutofetch().collectUsageViaGC();
|
||||
//Ebean.getServer(null).getAdminAutofetch().updateTunedQueryInfo();
|
||||
System.out.println("done");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "drel_booking")
|
||||
public class Booking {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "agent_invoice")
|
||||
private Invoice agentInvoice;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "client_invoice")
|
||||
private Invoice clientInvoice;
|
||||
|
||||
@OneToMany(mappedBy = "booking")//, cascade = CascadeType.ALL)
|
||||
private List<Invoice> invoices;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Invoice getAgentInvoice() {
|
||||
return agentInvoice;
|
||||
}
|
||||
|
||||
public void setAgentInvoice(Invoice agentInvoice) {
|
||||
this.agentInvoice = agentInvoice;
|
||||
}
|
||||
|
||||
public Invoice getClientInvoice() {
|
||||
return clientInvoice;
|
||||
}
|
||||
|
||||
public void setClientInvoice(Invoice clientInvoice) {
|
||||
this.clientInvoice = clientInvoice;
|
||||
}
|
||||
|
||||
public List<Invoice> getInvoices() {
|
||||
return invoices;
|
||||
}
|
||||
|
||||
public void setInvoices(List<Invoice> invoices) {
|
||||
this.invoices= invoices;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name="drel_invoice")
|
||||
public class Invoice {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "booking")
|
||||
private Booking booking;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Booking getBooking() {
|
||||
return booking;
|
||||
}
|
||||
|
||||
public void setBooking(Booking booking) {
|
||||
this.booking = booking;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestOne2OneBookingInvoice extends TestCase {
|
||||
|
||||
public void test() {
|
||||
Booking b = new Booking();
|
||||
|
||||
Invoice ai = new Invoice();
|
||||
Invoice ci = new Invoice();
|
||||
|
||||
ai.setBooking(b);
|
||||
ci.setBooking(b);
|
||||
|
||||
b.setAgentInvoice(ai);
|
||||
b.setClientInvoice(ci);
|
||||
|
||||
Ebean.save(b);
|
||||
|
||||
Booking b1 = Ebean.find(Booking.class, b.getId());
|
||||
|
||||
Invoice ai1 = b1.getAgentInvoice();
|
||||
Assert.assertNotNull(ai1);
|
||||
|
||||
Booking b2 = ai1.getBooking();
|
||||
Assert.assertNotNull(b2);
|
||||
Assert.assertEquals(b1.getId(), b2.getId());
|
||||
Assert.assertSame(b1, b2);
|
||||
|
||||
Invoice ci1 = b1.getClientInvoice();
|
||||
Booking b3 = ci1.getBooking();
|
||||
Assert.assertNotNull(b3);
|
||||
Assert.assertEquals(b1.getId(), b2.getId());
|
||||
Assert.assertSame(b1, b2);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
|
||||
public class TestOneToOneWheelTire extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Wheel w = new Wheel();
|
||||
Tire t = new Tire();
|
||||
t.setWheel(w);
|
||||
w.setTire(t);
|
||||
|
||||
Ebean.save(t);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tire")
|
||||
public class Tire {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
@Version
|
||||
private int version;
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "wheel")
|
||||
private Wheel wheel;
|
||||
|
||||
public Tire() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Wheel getWheel() {
|
||||
return wheel;
|
||||
}
|
||||
|
||||
public void setWheel(Wheel wheel) {
|
||||
this.wheel = wheel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.tests.basic.one2one;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
@Table(name = "wheel")
|
||||
public class Wheel {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
@Version
|
||||
private int version;
|
||||
@OneToOne(mappedBy = "wheel", cascade = CascadeType.ALL)
|
||||
private Tire tire;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Wheel() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Tire getTire() {
|
||||
return tire;
|
||||
}
|
||||
|
||||
public void setTire(Tire tire) {
|
||||
this.tire = tire;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
@Entity
|
||||
public class BSimpleWithGen {
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
String name;
|
||||
|
||||
@Transient
|
||||
Map<String, List<String>> someMap;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import com.avaje.tests.model.basic.EBasic.Status;
|
||||
|
||||
public class TestEnumValueAnnotation extends TestCase {
|
||||
|
||||
public void test(){
|
||||
|
||||
EBasic b = new EBasic();
|
||||
b.setName("Banana");
|
||||
b.setStatus(Status.NEW);
|
||||
|
||||
Ebean.save(b);
|
||||
|
||||
SqlQuery q = Ebean.createSqlQuery("select * from e_basic where id = :id");
|
||||
q.setParameter("id", b.getId());
|
||||
|
||||
SqlRow sqlRow = q.findUnique();
|
||||
String strStatus = sqlRow.getString("status");
|
||||
|
||||
Assert.assertEquals("N", strStatus);
|
||||
|
||||
EBasic b2 = new EBasic();
|
||||
b2.setName("Apple");
|
||||
b2.setStatus(Status.NEW);
|
||||
|
||||
|
||||
Ebean.save(b2);
|
||||
|
||||
EBasic b3 = Ebean.find(EBasic.class, b2.getId());
|
||||
b3.setName("Orange");
|
||||
|
||||
Ebean.save(b3);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import java.util.Currency;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.ESomeType;
|
||||
|
||||
public class TestExtraScalarTypes extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
Locale locale = Locale.getDefault();
|
||||
Currency currency = Currency.getInstance(locale);
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
|
||||
ESomeType e = new ESomeType();
|
||||
e.setLocale(locale);
|
||||
e.setTimeZone(tz);
|
||||
e.setCurrency(currency);
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
ESomeType e2 = Ebean.find(ESomeType.class)
|
||||
.setAutofetch(false)
|
||||
.setId(e.getId())
|
||||
.findUnique();
|
||||
|
||||
Assert.assertNotNull(e2.getCurrency());
|
||||
Assert.assertNotNull(e2.getLocale());
|
||||
Assert.assertNotNull(e2.getTimeZone());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.TOne;
|
||||
|
||||
public class TestQueryBooleanProperty extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
// when run in MySql is test for BUG 323
|
||||
Ebean.find(TOne.class)
|
||||
.where().eq("active", true)
|
||||
.findList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.tests.model.basic.TUuidEntity;
|
||||
|
||||
public class TestSqlRowUUID extends TestCase {
|
||||
|
||||
public void test() {
|
||||
|
||||
TUuidEntity e = new TUuidEntity();
|
||||
e.setName("blah");
|
||||
|
||||
Ebean.save(e);
|
||||
|
||||
SqlQuery q = Ebean.createSqlQuery("select * from tuuid_entity where id = :id");
|
||||
q.setParameter("id", e.getId());
|
||||
SqlRow sqlRow = q.findUnique();
|
||||
|
||||
UUID id = sqlRow.getUUID("id");
|
||||
|
||||
Assert.assertNotNull(id);
|
||||
|
||||
Boolean b = sqlRow.getBoolean("name");
|
||||
Assert.assertFalse(b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
|
||||
public class TestTransientMap extends TestCase {
|
||||
|
||||
public void testMe() {
|
||||
|
||||
GlobalProperties.put("classes", BSimpleWithGen.class.toString());
|
||||
|
||||
BSimpleWithGen b = new BSimpleWithGen();
|
||||
b.setName("blah");
|
||||
|
||||
Ebean.save(b);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.avaje.tests.basic.vanilla;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestVanillaQuery extends TestCase {
|
||||
|
||||
|
||||
public void test() {
|
||||
|
||||
Order beanEnhancedCheck = new Order();
|
||||
|
||||
if (beanEnhancedCheck instanceof EntityBean){
|
||||
// test only real when not using enhancement
|
||||
System.out.println("Not testing TestVanillaQuery as beans are enhanced");
|
||||
return;
|
||||
}
|
||||
|
||||
// These settings only work when test run standalone (Ebean not booted yet)
|
||||
GlobalProperties.put("ebean.vanillaMode", "true");
|
||||
GlobalProperties.put("ebean.vanillaRefMode", "true");
|
||||
|
||||
// actually only a vanilla class when using subclass generation
|
||||
Class<?> vanillaClass = Order.class;
|
||||
|
||||
// // ONLY Testing this when running test manually/standalone at this stage
|
||||
// Order oref = Ebean.getReference(Order.class, 1);
|
||||
// Class<?> refClass = oref.getClass();
|
||||
// Assert.assertEquals(vanillaClass, refClass);
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Order> list =
|
||||
Ebean.find(Order.class)
|
||||
.fetch("details")
|
||||
.setVanillaMode(true)
|
||||
.findList();
|
||||
|
||||
Assert.assertTrue(list.size() > 0);
|
||||
|
||||
Order o = list.get(0);
|
||||
|
||||
// actually only a vanilla class when using subclass generation
|
||||
Class<?> returnedClass = o.getClass();
|
||||
Assert.assertEquals(vanillaClass, returnedClass);
|
||||
|
||||
Ebean.refreshMany(o, "details");
|
||||
|
||||
Ebean.refresh(o);
|
||||
|
||||
if (!(o instanceof EntityBean)){
|
||||
// using subclass generation ...
|
||||
list =
|
||||
Ebean.find(Order.class)
|
||||
.setVanillaMode(false)
|
||||
.findList();
|
||||
|
||||
Class<?> entityBeanClass = list.get(0).getClass();
|
||||
|
||||
Assert.assertNotSame(vanillaClass, entityBeanClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.avaje.tests.batchinsert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Transaction;
|
||||
import com.avaje.tests.model.basic.UTDetail;
|
||||
import com.avaje.tests.model.basic.UTMaster;
|
||||
|
||||
public class TestBatchInsertSimple extends TestCase {
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
public void testSimpleJdbcBatching() {
|
||||
|
||||
int numOfMasters = 10;// 2 + random.nextInt(8);
|
||||
|
||||
List<UTMaster> masters = new ArrayList<UTMaster>();
|
||||
for (int i = 0; i < numOfMasters; i++) {
|
||||
masters.add(createMasterAndDetails(i));
|
||||
}
|
||||
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
try {
|
||||
transaction.setBatchMode(true);
|
||||
transaction.setBatchSize(4);
|
||||
//transaction.setLogLevel(LogLevel.SUMMARY);
|
||||
//transaction.setBatchGetGeneratedKeys(false);
|
||||
|
||||
Ebean.save(masters);
|
||||
|
||||
transaction.commit();
|
||||
|
||||
} finally {
|
||||
Ebean.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private UTMaster createMasterAndDetails(int masterPos) {
|
||||
|
||||
UTMaster master = createMaster(masterPos);
|
||||
List<UTDetail> details = new ArrayList<UTDetail>();
|
||||
|
||||
int count = 2 + random.nextInt(20);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
|
||||
int qty = 1 + random.nextInt(99);
|
||||
double amount = random.nextDouble();
|
||||
|
||||
details.add(createDetail(masterPos+"-"+i, qty, amount));
|
||||
}
|
||||
master.setDetails(details);
|
||||
return master;
|
||||
}
|
||||
|
||||
private UTMaster createMaster(int position) {
|
||||
UTMaster m = new UTMaster();
|
||||
m.setName("batchInsert-master" + position);
|
||||
return m;
|
||||
}
|
||||
|
||||
private UTDetail createDetail(String position, int qty, double amount) {
|
||||
|
||||
UTDetail detail = new UTDetail();
|
||||
detail.setName("batchInsert-detail-" + position);
|
||||
detail.setQty(Integer.valueOf(qty));
|
||||
detail.setAmount(Double.valueOf(amount));
|
||||
|
||||
//System.out.println("-- "+detail);
|
||||
|
||||
return detail;
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user