diff --git a/src/cc/firaja/generators/AbstractGenerator.java b/src/cc/firaja/generators/AbstractGenerator.java index 1ba58e6..ecf4f8f 100644 --- a/src/cc/firaja/generators/AbstractGenerator.java +++ b/src/cc/firaja/generators/AbstractGenerator.java @@ -58,13 +58,18 @@ public AbstractGenerator(final int limit) * is re-initialized to this state. * * @param copy the generator to be copied. + * @throws IllegalArgumentException if parameter copy is null */ public AbstractGenerator(final AbstractGenerator copy) { + if (copy == null) + { + throw new IllegalArgumentException("Argument cannot be null"); + } this.hasLimit = copy.hasLimit; this.limit = copy.limit; this.cursor = copy.cursor; - + this.result = copy.result; storeOriginals(); } diff --git a/testsrc/cc/firaja/generators/SequentialNumericalAbstractGenerator.java b/testsrc/cc/firaja/generators/SequentialNumericalAbstractGenerator.java index 79e8481..ad5a358 100644 --- a/testsrc/cc/firaja/generators/SequentialNumericalAbstractGenerator.java +++ b/testsrc/cc/firaja/generators/SequentialNumericalAbstractGenerator.java @@ -205,4 +205,52 @@ public void testNegativeLimitInResize() // THEN } + @Test + public void testConstructorCopy() + { + // GIVEN + Generator newGenerator; + + sequentialNumericalAbstractGenerator.next(); + sequentialNumericalAbstractGenerator.next(); + sequentialNumericalAbstractGenerator.next(); + int lastResult = sequentialNumericalAbstractGenerator.last(); + + + // WHEN + newGenerator = new AbstractGenerator(sequentialNumericalAbstractGenerator) + { + @Override + public Integer generate() + { + return c(); + } + }; + int nextResult = sequentialNumericalAbstractGenerator.next(); + + // THEN + assertEquals(lastResult, (int) newGenerator.last()); + assertEquals(nextResult, (int) newGenerator.next()); + } + + @Test(expected = IllegalArgumentException.class) + public void testConstructorNullCopy() + { + // GIVEN + + + // WHEN + new AbstractGenerator(null) + { + @Override + public Object generate() + { + return null; + } + }; + + // THEN + + } + }