View Javadoc
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  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.base.Function;
23  
24  /**
25   * Test cases for {@link Tables#transformValues}.
26   *
27   * @author Jared Levy
28   */
29  @GwtCompatible(emulated = true)
30  public class TablesTransformValuesTest extends AbstractTableTest {
31  
32    private static final Function<String, Character> FIRST_CHARACTER
33      = new Function<String, Character>() {
34        @Override public Character apply(String input) {
35          return input == null ? null : input.charAt(0);
36        }
37    };
38  
39    @Override protected Table<String, Integer, Character> create(
40        Object... data) {
41      Table<String, Integer, String> table = HashBasedTable.create();
42      checkArgument(data.length % 3 == 0);
43      for (int i = 0; i < data.length; i += 3) {
44        String value =
45            (data[i + 2] == null) ? null : (data[i + 2] + "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  
53    // put() and putAll() aren't supported.
54    @Override public void testPut() {
55      try {
56        table.put("foo", 1, 'a');
57        fail("Expected UnsupportedOperationException");
58      } catch (UnsupportedOperationException expected) {}
59      assertSize(0);
60    }
61  
62    @Override public void testPutAllTable() {
63      table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
64      Table<String, Integer, Character> other = HashBasedTable.create();
65      other.put("foo", 1, 'd');
66      other.put("bar", 2, 'e');
67      other.put("cat", 2, 'f');
68      try {
69        table.putAll(other);
70        fail("Expected UnsupportedOperationException");
71      } catch (UnsupportedOperationException expected) {}
72      assertEquals((Character) 'a', table.get("foo", 1));
73      assertEquals((Character) 'b', table.get("bar", 1));
74      assertEquals((Character) 'c', table.get("foo", 3));
75      assertSize(3);
76    }
77  
78    @Override public void testPutNull() {}
79    @Override public void testPutNullReplace() {}
80    @Override public void testRowClearAndPut() {}
81  }
82