Package org.apache.metamodel

Examples of org.apache.metamodel.DataContext


        assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
        assertFalse(ds.next());
    }

    public void testVaryingValueLengthsTooShortLength() throws Exception {
        DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
                new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 5, 7 }, true));
        try {
            dc.getDefaultSchema().getTables();
            fail("Exception expected");
        } catch (InconsistentValueWidthException e) {
            assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
            assertEquals("igreetinggreeter", e.getSourceLine());
            assertEquals("[i, greet, inggree]", Arrays.toString(e.getSourceResult()));
View Full Code Here


            assertEquals("[i, greet, inggree]", Arrays.toString(e.getSourceResult()));
        }
    }

    public void testVaryingValueLengthsTooShortLengthErrorTolerant() throws Exception {
        DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
                new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 5,
                        7 }, false));

        Table table = dc.getDefaultSchema().getTables()[0];
        assertEquals("[i, greet, inggree]", Arrays.toString(table.getColumnNames()));

        Query q = dc.query().from(table).select(table.getColumns()).toQuery();
        DataSet ds = dc.executeQuery(q);

        assertTrue(ds.next());
        assertEquals("[1, hello, worl]", Arrays.toString(ds.getRow().getValues()));
        assertTrue(ds.next());
        assertEquals("[2, hi, ther]", Arrays.toString(ds.getRow().getValues()));
View Full Code Here

        assertEquals("[3, howdy, part]", Arrays.toString(ds.getRow().getValues()));
        assertFalse(ds.next());
    }

    public void testVaryingValueLengthsTooLongLength() throws Exception {
        DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
                new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 8, 9 }, true));

        try {
            dc.getDefaultSchema().getTables();
            fail("Exception expected");
        } catch (InconsistentValueWidthException e) {
            assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
            assertEquals("igreetinggreeter", e.getSourceLine());
            assertEquals("[i, greeting, greeter]", Arrays.toString(e.getSourceResult()));
View Full Code Here

            assertEquals("[i, greeting, greeter]", Arrays.toString(e.getSourceResult()));
        }
    }

    public void testVaryingValueLengthsTooLongLengthErrorTolerant() throws Exception {
        DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
                new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 8,
                        9 }, false));

        Table table = dc.getDefaultSchema().getTables()[0];
        assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));

        Query q = dc.query().from(table).select(table.getColumns()).toQuery();
        DataSet ds = dc.executeQuery(q);

        assertTrue(ds.next());
        assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
        assertTrue(ds.next());
        assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
View Full Code Here

import org.apache.metamodel.schema.Table;

public class GroupedQueryBuilderImplTest extends TestCase {

  public void testFindColumnWithAlias() throws Exception {
    DataContext dataContext = EasyMock.createMock(DataContext.class);

    MutableTable table1 = new MutableTable("foo");
    table1.addColumn(new MutableColumn("col1").setTable(table1));
    table1.addColumn(new MutableColumn("col2").setTable(table1));
    table1.addColumn(new MutableColumn("col3").setTable(table1));
View Full Code Here

  }

  // test-case to recreate the problems reported at
  // http://eobjects.org/trac/discussion/7/134
  public void testLeftJoinQueries() throws Exception {
    DataContext dc = new AbstractDataContext() {

      @Override
      public DataSet executeQuery(Query query) throws MetaModelException {
        throw new UnsupportedOperationException();
      }

      @Override
      protected String[] getSchemaNamesInternal() {
        throw new UnsupportedOperationException();
      }

      @Override
      protected String getDefaultSchemaName() {
        throw new UnsupportedOperationException();
      }

      @Override
      protected Schema getSchemaByNameInternal(String name) {
        throw new UnsupportedOperationException();
      }
    };
    Table tableAB = new MutableTable("tableAB");
    Table tableC = new MutableTable("tableC");

    Column colA = new MutableColumn("colA", null, tableAB, 0, true);
    Column colB = new MutableColumn("colB", null, tableAB, 1, true);
    Column colC = new MutableColumn("colC", null, tableC, 0, true);

    Query q = dc.query().from(tableAB).leftJoin(tableC).on(colB, colC)
        .select(colA).as("a").select(colB).as("b").select(colC).as("c")
        .toQuery();

    assertEquals(
        "SELECT tableAB.colA AS a, tableAB.colB AS b, tableC.colC AS c FROM tableAB LEFT JOIN tableC ON tableAB.colB = tableC.colC",
View Full Code Here

        assertEquals(
                "SELECT persons.column_number FROM s.persons WHERE (persons.name = 'kasper' OR persons.role = 'user')",
                q.toString());

        DataContext dc = new QueryPostprocessDataContext() {

            @Override
            public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
                assertEquals(3, columns.length);
                assertEquals("column_number", columns[0].getName());
                assertEquals("name", columns[1].getName());
                assertEquals("role", columns[2].getName());
                SelectItem[] selectItems = new SelectItem[] { new SelectItem(col1), new SelectItem(col2),
                        new SelectItem(col3) };
                DataSetHeader header = new CachingDataSetHeader(selectItems);
                List<Row> rows = new LinkedList<Row>();
                rows.add(new DefaultRow(header, new Object[] { "foo", "bar", 1 }));
                rows.add(new DefaultRow(header, new Object[] { "kasper", "developer", 2 }));
                rows.add(new DefaultRow(header, new Object[] { "admin", "admin", 3 }));
                rows.add(new DefaultRow(header, new Object[] { "elikeon", "user", 4 }));
                rows.add(new DefaultRow(header, new Object[] { "someuser", "user", 5 }));
                rows.add(new DefaultRow(header, new Object[] { "hmm", "what-the", 6 }));

                return new InMemoryDataSet(header, rows);
            }

            @Override
            protected String getMainSchemaName() throws MetaModelException {
                return "s";
            }

            @Override
            protected Schema getMainSchema() throws MetaModelException {
                return schema;
            }
        };

        DataSet result = dc.executeQuery(q);
        List<Object[]> objectArrays = result.toObjectArrays();
        assertEquals(3, objectArrays.size());
        assertEquals(2, objectArrays.get(0)[0]);
        assertEquals(4, objectArrays.get(1)[0]);
        assertEquals(5, objectArrays.get(2)[0]);
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

      .getTables()[0];

  public void testInterceptSchema() throws Exception {
    // without an interceptor
    {
      DataContext dc = new InterceptableDataContext(delegateDataContext);

      Schema schema = dc.getDefaultSchema();
      Schema[] schemas = dc.getSchemas();

      assertEquals("schema", schema.getName());
      assertEquals(MutableSchema.class, schema.getClass());
      assertEquals("[information_schema, schema]",
          Arrays.toString(dc.getSchemaNames()));
      assertEquals(2, schemas.length);
      assertEquals("information_schema", schemas[0].getName());
      assertEquals("schema", schemas[1].getName());
    }

    // with an interceptor
    {
      DataContext dc = new InterceptableDataContext(delegateDataContext)
          .addSchemaInterceptor(new SchemaInterceptor() {
            @Override
            public Schema intercept(Schema input) {
              return new MutableSchema(input.getName() + " foo!");
            }
          });

      Schema schema = dc.getDefaultSchema();
      Schema[] schemas = dc.getSchemas();

      assertEquals("schema foo!", schema.getName());
      assertEquals(MutableSchema.class, schema.getClass());
      assertEquals("[information_schema foo!, schema foo!]",
          Arrays.toString(dc.getSchemaNames()));
      assertEquals(2, schemas.length);
      assertEquals("information_schema foo!", schemas[0].getName());
      assertEquals("schema foo!", schemas[1].getName());
    }
  }
View Full Code Here

      assertEquals("schema foo!", schemas[1].getName());
    }
  }

  public void testInterceptDataSet() throws Exception {
    DataContext dc = new InterceptableDataContext(delegateDataContext)
        .addDataSetInterceptor(new DataSetInterceptor() {
          @Override
          public DataSet intercept(DataSet dataSet) {
            return new MaxRowsDataSet(dataSet, 1);
          }
        });

    DataSet ds = dc.query().from(table).select("foo").execute();
    assertEquals(MaxRowsDataSet.class, ds.getClass());
    assertEquals(1, ds.toObjectArrays().size());
  }
View Full Code Here

TOP

Related Classes of org.apache.metamodel.DataContext

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.