• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.MapConstraintsTest.TestKeyException;
22 import com.google.common.collect.MapConstraintsTest.TestValueException;
23 import com.google.common.collect.testing.MapTestSuiteBuilder;
24 import com.google.common.collect.testing.TestStringMapGenerator;
25 import com.google.common.collect.testing.features.CollectionFeature;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28 
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 
37 /**
38  * Tests for {@link MapConstraints#constrainedMap}.
39  *
40  * @author Jared Levy
41  * @author Louis Wasserman
42  */
43 @GwtCompatible(emulated = true)
44 public class ConstrainedMapTest extends TestCase {
45 
46   private static final String TEST_KEY = "42";
47   private static final String TEST_VALUE = "test";
48   private static final MapConstraint<String, String> TEST_CONSTRAINT = new TestConstraint();
49 
50   @GwtIncompatible("suite")
suite()51   public static Test suite() {
52     TestSuite suite = new TestSuite();
53     suite.addTest(MapTestSuiteBuilder
54         .using(new ConstrainedMapGenerator())
55         .named("Maps.constrainedMap[HashMap]")
56         .withFeatures(
57             CollectionSize.ANY,
58             MapFeature.ALLOWS_NULL_KEYS,
59             MapFeature.ALLOWS_NULL_VALUES,
60             MapFeature.ALLOWS_ANY_NULL_QUERIES,
61             MapFeature.GENERAL_PURPOSE,
62             CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
63         .createTestSuite());
64     suite.addTestSuite(ConstrainedMapTest.class);
65     return suite;
66   }
67 
testPutWithForbiddenKeyForbiddenValue()68   public void testPutWithForbiddenKeyForbiddenValue() {
69     Map<String, String> map = MapConstraints.constrainedMap(
70         new HashMap<String, String>(),
71         TEST_CONSTRAINT);
72     try {
73       map.put(TEST_KEY, TEST_VALUE);
74       fail("Expected IllegalArgumentException");
75     } catch (IllegalArgumentException expected) {
76       // success
77     }
78   }
79 
testPutWithForbiddenKeyAllowedValue()80   public void testPutWithForbiddenKeyAllowedValue() {
81     Map<String, String> map = MapConstraints.constrainedMap(
82         new HashMap<String, String>(),
83         TEST_CONSTRAINT);
84     try {
85       map.put(TEST_KEY, "allowed");
86       fail("Expected IllegalArgumentException");
87     } catch (IllegalArgumentException expected) {
88       // success
89     }
90   }
91 
testPutWithAllowedKeyForbiddenValue()92   public void testPutWithAllowedKeyForbiddenValue() {
93     Map<String, String> map = MapConstraints.constrainedMap(
94         new HashMap<String, String>(),
95         TEST_CONSTRAINT);
96     try {
97       map.put("allowed", TEST_VALUE);
98       fail("Expected IllegalArgumentException");
99     } catch (IllegalArgumentException expected) {
100       // success
101     }
102   }
103 
104   public static final class ConstrainedMapGenerator extends TestStringMapGenerator {
105     @Override
create(Entry<String, String>[] entries)106     protected Map<String, String> create(Entry<String, String>[] entries) {
107       Map<String, String> map = MapConstraints.constrainedMap(
108           new HashMap<String, String>(),
109           TEST_CONSTRAINT);
110       for (Entry<String, String> entry : entries) {
111         map.put(entry.getKey(), entry.getValue());
112       }
113       return map;
114     }
115   }
116 
117   private static final class TestConstraint implements MapConstraint<String, String> {
118     @Override
checkKeyValue(String key, String value)119     public void checkKeyValue(String key, String value) {
120       if (TEST_KEY.equals(key)) {
121         throw new TestKeyException();
122       }
123       if (TEST_VALUE.equals(value)) {
124         throw new TestValueException();
125       }
126     }
127 
128     private static final long serialVersionUID = 0;
129   }
130 }
131