KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > junit > SessionTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.ws.jaxme.pm.junit;
18
19 import java.sql.Connection JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /** Unit test for the JDBC manager.
35  */

36 public class SessionTest extends TestCase {
37   private static int num;
38
39   /** Creates a new instance of the test with the given name.
40    */

41   public SessionTest(String JavaDoc pName) {
42     super(pName);
43   }
44
45   private JAXBContextImpl getFactory() throws JAXBException {
46     String JavaDoc 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 JavaDoc 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   /** Tests, whether the factory can be created.
70    */

71   public void testCreateFactory() throws Exception JavaDoc {
72     getFactory();
73   }
74
75   /** Tests, whether a manager created.
76    */

77   public void testCreateManager() throws Exception JavaDoc {
78     PM pm = getPM();
79     assertTrue(pm instanceof SessionTypePM);
80     SessionTypePM sessionTypePM = (SessionTypePM) pm;
81     Connection JavaDoc conn = sessionTypePM.getConnection();
82     assertNotNull(conn);
83     assertTrue(conn.getAutoCommit());
84   }
85
86   /** Tests, whether all entries can be removed from
87    * the database.
88    */

89   public void testClear() throws Exception JavaDoc {
90     PM pm = getPM();
91     for (Iterator JavaDoc iter = pm.select(null); iter.hasNext(); ) {
92       pm.delete((Session) iter.next());
93     }
94   }
95
96   /** Tests, whether a new row can be created,
97    * which is not stored into the database.
98    */

99   public void testCreate() throws Exception JavaDoc {
100     PM pm = getPM();
101     getSession(pm);
102   }
103
104   /** Tests, whether a new row can be created
105    * and stored into the database.
106    */

107   public void testInsert() throws Exception JavaDoc {
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   /** Test, that reads all rows from the database.
119    */

120   public void testSelectAll() throws Exception JavaDoc {
121     PM pm = getPM();
122     Iterator JavaDoc 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   /** Test, that reads a single row from the database.
133    */

134   public void testSelect() throws Exception JavaDoc {
135     PM pm = getPM();
136     Iterator JavaDoc 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   /** Test, that updates a row in the database.
146    */

147   public void testUpdate() throws Exception JavaDoc {
148     PM pm = getPM();
149     Iterator JavaDoc 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   /** Test, that deletes a row in the database.
171    */

172   public void testDelete() throws Exception JavaDoc {
173     testClear();
174     PM pm = getPM();
175     assertTrue(!pm.select(null).hasNext());
176   }
177 }
178
Popular Tags