Common utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

61 lines
1.7 KiB

  1. package org.cobbzilla.util.reflect;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import org.junit.Test;
  6. import static org.cobbzilla.util.daemon.ZillaRuntime.die;
  7. import static org.cobbzilla.util.daemon.ZillaRuntime.now;
  8. import static org.junit.Assert.*;
  9. public class ReflectionUtilTest {
  10. @AllArgsConstructor
  11. public static class Dummy {
  12. @Getter @Setter public Long id;
  13. @Getter public String name;
  14. public void setName (String name) {
  15. this.name = name;
  16. }
  17. public void setName (Dummy something) {
  18. die("should not get called!");
  19. }
  20. }
  21. private static final String ID = "id";
  22. public static final String NAME = "name";
  23. @Test public void testGetSet () throws Exception {
  24. Long testValue = now();
  25. Dummy dummy = new Dummy(testValue, NAME);
  26. assertEquals(ReflectionUtil.get(dummy, ID), testValue);
  27. ReflectionUtil.set(dummy, ID, null);
  28. assertNull(ReflectionUtil.get(dummy, ID));
  29. testValue += 10;
  30. ReflectionUtil.set(dummy, ID, testValue);
  31. assertEquals(ReflectionUtil.get(dummy, ID), testValue);
  32. ReflectionUtil.setNull(dummy, ID, Long.class);
  33. assertNull(ReflectionUtil.get(dummy, ID));
  34. ReflectionUtil.set(dummy, NAME, "a value");
  35. assertEquals(ReflectionUtil.get(dummy, NAME), "a value");
  36. try {
  37. ReflectionUtil.set(dummy, NAME, null);
  38. fail("should not have been able to set name field to null");
  39. } catch (Exception expected) {}
  40. ReflectionUtil.setNull(dummy, NAME, String.class);
  41. assertNull(ReflectionUtil.get(dummy, NAME));
  42. }
  43. }