@Test(timeout = 120000) public void testIssue583MapReplaceShouldTriggerMapStore() { final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>(); final MapStore<String, Long> myMapStore = new SimpleMapStore<String, Long>(store); Config config = getConfig(); config .getMapConfig("myMap") .setMapStoreConfig(new MapStoreConfig().setImplementation(myMapStore)); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3); HazelcastInstance hc = nodeFactory.newHazelcastInstance(config); IMap<String, Long> myMap = hc.getMap("myMap"); myMap.put("one", 1L); assertEquals(1L, myMap.get("one").longValue()); assertEquals(1L, store.get("one").longValue()); myMap.putIfAbsent("two", 2L); assertEquals(2L, myMap.get("two").longValue()); assertEquals(2L, store.get("two").longValue()); myMap.putIfAbsent("one", 5L); assertEquals(1L, myMap.get("one").longValue()); assertEquals(1L, store.get("one").longValue()); myMap.replace("one", 1L, 111L); assertEquals(111L, myMap.get("one").longValue()); assertEquals(111L, store.get("one").longValue()); myMap.replace("one", 1L); assertEquals(1L, myMap.get("one").longValue()); assertEquals(1L, store.get("one").longValue()); }
@Test public void replace() { HazelcastClient hClient = getHazelcastClient(); IMap<String, String> map = hClient.getMap("replace"); String result = map.put("1", "CBDEF"); assertNull(result); assertEquals("CBDEF", map.replace("1", "CBD")); assertNull(map.replace("2", "CBD")); assertFalse(map.replace("2", "CBD", "ABC")); assertTrue(map.replace("1", "CBD", "XX")); }
public static void main(String[] args) { // enter your licenceKey below String licenceKey = "---- LICENCE KEY ----"; Config config = createConfig(licenceKey); Hazelcast.newHazelcastInstance(config); HazelcastInstance client = HazelcastClient.newHazelcastClient(); IMap<Object, Object> acceptedMap = client.getMap(ACCEPTED_MAP_NAME); IMap<Object, Object> deniedMap = client.getMap(DENIED_MAP_NAME); acceptedMap.put(ACCEPTED_KEY, ACCEPTED_VALUE); try { deniedMap.put(ACCEPTED_KEY, ACCEPTED_VALUE); System.err.println("Should be denied!!!!"); } catch (Exception expected) { EmptyStatement.ignore(expected); } try { acceptedMap.put(ACCEPTED_KEY, DENIED_VALUE); System.err.println("Should be denied!!!!"); } catch (Exception expected) { EmptyStatement.ignore(expected); } try { acceptedMap.put(DENIED_KEY, ACCEPTED_VALUE); System.err.println("Should be denied!!!!"); } catch (Exception expected) { EmptyStatement.ignore(expected); } try { acceptedMap.replace(ACCEPTED_KEY, ACCEPTED_VALUE); System.err.println("Should be denied!!!!"); } catch (Exception expected) { EmptyStatement.ignore(expected); } }
@Test(timeout = 120000) public void testIssue1115EnablingMapstoreMutatingValue() throws InterruptedException { Config config = getConfig(); String mapName = "testIssue1115"; MapStore mapStore = new ProcessingStore(); MapStoreConfig mapStoreConfig = new MapStoreConfig(); mapStoreConfig.setEnabled(true); mapStoreConfig.setImplementation(mapStore); config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig); TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config); HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config); IMap<Integer, Employee> map = instance1.getMap(mapName); Random random = new Random(); // testing put with new object for (int i = 0; i < 10; i++) { Employee emp = new Employee(); emp.setAge(random.nextInt(20) + 20); map.put(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with existing object for (int i = 0; i < 10; i++) { Employee emp = map.get(i); emp.setAge(random.nextInt(20) + 20); map.put(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with replace for (int i = 0; i < 10; i++) { Employee emp = map.get(i); emp.setAge(random.nextInt(20) + 20); map.replace(i, emp); } for (int i = 0; i < 10; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } // testing put with putIfAbsent for (int i = 10; i < 20; i++) { Employee emp = new Employee(); emp.setAge(random.nextInt(20) + 20); map.putIfAbsent(i, emp); } for (int i = 10; i < 20; i++) { Employee employee = map.get(i); assertEquals(employee.getAge() * 1000, employee.getSalary(), 0); } }