示例#1
0
  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);
  }
示例#2
0
  public void testSetFieldValues() throws Exception {
    DescriptorSupport descriptor = new DescriptorSupport();
    String[] names = new String[] {"name", "descriptorType"};
    Object[] values = new Object[] {"testName", "testType"};
    descriptor.setFields(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();
    descriptor.setFields(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();
      descriptor.setFields(null, null);
    } catch (RuntimeOperationsException e) {
      caught = true;
    }
    assertTrue("Expected an exception for null names and values", caught);

    caught = false;
    try {
      descriptor = new DescriptorSupport();
      descriptor.setFields(null, values);
    } catch (RuntimeOperationsException e) {
      caught = true;
    }
    assertTrue("Expected an exception for null names", caught);

    caught = false;
    try {
      descriptor = new DescriptorSupport();
      descriptor.setFields(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();
      descriptor.setFields(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();
      descriptor.setFields(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();
      descriptor.setFields(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();
      descriptor.setFields(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();
    descriptor.setFields(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();
    descriptor.setFields(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());
  }