Example usage for org.apache.commons.lang3.reflect FieldUtils removeFinalModifier

List of usage examples for org.apache.commons.lang3.reflect FieldUtils removeFinalModifier

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils removeFinalModifier.

Prototype

public static void removeFinalModifier(final Field field) 

Source Link

Document

Removes the final modifier from a Field .

Usage

From source file:org.apache.samza.zk.TestZkUtils.java

@Test
public void testZKProtocolVersion() {
    // first time connect, version should be set to ZkUtils.ZK_PROTOCOL_VERSION
    ZkLeaderElector le = new ZkLeaderElector("1", zkUtils);
    ZkControllerImpl zkController = new ZkControllerImpl("1", zkUtils, null, le);
    zkController.register();/*w w  w  .  j a v  a 2  s  . com*/
    String root = zkUtils.getKeyBuilder().getRootPath();
    String ver = (String) zkUtils.getZkClient().readData(root);
    Assert.assertEquals(ZkUtils.ZK_PROTOCOL_VERSION, ver);

    // do it again (in case original value was null
    zkController = new ZkControllerImpl("1", zkUtils, null, le);
    zkController.register();
    ver = (String) zkUtils.getZkClient().readData(root);
    Assert.assertEquals(ZkUtils.ZK_PROTOCOL_VERSION, ver);

    // now negative case
    zkUtils.getZkClient().writeData(root, "2.0");
    try {
        zkController = new ZkControllerImpl("1", zkUtils, null, le);
        zkController.register();
        Assert.fail("Expected to fail because of version mismatch 2.0 vs 1.0");
    } catch (SamzaException e) {
        // expected
    }

    // validate future values, let's say that current version should be 3.0
    try {
        Field f = zkUtils.getClass().getDeclaredField("ZK_PROTOCOL_VERSION");
        FieldUtils.removeFinalModifier(f);
        f.set(null, "3.0");
    } catch (Exception e) {
        System.out.println(e);
        Assert.fail();
    }

    try {
        zkController = new ZkControllerImpl("1", zkUtils, null, le);
        zkController.register();
        Assert.fail("Expected to fail because of version mismatch 2.0 vs 3.0");
    } catch (SamzaException e) {
        // expected
    }
}