cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1397611 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/jpa/ test/java/org/apache/cxf/jaxrs/ext/search/jpa/
Date Fri, 12 Oct 2012 15:16:36 GMT
Author: sergeyb
Date: Fri Oct 12 15:16:36 2012
New Revision: 1397611

URL: http://svn.apache.org/viewvc?rev=1397611&view=rev
Log:
Adding few more FIQL to JPA converter tests

Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java?rev=1397611&r1=1397610&r2=1397611&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
Fri Oct 12 15:16:36 2012
@@ -119,8 +119,11 @@ public class JPATypedQueryVisitor<T> ext
             break;
         case EQUALS:
             if (clazz.equals(String.class)) {
-                pred = builder.like(path.as(String.class), "%"
-                                    + (String) value + "%");
+                String theValue = (String)value;
+                if (theValue.contains("*")) {
+                    theValue = ((String)value).replaceAll("\\*", "");
+                }
+                pred = builder.like(path.as(String.class), "%" + theValue + "%");
             } else {
                 pred = builder.equal(path.as(clazz), clazz.cast(value));
             }

Modified: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book.java?rev=1397611&r1=1397610&r2=1397611&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book.java
Fri Oct 12 15:16:36 2012
@@ -25,7 +25,9 @@ import javax.persistence.Id;
 public class Book {
     @Id
     private int id;
-
+    private String name;
+    
+    
     public int getId() {
         return id;
     }
@@ -33,4 +35,12 @@ public class Book {
     public void setId(int id) {
         this.id = id;
     }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
 }

Modified: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java?rev=1397611&r1=1397610&r2=1397611&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
Fri Oct 12 15:16:36 2012
@@ -59,14 +59,17 @@ public class JPATypedQueryVisitorTest ex
             em.getTransaction().begin();
             Book b1 = new Book();
             b1.setId(9);
+            b1.setName("num9");
             em.persist(b1);
             assertTrue(em.contains(b1));
             Book b2 = new Book();
             b2.setId(10);
+            b2.setName("num10");
             em.persist(b2);
             assertTrue(em.contains(b2));
             Book b3 = new Book();
             b3.setId(11);
+            b3.setName("num11");
             em.persist(b3);
             assertTrue(em.contains(b3));
             
@@ -97,14 +100,83 @@ public class JPATypedQueryVisitorTest ex
     }
     
     @Test
-    public void testSimpleQuery() throws Exception {
-        SearchCondition<Book> filter = new FiqlParser<Book>(Book.class).parse("id=lt=10,id=gt=10");
-        JPATypedQueryVisitor<Book> jpa = new JPATypedQueryVisitor<Book>(em, Book.class);
-        filter.accept(jpa);
-        TypedQuery<Book> query = jpa.getQuery();
-        List<Book> books = query.getResultList();
+    public void testOrQuery() throws Exception {
+        List<Book> books = queryBooks("id=lt=10,id=gt=10");
         assertEquals(2, books.size());
         assertTrue(9 == books.get(0).getId() && 11 == books.get(1).getId()
             || 11 == books.get(0).getId() && 9 == books.get(1).getId());
     }
+    
+    @Test
+    public void testOrQueryNoMatch() throws Exception {
+        List<Book> books = queryBooks("id==7,id==5");
+        assertEquals(0, books.size());
+    }
+    
+    @Test
+    public void testAndQuery() throws Exception {
+        List<Book> books = queryBooks("id==10;name==num10");
+        assertEquals(1, books.size());
+        assertTrue(10 == books.get(0).getId() && "num10".equals(books.get(0).getName()));
+    }
+    
+    @Test
+    public void testAndQueryNoMatch() throws Exception {
+        List<Book> books = queryBooks("id==10;name==num9");
+        assertEquals(0, books.size());
+    }
+    
+    @Test
+    public void testEqualsQuery() throws Exception {
+        List<Book> books = queryBooks("id==10");
+        assertEquals(1, books.size());
+        assertTrue(10 == books.get(0).getId());
+    }
+    
+    @Test
+    public void testEqualsWildcard() throws Exception {
+        List<Book> books = queryBooks("name==num1*");
+        assertEquals(2, books.size());
+        assertTrue(10 == books.get(0).getId() && 11 == books.get(1).getId()
+            || 11 == books.get(0).getId() && 10 == books.get(1).getId());
+    }
+    
+    @Test
+    public void testGreaterQuery() throws Exception {
+        List<Book> books = queryBooks("id=gt=10");
+        assertEquals(1, books.size());
+        assertTrue(11 == books.get(0).getId());
+    }
+    
+    @Test
+    public void testGreaterEqualQuery() throws Exception {
+        List<Book> books = queryBooks("id=ge=10");
+        assertEquals(2, books.size());
+        assertTrue(10 == books.get(0).getId() && 11 == books.get(1).getId()
+            || 11 == books.get(0).getId() && 10 == books.get(1).getId());
+    }
+    
+    @Test
+    public void testLessEqualQuery() throws Exception {
+        List<Book> books = queryBooks("id=le=10");
+        assertEquals(2, books.size());
+        assertTrue(9 == books.get(0).getId() && 10 == books.get(1).getId()
+            || 9 == books.get(0).getId() && 10 == books.get(1).getId());
+    }
+    
+    @Test
+    public void testNotEqualsQuery() throws Exception {
+        List<Book> books = queryBooks("id!=10");
+        assertEquals(2, books.size());
+        assertTrue(9 == books.get(0).getId() && 11 == books.get(1).getId()
+            || 11 == books.get(0).getId() && 9 == books.get(1).getId());
+    }
+    
+    private List<Book> queryBooks(String expression) throws Exception {
+        SearchCondition<Book> filter = new FiqlParser<Book>(Book.class).parse(expression);
+        JPATypedQueryVisitor<Book> jpa = new JPATypedQueryVisitor<Book>(em, Book.class);
+        filter.accept(jpa);
+        TypedQuery<Book> query = jpa.getQuery();
+        return query.getResultList();
+    }
 }



Mime
View raw message