2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.utils;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import org.junit.jupiter.api.Test;
34 void testReference() {
35 Reference<Integer> val = new Reference<>(null);
36 assertNull(val.get());
38 val = new Reference<>(10);
39 assertEquals(10, val.get().intValue());
43 void testGet_testSet() {
44 Reference<Integer> val = new Reference<>(null);
45 assertNull(val.get());
48 assertEquals(20, val.get().intValue());
51 assertEquals(30, val.get().intValue());
55 void testCompareAndSet() {
56 Reference<Integer> val = new Reference<>(null);
58 Integer valCompare = 100;
60 // try an incorrect value - should fail and leave value unchanged
61 assertFalse(val.compareAndSet(500, valCompare));
62 assertNull(val.get());
64 assertTrue(val.compareAndSet(null, valCompare));
65 assertEquals(valCompare, val.get());
67 // try an incorrect value - should fail and leave value unchanged
69 assertFalse(val.compareAndSet(600, v2));
70 assertEquals(valCompare, val.get());
72 // now try again, this time with the correct value
73 assertTrue(val.compareAndSet(valCompare, v2));
74 assertEquals(v2, val.get());
77 assertTrue(val.compareAndSet(v2, v3));
78 assertEquals(v3, val.get());
80 // try setting it back to null
81 assertTrue(val.compareAndSet(v3, null));
82 assertNull(val.get());