| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.util.ajax; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Collections; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.HashSet; |
| 25 |
import java.util.LinkedList; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import java.util.Set; |
| 29 |
import java.util.TreeSet; |
| 30 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 31 |
|
| 32 |
import org.junit.jupiter.api.Test; |
| 33 |
|
| 34 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 35 |
import static org.hamcrest.Matchers.containsString; |
| 36 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 |
import static org.junit.jupiter.api.Assertions.assertSame; |
| 38 |
|
| 39 |
public class JSONCollectionConvertorTest |
| 40 |
{ |
| 41 |
@Test |
| 42 |
public void testArrayList() throws Exception |
| 43 |
{ |
| 44 |
List<String> list = new ArrayList<String>(); |
| 45 |
Collections.addAll(list, "one", "two"); |
| 46 |
testList(list); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void testLinkedList() throws Exception |
| 51 |
{ |
| 52 |
List<String> list = new LinkedList<String>(); |
| 53 |
Collections.addAll(list, "one", "two"); |
| 54 |
testList(list); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void testCopyOnWriteArrayList() throws Exception |
| 59 |
{ |
| 60 |
List<String> list = new CopyOnWriteArrayList<String>(); |
| 61 |
Collections.addAll(list, "one", "two"); |
| 62 |
testList(list); |
| 63 |
} |
| 64 |
|
| 65 |
private void testList(List<String> list1) throws Exception |
| 66 |
{ |
| 67 |
JSON json = new JSON(); |
| 68 |
json.addConvertor(List.class, new JSONCollectionConvertor()); |
| 69 |
|
| 70 |
Map<String, Object> object1 = new HashMap<String, Object>(); |
| 71 |
String field = "field"; |
| 72 |
object1.put(field, list1); |
| 73 |
|
| 74 |
String string = json.toJSON(object1); |
| 75 |
assertThat(string, containsString(list1.getClass().getName())); |
| 76 |
|
| 77 |
@SuppressWarnings("unchecked") |
| 78 |
Map<String, Object> object2 = (Map<String, Object>)json.parse(new JSON.StringSource(string)); |
| 79 |
@SuppressWarnings("unchecked") |
| 80 |
List<String> list2 = (List<String>)object2.get(field); |
| 81 |
|
| 82 |
assertSame(list1.getClass(), list2.getClass()); |
| 83 |
assertEquals(list1, list2); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void testHashSet() throws Exception |
| 88 |
{ |
| 89 |
Set<String> set = new HashSet<String>(); |
| 90 |
Collections.addAll(set, "one", "two", "three"); |
| 91 |
testSet(set); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void testTreeSet() throws Exception |
| 96 |
{ |
| 97 |
Set<String> set = new TreeSet<String>(); |
| 98 |
Collections.addAll(set, "one", "two", "three"); |
| 99 |
testSet(set); |
| 100 |
} |
| 101 |
|
| 102 |
private void testSet(Set<String> set1) |
| 103 |
{ |
| 104 |
JSON json = new JSON(); |
| 105 |
json.addConvertor(Set.class, new JSONCollectionConvertor()); |
| 106 |
|
| 107 |
String string = json.toJSON(set1); |
| 108 |
assertThat(string, containsString(set1.getClass().getName())); |
| 109 |
|
| 110 |
@SuppressWarnings("unchecked") |
| 111 |
Set<String> set2 = (Set<String>)json.parse(new JSON.StringSource(string)); |
| 112 |
|
| 113 |
assertSame(set1.getClass(), set2.getClass()); |
| 114 |
assertEquals(set1, set2); |
| 115 |
} |
| 116 |
} |