Refactored internal api
This commit is contained in:
parent
3f31bb8a3a
commit
6ccd763d1f
361 changed files with 23049 additions and 4498 deletions
|
|
@ -0,0 +1,67 @@
|
|||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.jdbc.JdbcDataContext;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
|
||||
|
||||
public class BravoTest extends TestCase {
|
||||
|
||||
private Connection conn;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Class.forName("org.postgresql.Driver");
|
||||
conn = DriverManager.getConnection("jdbc:postgresql://localhost/openbravo","postgres","postgresql");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void testDataTypeBoolean() throws Exception {
|
||||
|
||||
/*
|
||||
Statement s = conn.createStatement();
|
||||
s.executeUpdate("DROP TABLE test_type_bool IF EXISTS;");
|
||||
s.executeUpdate("CREATE TABLE test_type_bool (id IDENTITY not null primary key,name varchar not null,active boolean not null);");
|
||||
s.executeUpdate("INSERT INTO test_type_bool VALUES(1,\'name\',true);");
|
||||
|
||||
// Test working data first
|
||||
JdbcDataContext dc = new JdbcDataContext(conn);
|
||||
Table table = dc.getDefaultSchema().getTableByName("TEST_TYPE_BOOL");
|
||||
assertNotNull(table);
|
||||
DataSet ds = dc.query().from(table).select(table.getColumns()).execute();
|
||||
assertTrue(ds.next());
|
||||
ds = dc.query().from(table).selectCount().execute();
|
||||
assertTrue(ds.next());
|
||||
|
||||
// Now let it fail
|
||||
s.executeUpdate("DROP TABLE test_type_bool IF EXISTS;");
|
||||
s.executeUpdate("CREATE TABLE test_type_bool (id IDENTITY not null primary key,name varchar not null,active boolean not null);");
|
||||
s.executeUpdate("INSERT INTO test_type_bool VALUES(1,\'\',true);");
|
||||
|
||||
// Make sure it works from jdbc driver
|
||||
Boolean active = null;
|
||||
s.execute("SELECT * FROM TEST_TYPE_BOOL");
|
||||
ResultSet rs = s.getResultSet();
|
||||
while (rs.next()) {
|
||||
active = rs.getBoolean(3); // this should be same method as exception
|
||||
}
|
||||
assertTrue(active);
|
||||
*/
|
||||
// Fails on execute.
|
||||
long startTime = System.currentTimeMillis();
|
||||
JdbcDataContext dc = new JdbcDataContext(conn);
|
||||
long stopTime = System.currentTimeMillis();
|
||||
System.out.println("DC init took: "+(stopTime-startTime)+" ms. for total tables: "+dc.getDefaultSchema().getTableCount());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.jdbc.JdbcDataContext;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
|
||||
|
||||
public class H2Test extends TestCase {
|
||||
|
||||
private Connection conn;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
Class.forName("org.h2.Driver");
|
||||
conn = DriverManager.getConnection("jdbc:h2:mem:");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void testDataTypeBoolean() throws Exception {
|
||||
|
||||
// This works see diff is empty value in column before boolean column ...
|
||||
Statement s = conn.createStatement();
|
||||
s.executeUpdate("DROP TABLE test_type_bool IF EXISTS;");
|
||||
s.executeUpdate("CREATE TABLE test_type_bool (id IDENTITY not null primary key,name varchar not null,active boolean not null);");
|
||||
s.executeUpdate("INSERT INTO test_type_bool VALUES(1,\'name\',true);");
|
||||
|
||||
// Test working data first
|
||||
JdbcDataContext dc = new JdbcDataContext(conn);
|
||||
Table table = dc.getDefaultSchema().getTableByName("TEST_TYPE_BOOL");
|
||||
assertNotNull(table);
|
||||
DataSet ds = dc.query().from(table).select(table.getColumns()).execute();
|
||||
assertTrue(ds.next());
|
||||
ds = dc.query().from(table).selectCount().execute();
|
||||
assertTrue(ds.next());
|
||||
|
||||
// Now let it fail
|
||||
s.executeUpdate("DROP TABLE test_type_bool IF EXISTS;");
|
||||
s.executeUpdate("CREATE TABLE test_type_bool (id IDENTITY not null primary key,name varchar not null,active boolean not null);");
|
||||
s.executeUpdate("INSERT INTO test_type_bool VALUES(1,\'\',true);");
|
||||
|
||||
// Make sure it works from jdbc driver
|
||||
Boolean active = null;
|
||||
s.execute("SELECT * FROM TEST_TYPE_BOOL");
|
||||
ResultSet rs = s.getResultSet();
|
||||
while (rs.next()) {
|
||||
active = rs.getBoolean(3); // this should be same method as exception
|
||||
}
|
||||
assertTrue(active);
|
||||
|
||||
// Fails on execute.
|
||||
dc = new JdbcDataContext(conn);
|
||||
table = dc.getDefaultSchema().getTableByName("TEST_TYPE_BOOL");
|
||||
assertNotNull(table);
|
||||
ds = dc.query().from(table).select(table.getColumns()).execute();
|
||||
assertTrue(ds.next());
|
||||
ds = dc.query().from(table).selectCount().execute();
|
||||
assertTrue(ds.next());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.eobjects.metamodel.UpdateCallback;
|
||||
import org.eobjects.metamodel.UpdateScript;
|
||||
import org.eobjects.metamodel.csv.CsvDataContext;
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.data.Row;
|
||||
import org.eobjects.metamodel.schema.Schema;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
import org.eobjects.metamodel.util.MutableRef;
|
||||
|
||||
public class SwingLoadTest {
|
||||
private Table table = null;
|
||||
public static void main(String[] argu) {
|
||||
try {SwingLoadTest test = new SwingLoadTest();test.run(argu);} catch (Exception e) {e.printStackTrace();}
|
||||
}
|
||||
public void run(String[] argu) throws Exception {
|
||||
File tmpFile = File.createTempFile("test-class-loading", "csv");
|
||||
tmpFile.deleteOnExit();
|
||||
CsvDataContext dataContext = new CsvDataContext(tmpFile);
|
||||
final Schema schema = dataContext.getDefaultSchema();
|
||||
dataContext.executeUpdate(new UpdateScript() {
|
||||
public void run(UpdateCallback cb) {
|
||||
MutableRef<Table> tableRef = new MutableRef<Table>();
|
||||
table = cb.createTable(schema, "table").withColumn("column").execute();
|
||||
tableRef.set(table);
|
||||
cb.insertInto(table).value(0,"data").execute();
|
||||
}
|
||||
});
|
||||
DataSet ds = dataContext.query().from(table).select(table.getColumns()).execute();
|
||||
while (ds.next()) {
|
||||
Row row = ds.getRow();
|
||||
System.out.println("Row value contains "+row.getValue(0));
|
||||
}
|
||||
ds.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package net.forwardfire.vasc.backend.metamodel.bundle;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.eobjects.metamodel.DataContext;
|
||||
import org.eobjects.metamodel.UpdateableDataContext;
|
||||
import org.eobjects.metamodel.csv.CsvDataContext;
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.data.Row;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class ResouceBundleTest extends TestCase {
|
||||
|
||||
public void testBundle() {
|
||||
final String tableName = "mm-bundle-test"; // Setup some small test data in tmp
|
||||
File testFile = new File(System.getProperty("java.io.tmpdir")+File.separatorChar+tableName+".properties");
|
||||
final boolean createTable = testFile.exists()==false;
|
||||
DataContext dataContext = new ResourceBundleDataContext(testFile);
|
||||
assertTrue(dataContext instanceof UpdateableDataContext);
|
||||
|
||||
for (String t:dataContext.getDefaultSchema().getTableNames()) {
|
||||
System.out.println("table: '"+t+"'");
|
||||
}
|
||||
|
||||
Table table = dataContext.getDefaultSchema().getTableByName("en");
|
||||
assertNotNull(table);
|
||||
|
||||
DataSet ds = dataContext.query().from(table).select(table.getColumns()).execute();
|
||||
while (ds.next()) {
|
||||
Row row = ds.getRow();
|
||||
System.out.println("row: "+row.getValue(0)+" "+row.getValue(1)+" "+row.getValue(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
package net.forwardfire.vasc.backend.metamodel.crud;
|
||||
|
||||
import java.io.File;
|
||||
import org.eobjects.metamodel.DataContext;
|
||||
import org.eobjects.metamodel.UpdateCallback;
|
||||
import org.eobjects.metamodel.UpdateScript;
|
||||
import org.eobjects.metamodel.UpdateableDataContext;
|
||||
import org.eobjects.metamodel.csv.CsvDataContext;
|
||||
import org.eobjects.metamodel.data.DataSet;
|
||||
import org.eobjects.metamodel.data.Row;
|
||||
import org.eobjects.metamodel.schema.Schema;
|
||||
import org.eobjects.metamodel.schema.Table;
|
||||
import org.eobjects.metamodel.util.MutableRef;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class CrudContextTest extends TestCase {
|
||||
|
||||
public void testCrud() {
|
||||
final String tableName = "test-crud"; // Setup some small test data in tmp
|
||||
File testFile = new File(System.getProperty("java.io.tmpdir")+File.separatorChar+tableName+".csv");
|
||||
final boolean createTable = testFile.exists()==false;
|
||||
CsvDataContext dataContext = new CsvDataContext(testFile);
|
||||
assertTrue(dataContext instanceof UpdateableDataContext);
|
||||
final Schema schema = dataContext.getDefaultSchema();
|
||||
final MutableRef<Table> tableRef = new MutableRef<Table>();
|
||||
dataContext.executeUpdate(new UpdateScript() {
|
||||
public void run(UpdateCallback cb) {
|
||||
Table table = null;
|
||||
int idx = 0;
|
||||
if (createTable) {
|
||||
table = cb.createTable(schema, tableName).withColumn("id").withColumn("name").withColumn("active").execute();
|
||||
tableRef.set(table);
|
||||
} else {
|
||||
table = cb.getDataContext().getDefaultSchema().getTableByName(tableName);
|
||||
idx = generateNextId(cb.getDataContext(),table);
|
||||
}
|
||||
cb.insertInto(table).value(0,idx+1).value(1, "foo").value(2, true).execute();
|
||||
cb.insertInto(table).value(0,idx+2).value(1, "foo").value(2, false).execute();
|
||||
cb.insertInto(table).value(0,idx+3).value(1, "bar").value(2, false).execute();
|
||||
cb.insertInto(table).value(0,idx+4).value(1, "bar").value(2, true).execute();
|
||||
cb.insertInto(table).value(0,idx+5).value(1, "object").value(2, true).execute();
|
||||
}
|
||||
});
|
||||
|
||||
// Wrap to CrudDataContext and get table to test with.
|
||||
CrudDataContext crudDataContext = new CrudDataContextImpl((UpdateableDataContext)dataContext);
|
||||
Table table = crudDataContext.getDefaultSchema().getTableByName(tableName);
|
||||
assertNotNull(table);
|
||||
|
||||
// Fetch and update data
|
||||
DataSet ds = crudDataContext.query().from(table).select(table.getColumns()).execute();
|
||||
while (ds.next()) {
|
||||
Row row = ds.getRow();
|
||||
if (row instanceof UpdateableRow) {
|
||||
UpdateableRow rowCrud = (UpdateableRow)row;
|
||||
if (rowCrud.getValue("name")!=null && rowCrud.getValue("name").toString().startsWith("test")==false) {
|
||||
rowCrud.setValue("active", false);
|
||||
crudDataContext.merge(rowCrud);
|
||||
}
|
||||
}
|
||||
}
|
||||
ds.close();
|
||||
|
||||
// Lets add an record.
|
||||
UpdateableRow newRow = crudDataContext.createRow(table);
|
||||
newRow.setValue("id", generateNextId(crudDataContext,table)); // Leave key out on jdbc/mongo backends for sequence/auto id.
|
||||
newRow.setValue("name", "test date "+System.currentTimeMillis());
|
||||
newRow.setValue("active", true);
|
||||
crudDataContext.persist(newRow);
|
||||
|
||||
// Delete some data.
|
||||
DataSet dsDel = crudDataContext.query().from(table).select(table.getColumns()).where("name").equals("foo").or("name").equals("bar").execute();
|
||||
while (dsDel.next()) {
|
||||
crudDataContext.delete((UpdateableRow)dsDel.getRow());
|
||||
}
|
||||
dsDel.close();
|
||||
//testFile.deleteOnExit();
|
||||
}
|
||||
|
||||
private int generateNextId(DataContext dataContext,Table table) {
|
||||
//DataSet dsMax = dataContext.query().from(table).select(FunctionType.MAX, table.getColumn(0)).execute();
|
||||
//assertTrue(dsMax.next());
|
||||
//Row rowMax = dsMax.getRow();
|
||||
//int recordId = new Integer(rowMax.getValue(0).toString()); // MAX stops on first row ?
|
||||
//dsMax.close();
|
||||
int recordId = 0;
|
||||
DataSet ds = dataContext.query().from(table).select(table.getColumns()).execute();
|
||||
while (ds.next()) {
|
||||
Row row = ds.getRow();
|
||||
int id = new Integer(row.getValue(0).toString());
|
||||
if (id > recordId) {
|
||||
recordId = id;
|
||||
}
|
||||
}
|
||||
return recordId + 1; // Fetch new ID because CSV has no auto ID
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue