LCOV - code coverage report
Current view: top level - acceptance - AssertUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 13 14 92.9 %
Date: 2022-11-19 15:00:39 Functions: 1 2 50.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.acceptance;
      16             : 
      17             : import static com.google.common.truth.Truth.assertWithMessage;
      18             : import static com.google.gerrit.server.config.ConfigUtil.skipField;
      19             : 
      20             : import java.lang.reflect.Field;
      21             : import java.util.Arrays;
      22             : import java.util.HashSet;
      23             : import java.util.Set;
      24             : 
      25           0 : public class AssertUtil {
      26             :   public static <T> void assertPrefs(T actual, T expected, String... fieldsToExclude)
      27             :       throws IllegalArgumentException, IllegalAccessException {
      28           2 :     Set<String> exludedFields = new HashSet<>(Arrays.asList(fieldsToExclude));
      29           2 :     for (Field field : actual.getClass().getDeclaredFields()) {
      30           2 :       if (exludedFields.contains(field.getName()) || skipField(field)) {
      31           2 :         continue;
      32             :       }
      33           2 :       Object actualVal = field.get(actual);
      34           2 :       Object expectedVal = field.get(expected);
      35           2 :       if (field.getType().isAssignableFrom(Boolean.class)) {
      36           2 :         if (actualVal == null) {
      37           2 :           actualVal = false;
      38             :         }
      39           2 :         if (expectedVal == null) {
      40           1 :           expectedVal = false;
      41             :         }
      42             :       }
      43           2 :       assertWithMessage(field.getName()).that(actualVal).isEqualTo(expectedVal);
      44             :     }
      45           2 :   }
      46             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750