public void testClone() throws Exception { String[] names = new String[] {"name", "descriptorType"}; Object[] values = new Object[] {"testName", "testType"}; DescriptorSupport descriptor = new DescriptorSupport(names, values); DescriptorSupport clone = (DescriptorSupport) descriptor.clone(); compareFields(descriptor.getFields(), clone.getFields()); }
public void testGetFieldValues() throws Exception { String[] names = new String[] {"name", "descriptorType"}; String[] values = new String[] {"testName", "testType"}; DescriptorSupport descriptor = new DescriptorSupport(names, values); Object[] result = descriptor.getFieldValues(null); compareFieldValuesUnsorted(values, result); result = descriptor.getFieldValues(new String[0]); compareFieldValues(new Object[0], result); result = descriptor.getFieldValues(names); compareFieldValues(values, result); result = descriptor.getFieldValues(new String[] {"name"}); compareFieldValues(new Object[] {"testName"}, result); result = descriptor.getFieldValues(new String[] {"descriptorType", "name"}); compareFieldValues(new Object[] {"testType", "testName"}, result); result = descriptor.getFieldValues(new String[] {"NAME"}); compareFieldValues(new Object[] {"testName"}, result); result = descriptor.getFieldValues(new String[] {null}); compareFieldValues(new Object[] {null}, result); result = descriptor.getFieldValues(new String[] {""}); compareFieldValues(new Object[] {null}, result); }
public void testSizeConstructor() throws Exception { DescriptorSupport descriptor = new DescriptorSupport(100); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); boolean caught = false; try { descriptor = new DescriptorSupport(-1); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for a negative size", caught); }
public void testSerialization() throws Exception { String[] names = new String[] {"name", "descriptorType"}; Object[] values = new Object[] {"testName", "testType"}; DescriptorSupport descriptor = new DescriptorSupport(names, values); // Serialize it ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(descriptor); // Deserialize it ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); Object result = ois.readObject(); compareFields(descriptor.getFields(), ((DescriptorSupport) result).getFields()); }
public void testGetFieldValue() throws Exception { String[] fields = new String[] {"name=testName", "descriptorType=testType"}; DescriptorSupport descriptor = new DescriptorSupport(fields); assertEquals("testName", descriptor.getFieldValue("name")); assertTrue( "Field names are not case sensitive", "testName".equals(descriptor.getFieldValue("NAME"))); assertTrue( "Non existent field should be null", descriptor.getFieldValue("nonExistent") == null); boolean caught = false; try { descriptor.getFieldValue(null); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null name", caught); caught = false; try { descriptor.getFieldValue(""); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for empty name", caught); // This is legal? assertTrue("Blank field name is allowed", descriptor.getFieldValue(" ") == null); }
public void testNameEqualsValueConstructorError() throws Exception { try { String[] fields = new String[] {"name=testName", "descriptorType=testType", "another="}; DescriptorSupport descriptor = new DescriptorSupport(fields); assertTrue("Should be three fields", descriptor.getFields().length == 3); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); assertTrue("another should be null", descriptor.getFieldValue("another") == null); assertTrue("Should be valid", descriptor.isValid()); } catch (Exception e) { fail("FAILS IN RI: 'another=' should be valid according to the javadoc " + e.toString()); } }
public void testNamesValuesConstructor() throws Exception { String[] names = new String[] {"name", "descriptorType"}; Object[] values = new Object[] {"testName", "testType"}; DescriptorSupport descriptor = new DescriptorSupport(names, values); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); assertTrue("Should be valid", descriptor.isValid()); descriptor = new DescriptorSupport(new String[0], new Object[0]); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); boolean caught = false; try { descriptor = new DescriptorSupport(null, null); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null names and values", caught); caught = false; try { descriptor = new DescriptorSupport(null, values); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null names", caught); caught = false; try { descriptor = new DescriptorSupport(names, null); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null values", caught); Object[] tooManyValues = new Object[] {"testName", "testType", "tooMany"}; caught = false; try { descriptor = new DescriptorSupport(names, tooManyValues); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for too many values", caught); Object[] tooFewValues = new Object[] {"testName"}; caught = false; try { descriptor = new DescriptorSupport(names, tooFewValues); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for too few values", caught); String[] nullName = new String[] {"name", null}; caught = false; try { descriptor = new DescriptorSupport(nullName, values); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null name", caught); String[] emptyName = new String[] {"name", ""}; caught = false; try { descriptor = new DescriptorSupport(emptyName, values); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for empty name ", caught); // This is legal? String[] notEmptyName = new String[] {"name", " "}; descriptor = new DescriptorSupport(notEmptyName, values); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue(" ")); assertFalse("Should not be valid", descriptor.isValid()); names = new String[] {"name", "descriptorType", "another"}; values = new Object[] {"testName", "testType", null}; descriptor = new DescriptorSupport(names, values); assertTrue("Should be three fields", descriptor.getFields().length == 3); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); assertTrue("another should be null", descriptor.getFieldValue("another") == null); assertTrue("Should be valid", descriptor.isValid()); }
private void validate(String[] names, Object[] values, boolean valid) { DescriptorSupport descriptor = null; RuntimeOperationsException caught = null; boolean descriptorValid = false; try { descriptor = new DescriptorSupport(names, values); descriptorValid = descriptor.isValid(); } catch (RuntimeOperationsException e) { caught = e; } if (valid && caught != null) throw caught; assertEquals( "Expected " + valid + " for new Descriptor(String[], String[]) names=" + Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid); caught = null; descriptorValid = false; try { String[] fields = new String[names.length]; for (int i = 0; i < fields.length; i++) { if (values[i] == null) fields[i] = names[i] + "="; else fields[i] = names[i] + "=" + values[i].toString(); } descriptor = new DescriptorSupport(names, values); descriptorValid = descriptor.isValid(); } catch (RuntimeOperationsException e) { caught = e; } if (valid && caught != null) throw caught; assertEquals( "Expected " + valid + " for new Descriptor(String[], String[]) names=" + Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid); caught = null; descriptorValid = false; try { descriptor = new DescriptorSupport(); for (int i = 0; i < names.length; i++) descriptor.setField(names[i], values[i]); descriptorValid = descriptor.isValid(); } catch (RuntimeOperationsException e) { caught = e; } if (valid && caught != null) throw caught; assertEquals( "Expected " + valid + " for new Descriptor(String[], String[]) names=" + Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid); caught = null; descriptorValid = false; try { descriptor = new DescriptorSupport(); descriptor.setFields(names, values); descriptorValid = descriptor.isValid(); } catch (RuntimeOperationsException e) { caught = e; } if (valid && caught != null) throw caught; assertEquals( "Expected " + valid + " for new Descriptor(String[], String[]) names=" + Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid); }
public void testCopyConstructor() throws Exception { DescriptorSupport descriptor = new DescriptorSupport((DescriptorSupport) null); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); descriptor = new DescriptorSupport(descriptor); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); descriptor.setField("name", "testName"); descriptor.setField("descriptorType", "testType"); descriptor = new DescriptorSupport(descriptor); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); assertTrue("Should be valid", descriptor.isValid()); }
public void testDefaultConstructor() throws Exception { DescriptorSupport descriptor = new DescriptorSupport(); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); }
public void testRemove() throws Exception { String[] names = new String[] {"name", "descriptorType"}; Object[] values = new Object[] {"testName", "testType"}; DescriptorSupport descriptor = new DescriptorSupport(names, values); descriptor.removeField("name"); assertTrue("Should be one field", descriptor.getFields().length == 1); assertTrue("name should not be present", descriptor.getFieldValue("name") == null); assertEquals("testType", descriptor.getFieldValue("descriptorType")); descriptor = new DescriptorSupport(names, values); descriptor.removeField("NAME"); assertTrue("Should be one field", descriptor.getFields().length == 1); assertTrue("name should not be present", descriptor.getFieldValue("name") == null); assertEquals("testType", descriptor.getFieldValue("descriptorType")); descriptor = new DescriptorSupport(names, values); descriptor.removeField("notPresent"); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); descriptor.removeField(null); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); descriptor.removeField(""); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); }
public void testGetFieldNames() throws Exception { String[] fields = new String[] {"name=testName", "descriptorType=testType"}; DescriptorSupport descriptor = new DescriptorSupport(fields); String[] names = descriptor.getFieldNames(); compareFieldNames(fields, names); }
public void testSetFieldValue() throws Exception { String[] fields = new String[] {"name=testName", "descriptorType=testType"}; DescriptorSupport descriptor = new DescriptorSupport(fields); assertEquals("testName", descriptor.getFieldValue("name")); descriptor.setField("name", "newName"); assertEquals("newName", descriptor.getFieldValue("name")); descriptor.setField("NAME", "newNAME"); assertEquals("newNAME", descriptor.getFieldValue("name")); boolean caught = false; try { descriptor.setField(null, "null"); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for null name", caught); caught = false; try { descriptor.setField("", "empty"); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for empty name", caught); // This is legal? descriptor.setField(" ", "blank"); assertEquals("blank", descriptor.getFieldValue(" ")); descriptor.setField("newField", "newValue"); assertEquals("newValue", descriptor.getFieldValue("newField")); }
public void testNameEqualsValueConstructor() throws Exception { String[] fields = new String[] {"name=testName", "descriptorType=testType"}; DescriptorSupport descriptor = new DescriptorSupport(fields); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("testType", descriptor.getFieldValue("descriptorType")); assertTrue("Should be valid", descriptor.isValid()); descriptor = new DescriptorSupport((String[]) null); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); descriptor = new DescriptorSupport(new String[0]); assertTrue("Should be empty", descriptor.getFields().length == 0); assertFalse("Should not be valid", descriptor.isValid()); String[] nullName = new String[] {"name=testName", "=rubbish"}; boolean caught = false; try { descriptor = new DescriptorSupport(nullName); } catch (RuntimeOperationsException e) { caught = true; } assertTrue("Expected an exception for '=rubbish'", caught); // This is legal? String[] notEmptyName = new String[] {"name=testName", " =rubbish"}; descriptor = new DescriptorSupport(notEmptyName); assertTrue("Should be two fields", descriptor.getFields().length == 2); assertEquals("testName", descriptor.getFieldValue("name")); assertEquals("rubbish", descriptor.getFieldValue(" ")); assertFalse("Should not be valid", descriptor.isValid()); }