-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #711 from getodk/JAVAROSA-686
Values referenced from jr:repeat-count should be coerced to int
- Loading branch information
Showing
6 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/org/javarosa/core/model/data/helper/AnswerDataUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.javarosa.core.model.data.helper; | ||
|
||
import org.javarosa.core.model.data.IAnswerData; | ||
|
||
public class AnswerDataUtil { | ||
public static int answerDataToInt(IAnswerData countData) { | ||
if (countData == null) { | ||
return 0; | ||
} | ||
|
||
Object count = countData.getValue(); | ||
if (count instanceof Integer) { | ||
return (int) count; | ||
} else if (count instanceof Double) { | ||
return (int) Math.floor((Double) count); | ||
} else if (count instanceof Long) { | ||
return (int) ((Long) count).longValue(); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/org/javarosa/core/model/data/helper/AnswerDataUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.javarosa.core.model.data.helper; | ||
|
||
import org.javarosa.core.model.data.BooleanData; | ||
import org.javarosa.core.model.data.DateData; | ||
import org.javarosa.core.model.data.DecimalData; | ||
import org.javarosa.core.model.data.IntegerData; | ||
import org.javarosa.core.model.data.LongData; | ||
import org.javarosa.core.model.data.SelectOneData; | ||
import org.javarosa.core.model.data.StringData; | ||
import org.junit.Test; | ||
|
||
import java.util.Date; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AnswerDataUtilTest { | ||
|
||
@Test | ||
public void NumericalDataReturnsProperValueWhenConvertedToInt() { | ||
assertEquals(5, AnswerDataUtil.answerDataToInt(new IntegerData(5))); | ||
|
||
assertEquals(7, AnswerDataUtil.answerDataToInt(new DecimalData(7.35))); | ||
|
||
assertEquals(3, AnswerDataUtil.answerDataToInt(new LongData(3L))); | ||
} | ||
|
||
@Test | ||
public void NonNumericalDataReturnsZeroWhenConvertedToInt() { | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new BooleanData(true))); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new SelectOneData(new Selection("Selection1")))); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new DateData(new Date()))); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new StringData("blah"))); | ||
} | ||
|
||
@Test | ||
public void IfDataTypeHasNoAnswerReturnsZeroWhenConvertedToInt() { | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new IntegerData())); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new DecimalData())); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new LongData())); | ||
assertEquals(0, AnswerDataUtil.answerDataToInt(new BooleanData())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters