Permalink
Browse files

Added number sequence

  • Loading branch information...
1 parent 2da5fbd commit 2df2dd7f69fc9397b05bbb86861324ad5c1f42fa @ahirata ahirata committed Apr 11, 2011
@@ -13,14 +13,17 @@
import br.com.bfgex.Gender;
import br.com.fixturefactory.function.CalendarInterval;
-import br.com.fixturefactory.function.CalendarSequenceFunction;
+import br.com.fixturefactory.function.CalendarSequence;
import br.com.fixturefactory.function.DateTimeFunction;
import br.com.fixturefactory.function.FixtureFunction;
import br.com.fixturefactory.function.Function;
import br.com.fixturefactory.function.NameFunction;
+import br.com.fixturefactory.function.NumberSequence;
import br.com.fixturefactory.function.RandomFunction;
import br.com.fixturefactory.function.Range;
import br.com.fixturefactory.function.RegexFunction;
+import br.com.fixturefactory.function.Sequence;
+import br.com.fixturefactory.function.SequenceFunction;
import br.com.fixturefactory.util.DateTimeUtil;
public class Rule {
@@ -95,8 +98,12 @@ public Range range(Number start, Number end) {
return new Range(start, end);
}
- public Function sequence(String base, DateFormat format, CalendarInterval interval) {
- return new CalendarSequenceFunction(DateTimeUtil.toCalendar(base, format), interval);
+ public Function sequence(Sequence<?> sequence) {
+ return new SequenceFunction(sequence);
+ }
+
+ public CalendarSequence startWith(String base, DateFormat format, CalendarInterval interval) {
+ return new CalendarSequence(DateTimeUtil.toCalendar(base, format), interval);
}
public CalendarInterval incrementBy(int amount, int calendarField) {
@@ -107,8 +114,19 @@ public CalendarInterval decrementBy(int amount, int calendarField) {
return new CalendarInterval(amount*(-1), calendarField);
}
+ public Sequence<Number> startWith(Number start, int amount) {
+ return new NumberSequence(start, amount);
+ }
+
+ public int incrementBy(int amount) {
+ return amount;
+ }
+
+ public int decrementBy(int amount) {
+ return amount*(-1);
+ }
+
public Set<Property> getProperties() {
return properties;
}
-
}
@@ -2,26 +2,24 @@
import java.util.Calendar;
-public class CalendarSequenceFunction implements Function {
+public class CalendarSequence implements Sequence<Calendar> {
private Calendar baseCalendar;
private CalendarInterval interval;
-
private int multiplier;
- public CalendarSequenceFunction(Calendar calendar, CalendarInterval interval) {
+ public CalendarSequence(Calendar baseCalendar, CalendarInterval interval) {
super();
- this.baseCalendar = (Calendar) calendar.clone();
+ this.baseCalendar = (Calendar) baseCalendar.clone();
this.interval = interval;
}
@Override
- @SuppressWarnings("unchecked")
- public <T> T generateValue() {
+ public Calendar nextValue() {
Calendar result = (Calendar) this.baseCalendar.clone();
result.add(this.interval.getField(), this.interval.getAmount() * this.multiplier);
this.multiplier++;
- return (T) result;
+ return result;
}
}
@@ -0,0 +1,36 @@
+package br.com.fixturefactory.function;
+
+public class NumberSequence implements Sequence<Number> {
+
+ private Number base;
+ private int amount;
+ private int multiplier;
+
+ public NumberSequence(Number base, int amount) {
+ super();
+ this.base = base;
+ this.amount = amount;
+ }
+
+ @Override
+ public Number nextValue() {
+ Number result = null;
+
+ if (this.base instanceof Integer) {
+ result = this.base.intValue() + (this.amount * this.multiplier);
+
+ } else if (this.base instanceof Long) {
+ result = this.base.longValue() + (this.amount * this.multiplier);
+
+ } else if (this.base instanceof Float) {
+ result = this.base.floatValue() + (this.amount * this.multiplier);
+
+ } else if (this.base instanceof Double) {
+ result = this.base.doubleValue() + (this.amount * this.multiplier);
+ }
+
+ this.multiplier++;
+
+ return result;
+ }
+}
@@ -0,0 +1,6 @@
+package br.com.fixturefactory.function;
+
+public interface Sequence<T> {
+
+ public T nextValue();
+}
@@ -0,0 +1,17 @@
+package br.com.fixturefactory.function;
+
+public class SequenceFunction implements Function {
+
+ private Sequence<?> sequence;
+
+ public SequenceFunction(Sequence<?> sequence) {
+ super();
+ this.sequence = sequence;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T generateValue() {
+ return (T) sequence.nextValue();
+ }
+}
@@ -25,12 +25,12 @@ public void setUp() {
}}).addTemplate("previousInvoices", new Rule() {{
add("id", regex("\\d{3,5}"));
add("ammount", random(new BigDecimal("58.67"), new BigDecimal("45.89")));
- add("dueDate", sequence("2011-04-01", new SimpleDateFormat("yyyy-MM-dd"), decrementBy(1, Calendar.DAY_OF_MONTH)));
+ add("dueDate", sequence(startWith("2011-04-01", new SimpleDateFormat("yyyy-MM-dd"), decrementBy(1, Calendar.DAY_OF_MONTH))));
}}).addTemplate("nextInvoices", new Rule() {{
add("id", regex("\\d{3,5}"));
add("ammount", random(new BigDecimal("58.67"), new BigDecimal("45.89")));
- add("dueDate", sequence("2011-04-30", new SimpleDateFormat("yyyy-MM-dd"), incrementBy(1, Calendar.DAY_OF_MONTH)));
+ add("dueDate", sequence(startWith("2011-04-30", new SimpleDateFormat("yyyy-MM-dd"), incrementBy(1, Calendar.DAY_OF_MONTH))));
}});
}
@@ -6,34 +6,61 @@
import org.junit.Test;
import br.com.bfgex.Gender;
+import br.com.fixturefactory.function.NumberSequence;
+import br.com.fixturefactory.function.Sequence;
import br.com.fixturefactory.model.Student;
public class FixtureStudentTest {
@Before
public void setUp() {
Fixture.of(Student.class).addTemplate("valid", new Rule(){{
+ add("id", sequence(startWith(1L, incrementBy(1))));
add("firstName", firstName());
add("lastName", lastName());
add("gender", random(Gender.class));
- }}
- ).addTemplate("validFemaleStudent", new Rule(){{
+
+ }}).addTemplate("validFemaleStudent", new Rule(){{
+ add("id", sequence(startWith(200L, incrementBy(2))));
add("firstName", firstName(Gender.FEMALE));
add("lastName", lastName());
add("gender", Gender.FEMALE);
+
+ }});
+
+ final Sequence<Number> numberSequence = new NumberSequence(1L, 1);
+
+ Fixture.of(Student.class).addTemplate("sharedSequence", new Rule() {{
+ add("id", sequence(numberSequence));
+
+ }}).addTemplate("otherSharedSequence", new Rule() {{
+ add("id", sequence(numberSequence));
+
}});
}
@Test
public void fixtureAnyStudent() {
Student student = Fixture.of(Student.class).gimme("valid");
Assert.assertNotNull("Student should not be null", student);
+ Assert.assertNotNull("Students id should not be null", student.getId());
+ Assert.assertTrue("Students it should be 1", student.getId() == 1);
}
@Test
public void fixtureFemaleStudent() {
Student student = Fixture.of(Student.class).gimme("validFemaleStudent");
Assert.assertNotNull("Female Student should not be null", student);
+ Assert.assertNotNull("Students id should not be null", student.getId());
+ Assert.assertTrue("Students it should be 1", student.getId() == 200);
}
+ @Test
+ public void fixtureSharedSequence() {
+ Student oneStudent = Fixture.of(Student.class).gimme("sharedSequence");
+ Student otherStudent = Fixture.of(Student.class).gimme("otherSharedSequence");
+
+ Assert.assertTrue("Students id should be 1", oneStudent.getId() == 1L);
+ Assert.assertTrue("otherStudes id should be 2", otherStudent.getId() == 2L);
+ }
}
@@ -15,7 +15,7 @@
@Test
public void addOneDay() {
Calendar baseCalendar = DateTimeUtil.toCalendar("2011-04-09", new SimpleDateFormat("yyyy-MM-dd"));
- CalendarSequenceFunction sequenceFunction = new CalendarSequenceFunction(baseCalendar, new CalendarInterval(1, Calendar.DAY_OF_MONTH));
+ SequenceFunction sequenceFunction = new SequenceFunction(new CalendarSequence(baseCalendar, new CalendarInterval(1, Calendar.DAY_OF_MONTH)));
for (int i=0; i<3; i++) {
Calendar calendar = sequenceFunction.generateValue();
@@ -27,7 +27,7 @@ public void addOneDay() {
@Test
public void subtractOneDay() {
Calendar baseCalendar = DateTimeUtil.toCalendar("2011-04-09", new SimpleDateFormat("yyyy-MM-dd"));
- CalendarSequenceFunction sequenceFunction = new CalendarSequenceFunction(baseCalendar, new CalendarInterval(-1, Calendar.DAY_OF_MONTH));
+ SequenceFunction sequenceFunction = new SequenceFunction(new CalendarSequence(baseCalendar, new CalendarInterval(-1, Calendar.DAY_OF_MONTH)));
for (int i=0; i<3; i++) {
Calendar calendar = sequenceFunction.generateValue();
@@ -39,7 +39,7 @@ public void subtractOneDay() {
@Test
public void addThreeDays() {
Calendar baseCalendar = DateTimeUtil.toCalendar("2011-04-09", new SimpleDateFormat("yyyy-MM-dd"));
- CalendarSequenceFunction sequenceFunction = new CalendarSequenceFunction(baseCalendar, new CalendarInterval(3, Calendar.DAY_OF_MONTH));
+ SequenceFunction sequenceFunction = new SequenceFunction(new CalendarSequence(baseCalendar, new CalendarInterval(3, Calendar.DAY_OF_MONTH)));
for (int i=0; i<3; i++) {
Calendar calendar = sequenceFunction.generateValue();
@@ -51,7 +51,7 @@ public void addThreeDays() {
@Test
public void subtractThreeDays() {
Calendar baseCalendar = DateTimeUtil.toCalendar("2011-04-09", new SimpleDateFormat("yyyy-MM-dd"));
- CalendarSequenceFunction sequenceFunction = new CalendarSequenceFunction(baseCalendar, new CalendarInterval(-3, Calendar.DAY_OF_MONTH));
+ SequenceFunction sequenceFunction = new SequenceFunction(new CalendarSequence(baseCalendar, new CalendarInterval(-3, Calendar.DAY_OF_MONTH)));
for (int i=0; i<3; i++) {
Calendar calendar = sequenceFunction.generateValue();
@@ -0,0 +1,44 @@
+package br.com.fixturefactory.function;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class NumberSequenceFunctionTest {
+
+ @Test
+ public void intSequence() {
+ SequenceFunction function = new SequenceFunction(new NumberSequence(0, 1));
+
+ for (int i=0; i<3; i++) {
+ Assert.assertEquals("integers should be equal", function.generateValue(), i);
+ }
+ }
+
+ @Test
+ public void longSequence() {
+ SequenceFunction function = new SequenceFunction(new NumberSequence(1L, 2));
+
+ for (int i=1; i<=5; i=i+2) {
+ Assert.assertEquals("longs should be equal", function.generateValue(), (long) i);
+ }
+ }
+
+ @Test
+ public void floatSequence() {
+ SequenceFunction function = new SequenceFunction(new NumberSequence(1.2f, 1));
+
+ for (int i=1; i<=3; i++) {
+ Assert.assertEquals("floats should be equal", function.generateValue(), (float) i+(.2F));
+ }
+ }
+
+ @Test
+ public void doubleSequence() {
+ SequenceFunction function = new SequenceFunction(new NumberSequence(1.23d, 2));
+
+ for (int i=1; i<=5; i=i+2) {
+ Assert.assertEquals("doubles should be equal", function.generateValue(), (double) i+(.23d));
+ }
+ }
+}
@@ -8,10 +8,18 @@
private static final long serialVersionUID = 1659316546025160752L;
+ private Long id;
private String firstName;
private String lastName;
private Gender gender;
+ public Long getId() {
+ return id;
+ }
+ public void setId(Long id) {
+ this.id = id;
+ }
+
public String getFirstName() {
return firstName;
}

0 comments on commit 2df2dd7

Please sign in to comment.