-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for generating ISSNS codes.
- Loading branch information
Showing
7 changed files
with
93 additions
and
6 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
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
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,38 @@ | ||
package net.andreinc.mockneat.unit.misc; | ||
|
||
import net.andreinc.mockneat.MockNeat; | ||
import net.andreinc.mockneat.interfaces.MockUnitString; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.apache.commons.lang3.StringUtils.join; | ||
|
||
/** | ||
* Created by andreinicolinciobanu on 26/03/17. | ||
*/ | ||
public class ISSNS implements MockUnitString { | ||
|
||
private static final String ISSN_PREFIX = "ISSN"; | ||
|
||
private MockNeat mock; | ||
|
||
public ISSNS(MockNeat mockNeat) { | ||
this.mock = mockNeat; | ||
} | ||
|
||
@Override | ||
public Supplier<String> supplier() { | ||
return () -> { | ||
Integer[] array = mock.ints().range(0, 10).array(7).val(); | ||
int sum = 0; | ||
for(int i = 0, j = 8; i < array.length; i++, j--) { | ||
sum += j * array[i]; | ||
} | ||
int remainder = sum % 11; | ||
if (remainder != 0) { remainder = 11 - remainder; } | ||
return ISSN_PREFIX + " " + join(array, "", 0, 4) | ||
+ "-" + join(array, "", 4, 7) | ||
+ ((remainder == 10) ? "X" : remainder + ""); | ||
}; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/test/java/net/andreinc/mockneat/unit/misc/ISSNSTest.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,28 @@ | ||
package net.andreinc.mockneat.unit.misc; | ||
|
||
import org.apache.commons.validator.routines.ISSNValidator; | ||
import org.junit.Test; | ||
|
||
import static net.andreinc.mockneat.Constants.*; | ||
import static net.andreinc.mockneat.utils.LoopsUtils.loop; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Created by andreinicolinciobanu on 26/03/17. | ||
*/ | ||
public class ISSNSTest { | ||
|
||
private ISSNValidator validator = ISSNValidator.getInstance(); | ||
|
||
@Test | ||
public void test() throws Exception { | ||
loop( | ||
true, | ||
ISSNS_CYCLES, | ||
MOCKS, | ||
m -> m.issns().val(), | ||
issn -> assertTrue(validator.validate(issn)!=null) | ||
); | ||
System.out.println(M.issns().val()); | ||
} | ||
} |