From 7c6a1e66dd8ff52aaf99fb086df656db149b58d5 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Sat, 13 Oct 2018 14:21:09 -0700 Subject: Cleanup metrics API exception messages. (#1494) * Cleanup metrics API exception messages. * Fix MutableAggregationTest.java --- .../io/opencensus/metrics/export/Distribution.java | 36 +++++++++------------- .../metrics/export/DistributionTest.java | 22 +++++++++---- 2 files changed, 31 insertions(+), 27 deletions(-) (limited to 'api') diff --git a/api/src/main/java/io/opencensus/metrics/export/Distribution.java b/api/src/main/java/io/opencensus/metrics/export/Distribution.java index eb0add86..d55f101c 100644 --- a/api/src/main/java/io/opencensus/metrics/export/Distribution.java +++ b/api/src/main/java/io/opencensus/metrics/export/Distribution.java @@ -69,16 +69,11 @@ public abstract class Distribution { sumOfSquaredDeviations == 0, "sum of squared deviations should be 0 if count is 0."); } Utils.checkNotNull(bucketOptions, "bucketOptions"); - + List bucketsCopy = + Collections.unmodifiableList(new ArrayList(Utils.checkNotNull(buckets, "buckets"))); + Utils.checkListElementNotNull(bucketsCopy, "bucket"); return new AutoValue_Distribution( - count, sum, sumOfSquaredDeviations, bucketOptions, copyBucketCount(buckets)); - } - - private static List copyBucketCount(List buckets) { - Utils.checkNotNull(buckets, "bucket list should not be null."); - List bucketsCopy = new ArrayList(buckets); - Utils.checkListElementNotNull(bucketsCopy, "bucket should not be null."); - return Collections.unmodifiableList(bucketsCopy); + count, sum, sumOfSquaredDeviations, bucketOptions, bucketsCopy); } /** @@ -204,24 +199,23 @@ public abstract class Distribution { * @since 0.17 */ private static ExplicitOptions create(List bucketBoundaries) { - Utils.checkNotNull(bucketBoundaries, "bucketBoundaries list should not be null."); - return new AutoValue_Distribution_BucketOptions_ExplicitOptions( - checkBucketBoundsAreSorted(bucketBoundaries)); + Utils.checkNotNull(bucketBoundaries, "bucketBoundaries"); + List bucketBoundariesCopy = + Collections.unmodifiableList(new ArrayList(bucketBoundaries)); + checkBucketBoundsAreSorted(bucketBoundariesCopy); + return new AutoValue_Distribution_BucketOptions_ExplicitOptions(bucketBoundariesCopy); } - private static List checkBucketBoundsAreSorted(List bucketBoundaries) { - List bucketBoundariesCopy = new ArrayList(bucketBoundaries); // Deep copy. - // Check if sorted. - if (bucketBoundariesCopy.size() >= 1) { - double previous = bucketBoundariesCopy.get(0); - Utils.checkArgument(previous > 0, "bucket boundaries should be > 0"); - for (int i = 1; i < bucketBoundariesCopy.size(); i++) { - double next = bucketBoundariesCopy.get(i); + private static void checkBucketBoundsAreSorted(List bucketBoundaries) { + if (bucketBoundaries.size() >= 1) { + double previous = Utils.checkNotNull(bucketBoundaries.get(0), "bucketBoundary"); + Utils.checkArgument(previous > 0, "bucket boundary should be > 0"); + for (int i = 1; i < bucketBoundaries.size(); i++) { + double next = Utils.checkNotNull(bucketBoundaries.get(i), "bucketBoundary"); Utils.checkArgument(previous < next, "bucket boundaries not sorted."); previous = next; } } - return Collections.unmodifiableList(bucketBoundariesCopy); } /** diff --git a/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java index ad89d338..85b31498 100644 --- a/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java +++ b/api/src/test/java/io/opencensus/metrics/export/DistributionTest.java @@ -98,9 +98,9 @@ public class DistributionTest { @Test public void createAndGet_ExplicitBucketsNegativeBounds() { - List bucketBounds = Arrays.asList(-1.0); + List bucketBounds = Collections.singletonList(-1.0); thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("bucket boundaries should be > 0"); + thrown.expectMessage("bucket boundary should be > 0"); BucketOptions.explicitOptions(bucketBounds); } @@ -252,14 +252,24 @@ public class DistributionTest { } @Test - public void createDistribution_NullBucketBounds() { + public void createDistribution_NullBucketBoundaries() { List buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucketBoundaries list should not be null."); + thrown.expectMessage("bucketBoundaries"); Distribution.create(10, 6.6, 678.54, BucketOptions.explicitOptions(null), buckets); } + @Test + public void createDistribution_NullBucketBoundary() { + List buckets = + Arrays.asList(Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4)); + thrown.expect(NullPointerException.class); + thrown.expectMessage("bucketBoundary"); + Distribution.create( + 10, 6.6, 678.54, BucketOptions.explicitOptions(Arrays.asList(2.5, null)), buckets); + } + @Test public void createDistribution_NullBucketOptions() { List buckets = @@ -274,7 +284,7 @@ public class DistributionTest { List bucketBounds = Arrays.asList(1.0, 2.0, 5.0); BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucket list should not be null."); + thrown.expectMessage("buckets"); Distribution.create(10, 6.6, 678.54, bucketOptions, null); } @@ -285,7 +295,7 @@ public class DistributionTest { List buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), null, Bucket.create(4)); thrown.expect(NullPointerException.class); - thrown.expectMessage("bucket should not be null."); + thrown.expectMessage("bucket"); Distribution.create(10, 6.6, 678.54, bucketOptions, buckets); } -- cgit v1.2.3