/** * Add a sub {@code criteria} to the Criteria that is building. A sub * criteria is one that is wrapped in parenthesis. * * @param criteria * @return the builder */ public BuildableStartState group(Criteria criteria) { this.criteria.add(criteria); return new BuildableStartState(this.criteria); }
/** * Start building a new {@link Criteria}. * * @return the Criteria builder */ public static StartState where() { return new StartState(new Criteria()); }
/** * Build a conjunctive clause onto the {@link Criteria} that is building. * * @return the builder */ public StartState and() { criteria.add(ConjunctionSymbol.AND); return new StartState(criteria); }
@Test public void testRoundTrip() { String value = "`bar`"; Criteria expected = Criteria.where().key("foo") .operator(Operator.EQUALS).value(value).build(); Criteria actual = Language.translateFromThriftCriteria( Language.translateToThriftCriteria(expected)); Assert.assertEquals(expected, actual); }
@Test public void testComplexTObjectBinaryFormat() { Criteria criteria = Criteria.where().key("name") .operator(Operator.EQUALS).value("jeff").at(Timestamp.now()) .and().key("comapny").operator(Operator.EQUALS) .value("cinchapi").at(Timestamp.now()).build(); TCriteria tcriteria = Language.translateToThriftCriteria(criteria); ComplexTObject expected = ComplexTObject.fromJavaObject(tcriteria); ComplexTObject actual = ComplexTObject .fromByteBuffer(expected.toByteBuffer()); Assert.assertEquals(expected, actual); }
/** * Add the specified {@code value} to the {@link Criteria} that is building. * * @param value * @return the builder */ public ValueState value(Object value) { criteria.add(new ValueSymbol(value)); return new ValueState(criteria); }
@Test public void testSimpleOrAnd() { Assert.assertTrue(hasSameResults(Criteria.where().key("graduation_rate") .operator(Operator.GREATER_THAN).value(90).or() .key("percent_undergrad_black") .operator(Operator.GREATER_THAN_OR_EQUALS).value(5).and() .key("total_cost_out_state").operator(Operator.GREATER_THAN) .value(50000).build())); }
@Test public void testAndGroupOr() { Assert.assertTrue(hasSameResults(Criteria.where().key("graduation_rate") .operator(Operator.GREATER_THAN).value(90).and() .group(Criteria.where().key("percent_undergrad_black") .operator(Operator.GREATER_THAN_OR_EQUALS).value(5).or() .key("total_cost_out_state") .operator(Operator.GREATER_THAN).value(50000).build()) .build())); }
/** * Build a disjunctive clause onto the {@link Criteria} that is building. * * @return the builder */ public StartState or() { criteria.add(ConjunctionSymbol.OR); return new StartState(criteria); }
@Test public void testOrGroupAnd() { Assert.assertTrue(hasSameResults(Criteria.where().key("graduation_rate") .operator(Operator.GREATER_THAN).value(90).or() .group(Criteria.where().key("percent_undergrad_black") .operator(Operator.GREATER_THAN_OR_EQUALS).value(5) .and().key("total_cost_out_state") .operator(Operator.GREATER_THAN).value(50000).build()) .build())); }
@Test(expected = TransactionException.class) public void testNoPhantomRead() { client.add("foo", 10, 1); client.stage(); client.find(Criteria.where().key("foo").operator(Operator.BETWEEN) .value(5).value(20)); client2.add("foo", 15, 2); client.find(Criteria.where().key("foo").operator(Operator.BETWEEN) .value(5).value(20)); }
@Test public void testCachedObject() { Criteria criteria = Criteria.where().key(Random.getString()) .operator(Operator.EQUALS).value(Random.getObject()).build(); TCriteria expected = Language.translateToThriftCriteria(criteria); ComplexTObject complex = ComplexTObject.fromJavaObject(expected); TCriteria cachedObj = complex.getJavaObject(); // check if it is same reference if(cachedObj != complex.getJavaObject()) { Assert.fail(); } }
@Test(expected = TransactionException.class) public void testPreCommitTransactionFailuresAreIndicatedWithExceptionFindCriteria() { try { client.stage(); client.get("foo", 1); client2.set("foo", "baz", 1); client.find(Criteria.where().key("foo").operator(Operator.EQUALS) .value("bar")); } finally { client.abort(); } }