-
Notifications
You must be signed in to change notification settings - Fork 102
/
RecordConstructorValueTest.java
151 lines (129 loc) · 7.05 KB
/
RecordConstructorValueTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* RecordConstructorValueTest.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2015-2022 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apple.foundationdb.record.query.plan.cascades.values;
import com.apple.foundationdb.record.query.plan.cascades.typing.Type;
import com.apple.foundationdb.record.query.plan.cascades.typing.TypeRepository;
import com.google.protobuf.Descriptors;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Test cases to test {@link RecordConstructorValue#deepCopyIfNeeded}.
*/
public class RecordConstructorValueTest {
private static final String[] suites = new String[] {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
private static Type.Enum getCardsEnum() {
final var enumValues = new ArrayList<Type.Enum.EnumValue>();
for (var i = 0; i < suites.length; i++) {
enumValues.add(new Type.Enum.EnumValue(suites[i], i));
}
return new Type.Enum(false, enumValues, "enumType");
}
@Test
public void deepCopyMessageWithEnumTest() {
final var enumType = getCardsEnum();
var type = Type.Record.fromFieldsWithName("simpleType", false, List.of(Type.Record.Field.of(enumType, Optional.of("suit"))));
var repo = TypeRepository.newBuilder();
enumType.defineProtoType(repo);
type.defineProtoType(repo);
final var typeRepoSrc = repo.build();
final var typeRepoTarget = repo.build();
final var typeDescriptor = typeRepoSrc.getMessageDescriptor("simpleType");
var messageBuilder = DynamicMessage.newBuilder(Objects.requireNonNull(typeDescriptor));
var enumFieldSrc = typeDescriptor.findFieldByName("suit");
messageBuilder.setField(enumFieldSrc, Objects.requireNonNull(typeRepoSrc.getEnumValue("enumType", "SPADES")));
var message = messageBuilder.build();
var copiedMessage = RecordConstructorValue.deepCopyIfNeeded(typeRepoTarget, type, message);
Assertions.assertNotNull(copiedMessage);
Assertions.assertNotSame(message, copiedMessage);
var enumFieldTargetValue = ((Message)copiedMessage).getField(Objects.requireNonNull(typeRepoTarget.getMessageDescriptor("simpleType")).findFieldByName("suit"));
Assertions.assertTrue(enumFieldTargetValue instanceof Descriptors.EnumValueDescriptor);
Assertions.assertEquals(((Descriptors.EnumValueDescriptor) message.getField(enumFieldSrc)).getName(), ((Descriptors.EnumValueDescriptor) enumFieldTargetValue).getName());
Assertions.assertNotSame(message.getField(enumFieldSrc), enumFieldTargetValue);
}
@Test
public void deepCopyEnumValueTest() {
final var enumType = getCardsEnum();
var repo = TypeRepository.newBuilder();
enumType.defineProtoType(repo);
final var typeRepoSrc = repo.build();
final var typeRepoTarget = repo.build();
final var enumValueSrc = typeRepoSrc.getEnumValue("enumType", "SPADES");
final var copied = RecordConstructorValue.deepCopyIfNeeded(typeRepoTarget, enumType, enumValueSrc);
Assertions.assertTrue(copied instanceof Descriptors.EnumValueDescriptor);
Assertions.assertEquals(enumValueSrc.getName(), ((Descriptors.EnumValueDescriptor) copied).getName());
Assertions.assertNotSame(enumValueSrc, copied);
}
@Test
public void deepCopyMessageWithRepeatedEnumValueTest() {
final var enumType = getCardsEnum();
var arrayType = new Type.Array(false, enumType);
var type = Type.Record.fromFieldsWithName("simpleType", false, List.of(Type.Record.Field.of(arrayType, Optional.of("suits"))));
var repo = TypeRepository.newBuilder();
enumType.defineProtoType(repo);
arrayType.defineProtoType(repo);
type.defineProtoType(repo);
final var typeRepoSrc = repo.build();
final var typeRepoTarget = repo.build();
final var typeDescriptor = typeRepoSrc.getMessageDescriptor("simpleType");
var messageBuilder = DynamicMessage.newBuilder(Objects.requireNonNull(typeDescriptor));
var enumFieldSrc = typeDescriptor.findFieldByName("suits");
for (int i = suites.length - 1; i >= 0; i--) {
messageBuilder.addRepeatedField(enumFieldSrc, Objects.requireNonNull(typeRepoSrc.getEnumValue("enumType", suites[i])));
}
var message = messageBuilder.build();
var copiedMessage = RecordConstructorValue.deepCopyIfNeeded(typeRepoTarget, type, message);
Assertions.assertNotNull(copiedMessage);
var enumFieldTarget = Objects.requireNonNull(typeRepoTarget.getMessageDescriptor("simpleType")).findFieldByName("suits");
for (int i = 0; i < 4; i++) {
var enumFieldTargetValue = ((Message) copiedMessage).getRepeatedField(enumFieldTarget, i);
Assertions.assertTrue(enumFieldTargetValue instanceof Descriptors.EnumValueDescriptor);
Assertions.assertEquals(((Descriptors.EnumValueDescriptor) message.getRepeatedField(enumFieldSrc, i)).getName(), ((Descriptors.EnumValueDescriptor) enumFieldTargetValue).getName());
Assertions.assertNotSame(message.getRepeatedField(enumFieldSrc, i), enumFieldTargetValue);
}
}
@Test
public void deepCopyEnumValueArrayTest() {
final var enumType = getCardsEnum();
var arrayType = new Type.Array(false, enumType);
var repo = TypeRepository.newBuilder();
enumType.defineProtoType(repo);
arrayType.defineProtoType(repo);
final var typeRepoSrc = repo.build();
final var typeRepoTarget = repo.build();
final var list = new ArrayList<Descriptors.EnumValueDescriptor>();
for (int i = suites.length - 1; i >= 0; i--) {
list.add(Objects.requireNonNull(typeRepoSrc.getEnumValue("enumType", suites[i])));
}
var copied = RecordConstructorValue.deepCopyIfNeeded(typeRepoTarget, arrayType, list);
Assertions.assertNotNull(copied);
for (int i = 0; i < 4; i++) {
var enumFieldTargetValue = ((List<?>) copied).get(i);
Assertions.assertTrue(enumFieldTargetValue instanceof Descriptors.EnumValueDescriptor);
Assertions.assertEquals(list.get(i).getName(), ((Descriptors.EnumValueDescriptor) enumFieldTargetValue).getName());
Assertions.assertNotSame(list.get(i), enumFieldTargetValue);
}
}
}