Esempio n. 1
0
 public void testValueOfFunction_equals() {
   new EqualsTester()
       .addEqualityGroup(
           Enums.valueOfFunction(TestEnum.class), Enums.valueOfFunction(TestEnum.class))
       .addEqualityGroup(Enums.valueOfFunction(OtherEnum.class))
       .testEquals();
 }
Esempio n. 2
0
  @GwtIncompatible("reflection")
  public void testGetField() {
    Field foo = Enums.getField(AnEnum.FOO);
    assertEquals("FOO", foo.getName());
    assertTrue(foo.isAnnotationPresent(ExampleAnnotation.class));

    Field bar = Enums.getField(AnEnum.BAR);
    assertEquals("BAR", bar.getName());
    assertFalse(bar.isAnnotationPresent(ExampleAnnotation.class));
  }
Esempio n. 3
0
  public IndicatorStyle seenIndicatorStyle() {
    String value =
        preferences.getString("pref_seen_indicator_style", IndicatorStyle.NONE.toString());

    assert value != null;
    return Enums.getIfPresent(IndicatorStyle.class, value).or(IndicatorStyle.NONE);
  }
Esempio n. 4
0
 public NetworkGroupFilterSupport() {
   super(
       builderFor(NetworkGroup.class)
           .withTagFiltering(NetworkGroupTag.class, "networkGroup")
           .withStringProperty("description", FilterFunctions.DESCRIPTION)
           .withStringProperty("group-id", FilterFunctions.GROUP_ID)
           .withStringProperty("group-name", CloudMetadatas.toDisplayName())
           .withStringSetProperty("ip-permission.cidr", FilterSetFunctions.PERMISSION_CIDR)
           .withStringSetProperty(
               "ip-permission.from-port", FilterSetFunctions.PERMISSION_FROM_PORT)
           .withStringSetProperty(
               "ip-permission.group-name", FilterSetFunctions.PERMISSION_GROUP)
           .withStringSetProperty(
               "ip-permission.protocol", FilterSetFunctions.PERMISSION_PROTOCOL)
           .withStringSetProperty("ip-permission.to-port", FilterSetFunctions.PERMISSION_TO_PORT)
           .withStringSetProperty(
               "ip-permission.user-id", FilterSetFunctions.PERMISSION_ACCOUNT_ID)
           .withStringProperty("owner-id", FilterFunctions.ACCOUNT_ID)
           .withPersistenceAlias("networkRules", "networkRules")
           .withPersistenceFilter("description")
           .withPersistenceFilter("group-id", "groupId")
           .withPersistenceFilter("group-name", "displayName")
           .withPersistenceFilter(
               "ip-permission.from-port", "networkRules.lowPort", PersistenceFilter.Type.Integer)
           .withPersistenceFilter(
               "ip-permission.protocol",
               "networkRules.protocol",
               Enums.valueOfFunction(NetworkRule.Protocol.class))
           .withPersistenceFilter(
               "ip-permission.to-port", "networkRules.highPort", PersistenceFilter.Type.Integer)
           .withPersistenceFilter("owner-id", "ownerAccountNumber"));
 }
Esempio n. 5
0
  public void testGetIfPresent() {
    assertEquals(Optional.of(TestEnum.CHEETO), Enums.getIfPresent(TestEnum.class, "CHEETO"));
    assertEquals(Optional.of(TestEnum.HONDA), Enums.getIfPresent(TestEnum.class, "HONDA"));
    assertEquals(Optional.of(TestEnum.POODLE), Enums.getIfPresent(TestEnum.class, "POODLE"));

    assertTrue(Enums.getIfPresent(TestEnum.class, "CHEETO").isPresent());
    assertTrue(Enums.getIfPresent(TestEnum.class, "HONDA").isPresent());
    assertTrue(Enums.getIfPresent(TestEnum.class, "POODLE").isPresent());

    assertEquals(TestEnum.CHEETO, Enums.getIfPresent(TestEnum.class, "CHEETO").get());
    assertEquals(TestEnum.HONDA, Enums.getIfPresent(TestEnum.class, "HONDA").get());
    assertEquals(TestEnum.POODLE, Enums.getIfPresent(TestEnum.class, "POODLE").get());
  }
Esempio n. 6
0
 public void testStringConverter_convertError() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   try {
     converter.convert("xxx");
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Esempio n. 7
0
 public void testStringConverter_convert() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   assertEquals(TestEnum.CHEETO, converter.convert("CHEETO"));
   assertEquals(TestEnum.HONDA, converter.convert("HONDA"));
   assertEquals(TestEnum.POODLE, converter.convert("POODLE"));
   assertNull(converter.convert(null));
   assertNull(converter.reverse().convert(null));
 }
Esempio n. 8
0
 @GwtIncompatible("NullPointerTester")
 public void testStringConverter_nullPointerTester() throws Exception {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   NullPointerTester tester = new NullPointerTester();
   tester.testAllPublicInstanceMethods(converter);
 }
Esempio n. 9
0
 public void testValueOfFunction_nullWhenNoMatchingConstant() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   assertNull(function.apply("WOMBAT"));
 }
Esempio n. 10
0
 public void testValueOfFunction_caseSensitive() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   assertNull(function.apply("cHEETO"));
   assertNull(function.apply("Honda"));
   assertNull(function.apply("poodlE"));
 }
Esempio n. 11
0
 public void testValueOfFunction() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   assertEquals(TestEnum.CHEETO, function.apply("CHEETO"));
   assertEquals(TestEnum.HONDA, function.apply("HONDA"));
   assertEquals(TestEnum.POODLE, function.apply("POODLE"));
 }
Esempio n. 12
0
 public <A extends Enum<A>> A getEnumValue(final String name, final Class<A> type) {
   return Enums.stringConverter(type).convert(getString(name));
 }
Esempio n. 13
0
 public void testStringConverter_serialization() {
   SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class));
 }
Esempio n. 14
0
 public static CpuFamily fromValue(String v) {
   return Enums.getIfPresent(CpuFamily.class, v).or(UNRECOGNIZED);
 }
Esempio n. 15
0
 public void testGetIfPresent_caseSensitive() {
   assertFalse(Enums.getIfPresent(TestEnum.class, "cHEETO").isPresent());
   assertFalse(Enums.getIfPresent(TestEnum.class, "Honda").isPresent());
   assertFalse(Enums.getIfPresent(TestEnum.class, "poodlE").isPresent());
 }
Esempio n. 16
0
 @GwtIncompatible("SerializableTester")
 public void testValueOfFunction_serialization() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   SerializableTester.reserializeAndAssert(function);
 }
Esempio n. 17
0
 public void testStringConverter_reverse() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO));
   assertEquals("HONDA", converter.reverse().convert(TestEnum.HONDA));
   assertEquals("POODLE", converter.reverse().convert(TestEnum.POODLE));
 }
Esempio n. 18
0
 public void testStringConverter_nullConversions() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   assertNull(converter.convert(null));
   assertNull(converter.reverse().convert(null));
 }
Esempio n. 19
0
 @GwtIncompatible("Class.getName()")
 public void testStringConverter_toString() {
   assertEquals(
       "Enums.stringConverter(com.google.common.base.EnumsTest$TestEnum.class)",
       Enums.stringConverter(TestEnum.class).toString());
 }
Esempio n. 20
0
 public void testGetIfPresent_whenNoMatchingConstant() {
   assertEquals(Optional.absent(), Enums.getIfPresent(TestEnum.class, "WOMBAT"));
 }
Esempio n. 21
0
 static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
   WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
   return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
 }