@Test public void testGeneratePrimaryKey() throws Exception { final String id = "id"; User user = (User) ParameterUtil.generatePrimaryKey(new User(), id); Assert.assertEquals(id, user.getId()); }
@Test public void testExtractPrimaryKey() throws Exception { final String id = "id"; User user = new User(); user.setId(id); Serializable privaryKey = ParameterUtil.extractPrimaryKey(user); Assert.assertEquals(privaryKey, id); }
@Test @SuppressWarnings("unchecked") public void testArray() throws Exception { String[] parameters = new String[] {"a1", "a2"}; Object obj = ParameterUtil.resolve(parameters, shardId); Assert.assertTrue(obj instanceof Map); Map<String, Object> map = (Map<String, Object>) obj; Assert.assertEquals(PREFIX_VALUE, map.get(PREFIX)); Assert.assertEquals(SUFFIX_VALUE, map.get(SUFFIX)); Assert.assertEquals(parameters, map.get("array")); }
@Test @SuppressWarnings("unchecked") public void testList() throws Exception { List<String> parameter = new ArrayList<String>(); parameter.add("s1"); parameter.add("s2"); Object obj = ParameterUtil.resolve(parameter, shardId); Assert.assertTrue(obj instanceof Map); Map<String, Object> map = (Map<String, Object>) obj; Assert.assertEquals(PREFIX_VALUE, map.get(PREFIX)); Assert.assertEquals(SUFFIX_VALUE, map.get(SUFFIX)); Assert.assertEquals(parameter, map.get("list")); }
@Test @SuppressWarnings("unchecked") public void testMap() throws Exception { Map<String, Object> parameter = new HashMap<String, Object>(); parameter.put("id", 1); parameter.put("name", "makersoft"); Object obj = ParameterUtil.resolve(parameter, shardId); Assert.assertTrue(obj instanceof Map); Map<String, Object> map = (Map<String, Object>) obj; Assert.assertEquals(PREFIX_VALUE, map.get(PREFIX)); Assert.assertEquals(SUFFIX_VALUE, map.get(SUFFIX)); Assert.assertEquals(parameter.get("id"), map.get("id")); Assert.assertEquals(parameter.get("name"), map.get("name")); }
@Test @SuppressWarnings("unchecked") public void testObject() throws Exception { User user = new User(); user.setId("id"); user.setUsername("makersoft"); user.setPassword("password"); user.setAge(20); Object obj = ParameterUtil.resolve(user, shardId); Assert.assertTrue(obj instanceof Map); Map<String, Object> map = (Map<String, Object>) obj; Assert.assertEquals(PREFIX_VALUE, map.get(PREFIX)); Assert.assertEquals(SUFFIX_VALUE, map.get(SUFFIX)); Assert.assertEquals(user.getId(), map.get("id")); Assert.assertEquals(user.getUsername(), map.get("username")); Assert.assertEquals(user.getPassword(), map.get("password")); Assert.assertEquals(user.getAge(), map.get("age")); }
@Test @SuppressWarnings("unchecked") public void testPrimitiveTypes() throws Exception { Object[] primitiveParams = new Object[] {"string", 1L, 10, true, 'c'}; for (Object parameter : primitiveParams) { Object obj = ParameterUtil.resolve(parameter, shardId); Assert.assertTrue(obj instanceof Map); Map<String, Object> map = (Map<String, Object>) obj; Assert.assertEquals(PREFIX_VALUE, map.get(PREFIX)); Assert.assertEquals(SUFFIX_VALUE, map.get(SUFFIX)); // 对于任何key,调用map的containKey方法均返回true Assert.assertTrue(map.containsKey("any_key")); // 对于任何非prefix,suffix 的 key均返回原来传递的参数 Assert.assertEquals(parameter, map.get("any_key")); } }
@Test public void testNull() throws Exception { Object obj = ParameterUtil.resolve(null, shardId); Assert.assertNull(obj); }