Skip to main content

[el-spec commits] [el-spec~source-code:35] Add junit tests

  • From: kchung@...
  • To: commits@...
  • Subject: [el-spec commits] [el-spec~source-code:35] Add junit tests
  • Date: Wed, 11 Jan 2012 00:05:06 +0000

Project:    el-spec
Repository: source-code
Revision:   35
Author:     kchung
Date:       2012-01-11 00:05:04 UTC
Link:       

Log Message:
------------
Add junit tests



Revisions:
----------
35


Modified Paths:
---------------
trunk/impl/pom.xml
trunk/impl


Added Paths:
------------
trunk/impl/src/test/java/org/glassfish
trunk/impl/src/test/java/org/glassfish/el/test/ELProcessorTest.java
trunk/impl/src/test/java/org/glassfish/el/test
trunk/impl/src/test/java
trunk/impl/src/test/java/org/glassfish/el/test/Order.java
trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java
trunk/impl/src/test/java/org/glassfish/el/test/Product.java
trunk/impl/src/test/java/org/glassfish/el/test/Customer.java
trunk/impl/src/test/java/org/glassfish/el
trunk/impl/src/test
trunk/impl/src/test/java/org/glassfish/el/test/Manager.java
trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java.sav
trunk/impl/src/test/java/org
trunk/impl/src/test/java/org/glassfish/el/test/DataBase.java


Diffs:
------
Index: trunk/impl/src/test/java/org/glassfish/el/test/ELProcessorTest.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/ELProcessorTest.java 
(revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/ELProcessorTest.java 
(revision 35)
@@ -0,0 +1,36 @@
+package org.glassfish.el.test;
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.el.ELProcessor;
+
+public class ELProcessorTest {
+
+    ELProcessor elp;
+
+    @Before
+    public void setUp() {
+        elp = Manager.getManager().getElp();
+    }
+
+    @Test
+    public void testGetValue() {
+        Object result = elp.getValue("10 + 1");
+        assertEquals(result.toString(), "11");
+        result = elp.getValue("10 + 2", String.class);
+        assertEquals(result, "12");
+    }
+
+    @Test
+    public void testSetVariable () {
+        elp.setVariable("xx", "100");
+        Object result = elp.getValue("xx + 11", String.class);
+        assertEquals(result, "111");
+        elp.setVariable("xx", null);
+        assertEquals(elp.getValue("xx"), null);
+    }
+}
Index: trunk/impl/src/test/java/org/glassfish/el/test/Product.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/Product.java (revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/Product.java (revision 35)
@@ -0,0 +1,34 @@
+package org.glassfish.el.test;
+
+public class Product {
+
+    public int productID; 
+    public String name;
+    public String category;
+    public double unitPrice;
+    public int unitsInStock;
+
+    Product (int productID, String name, String category,
+             double unitPrice, int unitsInStock) {
+
+        this.productID = productID;
+        this.name = name;
+        this.category = category;
+        this.unitPrice = unitPrice;
+        this.unitsInStock = unitsInStock;
+    }
+
+    public String toString() {
+        return "Product: id=" + productID + ", name=" + name +
+            ", category=" +
+            category + ", unitPrice=" + unitPrice + ", unitsInStock=" +
+            unitsInStock;
+    }
+
+    public int getProductID() { return productID; }
+    public String getName() { return name; }
+    public String getCategory() { return category; }
+    public double getUnitPrice() { return unitPrice; }
+    public int getUnitsInStock() { return unitsInStock; }
+
+}
Index: trunk/impl/src/test/java/org/glassfish/el/test/Customer.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/Customer.java        
(revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/Customer.java        
(revision 35)
@@ -0,0 +1,33 @@
+package org.glassfish.el.test;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class Customer {
+    int customerID;
+    String name;
+    String address;
+    String city;
+    String country;
+    String phone;
+    List<Order> orders;
+
+    public Customer(int customerID, String name, String address, String city,
+             String country, String phone) {
+        this.customerID = customerID;
+        this.name = name;
+        this.address = address;
+        this.city = city;
+        this.country = country;
+        this.phone = phone;
+        this.orders = new ArrayList<Order>();
+    }
+
+    public int getCustomerID() { return customerID;}
+    public String getName() { return name;}
+    public String getAddress() { return address; }
+    public String getCity() { return city; }
+    public String getCountry() { return country; }
+    public String getPhone() { return phone; }
+    public List<Order> getOrders() { return orders; }
+}
Index: trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java        
(revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java        
(revision 35)
@@ -0,0 +1,150 @@
+package org.glassfish.el.test;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.el.ELProcessor;
+
+public class LinqTest {
+
+    ELProcessor elp;
+    Object ret;
+
+    @Before
+    public void setUP() {
+        Manager manager = Manager.getManager();
+        manager.setupDB();
+        elp = manager.getElp();
+    }
+
+    void p(String msg) {
+        System.out.println(msg);
+    }
+
+    static int v[] = {200, 203, 204};
+
+    @Test
+    public void testWhere() {
+        p("** Query ** products.where(p->p.unitPrice >= 10)");
+        ret = elp.getValue("products.where(p->p.unitPrice >= 10)");
+        assertTrue(Iterable.class.isInstance(ret));
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(((Product)item).getProductID() == v[indx++]);
+        }
+    }
+
+    static String pnames[] = {"Eagle", "Coming Home", "Greatest Hits",
+                       "History of Golf", "Toy Story" };
+    @Test
+    public void testSelect() {
+        p("** Query ** products.select(p->p.name)");
+        ret = elp.getValue("products.select(p->p.name)");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(pnames[indx++].equals(item));
+        }
+    }
+
+    static String p2names[] = {"Eagle", "History of Golf", "Toy Story"};
+    static double p2price[] = {12.5, 11.0, 10.0};
+    @Test
+    public void testSelect2() {
+        p("** Query ** products.where(p->p.unitPrice >= 10)");
+        p("                    .select(p->[p.name, p.unitPrice])");
+        ret = elp.getValue("products.where(p->p.unitPrice >= 10)" +
+                                   ".select(p->[p.name,p.unitPrice])");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(List.class.isInstance(item));
+            assertEquals(((List)item).get(0), p2names[indx]);
+            assertTrue(((List)item).get(1).equals(p2price[indx++]));
+        }
+    }
+
+    static int i3[] = {0, 3, 4};
+    @Test
+    public void testSelect3() {
+        p("** Query ** products.select((p,i)->{'product':p,'index':i}).");
+        p("                     where(p->p.product.unitPrice >= 10).");
+        p("                     select(p->p.index)");
+        ret = elp.getValue("products.select((p,i)->{'product':p, 
'index':i})." +
+                                    "where(p->p.product.unitPrice >= 10)." +
+                                    "select(p->p.index)");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(item.equals(i3[indx++]));
+        }
+    }
+
+    static int i2[] = {10, 11, 12, 13, 14};
+    @Test
+    public void testSelectMany() {
+        p("** Query ** customers.where(c->c.country == 'USA').");
+        p("                      selectMany(c->c.orders)");
+        ret = elp.getValue("customers.where(c->c.country == 'USA')." +
+                                     "selectMany(c->c.orders)");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertEquals(((Order)item).getOrderID(), i2[indx++]);
+        }
+    }
+
+    @Test
+    public void testSelectMany2() {
+        p("** Query ** customers.where(c->c.country == 'USA').");
+        p("                      selectMany(c->c.orders, 
(c,o)->{'o':o,'c':c}).");
+        p("                      where(co->co.o.orderData.year == 2011)");
+        p("                      select(co->[co.c.name, co.o.orderID])");
+        ret = elp.getValue(
+                 "customers.where(c->c.country == 'USA')." +
+                           "selectMany(c->c.orders, (c,o)->{'o':o,'c':c})." +
+                           "where(co->co.o.orderDate.year == 2011)." +
+                           "select(co->[co.c.name, co.o.orderID])");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.getClass().getName() + item.toString());
+            indx++;
+        }
+        assertTrue(indx == 3);
+    }
+
+    @Test
+    public void testSelectMany2a() {
+        p("** Query **");
+        p("customers.where(c->c.country == 'USA').");
+        p("          selectMany(c->c.orders).");
+        p("          where(o->o.orderData.year == 2011).");
+        p("          
select(o->[customers.where(c.customerID==o->o.customerID).");
+        p("                               select(c.name).");
+        p("                               single(),");
+        p("                     o.orderID])");
+
+        ret = elp.getValue(
+                 "customers.where(c->c.country == 'USA')." +
+                           "selectMany(c->c.orders)." +
+                           "where(o->o.orderDate.year == 2011)." +
+                           "select(o->" +
+                               
"[customers.where(c->c.customerID==o.customerID)." +
+                                          "select(c->c.name)." +
+                                          "single()," +
+                               "o.orderID])");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.getClass().getName() + item.toString());
+            indx++;
+        }
+        assertTrue(indx == 3);
+    }
+}
+
Index: trunk/impl/src/test/java/org/glassfish/el/test/DataBase.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/DataBase.java        
(revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/DataBase.java        
(revision 35)
@@ -0,0 +1,88 @@
+package org.glassfish.el.test;
+
+import java.util.Date;
+import java.util.List;
+import java.util.ArrayList;
+
+public class DataBase {
+
+    private int curCustomer = 100;
+    private int curProduct = 200;
+    private int curOrder = 10;
+    private boolean inited;
+
+    private List<Customer> customers;
+    private List<Product> products;
+    private List<Order> orders;
+
+    public List<Customer> getCustomers() { return this.customers; }
+    public List<Product> getProducts() { return this.products; }
+    public List<Order> getOrders() { return this.orders; }
+
+    public void init() {
+        if (inited) {
+            return;
+        }
+
+        inited = true;
+        customers = new ArrayList<Customer>();
+        orders = new ArrayList<Order>();
+        products = new ArrayList<Product>();
+        initCustomer();
+        initProduct();
+        initOrder();
+    }        
+
+    void initCustomer() {
+        c("John Doe", "123 Willow Road", "Menlo Park", "USA",
+                    "650-734-2187");
+        c("Mary Lane", "75 State Street", "Atlanta", "USA", "302-145-8765");
+        c("Charlie Yeh", "5 Nathan Road", "Kowlon", "Hong Kong", 
"11-7565-2323");
+    }
+
+    void initProduct() {
+        p("Eagle", "book", 12.50, 100);  // id: 200
+        p("Coming Home", "dvd", 8.00, 50);  // id: 201
+        p("Greatest Hits", "cd", 6.5, 200);  // id: 202
+        p("History of Golf", "book", 11.0, 30);  // id: 203
+        p("Toy Story", "dvd", 10.00, 1000);  // id: 204
+    }
+
+    void initOrder() {
+        o(100, new Date(2010, 2, 18), 20.80);
+        o(100, new Date(2011, 5, 3), 34.50);
+        o(100, new Date(2011, 8, 2), 210.75);
+        o(101, new Date(2011, 1, 15), 50.23);
+        o(101, new Date(2012, 1, 3), 126.77);
+        o(102, new Date(2011, 4, 15), 101.20);
+    }
+
+    void c(String name, String address, String city,
+                  String country, String phone) {
+        customers.add(new Customer(curCustomer++, name, address,
+                                   city, country, phone));
+    }
+
+    void o(int customerID, Date orderDate, double total) {
+        Order order = new Order(curOrder++, customerID, orderDate, total);
+        System.out.println("### order = " + curOrder + ", customer = " +
+            customerID);
+        this.orders.add(order);
+        findCustomer(customerID).getOrders().add(order);
+    }
+
+    void p(String name, String category, double unitPrice, int unitsInStock) 
{
+        products.add(new Product(curProduct++, name, category, unitPrice,
+                                 unitsInStock));
+    }
+
+    private Customer findCustomer(int id) {
+        for(Customer customer: customers) {
+            if (customer.customerID == id) {
+                return customer;
+            }
+        }
+        return null;
+    }
+}
+
Index: trunk/impl/src/test/java/org/glassfish/el/test/Manager.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/Manager.java (revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/Manager.java (revision 35)
@@ -0,0 +1,37 @@
+package org.glassfish.el.test;
+
+import javax.el.ELProcessor;
+
+public class Manager {
+
+    ELProcessor elp;
+    DataBase database = null;
+
+    public ELProcessor getElp() {
+        if (elp == null) {
+            elp = new ELProcessor();
+        }
+        return elp;
+    }
+
+    public void setupDB() {
+        if (database == null) {
+            database = new DataBase();
+            database.init();
+        }
+        getElp();
+        elp.defineBean("customers", database.getCustomers());
+        elp.defineBean("products", database.getProducts());
+        elp.defineBean("orders", database.getOrders());
+    }
+
+    static Manager manager;
+
+    static Manager getManager() {
+        if (manager == null) {
+            manager = new Manager();
+        }
+        return manager;
+    }
+}
+
Index: trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java.sav
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java.sav    
(revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/LinqTest.java.sav    
(revision 35)
@@ -0,0 +1,101 @@
+package org.glassfish.el.test;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import javax.el.ELProcessor;
+
+public class LinqTest {
+
+    ELProcessor elp;
+    Object ret;
+
+    @Before
+    public void setUP() {
+        Manager manager = Manager.getManager();
+        manager.setupDB();
+        elp = manager.getElp();
+    }
+
+    void p(String msg) {
+        System.out.println(msg);
+    }
+
+    static int v[] = {200, 203, 204};
+
+    @Test
+    public void testWhere() {
+        p("** Query ** products.where(p->p.unitPrice >= 10)");
+        ret = elp.getValue("products.where(p->p.unitPrice >= 10)");
+        assertTrue(Iterable.class.isInstance(ret));
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(((Product)item).getProductID() == v[indx++]);
+        }
+    }
+
+    static String pnames[] = {"Eagle", "Coming Home", "Greatest Hits",
+                       "History of Golf", "Toy Story" };
+    @Test
+    public void testSelect() {
+        p("** Query ** products.select(p->p.name)");
+        ret = elp.getValue("products.select(p->p.name)");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(pnames[indx++].equals(item));
+        }
+    }
+
+    static String p2names[] = {"Eagle", "History of Golf", "Toy Story"};
+    static double p2price[] = {12.5, 11.0, 10.0};
+    @Test
+    public void testSelect2() {
+        p("** Query ** products.where(p->p.unitPrice >= 10)");
+        p("                    .select(p->[p.name, p.unitPrice])");
+        ret = elp.getValue("products.where(p->p.unitPrice >= 10)" +
+                                   ".select(p->[p.name,p.unitPrice])");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(List.class.isInstance(item));
+            assertEquals(((List)item).get(0), p2names[indx]);
+            assertTrue(((List)item).get(1).equals(p2price[indx++]));
+        }
+    }
+
+    static int i3[] = {0, 3, 4};
+    @Test
+    public void testSelect3() {
+        p("** Query ** products.select((p,i)->{'product':p,'index':i}).");
+        p("                     where(p->p.product.unitPrice >= 10).");
+        p("                     select(p->p.index)");
+        ret = elp.getValue("products.select((p,i)->{'product':p, 
'index':i})." +
+                                    "where(p->p.product.unitPrice >= 10)." +
+                                    "select(p->p.index)");
+        int indx = 0;
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+            assertTrue(item.equals(i3[indx++]));
+        }
+    }
+
+    @Test
+    public void testSelectMany2() {
+        p("** Query ** customers.where(c->c.country == 'USA').");
+        p("                      selectMany(c->orders, 
(c,o)->{'o':o,'c':c}).");
+        p("                      where(co->co.o.orderData.year == 2011)");
+        ret = elp.getValue("customers.where(c->c.country == 'USA')." +
+                                     "selectMany(c->c.orders)");
+        for (Object item: (Iterable) ret) {
+            p(item.toString());
+        }
+    }
+}
+
Index: trunk/impl/src/test/java/org/glassfish/el/test/Order.java
===================================================================
--- trunk/impl/src/test/java/org/glassfish/el/test/Order.java   (revision 0)
+++ trunk/impl/src/test/java/org/glassfish/el/test/Order.java   (revision 35)
@@ -0,0 +1,28 @@
+package org.glassfish.el.test;
+
+import java.util.Date;
+
+public class Order {
+
+    int orderID; 
+    int customerID;
+    Date orderDate;
+    double total;
+
+    public Order(int orderID, int customerID, Date orderDate, double total) {
+        this.orderID = orderID;
+        this.customerID = customerID;
+        this.orderDate = orderDate;
+        this.total = total;
+    }
+
+    public String toString() {
+        return "Order: orderID = " + orderID + " customerID = " + customerID 
+
+            " orderDate = " + orderDate + " total = " + total;
+    }
+
+    public int getOrderID() { return orderID; }
+    public int getCustomerID() { return customerID; }
+    public Date getOrderDate() { return orderDate; }
+    public double getTotal() { return total; }
+}
Index: trunk/impl/pom.xml
===================================================================
--- trunk/impl/pom.xml  (revision 34)
+++ trunk/impl/pom.xml  (revision 35)
@@ -146,7 +146,8 @@
                         <Private-Package>
                             com.sun.el.lang,
                             com.sun.el.parser,
-                            com.sun.el.util
+                            com.sun.el.util,
+                            com.sun.el.query
                         </Private-Package>
                     </instructions>
                 </configuration>
@@ -244,6 +245,23 @@
                     <arguments>${release.arguments}</arguments>
                 </configuration>                                             
         
             </plugin>                    
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.7.1</version>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.apache.maven.surefire</groupId>
+                        <artifactId>surefire-junit47</artifactId>
+                        <version>2.7.1</version>
+                    </dependency>
+                </dependencies>
+                <configuration>
+                    <forkMode>never</forkMode>
+                    <!--
+                    <parallel>classes</parallel>
+                    -->
+                </configuration>
+            </plugin>
         </plugins>
         <resources>
             <resource>
@@ -284,5 +302,11 @@
             <artifactId>javax.el-api</artifactId>
             <version>3.0.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Property changes on: trunk/impl
___________________________________________________________________
Added: svn:ignore
   + target







[el-spec commits] [el-spec~source-code:35] Add junit tests

kchung 01/11/2012
 
 
Close
loading
Please Confirm
Close