1 22 package org.jboss.test.cmp2.ejbselect; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import javax.ejb.FinderException ; 28 29 import junit.framework.Test; 30 import net.sourceforge.junitejb.EJBTestCase; 31 import org.jboss.test.JBossTestCase; 32 33 36 public class EJBSelectUnitTestCase extends EJBTestCase 37 { 38 static org.jboss.logging.Logger log = 39 org.jboss.logging.Logger.getLogger(EJBSelectUnitTestCase.class); 40 41 public static Test suite() throws Exception 42 { 43 return JBossTestCase.getDeploySetup(EJBSelectUnitTestCase.class, "cmp2-ejbselect.jar"); 44 } 45 46 public EJBSelectUnitTestCase(String name) 47 { 48 super(name); 49 } 50 51 private ALocal a1; 52 private ALocal a2; 53 private BLocal b1; 54 private BLocal b2; 55 private BLocal b3; 56 private BLocal b4; 57 58 public void setUpEJB() throws Exception 59 { 60 ALocalHome ahome = AUtil.getLocalHome(); 61 BLocalHome bhome = BUtil.getLocalHome(); 62 63 a1 = ahome.create("A1"); 64 a1.setIntField(3); 65 Collection bs = a1.getBs(); 66 b1 = bhome.create("B1", "Alice", true); 67 bs.add(b1); 68 b2 = bhome.create("B2", "Bob", true); 69 bs.add(b2); 70 b3 = bhome.create("B3", "Charlie", false); 71 bs.add(b3); 72 b4 = bhome.create("B4", "Dan", false); 73 bs.add(b4); 74 75 a2 = ahome.create("A2"); 76 a2.setIntField(9); 77 } 78 79 public void tearDownEJB() throws Exception 80 { 81 a1.remove(); 82 a2.remove(); 83 } 84 85 public void testReturnedInterface() throws Exception 86 { 87 Iterator i = a1.getSomeBs().iterator(); 88 while(i.hasNext()) 89 { 90 Object obj = i.next(); 91 assertTrue(obj instanceof BLocal); 92 BLocal b = (BLocal)obj; 93 b.getName(); 94 } 95 96 i = a1.getSomeBs().iterator(); 97 while(i.hasNext()) 98 { 99 Object obj = i.next(); 100 assertTrue(obj instanceof BLocal); 101 BLocal b = (BLocal)obj; 102 b.getName(); 103 } 104 } 105 106 public void testEJBSelectFromEJBHomeMethod() throws Exception 107 { 108 ALocalHome ahome = AUtil.getLocalHome(); 109 Collection results = ahome.getSomeBs(a1); 110 for(Iterator iterator = results.iterator(); iterator.hasNext();) 111 { 112 Object obj = iterator.next(); 113 assertTrue(obj instanceof BLocal); 114 BLocal b = (BLocal)obj; 115 b.getName(); 116 } 117 118 assertTrue(results.contains(b1)); 119 assertTrue(results.contains(b2)); 120 assertTrue(results.contains(b3)); 121 assertTrue(results.contains(b4)); 122 assertEquals(4, results.size()); 123 } 124 125 public void testCheckFinderForNullInEJBSELECT() throws Exception 126 { 127 ALocalHome ahome = AUtil.getLocalHome(); 128 try 129 { 130 ahome.checkFinderForNull(); 131 fail("Should not be here"); 132 } 133 catch (FinderException expected) 134 { 135 log.debug("Got expected error", expected); 136 } 137 } 138 139 public void testGetSomeBxDeclaredSQL() throws Exception 140 { 141 ALocalHome ahome = AUtil.getLocalHome(); 142 Collection results = ahome.getSomeBsDeclaredSQL(a1); 143 for(Iterator iterator = results.iterator(); iterator.hasNext();) 144 { 145 Object obj = iterator.next(); 146 assertTrue(obj instanceof BLocal); 147 BLocal b = (BLocal)obj; 148 b.getName(); 149 } 150 151 assertTrue(results.contains(b1)); 152 assertTrue(results.contains(b2)); 153 assertTrue(results.contains(b3)); 154 assertTrue(results.contains(b4)); 155 assertEquals(4, results.size()); 156 } 157 158 public void testGetTrue() throws Exception 159 { 160 Collection bs = b1.getTrue(); 161 assertEquals(2, bs.size()); 162 assertTrue(bs.contains(b1)); 163 assertTrue(bs.contains(b2)); 164 assertTrue(!bs.contains(b3)); 165 assertTrue(!bs.contains(b4)); 166 167 Iterator i = bs.iterator(); 168 while(i.hasNext()) 169 { 170 BLocal b = (BLocal)i.next(); 171 assertTrue(b.getBool()); 172 } 173 } 174 175 public void testGetFalse() throws Exception 176 { 177 Collection bs = b1.getFalse(); 178 assertEquals(2, bs.size()); 179 assertTrue(!bs.contains(b1)); 180 assertTrue(!bs.contains(b2)); 181 assertTrue(bs.contains(b3)); 182 assertTrue(bs.contains(b4)); 183 184 Iterator i = bs.iterator(); 185 while(i.hasNext()) 186 { 187 BLocal b = (BLocal)i.next(); 188 assertTrue(!b.getBool()); 189 } 190 } 191 192 public void testGetAWithBs() throws Exception 193 { 194 Collection as = a1.getAWithBs(); 195 assertEquals(1, as.size()); 196 assertTrue(as.contains(a1)); 197 assertTrue(!as.contains(a2)); 198 199 Iterator i = as.iterator(); 200 while(i.hasNext()) 201 { 202 ALocal a = (ALocal)i.next(); 203 assertTrue(!a.getBs().isEmpty()); 204 } 205 } 206 207 209 public void testCountInSelectClause() throws Exception 210 { 211 Collection result = BUtil.getLocalHome().selectDynamic( 212 "SELECT COUNT(b.id) FROM B AS b", new Object []{} 213 ); 214 assertTrue("COUNT(b.id) = 4", ((Long )result.iterator().next()).longValue() == 4); 215 } 216 217 public void testMaxInSelectClause() throws Exception 218 { 219 Collection result = BUtil.getLocalHome().selectDynamic( 220 "SELECT MAX(a.intField) FROM A AS a", new Object []{} 221 ); 222 assertTrue("MAX(a.id) = 9", ((Double )result.iterator().next()).doubleValue() == 9.0); 223 } 224 225 public void testMinInSelectClause() throws Exception 226 { 227 Collection result = BUtil.getLocalHome().selectDynamic( 228 "SELECT MIN(a.intField) FROM A AS a", new Object []{} 229 ); 230 assertTrue("MIN(a.id) = 3", ((Double )result.iterator().next()).doubleValue() == 3.0); 231 } 232 233 public void testSumInSelectClause() throws Exception 234 { 235 Collection result = BUtil.getLocalHome().selectDynamic( 236 "SELECT SUM(a.intField) FROM A AS a", new Object []{} 237 ); 238 assertTrue("SUM(a.id) = 12", ((Double )result.iterator().next()).doubleValue() == 12.0); 239 } 240 241 public void testAvgInSelectClause() throws Exception 242 { 243 Collection result = BUtil.getLocalHome().selectDynamic( 244 "SELECT AVG(a.intField) FROM A AS a", new Object []{} 245 ); 246 assertTrue("AVG(a.id) = 6", ((Double )result.iterator().next()).doubleValue() == 6.0); 247 } 248 249 public void testSqrtInSelectClause() throws Exception 250 { 251 String pk = "B1"; 252 BLocal b = BUtil.getLocalHome().findByPrimaryKey(pk); 253 b.setLongField(64); 254 255 Collection result = BUtil.getLocalHome().selectDynamic( 256 "SELECT SQRT(b.longField) FROM B AS b WHERE b.id = ?1", new Object []{pk} 257 ); 258 assertTrue("SQRT(b.longField) = 8", ((Double )result.iterator().next()).doubleValue() == 8.0); 259 } 260 261 274 275 public void testLcaseInSelectClause() throws Exception 276 { 277 Collection result = BUtil.getLocalHome().selectDynamic( 278 "SELECT LCASE(b.name) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 279 ); 280 assertTrue("LCASE(b.name) = alice", "alice".equals(result.iterator().next())); 281 } 282 283 public void testUcaseInSelectClause() throws Exception 284 { 285 Collection result = BUtil.getLocalHome().selectDynamic( 286 "SELECT UCASE(b.name) FROM B AS b", new Object []{} 287 ); 288 assertTrue("result.size() == 4", result.size() == 4); 289 assertTrue("result.contains('ALICE')", result.contains("ALICE")); 290 assertTrue("result.contains('BOB')", result.contains("BOB")); 291 assertTrue("result.contains('CHARLIE')", result.contains("CHARLIE")); 292 assertTrue("result.contains('DAN')", result.contains("DAN")); 293 } 294 295 public void testLengthInSelectClause() throws Exception 296 { 297 Collection result = BUtil.getLocalHome().selectDynamic( 298 "SELECT LENGTH(b.name) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 299 ); 300 assertTrue("LENGTH(b.name) = 5", ((Long )result.iterator().next()).longValue() == 5); 301 } 302 303 public void testConcatInSelectClause() throws Exception 304 { 305 Collection result = BUtil.getLocalHome().selectDynamic( 306 "SELECT CONCAT('Dear ', b.name) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 307 ); 308 assertTrue("CONCAT('Dear ', b.name) = Dear Alice", "Dear Alice".equals(result.iterator().next())); 309 } 310 311 public void testLocateInSelectClause() throws Exception 312 { 313 Collection result = BUtil.getLocalHome().selectDynamic( 314 "SELECT LOCATE('ice', b.name, 1) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 315 ); 316 assertTrue("LOCATE('ice', b.name, 1) = 3", ((Long )result.iterator().next()).longValue() == 3); 317 } 318 319 public void testSubstringInSelectClause() throws Exception 320 { 321 Collection result = BUtil.getLocalHome().selectDynamic( 322 "SELECT SUBSTRING(b.name, 3, 5) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 323 ); 324 assertTrue("SUBSTRING(b.name, 3, 5) = ice", "ice".equals(result.iterator().next())); 325 } 326 327 public void testNestedFunctionsInSelectClause() throws Exception 328 { 329 Collection result = BUtil.getLocalHome().selectDynamic( 330 "SELECT UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) FROM B AS b WHERE b.id = ?1", new Object []{"B1"} 331 ); 332 assertTrue("UCASE(SUBSTRING(CONCAT(b.id, b.name), 5, 7)) = ICE", "ICE".equals(result.iterator().next())); 333 } 334 } 335 | Popular Tags |