1 20 21 package com.methodhead.property; 22 23 import java.util.*; 24 import java.io.*; 25 import java.sql.*; 26 import junit.framework.*; 27 import org.apache.log4j.*; 28 import com.methodhead.persistable.*; 29 import com.methodhead.test.*; 30 import com.methodhead.sitecontext.*; 31 32 public class PropertyTest extends DbTestCase { 33 34 SiteContext siteContext1_ = null; 35 36 ResultSet rs = null; 37 Property p = null; 38 39 private void createData() { 40 siteContext1_ = new SiteContext(); 41 siteContext1_.saveNew(); 42 } 43 44 public PropertyTest( String name ) { 45 super( name ); 46 } 47 48 protected void setUp() { 49 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 testSetProperty() { 62 try { 63 createData(); 64 65 Property.setProperty( siteContext1_, "property", "value", "description", new Boolean ( false ) ); 66 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 67 68 assertTrue( rs.next() ); 69 assertEquals( "value", rs.getString( "value" ) ); 70 assertEquals( "description", rs.getString( "description" ) ); 71 assertEquals( 0, rs.getInt( "system_property" ) ); 72 assertTrue( !rs.next() ); 73 74 ConnectionSingleton.close( rs ); 75 Property.setProperty( siteContext1_, "property", "value2", null, null ); 76 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 77 78 assertTrue( rs.next() ); 79 assertEquals( "value2", rs.getString( "value" ) ); 80 assertEquals( "description", rs.getString( "description" ) ); 81 assertEquals( 0, rs.getInt( "system_property" ) ); 82 assertTrue( !rs.next() ); 83 84 ConnectionSingleton.close( rs ); 85 Property.setProperty( siteContext1_, "property", null, "description2", null ); 86 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 87 88 assertTrue( rs.next() ); 89 assertEquals( "value2", rs.getString( "value" ) ); 90 assertEquals( "description2", rs.getString( "description" ) ); 91 assertEquals( 0, rs.getInt( "system_property" ) ); 92 assertTrue( !rs.next() ); 93 94 ConnectionSingleton.close( rs ); 95 Property.setProperty( siteContext1_, "property", null, null, new Boolean ( true ) ); 96 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 97 98 assertTrue( rs.next() ); 99 assertEquals( "value2", rs.getString( "value" ) ); 100 assertEquals( "description2", rs.getString( "description" ) ); 101 assertEquals( 1, rs.getInt( "system_property" ) ); 102 assertTrue( !rs.next() ); 103 104 ConnectionSingleton.close( rs ); 105 Property.setProperty( siteContext1_, "property2", null, null, null ); 106 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property2' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 107 108 assertTrue( rs.next() ); 109 assertEquals( "", rs.getString( "value" ) ); 110 assertEquals( "", rs.getString( "description" ) ); 111 assertEquals( 0, rs.getInt( "system_property" ) ); 112 assertTrue( !rs.next() ); 113 114 ConnectionSingleton.close( rs ); 115 Property.setProperty( siteContext1_, "property3", "value3" ); 116 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property3' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 117 118 assertTrue( rs.next() ); 119 assertEquals( "value3", rs.getString( "value" ) ); 120 assertEquals( "", rs.getString( "description" ) ); 121 assertEquals( 0, rs.getInt( "system_property" ) ); 122 assertTrue( !rs.next() ); 123 124 ConnectionSingleton.close( rs ); 125 Property.setProperty( siteContext1_, "property3", "value4" ); 126 rs = ConnectionSingleton.runQuery( "SELECT value, description, system_property FROM mh_property WHERE name='property3' AND sitecontext_id=" + siteContext1_.getInt( "id" ) ); 127 128 assertTrue( rs.next() ); 129 assertEquals( "value4", rs.getString( "value" ) ); 130 assertEquals( "", rs.getString( "description" ) ); 131 assertEquals( 0, rs.getInt( "system_property" ) ); 132 assertTrue( !rs.next() ); 133 134 ConnectionSingleton.close( rs ); 135 } 136 catch ( Exception e ) { 137 e.printStackTrace(); 138 fail(); 139 } 140 } 141 142 public void testGetProperty() { 143 try { 144 createData(); 145 146 assertNull( Property.getProperty( siteContext1_, "property" ) ); 147 148 Property.setProperty( siteContext1_, "property", "value" ); 149 150 assertNotNull( Property.getProperty( siteContext1_, "property" ) ); 151 assertEquals( "value", Property.getProperty( siteContext1_, "property" ) ); 152 assertEquals( "value", Property.getProperty( siteContext1_, "property", "default" ) ); 153 assertEquals( "default", Property.getProperty( siteContext1_, "property2", "default" ) ); 154 } 155 catch ( Exception e ) { 156 e.printStackTrace(); 157 fail(); 158 } 159 } 160 161 public void testGetPropertyArray() { 162 try { 163 createData(); 164 165 String [] vals = null; 166 167 assertNull( Property.getProperty( siteContext1_, "property" ) ); 168 169 Property.setProperty( siteContext1_, "property", "value" ); 170 171 vals = Property.getPropertyArray( siteContext1_, "property" ); 172 173 assertNotNull( vals ); 174 assertEquals( 1, vals.length ); 175 assertEquals( "value", vals[ 0 ] ); 176 177 Property.setProperty( siteContext1_, "property", " value1, value2 " ); 178 179 vals = Property.getPropertyArray( siteContext1_, "property" ); 180 181 assertNotNull( vals ); 182 assertEquals( 2, vals.length ); 183 assertEquals( "value1", vals[ 0 ] ); 184 assertEquals( "value2", vals[ 1 ] ); 185 } 186 catch ( Exception e ) { 187 e.printStackTrace(); 188 fail(); 189 } 190 } 191 192 public void testLoadForName() { 193 try { 194 createData(); 195 196 Property.setProperty( siteContext1_, "property", "value" ); 197 198 p = new Property(); 199 try { 200 p.loadForName( "property" ); 201 fail( "No exception thrown." ); 202 } 203 catch ( PersistableException e ) { 204 } 205 206 p = new Property(); 207 p.setSiteContext( siteContext1_ ); 208 p.loadForName( "property" ); 209 210 assertEquals( "property", p.getString( "name" ) ); 211 assertEquals( "value", p.getString( "value" ) ); 212 assertEquals( "", p.getString( "description" ) ); 213 assertEquals( false, p.getBoolean( "system_property" ) ); 214 } 215 catch ( Exception e ) { 216 e.printStackTrace(); 217 fail(); 218 } 219 } 220 221 public void testLoadAll() { 222 try { 223 createData(); 224 225 Property property = null; 226 List properties = null; 227 228 Property.setProperty( siteContext1_, "property2", "value" ); 229 Property.setProperty( siteContext1_, "property1", "value" ); 230 231 properties = Property.loadAll( null ); 232 assertNotNull( properties ); 233 assertEquals( 0, properties.size() ); 234 235 properties = Property.loadAll( siteContext1_ ); 236 assertNotNull( properties ); 237 assertEquals( 2, properties.size() ); 238 239 property = ( Property )properties.get( 0 ); 240 assertEquals( "property1", property.getString( "name" ) ); 241 242 property = ( Property )properties.get( 1 ); 243 assertEquals( "property2", property.getString( "name" ) ); 244 } 245 catch ( Exception e ) { 246 e.printStackTrace(); 247 fail(); 248 } 249 } 250 } 251 | Popular Tags |