Search in sources :

Example 46 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyWithOneTable.

@Test
public void testThrowOnPartialStrategyWithOneTable() throws Exception {
    createOneTable("SUS1");
    setStrategy(ThrowOnPartialSchemaStrategy.class);
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    try {
        node.performQueries(Collections.singletonList(query), observer);
        assertEquals(1, existingTables().size());
        fail("Must have thrown on partial schema");
    } catch (CayenneRuntimeException e) {
    // expected
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 47 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ThrowOnPartialSchemaStrategyIT method testThrowOnPartialStrategyTableNoExist.

@Test
public void testThrowOnPartialStrategyTableNoExist() throws Exception {
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    setStrategy(ThrowOnPartialSchemaStrategy.class);
    try {
        node.performQueries(Collections.singletonList((Query) query), observer);
    } catch (CayenneRuntimeException e) {
        assertNotNull(e);
    }
    try {
        node.performQueries(Collections.singletonList((Query) query), observer);
    } catch (CayenneRuntimeException e) {
        assertNotNull(e);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 48 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DataDomainIT method testLookupDataNode.

@Test
public void testLookupDataNode() {
    DataDomain domain = new DataDomain("test");
    DataMap m1 = new DataMap("m1");
    DataNode n1 = new DataNode("n1");
    n1.addDataMap(m1);
    domain.addNode(n1);
    DataMap m2 = new DataMap("m2");
    DataNode n2 = new DataNode("n2");
    n2.addDataMap(m2);
    domain.addNode(n2);
    assertSame(n1, domain.lookupDataNode(m1));
    assertSame(n2, domain.lookupDataNode(m2));
    try {
        domain.lookupDataNode(new DataMap("m3"));
        fail("must have thrown on missing Map to Node maping");
    } catch (CayenneRuntimeException e) {
    // expected
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 49 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class PropertyUtilsTest method testSetConvertedWithCustomConverter.

@Test
public void testSetConvertedWithCustomConverter() {
    // save old converter for restore
    Converter<Date> oldConverter = ConverterFactory.factory.getConverter(Date.class);
    try {
        ConverterFactory.addConverter(Date.class, new Converter<Date>() {

            @Override
            protected Date convert(Object value, Class<Date> type) {
                if (value == null)
                    return null;
                if (value instanceof Date) {
                    return (Date) value;
                }
                if (value instanceof String) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        return format.parse((String) value);
                    } catch (ParseException e) {
                        throw new CayenneRuntimeException("Unable to convert '" + value + "' to a Date", e);
                    }
                }
                throw new CayenneRuntimeException("Unable to convert '" + value + "' to a Date");
            }
        });
        TstJavaBean o1 = new TstJavaBean();
        // String to date
        PropertyUtils.setProperty(o1, "dateField", "2013-08-01");
        Calendar cal = new GregorianCalendar(2013, 7, 1, 0, 0, 0);
        assertEquals(cal.getTime(), o1.getDateField());
    } finally {
        // restore global date converter
        ConverterFactory.addConverter(Date.class, oldConverter);
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) GregorianCalendar(java.util.GregorianCalendar) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 50 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ASTExtractIT method testMonth.

@Test
public void testMonth() {
    Expression exp = ExpressionFactory.exp("month(dateColumn) = 3");
    try {
        long res = ObjectSelect.query(DateTestEntity.class, exp).selectCount(context);
        assertEquals(1, res);
    } catch (CayenneRuntimeException e) {
        if (unitDbAdapter.supportsExtractPart(ASTExtract.DateTimePart.MONTH)) {
            throw e;
        }
    // else ok
    }
}
Also used : Expression(org.apache.cayenne.exp.Expression) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9