public void testDualWrongTypeNumber() throws Exception { try { constraint = new SimpleConstraint(qeNum1, ConstraintOp.MATCHES, qeNum1); fail ("Expected: IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { constraint = new SimpleConstraint(qeNum1, ConstraintOp.DOES_NOT_MATCH, qeNum1); fail ("Expected: IllegalArgumentException"); } catch (IllegalArgumentException e) { } }
/** * Returns true if the given constraint is related to no FromElement. This should * only return true if c is a SimpleConstraint that only references constants. * * @param c the constraint to examine * @return true if constraint is not associated with a FromElement */ public static boolean isRelatedToNothing(Constraint c) { if (c instanceof SimpleConstraint) { SimpleConstraint sc = (SimpleConstraint) c; Set<QueryField> fields = getQueryFields(sc.getArg1()); fields.addAll(getQueryFields(sc.getArg2())); if (fields.size() == 0) { return true; } } return false; }
/** * Converts a SimpleConstraint object into a String suitable for putting in an SQL query. * * @param state the current SqlGenerator state * @param buffer the StringBuffer to place text into * @param c the SimpleConstraint object * @param q the Query * @throws ObjectStoreException if something goes wrong */ protected static void simpleConstraintToString(State state, StringBuffer buffer, SimpleConstraint c, Query q) throws ObjectStoreException { queryEvaluableToString(buffer, c.getArg1(), q, state); buffer.append(" " + c.getOp().toString()); if (c.getArg2() != null) { buffer.append(" "); queryEvaluableToString(buffer, c.getArg2(), q, state); } }
/** * Check whether a comparison is valid i.e. the arguments are comparable types and the * the operator is permitted for those types * @param arg1 the first argument * @param op how to compare the arguments * @param arg2 the second argument * @return whether the comparison is valid */ public static boolean validComparison(Class<?> arg1, ConstraintOp op, Class<?> arg2) { if (arg2 == null) { return op == ConstraintOp.IS_NULL || op == ConstraintOp.IS_NOT_NULL; } if (comparable(arg1, arg2)) { return validOps(arg1).contains(op); } return false; }
/** * Perform test for NOT NULL constraint on node * @param c the constraint to test */ public void apply(Constraint c) { if (c instanceof SimpleConstraint) { SimpleConstraint sc = (SimpleConstraint) c; if (sc.getOp().equals(ConstraintOp.IS_NOT_NULL)) { if (node.equals(sc.getArg1())) { exists = true; } } } } }
if ((origC.getOp() == ConstraintOp.IS_NULL) || (origC.getOp() == ConstraintOp.IS_NOT_NULL)) { return new SimpleConstraint((QueryEvaluable) cloneThing(origC.getArg1(), fromElementMap, qopeMap), origC.getOp()); } else { return new SimpleConstraint((QueryEvaluable) cloneThing(origC.getArg1(), fromElementMap, qopeMap), origC.getOp(), (QueryEvaluable) cloneThing(origC.getArg2(), fromElementMap, qopeMap));
} else if (constraint instanceof SimpleConstraint && constraint.getOp() == ConstraintOp.EQUALS && ((SimpleConstraint) constraint).getArg1() instanceof QueryField && ((SimpleConstraint) constraint).getArg2() instanceof QueryValue && ((QueryField) ((SimpleConstraint) constraint).getArg1()) .getFromElement() == qn && "id".equals(((QueryField) ((SimpleConstraint) constraint).getArg1()) .getFieldName())) { SimpleConstraint sc = new SimpleConstraint(new QueryField(qc, "identifier"), ConstraintOp.EQUALS, new QueryValue(idToIdentifier((Integer) (((QueryValue) ((SimpleConstraint) constraint).getArg2())).getValue()))); q.setConstraint(sc); } else { String clsName = ((QueryClass) qn).getType().getName(); String clsURI = TypeUtil.unqualifiedName(clsName); SimpleConstraint clsNameConstraint = new SimpleConstraint( new QueryField(qc, "className"), ConstraintOp.EQUALS, new QueryValue(clsURI));
public void testGetRealType() throws Exception { SimpleConstraint c1 = new SimpleConstraint(qeNum1, ConstraintOp.EQUALS, qeNum1); c1.negate(); assertEquals(c1.getOp(), ConstraintOp.NOT_EQUALS); SimpleConstraint c2 = new SimpleConstraint(qeNum1, ConstraintOp.NOT_EQUALS, qeNum1); c2.negate(); assertEquals(c2.getOp(), ConstraintOp.EQUALS); SimpleConstraint c3 = new SimpleConstraint(qeNum1, ConstraintOp.LESS_THAN, qeNum1); c3.negate(); assertEquals(c3.getOp(), ConstraintOp.GREATER_THAN_EQUALS); SimpleConstraint c4 = new SimpleConstraint(qeNum1, ConstraintOp.GREATER_THAN_EQUALS, qeNum1); c4.negate(); assertEquals(c4.getOp(), ConstraintOp.LESS_THAN); SimpleConstraint c5 = new SimpleConstraint(qeNum1, ConstraintOp.GREATER_THAN, qeNum1); c5.negate(); assertEquals(c5.getOp(), ConstraintOp.LESS_THAN_EQUALS); SimpleConstraint c6 = new SimpleConstraint(qeNum1, ConstraintOp.LESS_THAN_EQUALS, qeNum1); c6.negate(); assertEquals(c6.getOp(), ConstraintOp.GREATER_THAN); SimpleConstraint c7 = new SimpleConstraint(qeStr1, ConstraintOp.MATCHES, qeStr1); c7.negate(); assertEquals(c7.getOp(), ConstraintOp.DOES_NOT_MATCH); SimpleConstraint c8 = new SimpleConstraint(qeStr1, ConstraintOp.DOES_NOT_MATCH, qeStr1); c8.negate(); assertEquals(c8.getOp(), ConstraintOp.MATCHES); SimpleConstraint c9 = new SimpleConstraint(qeNum1, ConstraintOp.IS_NULL); c9.negate(); assertEquals(c9.getOp(), ConstraintOp.IS_NOT_NULL); SimpleConstraint c10 = new SimpleConstraint(qeNum1, ConstraintOp.IS_NOT_NULL); c10.negate();
public void testHashCode() throws Exception { SimpleConstraint c1 = new SimpleConstraint(qeNum1, ConstraintOp.EQUALS, qeNum1); SimpleConstraint c2 = new SimpleConstraint(qeNum1, ConstraintOp.EQUALS, qeNum1); SimpleConstraint c3 = new SimpleConstraint(qeStr1, ConstraintOp.EQUALS, qeStr2); SimpleConstraint c4 = new SimpleConstraint(qeStr1, ConstraintOp.EQUALS, qeStr2); c4.negate(); assertEquals(c1.hashCode(), c1.hashCode()); assertEquals(c1.hashCode(), c2.hashCode()); assertTrue("Expected c1 hashCode() to not equal c3.hashCode():", c1.hashCode() != c3.hashCode()); assertTrue("Expected c3.hashCode() to not equal c4.hashCode():", c3.hashCode() != c4.hashCode()); } }
public void testEquals() throws Exception { SimpleConstraint c1 = new SimpleConstraint(qeNum1, ConstraintOp.EQUALS, qeNum1); SimpleConstraint c2 = new SimpleConstraint(qeNum1, ConstraintOp.EQUALS, qeNum1); SimpleConstraint c3 = new SimpleConstraint(qeStr1, ConstraintOp.EQUALS, qeStr2); SimpleConstraint c4 = new SimpleConstraint(qeStr1, ConstraintOp.EQUALS, qeStr2); c4.negate(); assertEquals(c1, c1); assertEquals(c1, c2); assertTrue("Expected c1 to not equal c3:", !c1.equals(c3)); assertTrue("Expected c3 to not equal c4:", !c3.equals(c4)); }
public static Query whereSimpleNegEquals() throws Exception { QueryClass c1 = new QueryClass(Company.class); QueryValue v1 = new QueryValue(new Integer(1234)); QueryField f1 = new QueryField(c1, "vatNumber"); QueryField f2 = new QueryField(c1, "name"); SimpleConstraint sc1 = new SimpleConstraint(f1, ConstraintOp.EQUALS, v1); sc1.negate(); Query q1 = new Query(); q1.addFrom(c1); q1.addToSelect(f2); q1.setConstraint(sc1); return q1; }
left = ((ContainsConstraint) constraint).getReference(); } else if (constraint instanceof SimpleConstraint) { left = ((SimpleConstraint) constraint).getArg1(); } else if (constraint instanceof BagConstraint) { left = ((BagConstraint) constraint).getQueryNode();
right = ((ContainsConstraint) constraint).getQueryClass(); } else if (constraint instanceof SimpleConstraint) { right = ((SimpleConstraint) constraint).getArg2(); } else if (constraint instanceof SubqueryConstraint) { right = ((SubqueryConstraint) constraint).getQuery();
/** * Construct a Constraint. Check that correct constraint op is selected for * single QueryEvaluable constructor * * @param qe1 first QueryEvaluable for comparison * @param op define op of comparison * @throws IllegalArgumentException if op does not correspond to a defined operation */ public SimpleConstraint(QueryEvaluable qe1, ConstraintOp op) { if (qe1 == null) { throw new NullPointerException("qe1 cannot be null"); } if (op == null) { throw new NullPointerException("op cannot be null"); } if (!validComparison(qe1.getType(), op, null)) { throw new IllegalArgumentException("Invalid constraint: " + qe1.getType().getName() + " " + op); } this.qe1 = qe1; this.op = op; }
if ((origC.getOp() == ConstraintOp.IS_NULL) || (origC.getOp() == ConstraintOp.IS_NOT_NULL)) { return new SimpleConstraint((QueryEvaluable) cloneThing(origC.getArg1(), fromElementMap, qopeMap), origC.getOp()); } else { return new SimpleConstraint((QueryEvaluable) cloneThing(origC.getArg1(), fromElementMap, qopeMap), origC.getOp(), (QueryEvaluable) cloneThing(origC.getArg2(), fromElementMap, qopeMap));
} else if (constraint instanceof SimpleConstraint && constraint.getOp() == ConstraintOp.EQUALS && ((SimpleConstraint) constraint).getArg1() instanceof QueryField && ((SimpleConstraint) constraint).getArg2() instanceof QueryValue && ((QueryField) ((SimpleConstraint) constraint).getArg1()) .getFromElement() == qn && "id".equals(((QueryField) ((SimpleConstraint) constraint).getArg1()) .getFieldName())) { SimpleConstraint sc = new SimpleConstraint(new QueryField(qc, "identifier"), ConstraintOp.EQUALS, new QueryValue(idToIdentifier((Integer) (((QueryValue) ((SimpleConstraint) constraint).getArg2())).getValue()))); q.setConstraint(sc); } else { String clsName = ((QueryClass) qn).getType().getName(); String clsURI = TypeUtil.unqualifiedName(clsName); SimpleConstraint clsNameConstraint = new SimpleConstraint( new QueryField(qc, "className"), ConstraintOp.EQUALS, new QueryValue(clsURI));
public static Query whereSimpleNegEquals() throws Exception { QueryClass c1 = new QueryClass(Company.class); QueryValue v1 = new QueryValue(new Integer(1234)); QueryField f1 = new QueryField(c1, "vatNumber"); QueryField f2 = new QueryField(c1, "name"); SimpleConstraint sc1 = new SimpleConstraint(f1, ConstraintOp.EQUALS, v1); sc1.negate(); Query q1 = new Query(); q1.addFrom(c1); q1.addToSelect(f2); q1.setConstraint(sc1); return q1; }
/** * Perform test for NOT NULL constraint on node * @param c the constraint to test */ public void apply(Constraint c) { if (c instanceof SimpleConstraint) { SimpleConstraint sc = (SimpleConstraint) c; if (sc.getOp().equals(ConstraintOp.IS_NOT_NULL)) { if (node.equals(sc.getArg1())) { exists = true; } } } } }
left = ((ContainsConstraint) constraint).getReference(); } else if (constraint instanceof SimpleConstraint) { left = ((SimpleConstraint) constraint).getArg1(); } else if (constraint instanceof BagConstraint) { left = ((BagConstraint) constraint).getQueryNode();
right = ((ContainsConstraint) constraint).getQueryClass(); } else if (constraint instanceof SimpleConstraint) { right = ((SimpleConstraint) constraint).getArg2(); } else if (constraint instanceof SubqueryConstraint) { right = ((SubqueryConstraint) constraint).getQuery();