|
| 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 org.apache.fory.codegen.pkgprivate; |
| 21 | + |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | + |
| 24 | +import java.io.Serializable; |
| 25 | +import java.util.EnumMap; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.LinkedHashSet; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import org.apache.fory.Fory; |
| 32 | +import org.apache.fory.ThreadSafeFory; |
| 33 | +import org.apache.fory.config.Language; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +/** Regression test for codegen CompileException when map key/value types are package-private. */ |
| 37 | +public class PackagePrivateMapKeyTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testCodegenForMapWithPackagePrivateEnumKey() { |
| 41 | + ThreadSafeFory fury = |
| 42 | + Fory.builder() |
| 43 | + .withLanguage(Language.JAVA) |
| 44 | + .requireClassRegistration(false) |
| 45 | + .withRefTracking(true) |
| 46 | + .buildThreadSafeFory(); |
| 47 | + |
| 48 | + ReproContainer container = new ReproContainer("v1"); |
| 49 | + ReproNode parent = new ReproNode(ReproType.TYPE_A, "p1"); |
| 50 | + ReproNode child = new ReproNode(ReproType.TYPE_B, "c1"); |
| 51 | + parent.children.add(child); |
| 52 | + child.parents.computeIfAbsent(parent.type, k -> new LinkedHashSet<>()).add(parent); |
| 53 | + container.nodes.computeIfAbsent(ReproType.TYPE_A, k -> new HashMap<>()).put("p1", parent); |
| 54 | + container.nodes.computeIfAbsent(ReproType.TYPE_B, k -> new HashMap<>()).put("c1", child); |
| 55 | + |
| 56 | + byte[] bytes = fury.serialize(container); |
| 57 | + ReproContainer result = (ReproContainer) fury.deserialize(bytes); |
| 58 | + assertEquals(result.version, container.version); |
| 59 | + assertEquals(result.nodes.size(), container.nodes.size()); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// All package-private — this triggers the bug |
| 64 | +enum ReproType implements Serializable { |
| 65 | + TYPE_A, |
| 66 | + TYPE_B |
| 67 | +} |
| 68 | + |
| 69 | +class ReproNode implements Serializable { |
| 70 | + final ReproType type; |
| 71 | + final String id; |
| 72 | + final Set<ReproNode> children = new HashSet<>(); |
| 73 | + final Map<ReproType, Set<ReproNode>> parents = new EnumMap<>(ReproType.class); |
| 74 | + |
| 75 | + ReproNode(ReproType type, String id) { |
| 76 | + this.type = type; |
| 77 | + this.id = id; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class ReproContainer implements Serializable { |
| 82 | + final Map<ReproType, Map<String, ReproNode>> nodes = new EnumMap<>(ReproType.class); |
| 83 | + final String version; |
| 84 | + |
| 85 | + ReproContainer(String version) { |
| 86 | + this.version = version; |
| 87 | + } |
| 88 | +} |
0 commit comments