1 16 17 package org.springframework.orm.ojb; 18 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 23 import junit.framework.TestCase; 24 import org.apache.ojb.broker.PersistenceBroker; 25 import org.apache.ojb.broker.PBKey; 26 import org.apache.ojb.broker.PersistenceBrokerFactory; 27 import org.apache.ojb.broker.Identity; 28 import org.apache.ojb.broker.IdentityFactory; 29 import org.apache.ojb.broker.query.Query; 30 import org.apache.ojb.broker.query.QueryByCriteria; 31 import org.easymock.MockControl; 32 33 import org.springframework.beans.TestBean; 34 35 39 public class PersistenceBrokerTemplateTests extends TestCase { 40 41 public void testPbKey() { 42 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate(); 43 assertEquals(PersistenceBrokerFactory.getDefaultKey(), template.getPbKey()); 44 45 template = new PersistenceBrokerTemplate(); 46 template.setJcdAlias("alias1"); 47 assertEquals("alias1", template.getJcdAlias()); 48 assertEquals("alias1", template.getPbKey().getAlias()); 49 50 template = new PersistenceBrokerTemplate(); 51 template.setPbKey(new PBKey("alias2", "user2", "password2")); 52 assertEquals("alias2", template.getJcdAlias()); 53 assertEquals("alias2", template.getPbKey().getAlias()); 54 assertEquals("user2", template.getPbKey().getUser()); 55 assertEquals("password2", template.getPbKey().getPassword()); 56 } 57 58 public void testGetObjectById() { 59 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 60 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 61 MockControl idfControl = MockControl.createControl(IdentityFactory.class); 62 final IdentityFactory idf = (IdentityFactory) idfControl.getMock(); 63 64 Identity identity = new Identity(String .class, Object .class, new Object [] {"id"}); 65 pb.serviceIdentity(); 66 pbControl.setReturnValue(idf, 1); 67 idf.buildIdentity(String .class, "id"); 68 idfControl.setReturnValue(identity, 1); 69 pb.getObjectByIdentity(identity); 70 pbControl.setReturnValue("", 1); 71 pb.close(); 72 pbControl.setReturnValue(true, 1); 73 pbControl.replay(); 74 idfControl.replay(); 75 76 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 77 protected PersistenceBroker getPersistenceBroker() { 78 return pb; 79 } 80 }; 81 assertEquals("", template.getObjectById(String .class, "id")); 82 pbControl.verify(); 83 idfControl.verify(); 84 } 85 86 public void testGetObjectByQuery() { 87 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 88 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 89 Query query = new QueryByCriteria(TestBean.class); 90 pb.getObjectByQuery(query); 91 pbControl.setReturnValue("", 1); 92 pb.close(); 93 pbControl.setReturnValue(true, 1); 94 pbControl.replay(); 95 96 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 97 protected PersistenceBroker getPersistenceBroker() { 98 return pb; 99 } 100 }; 101 assertEquals("", template.getObjectByQuery(query)); 102 pbControl.verify(); 103 } 104 105 public void testGetCollectionByQuery() { 106 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 107 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 108 Query query = new QueryByCriteria(TestBean.class); 109 Collection result = new HashSet (); 110 pb.getCollectionByQuery(query); 111 pbControl.setReturnValue(result, 1); 112 pb.close(); 113 pbControl.setReturnValue(true, 1); 114 pbControl.replay(); 115 116 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 117 protected PersistenceBroker getPersistenceBroker() { 118 return pb; 119 } 120 }; 121 assertEquals(result, template.getCollectionByQuery(query)); 122 pbControl.verify(); 123 } 124 125 public void testGetIteratorByQuery() { 126 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 127 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 128 Query query = new QueryByCriteria(TestBean.class); 129 Iterator result = new HashSet ().iterator(); 130 pb.getIteratorByQuery(query); 131 pbControl.setReturnValue(result, 1); 132 pb.close(); 133 pbControl.setReturnValue(true, 1); 134 pbControl.replay(); 135 136 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 137 protected PersistenceBroker getPersistenceBroker() { 138 return pb; 139 } 140 }; 141 assertEquals(result, template.getIteratorByQuery(query)); 142 pbControl.verify(); 143 } 144 145 public void testGetReportQueryIteratorByQuery() { 146 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 147 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 148 Query query = new QueryByCriteria(TestBean.class); 149 Iterator result = new HashSet ().iterator(); 150 pb.getReportQueryIteratorByQuery(query); 151 pbControl.setReturnValue(result, 1); 152 pb.close(); 153 pbControl.setReturnValue(true, 1); 154 pbControl.replay(); 155 156 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 157 protected PersistenceBroker getPersistenceBroker() { 158 return pb; 159 } 160 }; 161 assertEquals(result, template.getReportQueryIteratorByQuery((query))); 162 pbControl.verify(); 163 } 164 165 public void testGetCount() { 166 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 167 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 168 Query query = new QueryByCriteria(TestBean.class); 169 pb.getCount(query); 170 pbControl.setReturnValue(0, 1); 171 pb.close(); 172 pbControl.setReturnValue(true, 1); 173 pbControl.replay(); 174 175 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 176 protected PersistenceBroker getPersistenceBroker() { 177 return pb; 178 } 179 }; 180 assertEquals(0, template.getCount(query)); 181 pbControl.verify(); 182 } 183 184 public void testRemoveFromCache() { 185 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 186 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 187 Object entity = new Object (); 188 pb.removeFromCache(entity); 189 pbControl.setVoidCallable(1); 190 pb.close(); 191 pbControl.setReturnValue(true, 1); 192 pbControl.replay(); 193 194 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 195 protected PersistenceBroker getPersistenceBroker() { 196 return pb; 197 } 198 }; 199 template.removeFromCache(entity); 200 pbControl.verify(); 201 } 202 203 public void testClearCache() { 204 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 205 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 206 pb.clearCache(); 207 pbControl.setVoidCallable(1); 208 pb.close(); 209 pbControl.setReturnValue(true, 1); 210 pbControl.replay(); 211 212 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 213 protected PersistenceBroker getPersistenceBroker() { 214 return pb; 215 } 216 }; 217 template.clearCache(); 218 pbControl.verify(); 219 } 220 221 public void testStore() { 222 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 223 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 224 Object entity = new Object (); 225 pb.store(entity); 226 pbControl.setVoidCallable(1); 227 pb.close(); 228 pbControl.setReturnValue(true, 1); 229 pbControl.replay(); 230 231 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 232 protected PersistenceBroker getPersistenceBroker() { 233 return pb; 234 } 235 }; 236 template.store(entity); 237 pbControl.verify(); 238 } 239 240 public void testDelete() { 241 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 242 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 243 Object entity = new Object (); 244 pb.delete(entity); 245 pbControl.setVoidCallable(1); 246 pb.close(); 247 pbControl.setReturnValue(true, 1); 248 pbControl.replay(); 249 250 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 251 protected PersistenceBroker getPersistenceBroker() { 252 return pb; 253 } 254 }; 255 template.delete(entity); 256 pbControl.verify(); 257 } 258 259 public void testDeleteByQuery() { 260 MockControl pbControl = MockControl.createControl(PersistenceBroker.class); 261 final PersistenceBroker pb = (PersistenceBroker) pbControl.getMock(); 262 Query query = new QueryByCriteria(TestBean.class); 263 pb.deleteByQuery(query); 264 pbControl.setVoidCallable(1); 265 pb.close(); 266 pbControl.setReturnValue(true, 1); 267 pbControl.replay(); 268 269 PersistenceBrokerTemplate template = new PersistenceBrokerTemplate() { 270 protected PersistenceBroker getPersistenceBroker() { 271 return pb; 272 } 273 }; 274 template.deleteByQuery(query); 275 pbControl.verify(); 276 } 277 278 } 279 | Popular Tags |