TablesTransformValuesTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect;
18
19import static com.google.common.base.Preconditions.checkArgument;
20
21import com.google.common.annotations.GwtCompatible;
22import com.google.common.annotations.GwtIncompatible;
23import com.google.common.base.Function;
24
25/**
26 * Test cases for {@link Tables#transformValues}.
27 *
28 * @author Jared Levy
29 */
30@GwtCompatible(emulated = true)
31public class TablesTransformValuesTest extends AbstractTableTest {
32
33  private static final Function<String, Character> FIRST_CHARACTER
34    = new Function<String, Character>() {
35      @Override public Character apply(String input) {
36        return input == null ? null : input.charAt(0);
37      }
38  };
39
40  @Override protected Table<String, Integer, Character> create(
41      Object... data) {
42    Table<String, Integer, String> table = HashBasedTable.create();
43    checkArgument(data.length % 3 == 0);
44    for (int i = 0; i < data.length; i+= 3) {
45      String value = (data[i+2] == null) ? null : data[i+2].toString() + "transformed";
46      table.put((String) data[i], (Integer) data[i+1], value);
47    }
48    return Tables.transformValues(table, FIRST_CHARACTER);
49  }
50
51  // Null support depends on the underlying table and function.
52  @GwtIncompatible("NullPointerTester")
53  @Override public void testNullPointerInstance() throws Exception {}
54
55  // put() and putAll() aren't supported.
56  @Override public void testPut() {
57    try {
58      table.put("foo", 1, 'a');
59      fail("Expected UnsupportedOperationException");
60    } catch (UnsupportedOperationException expected) {}
61    assertSize(0);
62  }
63
64  @Override public void testPutAllTable() {
65    table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
66    Table<String, Integer, Character> other = HashBasedTable.create();
67    other.put("foo", 1, 'd');
68    other.put("bar", 2, 'e');
69    other.put("cat", 2, 'f');
70    try {
71      table.putAll(other);
72      fail("Expected UnsupportedOperationException");
73    } catch (UnsupportedOperationException expected) {}
74    assertEquals((Character) 'a', table.get("foo", 1));
75    assertEquals((Character) 'b', table.get("bar", 1));
76    assertEquals((Character) 'c', table.get("foo", 3));
77    assertSize(3);
78  }
79
80  @Override public void testPutNull() {}
81  @Override public void testPutNullReplace() {}
82  @Override public void testRowClearAndPut() {}
83}
84