1   /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: Oct 22, 2002
5    * Time: 4:16:21 PM
6    * To change this template use Options | File Templates.
7    */
8   package test.atlassian.core.ofbiz;
9   
10  import com.atlassian.core.ofbiz.test.AbstractOFBizTestCase;
11  import com.atlassian.core.ofbiz.util.EntityUtils;
12  
13  import org.ofbiz.core.entity.EntityExpr;
14  import org.ofbiz.core.entity.EntityOperator;
15  import org.ofbiz.core.entity.GenericEntityException;
16  import org.ofbiz.core.entity.GenericValue;
17  import org.ofbiz.core.util.UtilDateTime;
18  import org.ofbiz.core.util.UtilMisc;
19  
20  import java.sql.Timestamp;
21  import java.util.Collections;
22  import java.util.List;
23  
24  public class TestEntityUtils extends AbstractOFBizTestCase
25  {
26      public void testGetOperator()
27      {
28          assertEquals(EntityOperator.EQUALS, EntityUtils.getOperator("="));
29          assertEquals(EntityOperator.GREATER_THAN, EntityUtils.getOperator(">"));
30          assertEquals(EntityOperator.GREATER_THAN_EQUAL_TO, EntityUtils.getOperator(">="));
31          assertEquals(EntityOperator.LESS_THAN, EntityUtils.getOperator("<"));
32          assertEquals(EntityOperator.LESS_THAN_EQUAL_TO, EntityUtils.getOperator("<="));
33          assertNull(EntityUtils.getOperator("foo"));
34      }
35  
36      public void testCreateValue() throws GenericEntityException
37      {
38          final GenericValue gv2 = EntityUtils.createValue("TestEntity", UtilMisc.toMap("id", new Long(20), "name", "baz"));
39          assertEquals(new Long(20), gv2.getLong("id"));
40          assertEquals("TestEntity", gv2.getEntityName());
41          assertEquals("baz", gv2.getString("name"));
42  
43          final GenericValue gv = EntityUtils.createValue("TestEntity", UtilMisc.toMap("name", "bar"));
44          assertEquals(new Long(1), gv.getLong("id"));
45          assertEquals("TestEntity", gv.getEntityName());
46          assertEquals("bar", gv.getString("name"));
47      }
48  
49      public void testContainsEntity()
50      {
51          final GenericValue issue = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(2),
52              "numvalue", new Long(1)));
53          final GenericValue action = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(3),
54              "numvalue", new Long(2)));
55  
56          assertTrue(EntityUtils.contains(UtilMisc.toList(action), action));
57          assertTrue(!EntityUtils.contains(UtilMisc.toList(action), issue));
58      }
59  
60      // always test identicals both ways around just to make sure
61      public void testIdentical()
62      {
63          assertTrue(EntityUtils.identical(null, null));
64  
65          final GenericValue entity1 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1)));
66          assertTrue(!EntityUtils.identical(entity1, null));
67          assertTrue(!EntityUtils.identical(null, entity1));
68          assertTrue(EntityUtils.identical(entity1, entity1));
69  
70          final GenericValue entity2 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity2", UtilMisc.toMap("id", new Long(1)));
71          assertTrue(!EntityUtils.identical(entity1, entity2));
72          assertTrue(!EntityUtils.identical(entity2, entity1));
73  
74          final GenericValue entity3 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(2)));
75          assertTrue(!EntityUtils.identical(entity1, entity3));
76          assertTrue(!EntityUtils.identical(entity3, entity1));
77  
78          final GenericValue issue4 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
79              "numvalue", new Long(4)));
80          assertTrue(!EntityUtils.identical(entity1, issue4));
81          assertTrue(!EntityUtils.identical(issue4, entity1));
82  
83          final GenericValue issue5 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
84              "numvalue", null));
85          assertTrue(EntityUtils.identical(entity1, issue5));
86          assertTrue(EntityUtils.identical(issue5, entity1));
87  
88          final Timestamp ts = UtilDateTime.nowTimestamp();
89  
90          final GenericValue tsIssue = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
91              "created", ts));
92          final GenericValue tsIssue2 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
93              "created", ts));
94          assertTrue(EntityUtils.identical(tsIssue, tsIssue2));
95          assertTrue(EntityUtils.identical(tsIssue2, tsIssue));
96  
97          final Timestamp ts2 = new Timestamp(ts.getTime() - 100000);
98          final GenericValue tsIssue3 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
99              "created", ts2));
100         assertTrue(!EntityUtils.identical(tsIssue, tsIssue3));
101         assertTrue(!EntityUtils.identical(tsIssue3, tsIssue));
102     }
103 
104     public void testFilterByAnd()
105     {
106         assertNull(EntityUtils.filterByAnd(null, null));
107 
108         final GenericValue entity1 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(1),
109             "numvalue", new Long(4)));
110         final GenericValue entity2 = com.atlassian.core.ofbiz.test.UtilsForTests.getTestEntity("TestEntity", UtilMisc.toMap("id", new Long(2),
111             "numvalue", new Long(5), "name", "bar"));
112 
113         final List issues = UtilMisc.toList(entity1, entity2);
114 
115         assertEquals(issues, EntityUtils.filterByAnd(issues, null));
116         assertEquals(issues, EntityUtils.filterByAnd(issues, Collections.EMPTY_LIST));
117 
118         assertEquals(UtilMisc.toList(entity2), EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("name", EntityOperator.EQUALS, "bar"))));
119         assertEquals(UtilMisc.toList(entity1), EntityUtils.filterByAnd(issues,
120             UtilMisc.toList(new EntityExpr("name", EntityOperator.NOT_EQUAL, "bar"))));
121         assertEquals(UtilMisc.toList(entity1), EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("numvalue", EntityOperator.LESS_THAN,
122             new Long(5)))));
123         assertEquals(UtilMisc.toList(entity1, entity2), EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("numvalue",
124             EntityOperator.LESS_THAN_EQUAL_TO, new Long(5)))));
125         assertEquals(UtilMisc.toList(entity2), EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("numvalue",
126             EntityOperator.GREATER_THAN_EQUAL_TO, new Long(5)))));
127         assertEquals(UtilMisc.toList(entity2), EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("numvalue",
128             EntityOperator.GREATER_THAN, new Long(4)))));
129 
130         boolean exceptionThrown = false;
131 
132         try
133         {
134             EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("project", EntityOperator.IN, new Long(5))));
135         }
136         catch (final IllegalArgumentException e)
137         {
138             exceptionThrown = true;
139         }
140 
141         assertTrue(exceptionThrown);
142 
143         exceptionThrown = false;
144 
145         try
146         {
147             EntityUtils.filterByAnd(issues, UtilMisc.toList(new EntityExpr("summary", EntityOperator.GREATER_THAN, new Long(5))));
148         }
149         catch (final IllegalArgumentException e)
150         {
151             exceptionThrown = true;
152         }
153     }
154 }