diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BasalWatchDataExt.java b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BasalWatchDataExt.java index 0739e8ac9d..9a23f491d9 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BasalWatchDataExt.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BasalWatchDataExt.java @@ -2,16 +2,11 @@ package info.nightscout.androidaps.testing.utils; import androidx.annotation.Nullable; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashSet; import java.util.Objects; -import java.util.Set; import info.nightscout.androidaps.data.BasalWatchData; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static info.nightscout.androidaps.testing.utils.ExtUtil.assertClassHaveSameFields; public class BasalWatchDataExt extends BasalWatchData { @@ -22,16 +17,9 @@ public class BasalWatchDataExt extends BasalWatchData { public BasalWatchDataExt(BasalWatchData ref) { super(); - Set parentFields = new HashSet<>(); - for (Field f : BasalWatchData.class.getDeclaredFields()) { - parentFields.add(f.getName()); - } - - Set knownFields = new HashSet<>(Arrays.asList("startTime,endTime,amount".split(","))); - // since we do not want modify BasalWatchData - we use this wrapper class // but we make sure it has same fields - assertThat(parentFields, is(knownFields)); + assertClassHaveSameFields(BasalWatchData.class, "startTime,endTime,amount"); this.startTime = ref.startTime; this.endTime = ref.endTime; diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BgWatchDataExt.java b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BgWatchDataExt.java index 0c1d4a0bf3..c49125809e 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BgWatchDataExt.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BgWatchDataExt.java @@ -2,16 +2,11 @@ package info.nightscout.androidaps.testing.utils; import androidx.annotation.Nullable; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashSet; import java.util.Objects; -import java.util.Set; import info.nightscout.androidaps.data.BgWatchData; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static info.nightscout.androidaps.testing.utils.ExtUtil.assertClassHaveSameFields; public class BgWatchDataExt extends BgWatchData { @@ -26,16 +21,9 @@ public class BgWatchDataExt extends BgWatchData { public BgWatchDataExt(BgWatchData ref) { super(); - Set parentFields = new HashSet<>(); - for (Field f : BgWatchData.class.getDeclaredFields()) { - parentFields.add(f.getName()); - } - - Set knownFields = new HashSet<>(Arrays.asList("sgv,high,low,timestamp,color".split(","))); - // since we do not want modify BgWatchDataExt - we use this wrapper class // but we make sure it has same fields - assertThat(parentFields, is(knownFields)); + assertClassHaveSameFields(BgWatchData.class, "sgv,high,low,timestamp,color"); this.sgv = ref.sgv; this.high = ref.high; diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BolusWatchDataExt.java b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BolusWatchDataExt.java index 13e6f52f8f..a5553f6a59 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/utils/BolusWatchDataExt.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/utils/BolusWatchDataExt.java @@ -2,16 +2,11 @@ package info.nightscout.androidaps.testing.utils; import androidx.annotation.Nullable; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashSet; import java.util.Objects; -import java.util.Set; import info.nightscout.androidaps.data.BolusWatchData; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static info.nightscout.androidaps.testing.utils.ExtUtil.assertClassHaveSameFields; public class BolusWatchDataExt extends BolusWatchData { @@ -22,16 +17,9 @@ public class BolusWatchDataExt extends BolusWatchData { public BolusWatchDataExt(BolusWatchData ref) { super(); - Set parentFields = new HashSet<>(); - for (Field f : BolusWatchData.class.getDeclaredFields()) { - parentFields.add(f.getName()); - } - - Set knownFields = new HashSet<>(Arrays.asList("date,bolus,carbs,isSMB,isValid".split(","))); - // since we do not want modify BolusWatchData - we use this wrapper class // but we make sure it has same fields - assertThat(parentFields, is(knownFields)); + assertClassHaveSameFields(BolusWatchData.class, "date,bolus,carbs,isSMB,isValid"); this.date = ref.date; this.bolus = ref.bolus; diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/utils/ExtUtil.java b/wear/src/test/java/info/nightscout/androidaps/testing/utils/ExtUtil.java new file mode 100644 index 0000000000..1559db11ae --- /dev/null +++ b/wear/src/test/java/info/nightscout/androidaps/testing/utils/ExtUtil.java @@ -0,0 +1,28 @@ +package info.nightscout.androidaps.testing.utils; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +class ExtUtil { + + static void assertClassHaveSameFields(Class checkedClass, String commaSeparatedFieldList) { + Set parentFields = new HashSet<>(); + for (Field f : checkedClass.getDeclaredFields()) { + final String fieldName = f.getName(); + // skip runtime-injected fields like $jacocoData + if (fieldName.startsWith("$")) { + continue; + } + parentFields.add(fieldName); + } + + Set knownFields = new HashSet<>(Arrays.asList(commaSeparatedFieldList.split(","))); + assertThat(parentFields, is(knownFields)); + } + +} diff --git a/wear/src/test/java/info/nightscout/androidaps/testing/utils/TempWatchDataExt.java b/wear/src/test/java/info/nightscout/androidaps/testing/utils/TempWatchDataExt.java index 171d8ac841..478c6de966 100644 --- a/wear/src/test/java/info/nightscout/androidaps/testing/utils/TempWatchDataExt.java +++ b/wear/src/test/java/info/nightscout/androidaps/testing/utils/TempWatchDataExt.java @@ -2,16 +2,11 @@ package info.nightscout.androidaps.testing.utils; import androidx.annotation.Nullable; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashSet; import java.util.Objects; -import java.util.Set; import info.nightscout.androidaps.data.TempWatchData; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static info.nightscout.androidaps.testing.utils.ExtUtil.assertClassHaveSameFields; public class TempWatchDataExt extends TempWatchData { @@ -23,16 +18,9 @@ public class TempWatchDataExt extends TempWatchData { public TempWatchDataExt(TempWatchData ref) { super(); - Set parentFields = new HashSet<>(); - for (Field f : TempWatchData.class.getDeclaredFields()) { - parentFields.add(f.getName()); - } - - Set knownFields = new HashSet<>(Arrays.asList("startTime,startBasal,endTime,endBasal,amount".split(","))); - - // since we do not want modify TempWatchData - we use this wrapper class + // since we do not want modify BolusWatchData - we use this wrapper class // but we make sure it has same fields - assertThat(parentFields, is(knownFields)); + assertClassHaveSameFields(TempWatchData.class, "startTime,startBasal,endTime,endBasal,amount"); this.startTime = ref.startTime; this.startBasal = ref.startBasal;