Example usage for org.apache.commons.lang3 SystemUtils IS_JAVA_1_8

List of usage examples for org.apache.commons.lang3 SystemUtils IS_JAVA_1_8

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SystemUtils IS_JAVA_1_8.

Prototype

boolean IS_JAVA_1_8

To view the source code for org.apache.commons.lang3 SystemUtils IS_JAVA_1_8.

Click Source Link

Document

Is true if this is Java version 1.8 (also 1.8.x versions).

Usage

From source file:com.haulmont.cuba.core.config.ConfigDefaultMethod.java

@Override
public Object invoke(ConfigHandler handler, Object[] args, Object proxy) {
    try {//from  w w w .  ja  v a 2  s.com
        if (SystemUtils.IS_JAVA_1_8) {
            // hack to invoke default method of an interface reflectively
            Constructor<MethodHandles.Lookup> lookupConstructor = MethodHandles.Lookup.class
                    .getDeclaredConstructor(Class.class, Integer.TYPE);
            if (!lookupConstructor.isAccessible()) {
                lookupConstructor.setAccessible(true);
            }
            return lookupConstructor.newInstance(configInterface, MethodHandles.Lookup.PRIVATE)
                    .unreflectSpecial(configMethod, configInterface).bindTo(proxy).invokeWithArguments(args);
        } else {
            return MethodHandles.lookup()
                    .findSpecial(configInterface, configMethod.getName(),
                            MethodType.methodType(configMethod.getReturnType(),
                                    configMethod.getParameterTypes()),
                            configInterface)
                    .bindTo(proxy).invokeWithArguments(args);
        }
    } catch (Throwable throwable) {
        throw new RuntimeException("Error invoking default method of config interface", throwable);
    }
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testNonPersistentSetServerAndClient() throws InitializationException, IOException {

    /**/*from ww w.j ava 2 s .  c om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedSetCacheServer server = new SetServer();
    runner.addControllerService("server", server);
    runner.enableControllerService(server);

    final DistributedSetCacheClientService client = createClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.addIfAbsent("test", serializer);
    assertTrue(added);

    final boolean contains = client.contains("test", serializer);
    assertTrue(contains);

    final boolean addedAgain = client.addIfAbsent("test", serializer);
    assertFalse(addedAgain);

    final boolean removed = client.remove("test", serializer);
    assertTrue(removed);

    final boolean containedAfterRemove = client.contains("test", serializer);
    assertFalse(containedAfterRemove);

    server.shutdownServer();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testPersistentSetServerAndClient() throws InitializationException, IOException {
    /**/* w w w .j  a  va 2s. c  om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());

    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);

    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedSetCacheServer server = new SetServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(server);

    DistributedSetCacheClientService client = createClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.addIfAbsent("test", serializer);
    final boolean added2 = client.addIfAbsent("test2", serializer);
    assertTrue(added);
    assertTrue(added2);

    final boolean contains = client.contains("test", serializer);
    final boolean contains2 = client.contains("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);

    final boolean addedAgain = client.addIfAbsent("test", serializer);
    assertFalse(addedAgain);

    final boolean removed = client.remove("test", serializer);
    assertTrue(removed);

    final boolean containedAfterRemove = client.contains("test", serializer);
    assertFalse(containedAfterRemove);

    server.shutdownServer();
    client.close();

    final DistributedSetCacheServer newServer = new SetServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(newServer);
    client = createClient(newServer.getPort());

    assertFalse(client.contains("test", serializer));
    assertTrue(client.contains("test2", serializer));

    newServer.shutdownServer();
    client.close();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testPersistentSetServerAndClientWithLFUEvictions() throws InitializationException, IOException {
    /**/*from ww w.  java 2 s .  c  om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);

    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedSetCacheServer server = new SetServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(server, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(server, DistributedSetCacheServer.EVICTION_POLICY,
            DistributedSetCacheServer.EVICTION_STRATEGY_LFU);
    runner.enableControllerService(server);

    DistributedSetCacheClientService client = createClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.addIfAbsent("test", serializer);
    waitABit();
    final boolean added2 = client.addIfAbsent("test2", serializer);
    waitABit();
    final boolean added3 = client.addIfAbsent("test3", serializer);
    waitABit();
    assertTrue(added);
    assertTrue(added2);
    assertTrue(added3);

    final boolean contains = client.contains("test", serializer);
    final boolean contains2 = client.contains("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);

    final boolean addedAgain = client.addIfAbsent("test", serializer);
    assertFalse(addedAgain);

    final boolean added4 = client.addIfAbsent("test4", serializer);
    assertTrue(added4);

    // ensure that added3 was evicted because it was used least frequently
    assertFalse(client.contains("test3", serializer));

    server.shutdownServer();

    final DistributedSetCacheServer newServer = new SetServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(newServer);
    client.close();
    client = createClient(newServer.getPort());

    assertTrue(client.contains("test", serializer));
    assertTrue(client.contains("test2", serializer));
    assertFalse(client.contains("test3", serializer));
    assertTrue(client.contains("test4", serializer));

    newServer.shutdownServer();
    client.close();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testPersistentMapServerAndClientWithLFUEvictions() throws InitializationException, IOException {
    /**/* www.j  a  va2s. c  om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);

    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedMapCacheServer server = new MapServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(server, DistributedMapCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(server, DistributedMapCacheServer.EVICTION_POLICY,
            DistributedMapCacheServer.EVICTION_STRATEGY_LFU);
    runner.enableControllerService(server);

    DistributedMapCacheClientService client = createMapClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.putIfAbsent("test", "1", serializer, serializer);
    waitABit();
    final boolean added2 = client.putIfAbsent("test2", "2", serializer, serializer);
    waitABit();
    final boolean added3 = client.putIfAbsent("test3", "3", serializer, serializer);
    waitABit();
    assertTrue(added);
    assertTrue(added2);
    assertTrue(added3);

    final boolean contains = client.containsKey("test", serializer);
    final boolean contains2 = client.containsKey("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);

    final boolean addedAgain = client.putIfAbsent("test", "1", serializer, serializer);
    assertFalse(addedAgain);

    final boolean added4 = client.putIfAbsent("test4", "4", serializer, serializer);
    assertTrue(added4);

    // ensure that added3 was evicted because it was used least frequently
    assertFalse(client.containsKey("test3", serializer));

    server.shutdownServer();

    final DistributedMapCacheServer newServer = new MapServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(newServer);
    client.close();
    client = createMapClient(newServer.getPort());

    assertTrue(client.containsKey("test", serializer));
    assertTrue(client.containsKey("test2", serializer));
    assertFalse(client.containsKey("test3", serializer));
    assertTrue(client.containsKey("test4", serializer));

    newServer.shutdownServer();
    client.close();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testPersistentSetServerAndClientWithFIFOEvictions() throws InitializationException, IOException {
    /**/*from  w  w  w .ja va2 s  . com*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());

    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);

    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedSetCacheServer server = new SetServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(server, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(server, DistributedSetCacheServer.EVICTION_POLICY,
            DistributedSetCacheServer.EVICTION_STRATEGY_FIFO);
    runner.enableControllerService(server);

    DistributedSetCacheClientService client = createClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();

    // add 3 entries to the cache. But, if we add too fast, we'll have the same millisecond
    // for the entry time so we don't know which entry will be evicted. So we wait a few millis in between
    final boolean added = client.addIfAbsent("test", serializer);
    waitABit();
    final boolean added2 = client.addIfAbsent("test2", serializer);
    waitABit();
    final boolean added3 = client.addIfAbsent("test3", serializer);
    waitABit();

    assertTrue(added);
    assertTrue(added2);
    assertTrue(added3);

    final boolean contains = client.contains("test", serializer);
    final boolean contains2 = client.contains("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);

    final boolean addedAgain = client.addIfAbsent("test", serializer);
    assertFalse(addedAgain);

    final boolean added4 = client.addIfAbsent("test4", serializer);
    assertTrue(added4);

    // ensure that added3 was evicted because it was used least frequently
    assertFalse(client.contains("test", serializer));
    assertTrue(client.contains("test3", serializer));

    server.shutdownServer();
    client.close();

    final DistributedSetCacheServer newServer = new SetServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedSetCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(newServer, DistributedSetCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(newServer, DistributedSetCacheServer.EVICTION_POLICY,
            DistributedSetCacheServer.EVICTION_STRATEGY_FIFO);
    runner.enableControllerService(newServer);

    client = createClient(newServer.getPort());
    assertFalse(client.contains("test", serializer));
    assertTrue(client.contains("test2", serializer));
    assertTrue(client.contains("test3", serializer));
    assertTrue(client.contains("test4", serializer));

    newServer.shutdownServer();
    client.close();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testNonPersistentMapServerAndClient()
        throws InitializationException, IOException, InterruptedException {
    /**/*from  w  w  w.  ja  va2  s  . c  om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());

    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);

    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(
            client, "client");
    client.initialize(clientInitContext);

    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties,
            clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();

    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer,
            deserializer);
    assertEquals(null, original);
    LOGGER.debug("end getAndPutIfAbsent");

    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    LOGGER.debug("end containsKey");

    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    LOGGER.debug("end putIfAbsent");

    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer,
            deserializer);
    assertEquals("test", originalAfterPut);
    LOGGER.debug("end getAndPutIfAbsent");

    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    LOGGER.debug("end remove");

    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);

    client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    client.close();
    try {
        client.containsKey("testKey", keySerializer);
        fail("Should be closed and not accessible");
    } catch (final Exception e) {

    }
    client = null;
    clientInitContext = null;
    clientContext = null;

    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(
            client2, "client2");
    client2.initialize(clientInitContext2);

    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties,
            clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    assertFalse(client2.putIfAbsent("testKey", "test", keySerializer, valueSerializer));
    assertTrue(client2.containsKey("testKey", keySerializer));
    server.shutdownServer();
    Thread.sleep(1000);
    try {
        client2.containsKey("testKey", keySerializer);
        fail("Should have blown exception!");
    } catch (final ConnectException e) {
        client2 = null;
        clientContext2 = null;
        clientInitContext2 = null;
    }
    LOGGER.debug("end testNonPersistentMapServerAndClient");
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testClientTermination() throws InitializationException, IOException, InterruptedException {

    /**/*from ww w  .java  2 s.  c om*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug See: https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "testClientTermination is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final MockControllerServiceInitializationContext serverInitContext = new MockControllerServiceInitializationContext(
            server, "server");
    server.initialize(serverInitContext);

    final Map<PropertyDescriptor, String> serverProperties = new HashMap<>();
    final MockConfigurationContext serverContext = new MockConfigurationContext(serverProperties,
            serverInitContext.getControllerServiceLookup());
    server.startServer(serverContext);

    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(
            client, "client");
    client.initialize(clientInitContext);

    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties,
            clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();

    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer,
            deserializer);
    assertEquals(null, original);

    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);

    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);

    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer,
            deserializer);
    assertEquals("test", originalAfterPut);

    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);

    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);

    client = null;
    clientInitContext = null;
    clientContext = null;
    Thread.sleep(2000);
    System.gc();
    server.shutdownServer();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testOptimisticLock() throws Exception {

    /**/*w w  w . j a v  a 2  s.  com*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());

    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);

    DistributedMapCacheClientService client1 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(
            client1, "client1");
    client1.initialize(clientInitContext1);

    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(
            client2, "client2");
    client1.initialize(clientInitContext2);

    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");

    MockConfigurationContext clientContext1 = new MockConfigurationContext(clientProperties,
            clientInitContext1.getControllerServiceLookup());
    client1.cacheConfig(clientContext1);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties,
            clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);

    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();

    final String key = "test-optimistic-lock";

    // Ensure there's no existing key
    assertFalse(client1.containsKey(key, stringSerializer));
    assertNull(client1.fetch(key, stringSerializer, stringDeserializer));

    // Client 1 inserts the key.
    client1.put(key, "valueC1-0", stringSerializer, stringSerializer);

    // Client 1 and 2 fetch the key
    AtomicDistributedMapCacheClient.CacheEntry<String, String> c1 = client1.fetch(key, stringSerializer,
            stringDeserializer);
    AtomicDistributedMapCacheClient.CacheEntry<String, String> c2 = client2.fetch(key, stringSerializer,
            stringDeserializer);
    assertEquals(0, c1.getRevision());
    assertEquals("valueC1-0", c1.getValue());
    assertEquals(0, c2.getRevision());
    assertEquals("valueC1-0", c2.getValue());

    // Client 1 replace
    boolean c1Result = client1.replace(key, "valueC1-1", stringSerializer, stringSerializer, c1.getRevision());
    assertTrue("C1 should be able to replace the key", c1Result);
    // Client 2 replace with the old revision
    boolean c2Result = client2.replace(key, "valueC2-1", stringSerializer, stringSerializer, c2.getRevision());
    assertFalse("C2 shouldn't be able to replace the key", c2Result);

    // Client 2 fetch the key again
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC1-1", c2.getValue());
    assertEquals(1, c2.getRevision());

    // Now, Client 2 knows the correct revision so it can replace the key
    c2Result = client2.replace(key, "valueC2-2", stringSerializer, stringSerializer, c2.getRevision());
    assertTrue("C2 should be able to replace the key", c2Result);

    // Assert the cache
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC2-2", c2.getValue());
    assertEquals(2, c2.getRevision());

    client1.close();
    client2.close();
    server.shutdownServer();
}

From source file:org.apache.nifi.distributed.cache.server.TestServerAndClient.java

@Test
public void testBackwardCompatibility() throws Exception {

    /**/*w  w w  . j  av  a 2s .  c o  m*/
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse(
            "test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
            SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());

    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));

    // Create a server that only supports protocol version 1.
    final DistributedMapCacheServer server = new MapServer() {
        @Override
        protected MapCacheServer createMapCacheServer(int port, int maxSize, SSLContext sslContext,
                EvictionPolicy evictionPolicy, File persistenceDir) throws IOException {
            return new MapCacheServer(getIdentifier(), sslContext, port, maxSize, evictionPolicy,
                    persistenceDir) {
                @Override
                protected StandardVersionNegotiator getVersionNegotiator() {
                    return new StandardVersionNegotiator(1);
                }
            };
        }
    };
    runner.addControllerService("server", server);
    runner.enableControllerService(server);

    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(
            client, "client");
    client.initialize(clientInitContext1);

    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");

    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties,
            clientInitContext1.getControllerServiceLookup());
    client.cacheConfig(clientContext);

    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();

    final String key = "test-backward-compatibility";

    // Version 1 operations should work
    client.put(key, "value1", stringSerializer, stringSerializer);
    assertEquals("value1", client.get(key, stringSerializer, stringDeserializer));

    assertTrue(client.containsKey(key, stringSerializer));

    try {
        client.fetch(key, stringSerializer, stringDeserializer);
        fail("Version 2 operations should NOT work.");
    } catch (UnsupportedOperationException e) {
    }

    try {
        client.replace(key, "value2", stringSerializer, stringSerializer, 0L);
        fail("Version 2 operations should NOT work.");
    } catch (UnsupportedOperationException e) {
    }

    client.close();
    server.shutdownServer();
}