Search in sources :

Example 86 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class JavaTimeTest method testDateTimeMethods.

/**
 * Test the use of the Date.getDay(), Date.getMonth(), Date.getYear(), Time.getHour(), Time.getMinute(), Time.getSecond() methods.
 */
public void testDateTimeMethods() {
    try {
        LocalDateTime dateTime1 = LocalDateTime.of(2008, 3, 17, 15, 9, 0, 0);
        LocalDateTime dateTime2 = LocalDateTime.of(2009, 5, 13, 7, 9, 26, 0);
        JavaTimeSample1 s1a = new JavaTimeSample1(1, dateTime1, dateTime2);
        LocalDateTime dateTime3 = LocalDateTime.of(2011, 10, 14, 1, 0, 15, 0);
        LocalDateTime dateTime4 = LocalDateTime.of(2012, 11, 1, 7, 9, 0, 0);
        JavaTimeSample1 s1b = new JavaTimeSample1(2, dateTime3, dateTime4);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id1 = null;
        Object id2 = null;
        try {
            tx.begin();
            pm.makePersistent(s1a);
            pm.makePersistent(s1b);
            tx.commit();
            id1 = JDOHelper.getObjectId(s1a);
            id2 = JDOHelper.getObjectId(s1b);
            tx.begin();
            try {
                Query q = pm.newQuery(JavaTimeSample1.class);
                q.setFilter("dateTime1.getDayOfMonth() == 17");
                List results = (List) q.execute();
                assertEquals("Received incorrect number of results for LocalDateTime.getDayOfMonth()", 1, results.size());
                JavaTimeSample1 first = (JavaTimeSample1) results.iterator().next();
                assertEquals("Retrieved object for getDayOfMonth() is incorrect", id1, JDOHelper.getObjectId(first));
                q.closeAll();
                q = pm.newQuery(JavaTimeSample1.class);
                q.setFilter("dateTime1.getMonthValue() == 10");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results for LocalDateTime.getMonthValue", 1, results.size());
                first = (JavaTimeSample1) results.iterator().next();
                assertEquals("Retrieved object for getMonth() is incorrect", id2, JDOHelper.getObjectId(first));
                q.closeAll();
                q = pm.newQuery(JavaTimeSample1.class);
                q.setFilter("dateTime1.getYear() == 2008");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results for LocalDateTime.getYear", 1, results.size());
                first = (JavaTimeSample1) results.iterator().next();
                assertEquals("Retrieved object for getYear() is incorrect", id1, JDOHelper.getObjectId(first));
                q.closeAll();
                // TODO sample can be persisted with timezone info resulting in H2 returning incorrect value for the test
                /*q = pm.newQuery(JavaTimeSample1.class);
                    q.setFilter("dateTime1.getHour() == 1");
                    results = (List)q.execute();
                    assertEquals("Received incorrect number of results for LocalDateTime.getHour", 1, results.size());
                    first = (JavaTimeSample1)results.iterator().next();
                    assertEquals("Retrieved object for getHour() is incorrect", id2, JDOHelper.getObjectId(first));
                    q.closeAll();*/
                q = pm.newQuery(JavaTimeSample1.class);
                q.setFilter("dateTime1.getMinute() == 9");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results for LocalDateTime.getMinute", 1, results.size());
                first = (JavaTimeSample1) results.iterator().next();
                assertEquals("Retrieved object for getMinute() is incorrect", id1, JDOHelper.getObjectId(first));
                q.closeAll();
                q = pm.newQuery(JavaTimeSample1.class);
                q.setFilter("dateTime1.getSecond() == 0");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results for LocalDateTime.getSecond", 1, results.size());
                first = (JavaTimeSample1) results.iterator().next();
                assertEquals("Retrieved object for getSecond() is incorrect", id1, JDOHelper.getObjectId(first));
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error("Exception during test", e);
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(JavaTimeSample1.class);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JavaTimeSample1(org.datanucleus.samples.types.javatime.JavaTimeSample1) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 87 with JDOUserException

use of javax.jdo.JDOUserException in project tests by datanucleus.

the class TransactionTest method testRollbackOnly.

/**
 * Test for the "rollback-only" functionality of a transaction.
 */
public void testRollbackOnly() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        tx.setRollbackOnly();
        assertTrue("Calling setRollbackOnly before starting the transaction should have had no effect, " + "but it set the rollbackOnly to true!", tx.getRollbackOnly() == false);
        try {
            tx.begin();
            Person pers = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            pm.makePersistent(pers);
            tx.setRollbackOnly();
            try {
                tx.commit();
                assertTrue("Calling commit() when setRollbackOnly is set should throw an exception, but didnt", false);
            } catch (JDOFatalDataStoreException fde) {
            // Should come through here (outside JavaEE container)
            } catch (JDOUserException fue) {
            // Should come through here (inside JavaEE container)
            }
            try {
                tx.commit();
                assertTrue("Calling commit() when setRollbackOnly is set should throw an exception, but didnt", false);
            } catch (JDOFatalDataStoreException fde) {
            // Should come through here
            } catch (JDOUserException fue) {
            // Should come through here (inside JavaEE container)
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOFatalDataStoreException(javax.jdo.JDOFatalDataStoreException) JDOUserException(javax.jdo.JDOUserException)

Example 88 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class PackageMetadataImpl method newClassMetadata.

/* (non-Javadoc)
     * @see javax.jdo.metadata.PackageMetadata#newClassMetadata(java.lang.Class)
     */
public ClassMetadata newClassMetadata(Class cls) {
    if (cls.isInterface()) {
        throw new JDOUserException("Canot create new class metadata for " + cls.getName() + " since it is an interface!");
    }
    ClassMetaData internalCmd = getInternal().newClassMetadata(ClassUtils.getClassNameForClass(cls));
    ClassMetadataImpl cmd = new ClassMetadataImpl(internalCmd);
    cmd.parent = this;
    return cmd;
}
Also used : JDOUserException(javax.jdo.JDOUserException) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 89 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class PackageMetadataImpl method newInterfaceMetadata.

/* (non-Javadoc)
     * @see javax.jdo.metadata.PackageMetadata#newInterfaceMetadata(java.lang.Class)
     */
public InterfaceMetadata newInterfaceMetadata(Class cls) {
    if (!cls.isInterface()) {
        throw new JDOUserException("Canot create new interface metadata for " + cls.getName() + " since not interface!");
    }
    InterfaceMetaData internalImd = getInternal().newInterfaceMetadata(ClassUtils.getClassNameForClass(cls));
    InterfaceMetadataImpl imd = new InterfaceMetadataImpl(internalImd);
    imd.parent = this;
    return imd;
}
Also used : InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) JDOUserException(javax.jdo.JDOUserException)

Example 90 with JDOUserException

use of javax.jdo.JDOUserException in project datanucleus-api-jdo by datanucleus.

the class JDOQLTypedQueryImpl method setParameters.

/* (non-Javadoc)
     * @see javax.jdo.JDOQLTypedQuery#setParameters(java.util.Map)
     */
@Override
public JDOQLTypedQuery<T> setParameters(Map namedParamMap) {
    assertIsOpen();
    discardCompiled();
    if (namedParamMap == null || namedParamMap.isEmpty()) {
        parameterValuesByName = null;
        return this;
    }
    if (parameterValuesByName == null) {
        parameterValuesByName = new ConcurrentHashMap<>();
    }
    Iterator<Map.Entry> entryIter = namedParamMap.entrySet().iterator();
    while (entryIter.hasNext()) {
        Map.Entry entry = entryIter.next();
        Object key = entry.getKey();
        Object val = entry.getValue();
        if (key instanceof String) {
            if (parameterExprByName == null || (parameterExprByName != null && !parameterExprByName.containsKey(key))) {
                throw new JDOUserException("Parameter with name " + key + " doesnt exist for this query");
            }
            parameterValuesByName.put((String) key, val);
        } else if (key instanceof Expression) {
            ParameterExpression internalParamExpr = (ParameterExpression) ((ExpressionImpl) key).getQueryExpression();
            if (parameterExprByName == null || (parameterExprByName != null && !parameterExprByName.containsKey(internalParamExpr.getAlias()))) {
                throw new JDOUserException("Parameter with name " + internalParamExpr.getAlias() + " doesnt exist for this query");
            }
            parameterValuesByName.put(internalParamExpr.getAlias(), val);
        }
    }
    return this;
}
Also used : CharacterExpression(javax.jdo.query.CharacterExpression) StringExpression(javax.jdo.query.StringExpression) NumericExpression(javax.jdo.query.NumericExpression) TimeExpression(javax.jdo.query.TimeExpression) ListExpression(javax.jdo.query.ListExpression) PersistableExpression(javax.jdo.query.PersistableExpression) OrderExpression(javax.jdo.query.OrderExpression) DateTimeExpression(javax.jdo.query.DateTimeExpression) DateExpression(javax.jdo.query.DateExpression) BooleanExpression(javax.jdo.query.BooleanExpression) CollectionExpression(javax.jdo.query.CollectionExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) MapExpression(javax.jdo.query.MapExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) Expression(javax.jdo.query.Expression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10