|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package javax.fory.test; |
| 21 | + |
| 22 | +import static org.testng.Assert.assertThrows; |
| 23 | +import static org.testng.Assert.assertTrue; |
| 24 | + |
| 25 | +import java.util.AbstractList; |
| 26 | +import java.util.AbstractMap; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import org.apache.fory.Fory; |
| 32 | +import org.apache.fory.memory.MemoryBuffer; |
| 33 | +import org.apache.fory.serializer.Serializer; |
| 34 | +import org.apache.fory.serializer.collection.AbstractCollectionSerializer; |
| 35 | +import org.apache.fory.serializer.collection.AbstractMapSerializer; |
| 36 | +import org.testng.annotations.Test; |
| 37 | + |
| 38 | +public class ResolverValidateSerializerTest { |
| 39 | + static final class InvalidList extends AbstractList<Object> { |
| 40 | + @Override |
| 41 | + public Object get(int index) { |
| 42 | + throw new IndexOutOfBoundsException(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public int size() { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + public static final class InvalidListSerializer extends Serializer<InvalidList> { |
| 51 | + public InvalidListSerializer(Fory fory) { |
| 52 | + super(fory, InvalidList.class); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void write(MemoryBuffer buffer, InvalidList value) { |
| 57 | + // no-op |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public InvalidList read(MemoryBuffer buffer) { |
| 62 | + return new InvalidList(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + static final class ValidList extends AbstractList<Object> { |
| 68 | + @Override |
| 69 | + public Object get(int index) { |
| 70 | + throw new IndexOutOfBoundsException(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int size() { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + public static final class ValidListSerializer extends AbstractCollectionSerializer<ValidList> { |
| 79 | + public ValidListSerializer(Fory fory) { |
| 80 | + super(fory, ValidList.class); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Collection<?> onCollectionWrite(MemoryBuffer buffer, ValidList value) { |
| 85 | + return Collections.emptyList(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ValidList read(MemoryBuffer buffer) { |
| 90 | + return onCollectionRead(Collections.emptyList()); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public ValidList onCollectionRead(Collection collection) { |
| 95 | + return new ValidList(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + static final class InvalidMap extends AbstractMap<Object, Object> { |
| 101 | + @Override |
| 102 | + public Set<Entry<Object, Object>> entrySet() { |
| 103 | + return Collections.emptySet(); |
| 104 | + } |
| 105 | + |
| 106 | + public static final class InvalidMapSerializer extends Serializer<InvalidMap> { |
| 107 | + public InvalidMapSerializer(Fory fory) { |
| 108 | + super(fory, InvalidMap.class); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void write(MemoryBuffer buffer, InvalidMap value) { |
| 113 | + // no-op |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public InvalidMap read(MemoryBuffer buffer) { |
| 118 | + return new InvalidMap(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + static final class ValidMap extends AbstractMap<Object, Object> { |
| 124 | + @Override |
| 125 | + public Set<Entry<Object, Object>> entrySet() { |
| 126 | + return Collections.emptySet(); |
| 127 | + } |
| 128 | + |
| 129 | + public static final class ValidMapSerializer extends AbstractMapSerializer<ValidMap> { |
| 130 | + public ValidMapSerializer(Fory fory) { |
| 131 | + super(fory, ValidMap.class); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Map<?, ?> onMapWrite(MemoryBuffer buffer, ValidMap value) { |
| 136 | + return Collections.emptyMap(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public ValidMap onMapCopy(Map map) { |
| 141 | + return new ValidMap(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public ValidMap read(MemoryBuffer buffer) { |
| 146 | + return onMapRead(Collections.emptyMap()); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public ValidMap onMapRead(Map map) { |
| 151 | + return new ValidMap(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testListAndMapSerializerRegistration() { |
| 158 | + Fory fory = Fory.builder().withRefTracking(true).requireClassRegistration(false).build(); |
| 159 | + // List invalid |
| 160 | + assertThrows( |
| 161 | + IllegalArgumentException.class, |
| 162 | + () -> fory.registerSerializer(InvalidList.class, InvalidList.InvalidListSerializer.class)); |
| 163 | + assertThrows( |
| 164 | + IllegalArgumentException.class, |
| 165 | + () -> |
| 166 | + fory.registerSerializer( |
| 167 | + InvalidList.class, new InvalidList.InvalidListSerializer(fory))); |
| 168 | + assertThrows( |
| 169 | + IllegalArgumentException.class, |
| 170 | + () -> |
| 171 | + fory.registerSerializer( |
| 172 | + InvalidList.class, f -> new InvalidList.InvalidListSerializer(f))); |
| 173 | + // List valid |
| 174 | + fory.register(ValidList.class); |
| 175 | + fory.registerSerializer(ValidList.class, new ValidList.ValidListSerializer(fory)); |
| 176 | + Object listResult = fory.deserialize(fory.serialize(new ValidList())); |
| 177 | + assertTrue(listResult instanceof ValidList); |
| 178 | + // Map invalid |
| 179 | + assertThrows( |
| 180 | + IllegalArgumentException.class, |
| 181 | + () -> fory.registerSerializer(InvalidMap.class, InvalidMap.InvalidMapSerializer.class)); |
| 182 | + assertThrows( |
| 183 | + IllegalArgumentException.class, |
| 184 | + () -> fory.registerSerializer(InvalidMap.class, new InvalidMap.InvalidMapSerializer(fory))); |
| 185 | + assertThrows( |
| 186 | + IllegalArgumentException.class, |
| 187 | + () -> |
| 188 | + fory.registerSerializer(InvalidMap.class, f -> new InvalidMap.InvalidMapSerializer(f))); |
| 189 | + // Map valid |
| 190 | + fory.register(ValidMap.class); |
| 191 | + fory.registerSerializer(ValidMap.class, new ValidMap.ValidMapSerializer(fory)); |
| 192 | + Object mapResult = fory.deserialize(fory.serialize(new ValidMap())); |
| 193 | + assertTrue(mapResult instanceof ValidMap); |
| 194 | + } |
| 195 | +} |
0 commit comments