1 package org.mmbase.storage.search.implementation; 2 3 import junit.framework.*; 4 5 import org.mmbase.bridge.Field; 6 import org.mmbase.module.core.*; 7 import org.mmbase.module.corebuilders.FieldDefs; 8 import org.mmbase.storage.search.*; 9 10 16 public class BasicStepFieldTest extends TestCase { 17 18 private final static String TEST_ALIAS = "abcd"; 19 20 private final static String BUILDER_NAME1 = "images"; 21 private final static String BUILDER_NAME2 = "news"; 22 private final static String FIELD_NAME1 = "title"; 23 24 25 private BasicStepField instance; 26 27 28 private BasicStep step; 29 30 31 private MMBase mmbase = null; 32 33 34 private MMObjectBuilder builder1 = null; 35 private MMObjectBuilder builder2 = null; 36 37 38 private FieldDefs fieldDefs = null; 39 40 public BasicStepFieldTest(java.lang.String testName) { 41 super(testName); 42 } 43 44 public static void main(java.lang.String [] args) { 45 junit.textui.TestRunner.run(suite()); 46 System.exit(0); 47 } 48 49 52 public void setUp() throws Exception { 53 MMBaseContext.init(); 54 mmbase = MMBase.getMMBase(); 55 builder1 = mmbase.getBuilder(BUILDER_NAME1); 56 builder2 = mmbase.getBuilder(BUILDER_NAME2); 57 fieldDefs = builder1.getField(FIELD_NAME1); 58 step = new BasicStep(builder1); 59 instance = new BasicStepField(step, fieldDefs); 60 } 61 62 65 public void tearDown() throws Exception {} 66 67 68 public void testConstructor() { 69 Step step2 = new BasicStep(builder2); 70 try { 72 new BasicStepField(step2, fieldDefs); 73 fail("FieldDefs object does not belong to step, should throw IllegalArgumentException."); 74 } catch (IllegalArgumentException e) {} 75 76 try { 78 new BasicStepField(null, fieldDefs); 79 fail("Null step, should throw IllegalArgumentException."); 80 } catch (IllegalArgumentException e) {} 81 82 try { 84 new BasicStepField(step2, null); 85 fail("Null field, should throw IllegalArgumentException."); 86 } catch (IllegalArgumentException e) {} 87 } 88 89 90 public void testSetAlias() { 91 assertTrue(instance.getAlias() == null); 93 94 BasicStepField result = instance.setAlias(TEST_ALIAS); 95 String alias = instance.getAlias(); 96 assertTrue(alias != null); 97 assertTrue(alias.equals(TEST_ALIAS)); 98 assertTrue(result == instance); 99 100 instance.setAlias(null); 102 assertTrue(instance.getAlias() == null); 103 104 try { 106 instance.setAlias(" "); 107 fail("Null value, should throw IllegalArgumentException."); 108 } catch (IllegalArgumentException e) {} 109 } 110 111 112 public void testGetFieldName() { 113 String fieldName = instance.getFieldName(); 114 assertTrue(fieldName != null); 115 assertTrue(fieldName.equals(FIELD_NAME1)); 116 } 117 118 119 public void testGetAlias() { 120 testSetAlias(); 122 } 123 124 125 public void testGetStep() { 126 assertTrue(instance.getStep() == step); 127 } 128 129 131 public void testGetType() { 132 133 MMObjectBuilder images = mmbase.getBuilder("images"); 134 FieldDefs imagesNumber = images.getField("number"); 135 FieldDefs imagesOwner = images.getField("owner"); 136 FieldDefs imagesTitle = images.getField("title"); 137 FieldDefs imagesDescription = images.getField("description"); 138 FieldDefs imagesHandle = images.getField("handle"); 139 FieldDefs imagesItype = images.getField("itype"); 140 141 Step step = new BasicStep(images); 142 instance = new BasicStepField(step, imagesNumber); 143 assertTrue(instance.getType() == Field.TYPE_NODE); 144 145 instance = new BasicStepField(step, imagesOwner); 146 assertTrue(instance.getType() == Field.TYPE_STRING); 147 148 instance = new BasicStepField(step, imagesTitle); 149 assertTrue(instance.getType() == Field.TYPE_STRING); 150 151 instance = new BasicStepField(step, imagesDescription); 152 assertTrue(instance.getType() == Field.TYPE_STRING); 153 154 instance = new BasicStepField(step, imagesHandle); 155 assertTrue(instance.getType() == Field.TYPE_BINARY); 156 157 instance = new BasicStepField(step, imagesItype); 158 assertTrue(instance.getType() == Field.TYPE_STRING); 159 } 160 161 162 public void testTestValue() { 163 MMObjectBuilder images = mmbase.getBuilder("images"); 164 FieldDefs imagesNumber = images.getField("number"); 165 FieldDefs imagesHandle = images.getField("handle"); 166 FieldDefs imagesOwner = images.getField("owner"); 167 168 Step step = new BasicStep(images); 169 instance = new BasicStepField(step, imagesNumber); 171 BasicStepField.testValue(new Integer (123), instance); 172 BasicStepField.testValue(new Long (123), instance); 173 BasicStepField.testValue(new Float (123), instance); 174 BasicStepField.testValue(new Double (123), instance); 175 try { 176 BasicStepField.testValue("123", instance); 178 fail("String value for NODE field, should throw IllegalArgumentException."); 179 } catch (IllegalArgumentException e) {} 180 181 try { 182 BasicStepField.testValue(new byte[] {(byte) 123}, instance); 183 fail("byte[] value for NODE field, should throw IllegalArgumentException."); 184 } catch (IllegalArgumentException e) {} 185 186 instance = new BasicStepField(step, imagesHandle); 188 BasicStepField.testValue(new byte[] {(byte) 123}, instance); 189 try { 190 BasicStepField.testValue(new Integer (123), instance); 192 fail("Integer value for BYTE field, should throw IllegalArgumentException."); 193 } catch (IllegalArgumentException e) {} 194 try { 195 BasicStepField.testValue(new Long (123), instance); 196 fail("Long value for BYTE field, should throw IllegalArgumentException."); 197 } catch (IllegalArgumentException e) {} 198 199 try { 200 BasicStepField.testValue(new Double (123), instance); 201 fail("Double value for BYTE field, should throw IllegalArgumentException."); 202 } catch (IllegalArgumentException e) {} 203 try { 204 BasicStepField.testValue("123", instance); 205 fail("String value for BYTE field, should throw IllegalArgumentException."); 206 } catch (IllegalArgumentException e) {} 207 208 209 instance = new BasicStepField(step, imagesOwner); 211 try { 212 BasicStepField.testValue(new byte[] {(byte) 123}, instance); 214 fail("Byte value for STRING field, should throw IllegalArgumentException."); 215 } catch (IllegalArgumentException e) {} 216 try { 217 BasicStepField.testValue(new Integer (123), instance); 219 fail("Integer value for STRING field, should throw IllegalArgumentException."); 220 } catch (IllegalArgumentException e) {} 221 try { 222 BasicStepField.testValue(new Long (123), instance); 224 fail("Long value for STRING field, should throw IllegalArgumentException."); 225 } catch (IllegalArgumentException e) {} 226 try { 227 BasicStepField.testValue(new Float (123), instance); 229 fail("Float value for STRING field, should throw IllegalArgumentException."); 230 } catch (IllegalArgumentException e) {} 231 try { 232 BasicStepField.testValue(new Double (123), instance); 234 fail("Double value for STRING field, should throw IllegalArgumentException."); 235 } catch (IllegalArgumentException e) {} 236 BasicStepField.testValue("123", instance); 237 } 238 239 240 public void testEqualFieldValues() { 241 assertTrue(BasicStepField.equalFieldValues(null, null)); 242 assertTrue(!BasicStepField.equalFieldValues("abc def", null)); 243 assertTrue(!BasicStepField.equalFieldValues(null, "abc def")); 244 assertTrue(BasicStepField.equalFieldValues("abc def", "abc def")); 245 assertTrue(!BasicStepField.equalFieldValues(new Integer (123), "abc def")); 246 assertTrue(!BasicStepField.equalFieldValues("abc def", new Integer (123))); 247 assertTrue(BasicStepField.equalFieldValues(new Integer (123), new Integer (123))); 248 assertTrue(BasicStepField.equalFieldValues(new Double (123), new Integer (123))); 249 assertTrue(BasicStepField.equalFieldValues(new Integer (123), new Double (123))); 250 assertTrue(BasicStepField.equalFieldValues(new Double (123), new Double (123))); 251 assertTrue(!BasicStepField.equalFieldValues(null, new Double (123))); 252 assertTrue(!BasicStepField.equalFieldValues(new Double (123), null)); 253 } 254 255 256 public void testEquals() { 257 } 259 260 261 public void testHashCode() { 262 } 264 265 266 public void testToString() { 267 assertTrue(instance.toString(), 269 instance.toString().equals(step.getTableName() + "." + instance.getFieldName())); 270 step.setAlias("abdef"); 272 assertTrue(instance.toString(), 273 instance.toString().equals(step.getAlias() + "." + instance.getFieldName())); 274 275 instance.setAlias("fedbac"); 277 assertTrue(instance.toString(), 278 instance.toString().equals(step.getAlias() + "." + instance.getFieldName() + " as " + instance.getAlias())); 279 } 280 281 public static Test suite() { 282 TestSuite suite = new TestSuite(BasicStepFieldTest.class); 283 284 return suite; 285 } 286 287 } 288 | Popular Tags |