Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new resources to JCassandra interface

getResultList() 
Object	getSingleResult() 
setFirstResult(int startPosition) 
setFlushMode(FlushModeType flushMode) 
setHint(String hintName, Object value) 
setMaxResults(int maxResult)
  • Loading branch information...
commit a906ff10f1d31ce704d22f1f35d46b1df38c09a8 1 parent 3d5a798
@otaviojava authored
View
19 readme.txt
@@ -3,23 +3,6 @@ Easy-Cassandra
Easily programming with this framework, the Easy Cassandra
The EasyCassandra uses the Thrift implementation and has like the main objective be one simple ORM( Object relational manager). It need the jdk 7 for run, because some parts in your code was replaced reflection for invoke dynamic. So will have a behavior faster than other framework.
-Version: 1.0.8
-* Select Key from 'in' CQL command
-* Create automatically the ColumnFamily in Run Time
-* Create automatically the IndexValue in Run Time
-* Suport with cql 2.0
-
-
-Version: 1.0.7
-* update cassandra-thrift to 1.0.7
-
-Version: 1.0.6
-* Fixes bug with File
-* Support Calendar interface
-
-Version: 1.0.5
-* Can now store files
-* Support java.io.File and java.nio.file.Path
Version: 1.0.4
* more performance
@@ -40,4 +23,4 @@ Version: 1.0.1
Modifications
* Allowed use ColumnValue and ColumnFamilyValue in default mode this way its get the field's name
* Fixes bug in Reflection
-* Auto-Increment saved in XML Document
+* Auto-Increment saved in XML Document
View
15 src/main/java/org/easycassandra/annotations/read/EnumRead.java
@@ -15,6 +15,7 @@
package org.easycassandra.annotations.read;
import java.nio.ByteBuffer;
+
import org.easycassandra.util.EncodingUtil;
/**
@@ -22,12 +23,22 @@
* @author otavio
*/
public class EnumRead implements ReadInterface {
-
+ private Object[] enums;
+ public EnumRead(Class<?> enumereted){
+ this.enums = enumereted.getEnumConstants();
+
+ }
@Override
public Object getObjectByByte(ByteBuffer buffer) {
String integerString = EncodingUtil.byteToString(buffer);
- return Integer.parseInt(integerString);
+ try{
+
+ return enums[Integer.parseInt(integerString)];
+ }catch(NumberFormatException exception){
+
+ }
+ return enums[0];
}
View
3  src/main/java/org/easycassandra/annotations/read/FileRead.java
@@ -24,6 +24,9 @@
@Override
public Object getObjectByByte(ByteBuffer buffer) {
String value=EncodingUtil.byteToString(buffer);
+ if(value.length()==0){
+ return null;
+ }
String nameFile=value.substring(0,value.indexOf('|'));
String[] byteString=value.substring(value.indexOf('|')+1,value.lastIndexOf(',')).split(",");
byte[] bytes=new byte[byteString.length];
View
1  src/main/java/org/easycassandra/annotations/write/EnumWrite.java
@@ -15,6 +15,7 @@
package org.easycassandra.annotations.write;
import java.nio.ByteBuffer;
+
import org.easycassandra.util.EncodingUtil;
/**
View
5 src/main/java/org/easycassandra/persistence/BasePersistence.java
@@ -222,10 +222,7 @@ protected void readObject(Map<String, ByteBuffer> listMap, Object bean) throws I
ByteBuffer bb = listMap.get(ColumnUtil.getEnumeratedName(field));
if (bb != null) {
- Object[] enums = field.getType().getEnumConstants();
- Integer index = (Integer) new EnumRead().getObjectByByte(bb);
-
- ReflectionUtil.setMethod(bean, field, enums[index]);
+ ReflectionUtil.setMethod(bean, field, new EnumRead(field.getType()).getObjectByByte(bb));
}
}
View
24 src/main/java/org/easycassandra/persistence/DescribeFamilyObject.java
@@ -14,7 +14,11 @@
* Name of Column family
*/
private String columnFamilyName;
-
+ /**
+ * map when key is column in Cassandra database
+ * and the value is the Object's name
+ */
+ private Map<String, String> columnName;
private Class<?> classFamily;
@@ -33,10 +37,20 @@
DescribeField describeField=new DescribeField();
describeField.setTypeField(field);
describeField.setName(field.getName());
- fields.put(describeField.getName().toLowerCase(), describeField);
+ columnName.put(describeField.getRealCqlName(), describeField.getName());
+ fields.put(describeField.getName(), describeField);
}
}
+
+ /**
+ * return the Object's name
+ * @param column
+ * @return
+ */
+ public String getFieldsName(String column){
+ return columnName.get(column);
+ }
/**
* key-value for DescribeField
* @param value
@@ -44,9 +58,9 @@
*/
public DescribeField getField(String value){
- return fields.get(value.toLowerCase());
+ return fields.get(value);
}
- {fields=new HashMap<>();}
+
public String getColumnFamilyName() {
return columnFamilyName;
@@ -72,5 +86,5 @@ public void setFields(Map<String,DescribeField> fields) {
this.fields = fields;
}
-
+ {fields=new HashMap<>(); columnName=new HashMap<>();}
}
View
4 src/main/java/org/easycassandra/persistence/EasyCassandraManager.java
@@ -118,7 +118,7 @@ public static Persistence getPersistence(String keySpace, String host, int port)
*/
public static boolean addFamilyObject(Class<?> classColumnFamily) {
DescribeFamilyObject describeFamilyObject=new DescribeFamilyObject(classColumnFamily);
- familyObjects.put(describeFamilyObject.getColumnFamilyName().toLowerCase(), describeFamilyObject);
+ familyObjects.put(describeFamilyObject.getClassFamily().getSimpleName(), describeFamilyObject);
return true;
}
/**
@@ -128,7 +128,7 @@ public static boolean addFamilyObject(Class<?> classColumnFamily) {
*/
static DescribeFamilyObject getFamily(String string) {
- return familyObjects.get(string.toLowerCase());
+ return familyObjects.get(string);
}
View
36 src/main/java/org/easycassandra/persistence/JCassandra.java
@@ -0,0 +1,36 @@
+package org.easycassandra.persistence;
+
+import java.util.List;
+
+/**
+ * Inteface to run CQL
+ * @author otavio
+ *
+ */
+public interface JCassandra {
+
+ /**
+ * Execute a cql and return the result results as a List.
+ * @return result in list
+ */
+ List getResultList();
+
+ /**
+ * Execute a cql that returns a single result.
+ * @return the single result
+ */
+ Object getSingleResult();
+
+ /**
+ * Set the position of the first result to retrieve.
+ * @param startPosition - the start position of the first result, numbered from 0
+ */
+ void setFirstResult(int startPosition);
+
+ /**
+ * Set the maximum number of results to retrieve.
+ * @param maxResult
+ */
+ void setMaxResults(int maxResult);
+
+}
View
310 src/main/java/org/easycassandra/persistence/JCassandraImpl.java
@@ -5,6 +5,12 @@
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.easycassandra.annotations.read.EnumRead;
+import org.easycassandra.persistence.DescribeField.TypeField;
+import org.easycassandra.util.EncodingUtil;
/**
@@ -12,18 +18,51 @@
* @author otavio
*
*/
-public class JCassandraImpl {
-
+ class JCassandraImpl implements JCassandra {
+ /**
+ * Class for run the queries
+ */
+ private Persistence persistence;
+ /**
+ *
+ */
+ private StringBuilder cql;
+
+ /**
+ * information about the query
+ */
+ private InformationQuery informationCQL;
- public boolean interpret(String expression) {
- InformationQuery informationCQL=new InformationQuery();
+ /**
+ * to start the position
+ */
+ private int startPosition=0;
+ /**
+ * maxResult in query
+ */
+ private int maxResult=-1;
+ /**
+ * set Persistence in Jcassandra Class
+ * @param persistence
+ */
+ public void setPersistence(Persistence persistence) {
+ this.persistence=persistence;
+
+ }
+
+ /**
+ * Constructor method
+ * @param expression
+ */
+ JCassandraImpl(String expression) {
+ informationCQL=new InformationQuery();
+
StringTokenizer expressionTokens = new StringTokenizer(cqlQuery(expression));
//in the future inner class
Period period=Period.BEFORE_SELECT;
- boolean needsComma=false;
- boolean needsCondition=false;
+
//end of inner class
while (expressionTokens.hasMoreTokens())
@@ -53,11 +92,10 @@ public boolean interpret(String expression) {
period=Period.WHERE;
break;
case ",":
- if(!needsComma){
- throw new EasyCassandraException(" Syntax error: unnecessary comma ");
- }
- needsComma=false;
- case "count":
+ isNeedComma();
+ break;
+ case "count(*)":
+ informationCQL.countMode=true;
break;
case "<":
case ">":
@@ -66,36 +104,68 @@ public boolean interpret(String expression) {
case ">=":
case "and":
case "or":
- needsCondition=false;
+ informationCQL.needsCondition=false;
break;
default:
- if(Period.SELECT.equals(period)){
- if(needsComma){
- throw new EasyCassandraException(" Syntax error: need separate the attributes with ',' ");
- }
- informationCQL.variable.add(actualExpression);
- needsComma=true;
- }
- else if(Period.WHERE.equals(period)){
- if(needsCondition){
- throw new EasyCassandraException(" Syntax error: need condiction in query ");
- }
-
- informationCQL.addVariable(actualExpression);
- needsCondition=true;
- }
+ defaultCondition(period, actualExpression);
break;
}
- //in the future methods trates excpecionts
- if(Period.BEFORE_SELECT.equals(period)){
- throw new EasyCassandraException(" Syntax error: the CQL must begin with the select command ");
- }
+ isStartCorrect(period);
+
+
+
+
}
+ verifySyntax(informationCQL);
+ cql=new StringBuilder(expression);
+
+
+ }
+ private void isNeedComma() {
+ if(!informationCQL.needsComma){
+ throw new EasyCassandraException(" Syntax error: unnecessary comma ");
+ }
+ informationCQL.needsComma=false;
+ }
- if(!informationCQL.iscondition){
+ private void isStartCorrect(Period period) {
+ if(Period.BEFORE_SELECT.equals(period)){
+ throw new EasyCassandraException(" Syntax error: the CQL must begin with the select command ");
+ }
+ }
+/**
+ * Execute when enter in default option
+ * @param period
+ * @param actualExpression
+ */
+ private void defaultCondition(Period period, String actualExpression) {
+ if(Period.SELECT.equals(period)){
+ if(informationCQL.needsComma){
+ throw new EasyCassandraException(" Syntax error: need separate the attributes with ',' ");
+ }
+ informationCQL.variable.add(actualExpression);
+ informationCQL.needsComma=true;
+ }
+ else if(Period.WHERE.equals(period)){
+ if(informationCQL.needsCondition){
+ throw new EasyCassandraException(" Syntax error: need condiction in query ");
+ }
+
+ informationCQL.addVariable(actualExpression);
+ informationCQL.needsCondition=true;
+ }
+ }
+/**
+ *
+ * @param informationCQL
+ */
+private void verifySyntax(final InformationQuery informationCQL) {
+
+
+ if(!informationCQL.iscondition){
throw new EasyCassandraException(" Syntax error: ");
}
DescribeFamilyObject describeFamilyObject=EasyCassandraManager.getFamily(informationCQL.columnFamily);
@@ -109,12 +179,15 @@ else if(Period.WHERE.equals(period)){
}
for(String key:informationCQL.variabledMap.keySet()){
- if(describeFamilyObject.getField(informationCQL.variabledMap.get(key).variableName)==null){
+ DescribeField describeField=describeFamilyObject.getField(informationCQL.variabledMap.get(key).variableName);
+ if(describeField==null){
throw new EasyCassandraException(" Syntax error: unknown column "+informationCQL.variabledMap.get(key).variableName+" in Column Family "+informationCQL.columnFamily);
}
+ if(TypeField.INDEX.equals(describeField.getTypeField())||TypeField.INDEX.equals(describeField.getTypeField())){
+ throw new EasyCassandraException("The field "+describeField.getName()+" must be a key or index for be in condition");
+ }
}
- return true;
- }
+}
private String cqlQuery(String cql){
@@ -133,7 +206,7 @@ private String cqlQuery(String cql){
firtTime++;
}
- return newCql.toString().trim().toLowerCase();
+ return newCql.toString().trim();
}
/**
@@ -142,11 +215,22 @@ private String cqlQuery(String cql){
*
*/
class InformationQuery{
- /**
+
+
+ /**
* Name of the column Family
*/
private String columnFamily;
+
+ /**
+ * all object checked
+ */
private boolean allObject;
+ /**
+ * using count in select
+ */
+ private boolean countMode;
+
/**
* name of the variables
*/
@@ -155,6 +239,15 @@ private String cqlQuery(String cql){
* verify if is condition or variable
*/
private boolean iscondition=true;
+ /**
+ * need the character ','
+ */
+ private boolean needsComma=false;
+ /**
+ * need the condition
+ */
+ private boolean needsCondition=false;
+
/**
*
*/
@@ -206,4 +299,147 @@ void addVariable(String value){
*
*/
enum Period{BEFORE_SELECT,SELECT,FROM,AFTER_FROM,WHERE}
+
+
+@Override
+public List<?> getResultList() {
+ DescribeFamilyObject describeFamilyObject= EasyCassandraManager.getFamily(informationCQL.columnFamily);
+ String cqlNew = replaceToCQLName(describeFamilyObject);
+ List<?> list=null;
+ if(informationCQL.countMode){
+ List<Long> longList=new ArrayList<>();
+ longList.add(persistence.executeCommandCQL(cqlNew.toString()).rows.get(0).getColumns().get(0).value.asLongBuffer().get());
+ list=longList;
+ }
+ else if(informationCQL.allObject){
+
+ list=executeAll(describeFamilyObject, cqlNew);
+ }else{
+ list=executeSomeFields(describeFamilyObject, cqlNew);
+ }
+
+ runStartPosition(list);
+ runMaxResult(list);
+ return list;
+}
+/**
+ * run the max resulst
+ * @param list
+ */
+private void runMaxResult(List<?> list) {
+ if(maxResult==-1){
+ return;
+ }
+ while(list.size()>maxResult){
+ list.remove(list.size()-1);
+ }
+}
+/**
+ * run the start position
+ * @param list
+ */
+private void runStartPosition(List<?> list) {
+ for(int index=0;index<startPosition;index++){
+ if(index>list.size()){
+ break;
+ }
+ list.remove(0);
+ }
+}
+/**
+ * When the query has only fields in the objects
+ * @param describeFamilyObject
+ * @param cql
+ * @return
+ */
+private List<?> executeSomeFields(DescribeFamilyObject describeFamilyObject,
+ String cql) {
+ List<Map<String, Object>> listCql= new ArrayList<>();
+ for(Map<String, String> resultSet:persistence.executeCql(cql)){
+ Map<String, Object> resulMap=new HashMap<>();
+ for(String key: resultSet.keySet()){
+
+
+ DescribeField describeField= describeFamilyObject.getField(describeFamilyObject.getFieldsName(key));
+ if(describeField.getClassField().isEnum()){
+
+ resulMap.put(describeField.getName(),new EnumRead(describeField.getClassField()).getObjectByByte(EncodingUtil.stringToByte(resultSet.get(key))));
+ }else{
+ resulMap.put(describeField.getName(), Persistence.getReadManager().convert(EncodingUtil.stringToByte(resultSet.get(key)), describeField.getClassField()));
+ }
+ }
+ listCql.add(resulMap);
+ }
+ return listCql;
+}
+
+/**
+ * when the query is for all fields in object
+ * @param describeFamilyObject
+ * @param cql
+ * @return
+ */
+private List<?> executeAll(DescribeFamilyObject describeFamilyObject, String cql) {
+ try {
+ return persistence.listbyQuery(persistence.executeCommandCQL(cql),describeFamilyObject.getClassFamily());
+ } catch (InstantiationException | IllegalAccessException exception) {
+ Logger.getLogger(Persistence.class.getName()).log(Level.SEVERE, null, exception);
+ }
+ return new ArrayList<>();
+}
+
+
+@Override
+public Object getSingleResult() {
+ List<?> list=getResultList();
+ if(list.size()>0){
+ return list.get(0);
+ }
+ return null;
+}
+/**
+ * replace the objects name to column's name
+ * @param describeFamilyObject
+ * @return cql with column's name
+ */
+private String replaceToCQLName(DescribeFamilyObject describeFamilyObject) {
+ String aux=cql.toString().replace(describeFamilyObject.getClassFamily().getSimpleName(), describeFamilyObject.getColumnFamilyName());
+ for(String column:informationCQL.variable){
+ if(aux.contains(column)){
+ aux=aux.replace(column, describeFamilyObject.getField(column).getRealCqlName());
+ }
+ }
+
+ for(String key:informationCQL.variabledMap.keySet()){
+ DescribeField describeField=describeFamilyObject.getField(informationCQL.variabledMap.get(key).variableName);
+ if(aux.contains(key)){
+ aux=aux.replace(key, describeField.getRealCqlName());
+ }
+ }
+ return aux;
+}
+
+@Override
+public void setFirstResult(int startPosition) {
+isNegativeValue(startPosition);
+this.startPosition=startPosition;
+}
+/**
+ * Verify is the value is negative
+ * @param startPosition
+ */
+private void isNegativeValue(int startPosition) {
+ if(startPosition<0){
+ throw new EasyCassandraException("Illegal Argument: The argument must be not a negative value");
+ }
+}
+
+@Override
+public void setMaxResults(int maxResult) {
+ isNegativeValue(maxResult);
+ this.maxResult=maxResult;
+
+}
+
+
}
View
20 src/main/java/org/easycassandra/persistence/Persistence.java
@@ -228,7 +228,7 @@ public boolean executeUpdateCql(String query){
* @return the result executing query
* @param the name of index
*/
- private CqlResult executeCommandCQL(String cql,IndexColumnName...index) {
+ CqlResult executeCommandCQL(String cql,IndexColumnName...index) {
try {
return client.execute_cql_query(ByteBuffer.wrap(cql.getBytes()), Compression.NONE);
} catch (InvalidRequestException | UnavailableException | TimedOutException | SchemaDisagreementException | TException exception) {
@@ -674,6 +674,24 @@ public void setColumnFamily(String columnFamily) {
}
+ /**
+ * Create
+ * @param cql
+ * @return
+ */
+ public JCassandra createJCassandra(String cql) {
+ JCassandraImpl jCassandra=null;
+ try{
+ jCassandra=new JCassandraImpl(cql);
+ jCassandra.setPersistence(this);
+ }
+ catch (EasyCassandraException exception) {
+ Logger.getLogger(Persistence.class.getName()).log(Level.WARNING, "Column Family created with success, now trying execute the command again");
+ }
+
+ return jCassandra;
+ }
+
View
6 src/test/java/org/easycassandra/bean/PersonDAOTest.java
@@ -126,6 +126,8 @@ public void executeUpdateCqlFailTest(){
public void executeCqlTest(){
Assert.assertNotNull(dao.executeCql("select * from person"));
}
+
+
private Address getAddress() {
Address address = new Address();
address.setCep("40243-543");
@@ -144,9 +146,7 @@ private Person getPerson() {
return person;
}
-
-
-
+
View
8 src/test/java/org/easycassandra/bean/dao/PersistenceDao.java
@@ -5,6 +5,7 @@
import org.easycassandra.ConsistencyLevelCQL;
import org.easycassandra.persistence.EasyCassandraManager;
+import org.easycassandra.persistence.JCassandra;
import org.easycassandra.persistence.Persistence;
public class PersistenceDao<T> {
@@ -86,5 +87,12 @@ public boolean executeUpdateCql(String string) {
public List<Map<String, String>> executeCql(String string) {
return persistence.executeCql(string);
}
+
+ public JCassandra createJCassandra(String cql) {
+
+ return persistence.createJCassandra(cql);
+ }
+
+
}
View
155 src/test/java/org/easycassandra/bean/dao/PersistenceDaoTest.java
@@ -0,0 +1,155 @@
+package org.easycassandra.bean.dao;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
+import org.easycassandra.bean.model.Person;
+import org.easycassandra.bean.model.Sex;
+import org.easycassandra.persistence.EasyCassandraException;
+import org.easycassandra.persistence.EasyCassandraManager;
+import org.easycassandra.persistence.JCassandra;
+import org.easycassandra.persistence.Persistence;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class PersistenceDaoTest {
+
+ private Persistence persistence;
+
+ private PersistenceDao<Person> dao;
+
+ @Test
+ public void createJCassandraTest(){
+
+ Assert.assertNotNull(persistence.createJCassandra("select * from Person"));
+ }
+
+ @Test(expected=EasyCassandraException.class)
+ public void createJCassandraFailTest(){
+
+ Assert.assertNotNull(persistence.createJCassandra("select * from Otavio"));
+ }
+
+ @Test
+ public void runCqlTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person");
+ Assert.assertNotNull(jCassandra.getResultList());
+ }
+
+ @Test
+ public void runCqlListAllTest(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person");
+
+ List<Person> persons=jCassandra.getResultList();
+ Assert.assertEquals(persons.size(), dao.listAll().size());
+ }
+
+
+ @Test
+ public void runCqlfindByIdInclude(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person where id = 31 ");
+
+ List<Person> persons=jCassandra.getResultList();
+ Assert.assertEquals(persons.get(0).getName(), dao.retrieve(1l).getName());
+ }
+
+
+ @Test
+ public void runCqlSomeFieldsTest(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select name, id, year from Person");
+
+
+ Assert.assertNotNull(jCassandra.getResultList());
+ }
+
+ @Test
+ public void runCqlSomeFields2Test(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select name, id, year from Person");
+
+ List list=jCassandra.getResultList();
+ Assert.assertNotNull(list);
+ }
+
+ @Test
+ public void runCqlEqualsFieldTest(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select personalFile, sex, id, year from Person");
+
+ List<Map<String, Object>> list=jCassandra.getResultList();
+ Assert.assertTrue(list.get(3).get("personalFile") instanceof File);
+ }
+
+ @Test
+ public void runCqlEqualsFieldEnumTest(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select personalFile, sex, id, year from Person");
+
+ List<Map<String, Object>> list=jCassandra.getResultList();
+ Sex sex=(Sex) list.get(3).get("sex");
+ Assert.assertEquals(Sex.MALE, sex);
+ }
+
+ @Test
+ public void getSingleResultTest(){
+
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person where id = 31 ");
+ Person person=(Person)jCassandra.getSingleResult();
+ Assert.assertEquals(person.getId(),Long.valueOf(1l));
+ }
+
+ @Test
+ public void setFirstResultTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person ");
+ jCassandra.setFirstResult(2);
+ Person person=(Person)jCassandra.getResultList().get(0);
+ Assert.assertEquals(person.getId(), dao.listAll().get(2).getId());
+
+ }
+ @Test(expected=EasyCassandraException.class)
+ public void setFirstResultNegativeValueTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person ");
+ jCassandra.setFirstResult(-1);
+ Person person=(Person)jCassandra.getResultList().get(0);
+ Assert.assertEquals(person.getId(), dao.listAll().get(2).getId());
+ }
+
+ @Test
+ public void setMaxResultsTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person ");
+ jCassandra.setMaxResults(2);
+
+ Assert.assertEquals(jCassandra.getResultList().size(),2);
+
+ }
+ @Test(expected=EasyCassandraException.class)
+ public void setMaxResultsNegativeTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select * from Person ");
+ jCassandra.setMaxResults(-1);
+
+ Assert.assertEquals(jCassandra.getResultList().size(),2);
+
+ }
+
+ @Test
+ public void countTest(){
+ JCassandra jCassandra=persistence.createJCassandra("select count(*) from Person ");
+ Assert.assertTrue(jCassandra.getResultList().get(0) instanceof Long);
+ }
+ @BeforeClass
+ public static void initStatic(){
+ EasyCassandraManager.addFamilyObject(Person.class);
+ }
+
+ @Before
+ public void init(){
+ persistence = EasyCassandraManager.getPersistence("javabahia", "localhost", 9160);
+ dao = new PersistenceDao<>(Person.class);
+ }
+}
View
5 src/test/java/org/easycassandra/persistence/EasyCassandraManagerTest.java
@@ -20,13 +20,14 @@ public void getPersistenceTest(){
}
@Test
- public void addColumnFamily(){
+ public void addColumnFamilyTest(){
Assert.assertTrue(EasyCassandraManager.addFamilyObject(Person.class));
}
@Test
- public void getFamilyObject(){
+ public void getFamilyObjectTest(){
Assert.assertNotNull(EasyCassandraManager.getFamily("Person"));
}
+
}
View
59 src/test/java/org/easycassandra/persistence/JCassandraImplTest.java
@@ -15,88 +15,91 @@ public static void init(){
@Test
public void correctSyntaxTest(){
- JCassandraImpl dvdInterpreterClient = new JCassandraImpl();
- Assert.assertTrue(dvdInterpreterClient.interpret("select * from Person"));
+ Assert.assertNotNull(new JCassandraImpl("select * from Person"));
}
@Test(expected=EasyCassandraException.class)
public void erroSyntaxeSelectTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret(" * from Person");
+ new JCassandraImpl(" * from Person");
}
@Test(expected=EasyCassandraException.class)
public void erroSyntaxeAllCheckEspaceTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select * , name from Person");
+ new JCassandraImpl("select * , name from Person");
}
@Test(expected=EasyCassandraException.class)
public void erroUnecessaryCommaTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select *, name , from Person");
+ new JCassandraImpl("select *, name , from Person");
}
@Test(expected=EasyCassandraException.class)
public void erroSyntaxeAllCheckTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select *, name from Person");
+
+ new JCassandraImpl("select *, name from Person");
}
@Test(expected=EasyCassandraException.class)
public void erroNeedComa(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name age from Person");
+
+ new JCassandraImpl("select name age from Person");
}
@Test
public void selectVariableTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from Person");
+
+ new JCassandraImpl("select name, id from Person");
}
@Test
public void selectWhereTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from Person where id = :32");
+
+ new JCassandraImpl("select name, id from Person where id = :32");
}
@Test
public void selectWhere2ConditionTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from Person where id = :32 and name = 'Person' ");
+
+ new JCassandraImpl("select name, id from Person where id = :32 ");
}
@Test(expected=EasyCassandraException.class)
public void selectWhere2FailConditionTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from Person where KEY = :32 and name = ");
+
+ new JCassandraImpl("select name, id from Person where KEY = :32 and name = ");
}
@Test(expected=EasyCassandraException.class)
public void selectWhereFailTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from Person where KEY name");
+
+ new JCassandraImpl("select name, id from Person where KEY name");
}
@Test(expected=EasyCassandraException.class)
public void unknownColumnFamilyTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select name, id from unknown ");
+
+ new JCassandraImpl("select name, id from unknown ");
}
@Test(expected=EasyCassandraException.class)
public void selectWhereUnknownColumnTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- cassandraInterpreterClient.interpret("select unknow,name,id from Person where id =32 ");
+
+ new JCassandraImpl("select unknow,name,id from Person where id =32 ");
}
@Test
public void selectTest(){
- JCassandraImpl cassandraInterpreterClient = new JCassandraImpl();
- Assert.assertTrue(cassandraInterpreterClient.interpret("select sex,name,id from Person where id =32 "));
+
+ new JCassandraImpl("select sex,name,id from Person where id =32 ");
}
+ @Test(expected=EasyCassandraException.class)
+ public void selectWhereNotIndexOrSecundaryKeyTest(){
+
+ new JCassandraImpl("select name, id from Person where id = :32 and name = 'Person' ");
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.