| 1 |
/* |
| 2 |
* Copyright (C) 2022 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.server.art.testing; |
| 18 |
|
| 19 |
import static com.google.common.truth.Truth.assertThat; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
|
| 23 |
import androidx.test.filters.SmallTest; |
| 24 |
import androidx.test.runner.AndroidJUnit4; |
| 25 |
|
| 26 |
import org.junit.After; |
| 27 |
import org.junit.Before; |
| 28 |
import org.junit.Test; |
| 29 |
import org.junit.runner.RunWith; |
| 30 |
import org.mockito.ArgumentMatcher; |
| 31 |
import org.mockito.internal.progress.ThreadSafeMockingProgress; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.HashSet; |
| 36 |
import java.util.List; |
| 37 |
import java.util.function.Consumer; |
| 38 |
|
| 39 |
@SmallTest |
| 40 |
@RunWith(AndroidJUnit4.class) |
| 41 |
public class TestingUtilsTest { |
| 42 |
@Before |
| 43 |
@After |
| 44 |
public void resetMockito() { |
| 45 |
ThreadSafeMockingProgress.mockingProgress().reset(); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void testDeepEquals() { |
| 50 |
var a = new Foo(); |
| 51 |
var b = new Foo(); |
| 52 |
assertThat(TestingUtils.deepEquals(a, b)).isTrue(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testDeepEqualsNull() { |
| 57 |
assertThat(TestingUtils.deepEquals(null, null)).isTrue(); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testDeepEqualsNullabilityMismatch() { |
| 62 |
var a = new Foo(); |
| 63 |
assertThat(TestingUtils.deepEquals(a, null)).isFalse(); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void testDeepEqualsTypeMismatch() { |
| 68 |
var a = new Foo(); |
| 69 |
var b = new Bar(); |
| 70 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void testDeepEqualsPrimitiveFieldMismatch() { |
| 75 |
var a = new Foo(); |
| 76 |
var b = new Foo(); |
| 77 |
b.mA = 11111111; |
| 78 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void testDeepEqualsStringFieldMismatch() { |
| 83 |
var a = new Foo(); |
| 84 |
var b = new Foo(); |
| 85 |
b.mB = "def"; |
| 86 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void deepEqualsNestedFieldMismatch() { |
| 91 |
var a = new Foo(); |
| 92 |
var b = new Foo(); |
| 93 |
b.mC.setB(11111111); |
| 94 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 95 |
} |
| 96 |
|
| 97 |
@Test(expected = UnsupportedOperationException.class) |
| 98 |
public void testDeepEqualsArrayNotSupported() throws Exception { |
| 99 |
int[] a = new int[] {1}; |
| 100 |
int[] b = new int[] {2}; |
| 101 |
TestingUtils.deepEquals(a, b); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void testListDeepEquals() throws Exception { |
| 106 |
var a = new ArrayList<Integer>(); |
| 107 |
a.add(1); |
| 108 |
a.add(2); |
| 109 |
a.add(3); |
| 110 |
a.add(4); |
| 111 |
a.add(5); |
| 112 |
var b = List.of(1, 2, 3, 4, 5); |
| 113 |
assertThat(TestingUtils.deepEquals(a, b)).isTrue(); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void testListDeepEqualsSizeMismatch() throws Exception { |
| 118 |
var a = new ArrayList<Integer>(); |
| 119 |
a.add(1); |
| 120 |
var b = new ArrayList<Integer>(); |
| 121 |
b.add(1); |
| 122 |
b.add(2); |
| 123 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void testListDeepEqualsElementMismatch() throws Exception { |
| 128 |
var a = new ArrayList<Integer>(); |
| 129 |
a.add(1); |
| 130 |
var b = new ArrayList<Integer>(); |
| 131 |
b.add(2); |
| 132 |
assertThat(TestingUtils.deepEquals(a, b)).isFalse(); |
| 133 |
} |
| 134 |
|
| 135 |
@Test(expected = UnsupportedOperationException.class) |
| 136 |
public void testDeepEqualsOtherContainerNotSupported() throws Exception { |
| 137 |
var a = new HashSet<Integer>(); |
| 138 |
a.add(1); |
| 139 |
var b = new HashSet<Integer>(); |
| 140 |
b.add(2); |
| 141 |
TestingUtils.deepEquals(a, b); |
| 142 |
} |
| 143 |
|
| 144 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 145 |
@Test |
| 146 |
public void testInAnyOrderDuplicates() { |
| 147 |
testInAnyOrderInternal(List.of(1, 1), List.of(1), false, TestingUtils::inAnyOrder); |
| 148 |
testInAnyOrderInternal(List.of(1, 1), List.of(1, 1), true, TestingUtils::inAnyOrder); |
| 149 |
} |
| 150 |
|
| 151 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 152 |
@Test |
| 153 |
public void testInAnyOrderDeepEqualsDuplicates() { |
| 154 |
testInAnyOrderInternal( |
| 155 |
Arrays.asList(1, 1), List.of(1), false, TestingUtils::inAnyOrderDeepEquals); |
| 156 |
testInAnyOrderInternal( |
| 157 |
Arrays.asList(1, 1), List.of(1, 1), true, TestingUtils::inAnyOrderDeepEquals); |
| 158 |
} |
| 159 |
|
| 160 |
private void testInAnyOrderInternal(@NonNull List<Integer> first, @NonNull List<Integer> second, |
| 161 |
boolean expected, @NonNull Consumer<Integer[]> inAnyOrderBlock) { |
| 162 |
inAnyOrderBlock.accept(first.toArray(new Integer[0])); |
| 163 |
var matchers = ThreadSafeMockingProgress.mockingProgress() |
| 164 |
.getArgumentMatcherStorage() |
| 165 |
.pullLocalizedMatchers(); |
| 166 |
assertThat(matchers).hasSize(1); |
| 167 |
// noinspection unchecked |
| 168 |
var matcher = (ArgumentMatcher<List<Integer>>) matchers.get(0).getMatcher(); |
| 169 |
assertThat(matcher.matches(second)).isEqualTo(expected); |
| 170 |
ThreadSafeMockingProgress.mockingProgress().reset(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
class Foo { |
| 175 |
public int mA = 1234567; |
| 176 |
public String mB = "abc"; |
| 177 |
public Bar mC = new Bar(); |
| 178 |
} |
| 179 |
|
| 180 |
class Bar { |
| 181 |
public static int sA = 10000000; |
| 182 |
private int mB = 7654321; |
| 183 |
public void setB(int b) { |
| 184 |
mB = b; |
| 185 |
} |
| 186 |
} |