Protected { public static <J extends JsonDdlObject<J>> J object(J value) { if (value instanceof JsonDdlObject.Builder) { return ((JsonDdlObject.Builder<J>) value).build(); } return value; }  static J object(J value); static List<T> object(List<T> values); static Map<K, V> object(Map<K, V> values); @SuppressWarnings("unchecked") static T object(T value);  }
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testJsonDdlObject() { JsonDdlObject<?> obj = createStrictMock(JsonDdlObject.class); JsonDdlObject.Builder builder = createStrictMock(JsonDdlObject.Builder.class); expect(builder.build()).andReturn(obj).times(2); replay(obj, builder); assertSame(obj, Protected.object((Object) obj)); assertSame(obj, Protected.object((Object) builder)); assertSame(obj, Protected.object((JsonDdlObject) obj)); assertSame(obj, Protected.object((JsonDdlObject) builder)); verify(obj, builder); }
@Test public void testList() { assertNull(Protected.object((List<?>) null)); assertSame(Collections.emptyList(), Protected.object(Collections.emptyList())); assertEquals(Collections.singletonList("a").getClass(), Protected.object(Arrays.asList("b")) .getClass()); List<String> input = Arrays.asList("a", "b", "c"); List<String> output = Protected.object(input); assertEquals(input, output); assertNotSame(input, output); assertSame(output, Protected.object(output)); assertNotSame(output, Protected.object(input)); input.set(0, "b"); assertEquals("a", output.get(0)); try { output.set(0, "b"); fail(); } catch (UnsupportedOperationException expected) {} }
@Test public void testMap() { assertNull(Protected.object((Map<?, ?>) null)); assertSame(Collections.emptyMap(), Protected.object(Collections.emptyMap())); Map<String, String> singletonMap = new HashMap<String, String>(); singletonMap.put("a", "b"); assertEquals(Collections.singletonMap("a", "b").getClass(), Protected.object(singletonMap) .getClass()); Map<String, String> input = new HashMap<String, String>(); input.put("a", "b"); input.put("c", "d"); input.put("e", "f"); Map<String, String> output = Protected.object(input); assertEquals(input, output); assertNotSame(input, output); assertSame(output, Protected.object(output)); assertNotSame(output, Protected.object(input)); input.put("a", "q"); assertEquals("b", output.get("a")); try { output.put("a", "b"); fail(); } catch (UnsupportedOperationException expected) {} }
@Test @SuppressWarnings("unchecked") public void testMixedTypes() { List<Object> input = Arrays.<Object> asList(null, true, 1, 1.0, "Hello world", ElementType.METHOD, Arrays.asList(1, 2, 3), Collections.singletonMap("a", "b")); List<Object> output = Protected.object(input); assertEquals(input, output); try { ((List<Object>) output.get(6)).set(0, 0); fail(); } catch (UnsupportedOperationException expected) {} try { ((Map<Object, Object>) output.get(7)).put(0, 0); fail(); } catch (UnsupportedOperationException expected) {} }
@Test public void testUnknown() { try { Protected.object(new Object()); fail(); } catch (IllegalArgumentException expected) {} }