aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/profile/test/CloningTestCase.java
blob: 437d5e3b9434386c6bf2217288779b46463d868f (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.test;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.search.Query;
import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.jdisc.http.HttpRequest.Method;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author bratseth
 */
public class CloningTestCase {

    @Test
    void testCloningWithVariants() {
        QueryProfile test = new QueryProfile("test");
        test.setDimensions(new String[]{"x"});
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q&x=x1", Method.GET), test.compile(null));
        q1.properties().set("a", "a1");
        Query q2 = q1.clone();
        q2.properties().set("a", "a2");
        assertEquals("a1", q1.properties().get("a"));
        assertEquals("a2", q2.properties().get("a"));
    }

    @Test
    void testShallowCloning() {
        QueryProfile test = new QueryProfile("test");
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));
        q1.properties().set("a", new MutableString("a1"));
        Query q2 = q1.clone();
        ((MutableString) q2.properties().get("a")).set("a2");
        assertEquals("a2", q1.properties().get("a").toString());
        assertEquals("a2", q2.properties().get("a").toString());
    }

    @Test
    void testShallowCloningWithVariants() {
        QueryProfile test = new QueryProfile("test");
        test.setDimensions(new String[]{"x"});
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q&x=x1", Method.GET), test.compile(null));
        q1.properties().set("a", new MutableString("a1"));
        Query q2 = q1.clone();
        ((MutableString) q2.properties().get("a")).set("a2");
        assertEquals("a2", q1.properties().get("a").toString());
        assertEquals("a2", q2.properties().get("a").toString());
    }

    @Test
    void testDeepCloning() {
        QueryProfile test = new QueryProfile("test");
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));
        q1.properties().set("a", new CloneableMutableString("a1"));
        Query q2 = q1.clone();
        ((MutableString) q2.properties().get("a")).set("a2");
        assertEquals("a1", q1.properties().get("a").toString());
        assertEquals("a2", q2.properties().get("a").toString());
    }

    @Test
    void testDeepCloningWithVariants() {
        QueryProfile test = new QueryProfile("test");
        test.setDimensions(new String[]{"x"});
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q&x=x1", Method.GET), test.compile(null));
        q1.properties().set("a", new CloneableMutableString("a1"));
        Query q2 = q1.clone();
        ((MutableString) q2.properties().get("a")).set("a2");
        assertEquals("a1", q1.properties().get("a").toString());
        assertEquals("a2", q2.properties().get("a").toString());
    }

    @Test
    void testReAssignment() {
        QueryProfile test = new QueryProfile("test");
        test.setDimensions(new String[]{"x"});
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q&x=x1", Method.GET), test.compile(null));
        q1.properties().set("a", "a1");
        q1.properties().set("a", "a2");
        assertEquals("a2", q1.properties().get("a"));
    }

    @Test
    void testThreeLevelsOfCloning() {
        QueryProfile test = new QueryProfile("test");
        test.set("a", "config-a", (QueryProfileRegistry) null);
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));

        q1.properties().set("a", "q1-a");
        Query q2 = q1.clone();
        q2.properties().set("a", "q2-a");
        Query q31 = q2.clone();
        q31.properties().set("a", "q31-a");
        Query q32 = q2.clone();
        q32.properties().set("a", "q32-a");

        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
        q2.properties().set("a", "q2-a-2");
        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a-2", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
    }

    @Test
    void testThreeLevelsOfCloningReverseSetOrder() {
        QueryProfile test = new QueryProfile("test");
        test.set("a", "config-a", (QueryProfileRegistry) null);
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));

        Query q2 = q1.clone();
        Query q31 = q2.clone();
        Query q32 = q2.clone();
        q32.properties().set("a", "q32-a");
        q31.properties().set("a", "q31-a");
        q2.properties().set("a", "q2-a");
        q1.properties().set("a", "q1-a");

        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
        q2.properties().set("a", "q2-a-2");
        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a-2", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
    }

    @Test
    void testThreeLevelsOfCloningMiddleFirstSetOrder1() {
        QueryProfile test = new QueryProfile("test");
        test.set("a", "config-a", (QueryProfileRegistry) null);
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));

        Query q2 = q1.clone();
        Query q31 = q2.clone();
        Query q32 = q2.clone();
        q2.properties().set("a", "q2-a");
        q32.properties().set("a", "q32-a");
        q31.properties().set("a", "q31-a");
        q1.properties().set("a", "q1-a");

        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
        q2.properties().set("a", "q2-a-2");
        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a-2", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("q32-a", q32.properties().get("a").toString());
    }

    @Test
    void testThreeLevelsOfCloningMiddleFirstSetOrder2() {
        QueryProfile test = new QueryProfile("test");
        test.set("a", "config-a", (QueryProfileRegistry) null);
        test.freeze();
        Query q1 = new Query(HttpRequest.createTestRequest("?query=q", Method.GET), test.compile(null));

        Query q2 = q1.clone();
        Query q31 = q2.clone();
        Query q32 = q2.clone();
        q2.properties().set("a", "q2-a");
        q31.properties().set("a", "q31-a");
        q1.properties().set("a", "q1-a");

        assertEquals("q1-a", q1.properties().get("a").toString());
        assertEquals("q2-a", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("config-a", q32.properties().get("a").toString());
        q1.properties().set("a", "q1-a-2");
        assertEquals("q1-a-2", q1.properties().get("a").toString());
        assertEquals("q2-a", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("config-a", q32.properties().get("a").toString());
        q2.properties().set("a", "q2-a-2");
        assertEquals("q1-a-2", q1.properties().get("a").toString());
        assertEquals("q2-a-2", q2.properties().get("a").toString());
        assertEquals("q31-a", q31.properties().get("a").toString());
        assertEquals("config-a", q32.properties().get("a").toString());
    }

    public static class MutableString {

        private String string;

        public MutableString(String string) {
            this.string=string;
        }

        public void set(String string) { this.string=string; }

        @Override
        public String toString() { return string; }

        @Override
        public int hashCode() { return string.hashCode(); }

        @Override
        public boolean equals(Object other) {
            if (other==this) return true;
            if ( ! (other instanceof MutableString)) return false;
            return ((MutableString)other).string.equals(string);
        }

    }

    public static class CloneableMutableString extends MutableString implements Cloneable {

        public CloneableMutableString(String string) {
            super(string);
        }

        @Override
        public CloneableMutableString clone() {
            try {
                return (CloneableMutableString)super.clone();
            }
            catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

    }

}