示例#1
0
  public void testDouble() {
    final MyProperties props = new MyProperties();

    // Default value.
    Assert.assertEquals(-3.14, props.doubleProp.get());
    Assert.assertEquals(.789, props.doubleProp.get(.789));

    double prev = props.doubleProp.set(.8);
    Assert.assertEquals(.8, props.doubleProp.get());
    Assert.assertEquals(-3.14, prev);

    prev = props.doubleProp.set(.0);
    Assert.assertEquals(.8, prev);
  }
示例#2
0
  public void testInt() {
    final MyProperties props = new MyProperties();

    // Default value.
    Assert.assertEquals(5, props.intProp.get());
    Assert.assertEquals(789, props.intProp.get(789));

    int prev = props.intProp.set(8);
    Assert.assertEquals(8, props.intProp.get());
    Assert.assertEquals(5, prev);

    prev = props.intProp.set(0);
    Assert.assertEquals(8, prev);
  }
示例#3
0
  public void testBooleanNoDefault() {
    final MyProperties props = new MyProperties();

    // As above, on property with no default value.
    Assert.assertEquals(false, props.booleanPropNoDefault.get());
    Assert.assertEquals(true, props.booleanPropNoDefault.get(true));
    Assert.assertEquals(false, props.booleanPropNoDefault.get(false));

    boolean prev = props.booleanPropNoDefault.set(true);
    Assert.assertEquals(false, prev);
    Assert.assertEquals(true, props.booleanPropNoDefault.get());
    Assert.assertEquals(true, props.booleanPropNoDefault.get(false));

    // Second time set returns the previous value.
    prev = props.booleanPropNoDefault.set(false);
    Assert.assertEquals(true, prev);

    // Setting null is not OK.
    try {
      props.booleanPropNoDefault.setString(null);
      fail("expected NPE");
    } catch (NullPointerException e) {
      // ok
    }
  }
示例#4
0
  public void testString() {
    final MyProperties props = new MyProperties();

    // Default value.
    Assert.assertEquals("foo", props.stringProp.get());
    Assert.assertEquals("xxxxx", props.stringProp.get("xxxxx"));

    // First time set returns the default value.
    String prev = props.stringProp.set("bar");
    Assert.assertEquals("bar", props.stringProp.get());
    Assert.assertEquals("foo", prev);

    // Second time set returns the previous value.
    prev = props.stringProp.set("baz");
    Assert.assertEquals("bar", prev);

    // Setting null is not OK.
    try {
      prev = props.stringProp.set(null);
      fail("expected NPE");
    } catch (NullPointerException e) {
      // ok
    }
  }
示例#5
0
  /**
   * Generates views based on annotations found in a persistent class. Typically @DocumentReferences
   * annotations.
   *
   * @param persistentType
   * @return a Map with generated views.
   */
  public Map<String, DesignDocument.View> generateViewsFromPersistentType(
      final Class<?> persistentType) {
    Assert.notNull(persistentType, "persistentType may not be null");
    final Map<String, DesignDocument.View> views = new HashMap<String, DesignDocument.View>();

    createDeclaredViews(views, persistentType);

    eachField(
        persistentType,
        new Predicate<Field>() {
          public boolean apply(Field input) {
            if (hasAnnotation(input, DocumentReferences.class)) {
              generateView(views, input);
            }
            return false;
          }
        });
    return views;
  }
示例#6
0
  public void testTrigger() {
    final MyProperties props = new MyProperties();
    final int[] ints = {0};
    final Trigger trigger =
        new Trigger() {
          public boolean isPersistent() {
            return false;
          }

          public int phase() {
            return 0;
          }

          public void execute(Property property, String value) throws VetoRT {
            int intValue = Integer.parseInt(value);
            if (intValue > 10) {
              ints[0] = intValue;
            }
            if (intValue > 100) {
              throw new VetoRT("too big");
            }
          }
        };
    props.intProp.addTrigger(trigger);
    props.intProp.set(5);
    assertEquals(0, ints[0]); // unchanged
    props.intProp.set(15);
    assertEquals(15, ints[0]); // changed by trigger
    try {
      props.intProp.set(120);
      fail("expecting exception");
    } catch (Trigger.VetoRT e) {
      assertEquals("too big", e.getMessage());
    }
    Assert.assertEquals(15, props.intProp.get()); // change was rolled back
  }
示例#7
0
  public void testIntNoDefault() {
    final MyProperties props = new MyProperties();

    // As above, on property with no default value.
    Assert.assertEquals(0, props.intPropNoDefault.get());
    Assert.assertEquals(17, props.intPropNoDefault.get(17));

    int prev = props.intPropNoDefault.set(-56);
    Assert.assertEquals(0, prev);
    Assert.assertEquals(-56, props.intPropNoDefault.get());
    Assert.assertEquals(-56, props.intPropNoDefault.get(17));

    // Second time set returns the previous value.
    prev = props.intPropNoDefault.set(12345);
    Assert.assertEquals(-56, prev);

    // Setting null is not OK.
    try {
      props.intPropNoDefault.setString(null);
      fail("expected NPE");
    } catch (NullPointerException e) {
      // ok
    }
  }
示例#8
0
  public void testStringNoDefault() {
    final MyProperties props = new MyProperties();

    // As above, on property with no default value.
    Assert.assertEquals(null, props.stringPropNoDefault.get());
    Assert.assertEquals("xx", props.stringPropNoDefault.get("xx"));

    String prev = props.stringPropNoDefault.set("paul");
    Assert.assertEquals(null, prev);
    Assert.assertEquals("paul", props.stringPropNoDefault.get());
    Assert.assertEquals("paul", props.stringPropNoDefault.get("xx"));

    // Second time set returns the previous value.
    prev = props.stringPropNoDefault.set("ringo");
    Assert.assertEquals("paul", prev);

    // Setting null is not OK.
    try {
      prev = props.stringPropNoDefault.set(null);
      fail("expected NPE");
    } catch (NullPointerException e) {
      // ok
    }
  }
示例#9
0
  public void testBoolean() {
    final MyProperties props = new MyProperties();

    // Default value.
    Assert.assertEquals(true, props.booleanProp.get());
    Assert.assertEquals(false, props.booleanProp.get(false));

    // First time set returns the default value.
    boolean prev = props.booleanProp.set(false);
    Assert.assertEquals(false, props.booleanProp.get());
    Assert.assertEquals(true, prev);

    // Second time set returns the previous value.
    prev = props.booleanProp.set(true);
    Assert.assertEquals(false, prev);

    // Various values all mean true.
    String prevString = props.booleanProp.setString("1");
    Assert.assertEquals(true, props.booleanProp.get());
    prevString = props.booleanProp.setString("true");
    Assert.assertEquals(true, props.booleanProp.get());
    prevString = props.booleanProp.setString("TRUE");
    Assert.assertEquals(true, props.booleanProp.get());
    prevString = props.booleanProp.setString("yes");
    Assert.assertEquals(true, props.booleanProp.get());
    prevString = props.booleanProp.setString("Yes");
    Assert.assertEquals(true, props.booleanProp.get());

    // Leading and trailing spaces are ignored.
    prevString = props.booleanProp.setString("  yes  ");
    Assert.assertEquals(true, props.booleanProp.get());
    prevString = props.booleanProp.setString("false   ");
    Assert.assertEquals(false, props.booleanProp.get());
    prevString = props.booleanProp.setString("true ");
    Assert.assertEquals(true, props.booleanProp.get());

    // All other values mean false.
    prevString = props.booleanProp.setString("");
    Assert.assertEquals(false, props.booleanProp.get());
    prevString = props.booleanProp.setString("no");
    Assert.assertEquals(false, props.booleanProp.get());
    prevString = props.booleanProp.setString("wombat");
    Assert.assertEquals(false, props.booleanProp.get());
    prevString = props.booleanProp.setString("0");
    Assert.assertEquals(false, props.booleanProp.get());
    prevString = props.booleanProp.setString("false");
    Assert.assertEquals(false, props.booleanProp.get());

    // Setting null is not OK.
    try {
      props.booleanProp.setString(null);
      fail("expected NPE");
    } catch (NullPointerException e) {
      // ok
    }
  }