Package org.apache.metamodel

Examples of org.apache.metamodel.MockUpdateableDataContext


import org.apache.metamodel.schema.Column;

public class ConvertedRowInsertionInterceptorTest extends TestCase {

  public void testConvertedInsert() throws Exception {
    MockUpdateableDataContext source = new MockUpdateableDataContext();
    Column fooColumn = source.getColumnByQualifiedLabel("schema.table.foo");
    assertNotNull(fooColumn);

    UpdateableDataContext intercepted = Converters.addTypeConverter(source,
        fooColumn, new StringToIntegerConverter());

    final List<Object[]> values = source.getValues();

    assertEquals(3, values.size());

    intercepted.executeUpdate(new UpdateScript() {
      @Override
View Full Code Here


import org.apache.metamodel.schema.Table;

public class ConvertersTest extends TestCase {

    public void testAutoDetectConverters() throws Exception {
        final MockUpdateableDataContext decoratedDataContext = new MockUpdateableDataContext();
        final Table table = decoratedDataContext.getDefaultSchema().getTables()[0];
        Map<Column, TypeConverter<?, ?>> converters = Converters.autoDetectConverters(decoratedDataContext, table, 2);
        assertEquals(1, converters.size());
        assertEquals(
                "[Column[name=foo,columnNumber=0,type=VARCHAR,nullable=null,nativeType=null,columnSize=null]]",
                converters.keySet().toString());
        assertEquals(StringToIntegerConverter.class, converters.values().iterator().next().getClass());

        final UpdateableDataContext dc = Converters.addTypeConverters(decoratedDataContext, converters);

        DataSet ds = dc.query().from(table).select(table.getColumns()).execute();
        assertEquals(ConvertedDataSet.class, ds.getClass());
        assertTrue(ds.next());
        assertEquals("Row[values=[1, hello]]", ds.getRow().toString());
        assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
        assertEquals(1, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
        assertEquals(2, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
        assertEquals(3, ds.getRow().getValue(0));
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 4).value("bar", "mrrrrh").execute();
            }
        });

        // query the decorator
        ds = dc.query().from(table).select(table.getColumns()).where("foo").eq(4).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[4, mrrrrh]]", ds.getRow().toString());
        assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
        assertEquals(4, ds.getRow().getValue(0));
        assertFalse(ds.next());
        ds.close();

        // query the decorated
        Object[] physicalRow = decoratedDataContext.getValues().get(3);
        assertEquals("[4, mrrrrh]", Arrays.toString(physicalRow));
        assertEquals(String.class, physicalRow[0].getClass());
    }
View Full Code Here

        assertEquals("[4, mrrrrh]", Arrays.toString(physicalRow));
        assertEquals(String.class, physicalRow[0].getClass());
    }

    public void testScenario() throws Exception {
        UpdateableDataContext dc = new MockUpdateableDataContext();
        List<Object[]> physicalValuesList = ((MockUpdateableDataContext) dc).getValues();
        assertEquals(3, physicalValuesList.size());
        for (Object[] physicalValues : physicalValuesList) {
            assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
        }

        final Table table = dc.getDefaultSchema().getTables()[0];
        Map<Column, TypeConverter<?, ?>> converters = Converters.autoDetectConverters(dc, table, 1000);
        assertEquals(1, converters.size());
        dc = Converters.addTypeConverters(dc, converters);

        final Query q = dc.query().from(table).select("foo").toQuery();
        assertEquals("SELECT table.foo FROM schema.table", q.toSql());
        DataSet ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals(1, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(2, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(3, ds.getRow().getValue(0));
        assertFalse(ds.next());

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 4).value("bar", "heidiho!").execute();
            }
        });

        ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals(1, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(2, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(3, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(4, ds.getRow().getValue(0));
        assertFalse(ds.next());
       
        assertEquals(4, physicalValuesList.size());
        for (Object[] physicalValues : physicalValuesList) {
            assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
        }
       
        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.insertInto(table).value("foo", 5).value("bar", "hejsa...").execute();
                callback.update(table).where("foo").lessThan(3).value("foo", 100).execute();
            }
        });
       
        ds = dc.executeQuery(q);
        assertTrue(ds.next());
        assertEquals(3, ds.getRow().getValue(0));
        assertTrue(ds.next());
        assertEquals(4, ds.getRow().getValue(0));
        assertTrue(ds.next());
View Full Code Here

import org.apache.metamodel.schema.Table;

public class ConvertedDataSetInterceptorTest extends TestCase {

  public void testConvertedQuery() throws Exception {
    UpdateableDataContext dc = new MockUpdateableDataContext();
    Column fooColumn = dc.getColumnByQualifiedLabel("schema.table.foo");
    assertNotNull(fooColumn);

    dc = Converters.addTypeConverter(dc, fooColumn,
        new StringToIntegerConverter());

    Table table = dc.getDefaultSchema().getTableByName("table");
    Query query = dc.query().from(table).select(table.getColumns())
        .toQuery();
    assertEquals("SELECT table.foo, table.bar FROM schema.table",
        query.toSql());

    DataSet ds = dc.executeQuery(query);
    assertEquals(ConvertedDataSet.class, ds.getClass());

    assertTrue(ds.next());
    assertEquals("Row[values=[1, hello]]", ds.getRow().toString());
    assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
View Full Code Here

    assertFalse(ds.next());
    ds.close();
  }

  public void testNonConvertedQuery() throws Exception {
    MockUpdateableDataContext source = new MockUpdateableDataContext();
    Column fooColumn = source.getColumnByQualifiedLabel("schema.table.foo");
    assertNotNull(fooColumn);

    Map<Column, TypeConverter<?, ?>> converters = new HashMap<Column, TypeConverter<?, ?>>();
    converters.put(fooColumn, new StringToIntegerConverter());
    DataContext converted = Converters.addTypeConverter(source, fooColumn,
View Full Code Here

import junit.framework.TestCase;

public class InterceptorsTest extends TestCase {

  public void testReuseInterceptor() throws Exception {
    MockUpdateableDataContext original = new MockUpdateableDataContext();
    InterceptableDataContext interceptor1 = Interceptors.intercept(original);
    InterceptableDataContext interceptor2 = Interceptors.intercept(interceptor1);
    assertSame(interceptor1, interceptor2);
  }
View Full Code Here

import junit.framework.TestCase;

public class InterceptorListTest extends TestCase {

  public void testGetInterceptorOfType() throws Exception {
    DataContext dc = new MockUpdateableDataContext();
    InterceptableDataContext interceptor = Interceptors.intercept(dc);
   
    InterceptorList<DataSet> list = interceptor.getDataSetInterceptors();
    ConvertedDataSetInterceptor convertedDataSetInterceptor = new ConvertedDataSetInterceptor();
    list.add(convertedDataSetInterceptor);
View Full Code Here

import org.apache.metamodel.schema.Table;

public class AbstractRowDeletionCallbackTest extends TestCase {

    public void testDelete() throws Exception {
        final MockUpdateableDataContext dc = new MockUpdateableDataContext();
        final Table table = dc.getDefaultSchema().getTables()[0];
        DataSet ds = dc.query().from(table).selectCount().execute();
        assertTrue(ds.next());
        assertEquals("3", ds.getRow().getValue(0).toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                callback.update(table).value("bar", "baz").execute();
                callback.update(table).value("foo", "4").where("foo").eq("3").execute();
            }
        });

        ds = dc.query().from(table).select(table.getColumns()).execute();
        assertTrue(ds.next());
        assertEquals("Row[values=[1, baz]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[2, baz]]", ds.getRow().toString());
        assertTrue(ds.next());
        assertEquals("Row[values=[4, baz]]", ds.getRow().toString());
        assertFalse(ds.next());
        ds.close();

        dc.executeUpdate(new UpdateScript() {
            @Override
            public void run(UpdateCallback callback) {
                RowDeletionBuilder delete = callback.deleteFrom(table);
                assertEquals("DELETE FROM schema.table", delete.toSql());
                delete.execute();

                assertEquals("DELETE FROM schema.table WHERE table.bar = 'baz'", callback.deleteFrom(table).where("bar")
                        .eq("baz").toSql());
            }
        });

        ds = dc.query().from(table).selectCount().execute();
        assertTrue(ds.next());
        assertEquals("0", ds.getRow().getValue(0).toString());
        assertFalse(ds.next());
        ds.close();
    }
View Full Code Here

TOP

Related Classes of org.apache.metamodel.MockUpdateableDataContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.