1 20 21 package com.methodhead.sitecontext; 22 23 import java.util.*; 24 import java.sql.*; 25 import java.io.*; 26 import junit.framework.*; 27 import org.apache.log4j.*; 28 import com.methodhead.persistable.*; 29 import com.methodhead.aikp.*; 30 import com.methodhead.test.*; 31 import com.methodhead.*; 32 import org.apache.commons.beanutils.*; 33 34 public class SiteContextAikpTest extends TestCase { 35 36 SiteContextAikp siteContextAikp = null; 37 ResultSet rs = null; 38 List list = null; 39 40 static { 41 TestUtils.initLogger(); 42 TestUtils.initDb(); 43 } 44 45 public SiteContextAikpTest( String name ) { 46 super( name ); 47 } 48 49 protected void setUp() { 50 try { 51 ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) ); 52 } 53 catch ( Exception e ) { 54 fail( e.getMessage() ); 55 } 56 } 57 58 protected void tearDown() { 59 } 60 61 public void testProperties() { 62 try { 63 TestData.createDynaClassAndTable(); 64 65 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 66 67 try { 71 siteContextAikp.getSiteContext(); 72 fail( "No exception thrown" ); 73 } 74 catch ( RuntimeException e ) { 75 } 77 78 siteContextAikp.setSiteContext( TestData.siteContext1 ); 82 assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() ); 83 assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) ); 84 } 85 catch ( Exception e ) { 86 e.printStackTrace(); 87 fail(); 88 } 89 } 90 91 public void testSaveNew() { 92 try { 93 TestData.createDynaClassAndTable(); 94 95 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 99 siteContextAikp.setInt( "id", 0 ); 100 siteContextAikp.setInt( "sitecontext_id", 0 ); 101 siteContextAikp.setString( "field", "value" ); 102 103 try { 107 siteContextAikp.saveNew(); 108 fail( "No exception thrown" ); 109 } 110 catch ( RuntimeException e ) { 111 } 113 114 siteContextAikp.setSiteContext( TestData.siteContext1 ); 118 siteContextAikp.saveNew(); 119 120 rs = ConnectionSingleton.runQuery( "SELECT field FROM sitecontextaikp WHERE sitecontext_id=1" ); 124 assertNotNull( rs ); 125 assertTrue( rs.next() ); 126 assertEquals( "value", rs.getString( "field" ) ); 127 assertTrue( !rs.next() ); 128 129 ConnectionSingleton.close( rs ); 130 } 131 catch ( Exception e ) { 132 e.printStackTrace(); 133 fail(); 134 } 135 } 136 137 public void testSave() { 138 try { 139 TestData.createDynaClassAndTable(); 140 141 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 145 siteContextAikp.setSiteContext( TestData.siteContext1 ); 146 siteContextAikp.setInt( "id", 0 ); 147 siteContextAikp.setInt( "sitecontext_id", 0 ); 148 siteContextAikp.setString( "field", "value" ); 149 siteContextAikp.saveNew(); 150 151 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 155 siteContextAikp.setSiteContext( TestData.siteContext1 ); 156 siteContextAikp.load( new IntKey( "1" ) ); 157 158 siteContextAikp.setInt( "sitecontext_id", 0 ); 163 164 siteContextAikp.save(); 165 166 rs = ConnectionSingleton.runQuery( "SELECT field FROM sitecontextaikp WHERE sitecontext_id=1" ); 170 assertNotNull( rs ); 171 assertTrue( rs.next() ); 172 assertEquals( "value", rs.getString( "field" ) ); 173 assertTrue( !rs.next() ); 174 175 ConnectionSingleton.close( rs ); 176 } 177 catch ( Exception e ) { 178 e.printStackTrace(); 179 fail(); 180 } 181 } 182 183 public void testLoad() { 184 try { 185 TestData.createSiteContextAikps(); 186 187 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 188 189 try { 193 siteContextAikp.load( new IntKey( 1 ) ); 194 fail( "No exception thrown" ); 195 } 196 catch ( RuntimeException e ) { 197 } 199 200 siteContextAikp.setSiteContext( TestData.siteContext1 ); 204 siteContextAikp.load( new IntKey( 1 ) ); 205 206 assertEquals( "siteContextAikp1", siteContextAikp.getString( "field" ) ); 210 211 try { 215 siteContextAikp.setSiteContext( TestData.siteContext2 ); 216 siteContextAikp.load( new IntKey( 1 ) ); 217 fail( "No exception thrown" ); 218 } 219 catch ( RuntimeException e ) { 220 } 222 } 223 catch ( Exception e ) { 224 e.printStackTrace(); 225 fail(); 226 } 227 } 228 229 public void testLoadWhere() { 230 try { 231 TestData.createSiteContextAikps(); 232 233 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 234 235 try { 239 siteContextAikp.load( "field='siteContextAikp1'" ); 240 fail( "No exception thrown" ); 241 } 242 catch ( RuntimeException e ) { 243 } 245 246 siteContextAikp.setSiteContext( TestData.siteContext1 ); 250 siteContextAikp.load( "field='siteContextAikp1'" ); 251 252 assertEquals( 1, siteContextAikp.getInt( "id" ) ); 253 254 try { 258 siteContextAikp.setSiteContext( TestData.siteContext2 ); 259 siteContextAikp.load( "field='siteContextAikp1'" ); 260 fail( "No exception thrown" ); 261 } 262 catch ( RuntimeException e ) { 263 } 265 } 266 catch ( Exception e ) { 267 e.printStackTrace(); 268 fail(); 269 } 270 } 271 272 public void testLoadAll() { 273 try { 274 TestData.createSiteContextAikps(); 275 276 siteContextAikp = new SiteContextAikp( TestData.dynaClass ); 277 278 try { 282 siteContextAikp.loadAll( null, "field" ); 283 fail( "No exception thrown" ); 284 } 285 catch ( RuntimeException e ) { 286 } 288 289 siteContextAikp.setSiteContext( TestData.siteContext1 ); 293 list = siteContextAikp.loadAll( null, "field" ); 294 295 assertEquals( 2, list.size() ); 300 301 siteContextAikp = ( SiteContextAikp )list.get( 0 ); 302 assertNotNull( siteContextAikp ); 303 assertEquals( "siteContextAikp1", siteContextAikp.getString( "field" ) ); 304 assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() ); 305 assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) ); 306 307 siteContextAikp = ( SiteContextAikp )list.get( 1 ); 308 assertNotNull( siteContextAikp ); 309 assertEquals( "siteContextAikp2", siteContextAikp.getString( "field" ) ); 310 assertEquals( TestData.siteContext1, siteContextAikp.getSiteContext() ); 311 assertEquals( 1, siteContextAikp.getInt( "sitecontext_id" ) ); 312 } 313 catch ( Exception e ) { 314 e.printStackTrace(); 315 fail(); 316 } 317 } 318 } 319 | Popular Tags |