1 package org.apache.ojb.odmg; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import org.apache.ojb.junit.ODMGTestCase; 7 import org.apache.ojb.odmg.shared.Article; 8 import org.apache.ojb.odmg.shared.Person; 9 import org.apache.ojb.odmg.shared.PersonImpl; 10 import org.odmg.OQLQuery; 11 import org.odmg.Transaction; 12 13 public class ProjectionAttributeTest extends ODMGTestCase 14 { 15 private int COUNT = 10; 16 17 public static void main(String [] args) 18 { 19 String [] arr = {ProjectionAttributeTest.class.getName()}; 20 junit.textui.TestRunner.main(arr); 21 } 22 23 public ProjectionAttributeTest(String name) 24 { 25 super(name); 26 } 27 28 private void createData(String id) throws Exception 29 { 30 Transaction tx = odmg.newTransaction(); 31 tx.begin(); 32 for (int i = 0; i < COUNT; i++) 33 { 34 Person aPerson = new PersonImpl(); 35 aPerson.setFirstname("firstname" + id +"_" + i); 36 aPerson.setLastname("lastname" + id +"_" + i); 37 database.makePersistent(aPerson); 38 } 39 tx.commit(); 40 } 41 42 45 public void testGetProjectionAttribute() throws Exception 46 { 47 String id = "_" + System.currentTimeMillis(); 48 createData(id); 49 50 Transaction tx = odmg.newTransaction(); 52 tx.begin(); 53 54 OQLQuery query = odmg.newOQLQuery(); 55 String sql = "select aPerson.firstname, aPerson.lastname from " + Person.class.getName() + " where firstname like $1"; 56 query.create(sql); 57 query.bind("%" + id + "%"); 58 59 Collection result = (Collection ) query.execute(); 60 61 Iterator it = result.iterator(); 63 int i = 0; 64 while (it.hasNext()) 65 { 66 Object [] res = (Object []) it.next(); 67 String firstname = (String ) res[0]; 68 String lastname = (String ) res[1]; 69 assertTrue(firstname.startsWith("firstname")); 70 assertTrue(lastname.startsWith("lastname")); 71 i++; 72 } 73 if (i < COUNT) 74 fail("Should have found at least " + COUNT + " items"); 75 76 OQLQuery query1 = odmg.newOQLQuery(); 77 query1.create("select distinct anArticle.productGroup.groupId from " + Article.class.getName()); 78 Collection result1 = (Collection ) query1.execute(); 79 for (it = result1.iterator(); it.hasNext(); ) 80 { 81 it.next(); 82 } 83 tx.commit(); 84 } 85 86 } 87 | Popular Tags |