From ce76ad1841eff6e84915e5841bd3e23affc71248 Mon Sep 17 00:00:00 2001 From: Jennifer Hickey Date: Wed, 31 Jul 2013 16:25:52 -0700 Subject: [PATCH] Make Long and Double test factories return unique vals Random was returning the same value multiple times, causing issues with Set tests --- .../data/redis/DoubleAsStringObjectFactory.java | 9 +++++---- .../data/redis/DoubleObjectFactory.java | 11 ++++------- .../data/redis/LongAsStringObjectFactory.java | 8 ++++---- .../springframework/data/redis/LongObjectFactory.java | 8 ++++---- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/test/java/org/springframework/data/redis/DoubleAsStringObjectFactory.java b/src/test/java/org/springframework/data/redis/DoubleAsStringObjectFactory.java index 010ee9872..a9d59cde0 100644 --- a/src/test/java/org/springframework/data/redis/DoubleAsStringObjectFactory.java +++ b/src/test/java/org/springframework/data/redis/DoubleAsStringObjectFactory.java @@ -15,20 +15,21 @@ */ package org.springframework.data.redis; -import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; /** * - * Implementation of {@link ObjectFactory that returns random Doubles as Strings + * Implementation of {@link ObjectFactory} that returns unique Doubles as Strings, + * incrementing by 1 each time * @author Jennifer Hickey * */ public class DoubleAsStringObjectFactory implements ObjectFactory { - private Random random = new Random(System.currentTimeMillis()); + private AtomicLong counter = new AtomicLong(); public String instance() { - return String.valueOf(random.nextDouble()); + return String.valueOf(Double.valueOf(counter.getAndIncrement())); } } diff --git a/src/test/java/org/springframework/data/redis/DoubleObjectFactory.java b/src/test/java/org/springframework/data/redis/DoubleObjectFactory.java index fbc06485b..f8bd5e055 100644 --- a/src/test/java/org/springframework/data/redis/DoubleObjectFactory.java +++ b/src/test/java/org/springframework/data/redis/DoubleObjectFactory.java @@ -15,23 +15,20 @@ */ package org.springframework.data.redis; -import java.text.DecimalFormat; -import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; /** - * Returns random Doubles + * Returns unique Doubles, incrementing by 1 each time * * @author Jennifer Hickey * */ public class DoubleObjectFactory implements ObjectFactory { - private Random random = new Random(System.currentTimeMillis()); - - private static final DecimalFormat TWO_D_FORM = new DecimalFormat("#.##"); + private AtomicLong counter = new AtomicLong(); public Double instance() { - return Double.valueOf(TWO_D_FORM.format(random.nextDouble())); + return Double.valueOf(counter.getAndIncrement()); } } diff --git a/src/test/java/org/springframework/data/redis/LongAsStringObjectFactory.java b/src/test/java/org/springframework/data/redis/LongAsStringObjectFactory.java index d643bb963..cd7622a21 100644 --- a/src/test/java/org/springframework/data/redis/LongAsStringObjectFactory.java +++ b/src/test/java/org/springframework/data/redis/LongAsStringObjectFactory.java @@ -15,20 +15,20 @@ */ package org.springframework.data.redis; -import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; /** * -* Implementation of {@link ObjectFactory that returns random Longs as Strings +* Implementation of {@link ObjectFactory} that returns unique incremented Longs as Strings * @author Jennifer Hickey * */ public class LongAsStringObjectFactory implements ObjectFactory { - private Random random = new Random(System.currentTimeMillis()); + private AtomicLong counter = new AtomicLong(); public String instance() { - return String.valueOf(random.nextLong()); + return String.valueOf(counter.getAndIncrement()); } } diff --git a/src/test/java/org/springframework/data/redis/LongObjectFactory.java b/src/test/java/org/springframework/data/redis/LongObjectFactory.java index fa816ee7c..d1c131f38 100644 --- a/src/test/java/org/springframework/data/redis/LongObjectFactory.java +++ b/src/test/java/org/springframework/data/redis/LongObjectFactory.java @@ -15,20 +15,20 @@ */ package org.springframework.data.redis; -import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; /** - * Returns random Longs + * Returns unique Longs by incrementing a counter each time * * @author Jennifer Hickey * */ public class LongObjectFactory implements ObjectFactory { - private Random random = new Random(System.currentTimeMillis()); + private AtomicLong counter = new AtomicLong(); public Long instance() { - return random.nextLong(); + return counter.getAndIncrement(); } }