Use one reflection util to assert all preferences

Reflection code to assert diff preferences existed twice and to assert
general preferences relection should be used as well.

Change-Id: I761909f929569f53ea0714d7c5403198a6c88ca5
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2016-06-03 15:05:26 +02:00
parent 3b8ff49c6e
commit f35d18655a
4 changed files with 54 additions and 91 deletions

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2016 The Android Open Source Project
//
// 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.google.gerrit.acceptance;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.server.config.ConfigUtil.skipField;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AssertUtil {
public static <T> void assertPrefs(T actual, T expected,
String... fieldsToExclude)
throws IllegalArgumentException, IllegalAccessException {
Set<String> exludedFields = new HashSet<>(Arrays.asList(fieldsToExclude));
for (Field field : actual.getClass().getDeclaredFields()) {
if (exludedFields.contains(field.getName()) || skipField(field)) {
continue;
}
Object actualVal = field.get(actual);
Object expectedVal = field.get(expected);
if (field.getType().isAssignableFrom(Boolean.class)) {
if (actualVal == null) {
actualVal = false;
}
if (expectedVal == null) {
expectedVal = false;
}
}
assertWithMessage(field.getName()).that(actualVal).isEqualTo(expectedVal);
}
}
}

View File

@@ -15,9 +15,8 @@
package com.google.gerrit.acceptance.api.accounts;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.acceptance.AssertUtil.assertPrefs;
import static com.google.gerrit.acceptance.GitUtil.fetch;
import static com.google.gerrit.server.config.ConfigUtil.skipField;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
@@ -36,10 +35,6 @@ import org.eclipse.jgit.junit.TestRepository;
import org.junit.After;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
@NoHttpd
public class DiffPreferencesIT extends AbstractDaemonTest {
@Inject
@@ -141,27 +136,4 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
// assert hard-coded defaults
assertPrefs(o, d, "lineLength", "tabSize");
}
private static void assertPrefs(DiffPreferencesInfo actual,
DiffPreferencesInfo expected, String... fieldsToExclude)
throws IllegalArgumentException, IllegalAccessException {
List<String> exludedFields = Arrays.asList(fieldsToExclude);
for (Field field : actual.getClass().getDeclaredFields()) {
if (exludedFields.contains(field.getName()) || skipField(field)) {
continue;
}
Object actualVal = field.get(actual);
Object expectedVal = field.get(expected);
if (field.getType().isAssignableFrom(Boolean.class)) {
if (actualVal == null) {
actualVal = false;
}
if (expectedVal == null) {
expectedVal = false;
}
}
assertWithMessage("field " + field.getName()).that(actualVal)
.isEqualTo(expectedVal);
}
}
}

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.acceptance.api.accounts;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.AssertUtil.assertPrefs;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
@@ -49,28 +50,8 @@ public class GeneralPreferencesIT extends AbstractDaemonTest {
GeneralPreferencesInfo o = gApi.accounts()
.id(user42.id.toString())
.getPreferences();
GeneralPreferencesInfo d = GeneralPreferencesInfo.defaults();
assertThat(o.changesPerPage).isEqualTo(d.changesPerPage);
assertThat(o.showSiteHeader).isEqualTo(d.showSiteHeader);
assertThat(o.useFlashClipboard).isEqualTo(d.useFlashClipboard);
assertThat(o.downloadScheme).isNull();
assertThat(o.downloadCommand).isEqualTo(d.downloadCommand);
assertThat(o.dateFormat).isEqualTo(d.getDateFormat());
assertThat(o.timeFormat).isEqualTo(d.getTimeFormat());
assertThat(o.emailStrategy).isEqualTo(d.getEmailStrategy());
assertThat(o.relativeDateInChangeTable).isNull();
assertThat(o.sizeBarInChangeTable).isEqualTo(d.sizeBarInChangeTable);
assertThat(o.legacycidInChangeTable).isNull();
assertThat(o.muteCommonPathPrefixes).isEqualTo(
d.muteCommonPathPrefixes);
assertThat(o.signedOffBy).isNull();
assertThat(o.reviewCategoryStrategy).isEqualTo(
d.getReviewCategoryStrategy());
assertThat(o.diffView).isEqualTo(d.getDiffView());
assertPrefs(o, GeneralPreferencesInfo.defaults(), "my");
assertThat(o.my).hasSize(7);
assertThat(o.urlAliases).isNull();
GeneralPreferencesInfo i = GeneralPreferencesInfo.defaults();
@@ -97,25 +78,7 @@ public class GeneralPreferencesIT extends AbstractDaemonTest {
o = gApi.accounts()
.id(user42.getId().toString())
.setPreferences(i);
assertThat(o.changesPerPage).isEqualTo(i.changesPerPage);
assertThat(o.showSiteHeader).isNull();
assertThat(o.useFlashClipboard).isNull();
assertThat(o.downloadScheme).isNull();
assertThat(o.downloadCommand).isEqualTo(i.downloadCommand);
assertThat(o.dateFormat).isEqualTo(i.getDateFormat());
assertThat(o.timeFormat).isEqualTo(i.getTimeFormat());
assertThat(o.emailStrategy).isEqualTo(i.emailStrategy);
assertThat(o.relativeDateInChangeTable).isEqualTo(
i.relativeDateInChangeTable);
assertThat(o.sizeBarInChangeTable).isNull();
assertThat(o.legacycidInChangeTable).isEqualTo(i.legacycidInChangeTable);
assertThat(o.muteCommonPathPrefixes).isNull();
assertThat(o.signedOffBy).isEqualTo(i.signedOffBy);
assertThat(o.reviewCategoryStrategy).isEqualTo(
i.getReviewCategoryStrategy());
assertThat(o.diffView).isEqualTo(i.getDiffView());
assertPrefs(o, i, "my");
assertThat(o.my).hasSize(1);
assertThat(o.urlAliases).hasSize(1);
}
}

View File

@@ -15,7 +15,7 @@
package com.google.gerrit.acceptance.rest.config;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.config.ConfigUtil.skipField;
import static com.google.gerrit.acceptance.AssertUtil.assertPrefs;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
@@ -23,8 +23,6 @@ import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import org.junit.Test;
import java.lang.reflect.Field;
@NoHttpd
public class DiffPreferencesIT extends AbstractDaemonTest {
@@ -32,7 +30,7 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
public void getDiffPreferences() throws Exception {
DiffPreferencesInfo result =
gApi.config().server().getDefaultDiffPreferences();
assertPrefsEqual(result, DiffPreferencesInfo.defaults());
assertPrefs(result, DiffPreferencesInfo.defaults());
}
@Test
@@ -47,23 +45,6 @@ public class DiffPreferencesIT extends AbstractDaemonTest {
result = gApi.config().server().getDefaultDiffPreferences();
DiffPreferencesInfo expected = DiffPreferencesInfo.defaults();
expected.lineLength = newLineLength;
assertPrefsEqual(result, expected);
}
private void assertPrefsEqual(DiffPreferencesInfo actual,
DiffPreferencesInfo expected) throws Exception {
for (Field field : actual.getClass().getDeclaredFields()) {
if (skipField(field)) {
continue;
}
Object actualField = field.get(actual);
Object expectedField = field.get(expected);
Class<?> type = field.getType();
if ((type == boolean.class || type == Boolean.class)
&& actualField == null) {
continue;
}
assertThat(actualField).named(field.getName()).isEqualTo(expectedField);
}
assertPrefs(result, expected);
}
}