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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations