Exemplo n.º 1
0
  @TestCoversMethods("fromMap")
  public void testFromMap_POJO() throws Exception {
    SecurityManager securityManager = new SecurityManager();
    securityManager.addAccessController(new FieldAccessController(new BaseAccessRule()));
    mapper.setSecurityManager(securityManager);

    Object owner = new Object();
    Object other = new Object();

    POJO1 p = new POJO1(owner, 1, "one");
    Map<String, Object> m = mapper.toMap(p, owner);
    assertTrue(m.containsKey("x"));
    assertTrue(m.containsKey("y"));

    POJO1 expected;

    // with owner
    expected = new POJO1(null, p.getX(), p.getY());
    assertEquals(expected, mapper.fromMap(new POJO1(owner), m, owner));

    // with other
    expected = new POJO1(null, 0, p.getY());
    assertEquals(expected, mapper.fromMap(new POJO1(owner), m, other));

    // noone
    assertEquals(expected, mapper.fromMap(new POJO1(owner), m, (Object[]) null));
    assertEquals(expected, mapper.fromMap(new POJO1(owner), m, new Object[] {}));

    mapper.setSecurityManager(null);
  }
Exemplo n.º 2
0
  @TestCoversMethods({"isReadable", "isWritable"})
  public void testIsAccessible() throws Exception {
    SecurityManager securityManager = new SecurityManager();
    FieldAccessController controller = new FieldAccessController(new BaseAccessRule());
    securityManager.addAccessController(controller);
    mapper.setSecurityManager(securityManager);

    Object owner = new Object();
    Object other = new Object();

    POJO1 entity = new POJO1(owner);

    List<com.syncnapsis.utils.reflections.Field> fields = ReflectionsUtil.findFields(POJO1.class);
    assertEquals(3, fields.size());

    com.syncnapsis.utils.reflections.Field xField = fields.get(0);
    assertEquals("x", xField.getName());
    com.syncnapsis.utils.reflections.Field yField = fields.get(1);
    assertEquals("y", yField.getName());
    com.syncnapsis.utils.reflections.Field ownersField = fields.get(2);
    assertEquals("owners", ownersField.getName());

    // READABLE
    // @formatter:off
    Object[][] authorities =
        new Object[][] {
          new Object[] {owner},
          new Object[] {other},
          new Object[] {owner, other},
          null,
          new Object[] {},
        };
    // @formatter:on

    for (int i = 0; i < authorities.length; i++) {
      assertEquals(
          controller.isAccessible(xField, AccessController.READ, entity, authorities[i]),
          mapper.isReadable(entity, xField, authorities[i]));
      assertEquals(
          controller.isAccessible(xField, AccessController.WRITE, entity, authorities[i]),
          mapper.isWritable(entity, xField, authorities[i]));
      assertEquals(
          controller.isAccessible(yField, AccessController.READ, entity, authorities[i]),
          mapper.isReadable(entity, yField, authorities[i]));
      assertEquals(
          controller.isAccessible(yField, AccessController.WRITE, entity, authorities[i]),
          mapper.isWritable(entity, yField, authorities[i]));
    }
  }
Exemplo n.º 3
0
  @TestCoversMethods({"toMap", "prepare"})
  public void testToMap_POJO() throws Exception {
    SecurityManager securityManager = new SecurityManager();
    securityManager.addAccessController(new FieldAccessController(new BaseAccessRule()));
    mapper.setSecurityManager(securityManager);

    Object owner = new Object();
    Object other = new Object();

    POJO1 p = new POJO1(owner, 1, "one");

    // with owner
    checkMap(mapper.toMap(p, owner), "x", p.getX(), "y", p.getY());

    // with other
    checkMap(mapper.toMap(p, other), "y", p.getY());

    // noone
    checkMap(mapper.toMap(p, (Object[]) null), "y", p.getY());
    checkMap(mapper.toMap(p, new Object[] {}), "y", p.getY());

    mapper.setSecurityManager(null);
  }
Exemplo n.º 4
0
 public void setAuthorities(Object[] authorities) {
   logger.debug("setting authorities: " + Arrays.asList(authorities));
   securityManager.getAuthorityProvider().set(authorities);
 }