1 16 17 package org.apache.ws.jaxme.pm.junit; 18 19 import java.sql.Connection ; 20 import java.util.Calendar ; 21 import java.util.Iterator ; 22 23 import javax.xml.bind.JAXBContext; 24 import javax.xml.bind.JAXBException; 25 26 import junit.framework.TestCase; 27 import org.apache.ws.jaxme.PM; 28 import org.apache.ws.jaxme.PMException; 29 import org.apache.ws.jaxme.impl.JAXBContextImpl; 30 import org.apache.ws.jaxme.test.pm.session.Session; 31 import org.apache.ws.jaxme.test.pm.session.impl.SessionTypePM; 32 33 34 36 public class SessionTest extends TestCase { 37 private static int num; 38 39 41 public SessionTest(String pName) { 42 super(pName); 43 } 44 45 private JAXBContextImpl getFactory() throws JAXBException { 46 String s = Session.class.getName(); 47 int offset = s.lastIndexOf('.'); 48 s = s.substring(0, offset); 49 return (JAXBContextImpl) JAXBContext.newInstance(s); 50 } 51 52 private PM getPM() throws JAXBException, PMException { 53 return getFactory().getJMPM(Session.class); 54 } 55 56 private Session getSession(PM pm) throws JAXBException { 57 Session session = (Session) pm.create(); 58 session.setCookie("abcdefg" + num++); 59 session.setEXPIRETIME((short) 900); 60 session.setIpAddress("192.168.5.1"); 61 Calendar now = Calendar.getInstance(); 62 session.setLastAction(now); 63 session.setLOGINTIME(now); 64 session.setPRECEDENCE(3.27); 65 session.setRANDOMSEED(0.01); 66 return session; 67 } 68 69 71 public void testCreateFactory() throws Exception { 72 getFactory(); 73 } 74 75 77 public void testCreateManager() throws Exception { 78 PM pm = getPM(); 79 assertTrue(pm instanceof SessionTypePM); 80 SessionTypePM sessionTypePM = (SessionTypePM) pm; 81 Connection conn = sessionTypePM.getConnection(); 82 assertNotNull(conn); 83 assertTrue(conn.getAutoCommit()); 84 } 85 86 89 public void testClear() throws Exception { 90 PM pm = getPM(); 91 for (Iterator iter = pm.select(null); iter.hasNext(); ) { 92 pm.delete((Session) iter.next()); 93 } 94 } 95 96 99 public void testCreate() throws Exception { 100 PM pm = getPM(); 101 getSession(pm); 102 } 103 104 107 public void testInsert() throws Exception { 108 PM pm = getPM(); 109 Session session = getSession(pm); 110 session.setID(1); 111 pm.insert(session); 112 session = getSession(pm); 113 session.setID(2); 114 session.setIpAddress("192.168.5.127"); 115 pm.insert(session); 116 } 117 118 120 public void testSelectAll() throws Exception { 121 PM pm = getPM(); 122 Iterator iter = pm.select("1=1 ORDER BY ID"); 123 assertTrue(iter.hasNext()); 124 Session session = (Session) iter.next(); 125 assertEquals(1, session.getID()); 126 assertTrue(iter.hasNext()); 127 session = (Session) iter.next(); 128 assertEquals(2, session.getID()); 129 assertTrue(!iter.hasNext()); 130 } 131 132 134 public void testSelect() throws Exception { 135 PM pm = getPM(); 136 Iterator iter = pm.select("IPADDRESS='192.168.5.127'"); 137 assertTrue(iter.hasNext()); 138 Session session = (Session) iter.next(); 139 assertEquals("192.168.5.127", session.getIpAddress()); 140 assertEquals(2, session.getID()); 141 assertTrue(!iter.hasNext()); 142 assertTrue(!pm.select("IPADDRESS='192.168.5.128'").hasNext()); 143 } 144 145 147 public void testUpdate() throws Exception { 148 PM pm = getPM(); 149 Iterator iter = pm.select("IPADDRESS='192.168.5.127'"); 150 Session session = (Session) iter.next(); 151 session.setIpAddress("192.168.5.128"); 152 pm.update(session); 153 154 assertTrue(!pm.select("IPADDRESS='192.168.5.127'").hasNext()); 155 iter = pm.select("IPADDRESS='192.168.5.128'"); 156 assertTrue(iter.hasNext()); 157 session = (Session) iter.next(); 158 assertEquals("192.168.5.128", session.getIpAddress()); 159 assertEquals(2, session.getID()); 160 assertTrue(!iter.hasNext()); 161 162 iter = pm.select("IPADDRESS='192.168.5.128'"); 163 session = (Session) iter.next(); 164 session.setIpAddress("192.168.5.127"); 165 pm.update(session); 166 167 testSelect(); 168 } 169 170 172 public void testDelete() throws Exception { 173 testClear(); 174 PM pm = getPM(); 175 assertTrue(!pm.select(null).hasNext()); 176 } 177 } 178 | Popular Tags |