PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/java/com/atlassian/fugue/ImmutableMapsTest.java

https://bitbucket.org/atlassian/fugue
Java | 513 lines | 415 code | 82 blank | 16 comment | 39 complexity | 4ecedfbbf1f323c7991078b00f8e9c21 MD5 | raw file
  1. /*
  2. Copyright 2011 Atlassian
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.atlassian.fugue;
  14. import com.google.common.base.Function;
  15. import com.google.common.collect.ImmutableMap;
  16. import com.google.common.collect.Maps;
  17. import org.junit.Test;
  18. import javax.annotation.Nullable;
  19. import java.util.Arrays;
  20. import java.util.Map;
  21. import static org.junit.Assert.assertEquals;
  22. public class ImmutableMapsTest {
  23. @Test public void testConvertIterablesOfMapEntriesToMap() {
  24. @SuppressWarnings("unchecked")
  25. Iterable<Map.Entry<String, Integer>> source = Arrays
  26. .asList(Maps.immutableEntry("a", 1), Maps.immutableEntry("b", 2), Maps.immutableEntry("c", 3));
  27. ImmutableMap<String, Integer> expected = ImmutableMap.of("a", 1, "b", 2, "c", 3);
  28. assertEquals(expected, ImmutableMaps.toMap(source));
  29. }
  30. @Test public void testConvertIterablesOfMapEntriesToMapContainingNull() {
  31. @SuppressWarnings("unchecked")
  32. Iterable<Map.Entry<String, Integer>> source = Arrays.asList(Maps.immutableEntry("a", 1), null, Maps.immutableEntry("c", 3));
  33. ImmutableMap<String, Integer> expected = ImmutableMap.of("a", 1, "c", 3);
  34. assertEquals(expected, ImmutableMaps.toMap(source));
  35. }
  36. @Test public void testConvertIterablesOfMapEntriesToMapWithNullKey() {
  37. @SuppressWarnings("unchecked")
  38. Iterable<Map.Entry<String, Integer>> source = Arrays.asList(Maps.immutableEntry("a", 1), Maps.<String, Integer> immutableEntry(null, 2),
  39. Maps.immutableEntry("c", 3));
  40. ImmutableMap<String, Integer> expected = ImmutableMap.of("a", 1, "c", 3);
  41. assertEquals(expected, ImmutableMaps.toMap(source));
  42. }
  43. @Test public void testConvertIterablesOfMapEntriesToMapWithNullValue() {
  44. @SuppressWarnings("unchecked")
  45. Iterable<Map.Entry<String, Integer>> source = Arrays.asList(Maps.immutableEntry("a", 1), Maps.<String, Integer> immutableEntry("b", null),
  46. Maps.immutableEntry("c", 3));
  47. ImmutableMap<String, Integer> expected = ImmutableMap.of("a", 1, "c", 3);
  48. assertEquals(expected, ImmutableMaps.toMap(source));
  49. }
  50. // Allow override instead of throwing exceptions?
  51. @Test(expected = IllegalArgumentException.class) public void testConvertIterablesOfMapEntriesToMapWithDuplicateKey() {
  52. @SuppressWarnings("unchecked")
  53. Iterable<Map.Entry<String, Integer>> source = Arrays
  54. .asList(Maps.immutableEntry("a", 1), Maps.immutableEntry("b", 2), Maps.immutableEntry("b", 3));
  55. ImmutableMaps.toMap(source);
  56. }
  57. @Test public void testTransformIterablesToMap() {
  58. Iterable<Integer> source = Arrays.asList(1, 2, 3);
  59. ImmutableMap<String, Integer> expected = ImmutableMap.of("-1", -1, "-2", -2, "-3", -3);
  60. assertEquals(expected, ImmutableMaps.toMap(source, new Function<Integer, String>() {
  61. @Override public String apply(Integer input) {
  62. return "-" + input;
  63. }
  64. }, new Function<Integer, Integer>() {
  65. @Override public Integer apply(Integer input) {
  66. return input * -1;
  67. }
  68. }));
  69. }
  70. @Test public void testTransformIterablesToMapContainingNull() {
  71. Iterable<Integer> source = Arrays.asList(1, null, 3);
  72. ImmutableMap<String, Integer> expected = ImmutableMap.of("-1", -1, "-null", 0, "-3", -3);
  73. assertEquals(expected, ImmutableMaps.toMap(source, new Function<Integer, String>() {
  74. @Override public String apply(Integer input) {
  75. return "-" + input;
  76. }
  77. }, new Function<Integer, Integer>() {
  78. @Override public Integer apply(Integer input) {
  79. return input == null ? 0 : input * -1;
  80. }
  81. }));
  82. }
  83. @Test public void testTransformIterablesToMapGeneratingNullKey() {
  84. Iterable<Integer> source = Arrays.asList(1, null, 3);
  85. ImmutableMap<String, Integer> expected = ImmutableMap.of("-1", -1, "-3", -3);
  86. assertEquals(expected, ImmutableMaps.toMap(source, new Function<Integer, String>() {
  87. @Override public String apply(Integer input) {
  88. return input == null ? null : "-" + input;
  89. }
  90. }, new Function<Integer, Integer>() {
  91. @Override public Integer apply(Integer input) {
  92. return input == null ? 0 : input * -1;
  93. }
  94. }));
  95. }
  96. @Test public void testTransformIterablesToMapGeneratingNullValue() {
  97. Iterable<Integer> source = Arrays.asList(1, null, 3);
  98. ImmutableMap<String, Integer> expected = ImmutableMap.of("-1", -1, "-3", -3);
  99. assertEquals(expected, ImmutableMaps.toMap(source, new Function<Integer, String>() {
  100. @Override public String apply(Integer input) {
  101. return "-" + input;
  102. }
  103. }, new Function<Integer, Integer>() {
  104. @Override public Integer apply(Integer input) {
  105. return input == null ? null : input * -1;
  106. }
  107. }));
  108. }
  109. @Test(expected = IllegalArgumentException.class) public void testTransformIterablesToMapGeneratingDuplicateKey() {
  110. Iterable<Integer> source = Arrays.asList(1, 2, 3);
  111. ImmutableMaps.toMap(source, new Function<Integer, String>() {
  112. @Override public String apply(Integer input) {
  113. return String.valueOf(input % 2);
  114. }
  115. }, new Function<Integer, Integer>() {
  116. @Override public Integer apply(Integer input) {
  117. return input * -1;
  118. }
  119. });
  120. }
  121. @Test public void testMapBy() {
  122. Iterable<Integer> source = Arrays.asList(1, 2, 3);
  123. ImmutableMap<String, Integer> expected = ImmutableMap.of("+1", 1, "+2", 2, "+3", 3);
  124. assertEquals(expected, ImmutableMaps.mapBy(source, new Function<Integer, String>() {
  125. @Override public String apply(Integer input) {
  126. return "+" + input;
  127. }
  128. }));
  129. }
  130. @Test public void testMapByContainingNull() {
  131. Iterable<Integer> source = Arrays.asList(1, null, 3);
  132. ImmutableMap<String, Integer> expected = ImmutableMap.of("+1", 1, "+3", 3);
  133. assertEquals(expected, ImmutableMaps.mapBy(source, new Function<Integer, String>() {
  134. @Override public String apply(Integer input) {
  135. return "+" + input;
  136. }
  137. }));
  138. }
  139. @Test(expected = IllegalArgumentException.class) public void testMapByDuplicateKey() {
  140. Iterable<Integer> source = Arrays.asList(1, 2, 3);
  141. ImmutableMaps.mapBy(source, new Function<Integer, String>() {
  142. @Override public String apply(Integer input) {
  143. return "+" + (input % 2);
  144. }
  145. });
  146. }
  147. @Test public void testMapTo() {
  148. Iterable<Integer> source = Arrays.asList(1, 2, 3);
  149. ImmutableMap<Integer, String> expected = ImmutableMap.of(1, "+1", 2, "+2", 3, "+3");
  150. assertEquals(expected, ImmutableMaps.mapTo(source, new Function<Integer, String>() {
  151. @Override public String apply(Integer input) {
  152. return "+" + input;
  153. }
  154. }));
  155. }
  156. @Test public void testMapToContainingNull() {
  157. Iterable<Integer> source = Arrays.asList(1, null, 3);
  158. ImmutableMap<Integer, String> expected = ImmutableMap.of(1, "+1", 3, "+3");
  159. assertEquals(expected, ImmutableMaps.mapTo(source, new Function<Integer, String>() {
  160. @Override public String apply(Integer input) {
  161. return "+" + input;
  162. }
  163. }));
  164. }
  165. @Test(expected = IllegalArgumentException.class) public void testMapToContainingDuplicates() {
  166. Iterable<Integer> source = Arrays.asList(1, 2, 1);
  167. ImmutableMaps.mapTo(source, new Function<Integer, String>() {
  168. @Override public String apply(Integer input) {
  169. return "+" + input;
  170. }
  171. });
  172. }
  173. @Test public void testTransformEntries() {
  174. Map<String, Integer> source = ImmutableMap.of("a", 1, "b", 2, "c", 3);
  175. ImmutableMap<Integer, String> expected = ImmutableMap.of(2, "aa", 4, "bb", 6, "cc");
  176. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<String, Integer>, Map.Entry<Integer, String>>() {
  177. @Override public Map.Entry<Integer, String> apply(@Nullable Map.Entry<String, Integer> input) {
  178. return Maps.immutableEntry(input.getValue() * 2, input.getKey() + input.getKey());
  179. }
  180. }));
  181. }
  182. @Test public void testTransformEntriesContainingNullKey() {
  183. Map<String, Integer> source = Maps.newHashMap();
  184. source.put("a", 1);
  185. source.put(null, 2);
  186. source.put("c", 3);
  187. ImmutableMap<Integer, String> expected = ImmutableMap.of(2, "aa", 4, "nullnull", 6, "cc");
  188. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<String, Integer>, Map.Entry<Integer, String>>() {
  189. @Override public Map.Entry<Integer, String> apply(@Nullable Map.Entry<String, Integer> input) {
  190. return Maps.immutableEntry(input.getValue() * 2, input.getKey() + input.getKey());
  191. }
  192. }));
  193. }
  194. @Test public void testTransformEntriesContainingNullValue() {
  195. Map<Integer, String> source = Maps.newHashMap();
  196. source.put(1, "a");
  197. source.put(2, null);
  198. source.put(3, "c");
  199. ImmutableMap<String, Integer> expected = ImmutableMap.of("aa", 2, "nullnull", 4, "cc", 6);
  200. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<Integer, String>, Map.Entry<String, Integer>>() {
  201. @Override public Map.Entry<String, Integer> apply(@Nullable Map.Entry<Integer, String> input) {
  202. return Maps.immutableEntry(input.getValue() + input.getValue(), input.getKey() * 2);
  203. }
  204. }));
  205. }
  206. @Test public void testTransformEntriesReturningNull() {
  207. Map<Integer, String> source = ImmutableMap.of(1, "a", 2, "b", 3, "c");
  208. ImmutableMap<String, Integer> expected = ImmutableMap.of("aa", 2, "cc", 6);
  209. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<Integer, String>, Map.Entry<String, Integer>>() {
  210. @Override public Map.Entry<String, Integer> apply(@Nullable Map.Entry<Integer, String> input) {
  211. return input.getKey() % 2 == 0 ? null : Maps.immutableEntry(input.getValue() + input.getValue(), input.getKey() * 2);
  212. }
  213. }));
  214. }
  215. @Test public void testTransformEntriesReturningNullKey() {
  216. Map<Integer, String> source = ImmutableMap.of(1, "a", 2, "b", 3, "c");
  217. ImmutableMap<String, Integer> expected = ImmutableMap.of("aa", 2, "cc", 6);
  218. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<Integer, String>, Map.Entry<String, Integer>>() {
  219. @Override public Map.Entry<String, Integer> apply(@Nullable Map.Entry<Integer, String> input) {
  220. return Maps.immutableEntry(input.getKey() % 2 == 0 ? null : input.getValue() + input.getValue(), input.getKey() * 2);
  221. }
  222. }));
  223. }
  224. @Test public void testTransformEntriesReturningNullValue() {
  225. Map<Integer, String> source = ImmutableMap.of(1, "a", 2, "b", 3, "c");
  226. ImmutableMap<String, Integer> expected = ImmutableMap.of("aa", 2, "cc", 6);
  227. assertEquals(expected, ImmutableMaps.transform(source, new Function<Map.Entry<Integer, String>, Map.Entry<String, Integer>>() {
  228. @Override public Map.Entry<String, Integer> apply(@Nullable Map.Entry<Integer, String> input) {
  229. return Maps.immutableEntry(input.getValue() + input.getValue(), input.getKey() % 2 == 0 ? null : input.getKey() * 2);
  230. }
  231. }));
  232. }
  233. @Test(expected = IllegalArgumentException.class) public void testTransformEntriesReturningDuplicateKey() {
  234. Map<String, Integer> source = ImmutableMap.of("a", 1, "b", 2, "c", 3);
  235. ImmutableMaps.transform(source, new Function<Map.Entry<String, Integer>, Map.Entry<Integer, String>>() {
  236. @Override public Map.Entry<Integer, String> apply(@Nullable Map.Entry<String, Integer> input) {
  237. return Maps.immutableEntry(input.getValue() % 2, input.getKey() + input.getKey());
  238. }
  239. });
  240. }
  241. @Test public void testTransformKeysAndValues() {
  242. Map<String, Integer> source = ImmutableMap.of("a", 1, "bb", 2, "ccc", 3);
  243. ImmutableMap<Integer, Boolean> expected = ImmutableMap.of(1, true, 2, false, 3, true);
  244. assertEquals(expected, ImmutableMaps.transform(source, new Function<String, Integer>() {
  245. @Override public Integer apply(@Nullable String input) {
  246. return input == null ? 0 : input.length();
  247. }
  248. }, new Function<Integer, Boolean>() {
  249. @Override public Boolean apply(@Nullable Integer input) {
  250. return input != null && input % 2 != 0;
  251. }
  252. }));
  253. }
  254. @Test public void testTransformKeysAndValuesContainingNullKey() {
  255. Map<String, Integer> source = Maps.newHashMap();
  256. source.put("a", 1);
  257. source.put(null, 2);
  258. source.put("ccc", 3);
  259. ImmutableMap<Integer, Boolean> expected = ImmutableMap.of(1, true, 0, false, 3, true);
  260. assertEquals(expected, ImmutableMaps.transform(source, new Function<String, Integer>() {
  261. @Override public Integer apply(@Nullable String input) {
  262. return input == null ? 0 : input.length();
  263. }
  264. }, new Function<Integer, Boolean>() {
  265. @Override public Boolean apply(@Nullable Integer input) {
  266. return input != null && input % 2 != 0;
  267. }
  268. }));
  269. }
  270. @Test public void testTransformKeysAndValuesContainingNullValue() {
  271. Map<Integer, String> source = Maps.newHashMap();
  272. source.put(1, "a");
  273. source.put(2, null);
  274. source.put(3, "ccc");
  275. ImmutableMap<String, Integer> expected = ImmutableMap.of("(1)", 1, "(2)", 0, "(3)", 3);
  276. assertEquals(expected, ImmutableMaps.transform(source, new Function<Integer, String>() {
  277. @Override public String apply(@Nullable Integer input) {
  278. return "(" + input + ")";
  279. }
  280. }, new Function<String, Integer>() {
  281. @Override public Integer apply(@Nullable String input) {
  282. return input == null ? 0 : input.length();
  283. }
  284. }));
  285. }
  286. @Test public void testTransformKeysAndValuesReturningNullKey() {
  287. Map<String, Integer> source = Maps.newHashMap();
  288. source.put("a", 1);
  289. source.put(null, 2);
  290. source.put("ccc", 3);
  291. ImmutableMap<Integer, Boolean> expected = ImmutableMap.of(1, true, 3, true);
  292. assertEquals(expected, ImmutableMaps.transform(source, new Function<String, Integer>() {
  293. @Override public Integer apply(@Nullable String input) {
  294. return input == null ? null : input.length();
  295. }
  296. }, new Function<Integer, Boolean>() {
  297. @Override public Boolean apply(@Nullable Integer input) {
  298. return input != null && input % 2 != 0;
  299. }
  300. }));
  301. }
  302. @Test public void testTransformKeysAndValuesReturningNullValue() {
  303. Map<Integer, String> source = Maps.newHashMap();
  304. source.put(1, "a");
  305. source.put(2, null);
  306. source.put(3, "ccc");
  307. ImmutableMap<String, Integer> expected = ImmutableMap.of("(1)", 1, "(3)", 3);
  308. assertEquals(expected, ImmutableMaps.transform(source, new Function<Integer, String>() {
  309. @Override public String apply(@Nullable Integer input) {
  310. return "(" + input + ")";
  311. }
  312. }, new Function<String, Integer>() {
  313. @Override public Integer apply(@Nullable String input) {
  314. return input == null ? null : input.length();
  315. }
  316. }));
  317. }
  318. @Test(expected = IllegalArgumentException.class) public void testTransformKeysAndValuesReturningDuplicateKey() {
  319. Map<Integer, String> source = ImmutableMap.of(1, "a", 2, "b", 3, "c");
  320. ImmutableMaps.transform(source, new Function<Integer, Boolean>() {
  321. @Override public Boolean apply(@Nullable Integer input) {
  322. return input != null && input % 2 != 0;
  323. }
  324. }, new Function<String, Integer>() {
  325. @Override public Integer apply(@Nullable String input) {
  326. return input == null ? 0 : input.length();
  327. }
  328. });
  329. }
  330. @Test public void testTransformKey() {
  331. Map<String, Integer> source = ImmutableMap.of("a", 10, "bb", 20, "ccc", 30);
  332. ImmutableMap<Integer, Integer> expected = ImmutableMap.of(1, 10, 2, 20, 3, 30);
  333. assertEquals(expected, ImmutableMaps.transformKey(source, new Function<String, Integer>() {
  334. @Override public Integer apply(@Nullable String input) {
  335. return input == null ? 0 : input.length();
  336. }
  337. }));
  338. }
  339. @Test public void testTransformKeyContainingNullKey() {
  340. Map<String, Integer> source = Maps.newHashMap();
  341. source.put("a", 10);
  342. source.put(null, 20);
  343. source.put("ccc", 30);
  344. ImmutableMap<Integer, Integer> expected = ImmutableMap.of(1, 10, 0, 20, 3, 30);
  345. assertEquals(expected, ImmutableMaps.transformKey(source, new Function<String, Integer>() {
  346. @Override public Integer apply(@Nullable String input) {
  347. return input == null ? 0 : input.length();
  348. }
  349. }));
  350. }
  351. @Test public void testTransformKeyContainingNullValue() {
  352. Map<Integer, String> source = Maps.newHashMap();
  353. source.put(1, "a");
  354. source.put(2, null);
  355. source.put(3, "ccc");
  356. ImmutableMap<String, String> expected = ImmutableMap.of("(1)", "a", "(3)", "ccc");
  357. assertEquals(expected, ImmutableMaps.transformKey(source, new Function<Integer, String>() {
  358. @Override public String apply(@Nullable Integer input) {
  359. return "(" + input + ")";
  360. }
  361. }));
  362. }
  363. @Test public void testTransformKeyReturningNullKey() {
  364. Map<String, Integer> source = Maps.newHashMap();
  365. source.put("a", 11);
  366. source.put(null, 12);
  367. source.put("ccc", 13);
  368. ImmutableMap<Integer, Integer> expected = ImmutableMap.of(1, 11, 3, 13);
  369. assertEquals(expected, ImmutableMaps.transformKey(source, new Function<String, Integer>() {
  370. @Override public Integer apply(@Nullable String input) {
  371. return input == null ? null : input.length();
  372. }
  373. }));
  374. }
  375. @Test(expected = IllegalArgumentException.class) public void testTransformKeyReturningDuplicateKey() {
  376. Map<Integer, String> source = ImmutableMap.of(1, "a", 2, "b", 3, "c");
  377. ImmutableMaps.transformKey(source, new Function<Integer, Boolean>() {
  378. @Override public Boolean apply(@Nullable Integer input) {
  379. return input != null && input % 2 != 0;
  380. }
  381. });
  382. }
  383. @Test public void testTransformValue() {
  384. Map<String, Integer> source = ImmutableMap.of("a", 1, "bb", 2, "ccc", 3);
  385. ImmutableMap<String, Boolean> expected = ImmutableMap.of("a", true, "bb", false, "ccc", true);
  386. assertEquals(expected, ImmutableMaps.transformValue(source, new Function<Integer, Boolean>() {
  387. @Override public Boolean apply(@Nullable Integer input) {
  388. return input != null && input % 2 != 0;
  389. }
  390. }));
  391. }
  392. @Test public void testTransformValueContainingNullKey() {
  393. Map<String, Integer> source = Maps.newHashMap();
  394. source.put("a", 1);
  395. source.put(null, 2);
  396. source.put("ccc", 3);
  397. ImmutableMap<String, Boolean> expected = ImmutableMap.of("a", true, "ccc", true);
  398. assertEquals(expected, ImmutableMaps.transformValue(source, new Function<Integer, Boolean>() {
  399. @Override public Boolean apply(@Nullable Integer input) {
  400. return input != null && input % 2 != 0;
  401. }
  402. }));
  403. }
  404. @Test public void testTransformValueContainingNullValue() {
  405. Map<Integer, String> source = Maps.newHashMap();
  406. source.put(11, "a");
  407. source.put(12, null);
  408. source.put(13, "ccc");
  409. ImmutableMap<Integer, Integer> expected = ImmutableMap.of(11, 1, 12, 0, 13, 3);
  410. assertEquals(expected, ImmutableMaps.transformValue(source, new Function<String, Integer>() {
  411. @Override public Integer apply(@Nullable String input) {
  412. return input == null ? 0 : input.length();
  413. }
  414. }));
  415. }
  416. @Test public void testTransformValueReturningNullValue() {
  417. Map<Integer, String> source = Maps.newHashMap();
  418. source.put(11, "a");
  419. source.put(12, null);
  420. source.put(13, "ccc");
  421. ImmutableMap<Integer, Integer> expected = ImmutableMap.of(11, 1, 13, 3);
  422. assertEquals(expected, ImmutableMaps.transformValue(source, new Function<String, Integer>() {
  423. @Override public Integer apply(@Nullable String input) {
  424. return input == null ? null : input.length();
  425. }
  426. }));
  427. }
  428. }