Skip to content

Instantly share code, notes, and snippets.

@alvarogarcia7
Last active June 20, 2018 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvarogarcia7/46917d8d160d46b145de8262e3965670 to your computer and use it in GitHub Desktop.
Save alvarogarcia7/46917d8d160d46b145de8262e3965670 to your computer and use it in GitHub Desktop.
Testing randomly generated values (pin codes)
package com.example;
import java.util.Objects;
public class PinCode {
public final String value;
public PinCode (final String value) {
this.value = value;
}
@Override
public boolean equals (final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final PinCode pinCode = (PinCode) o;
return Objects.equals(value, pinCode.value);
}
@Override
public int hashCode () {
return Objects.hash(value);
}
@Override
public String toString () {
final StringBuffer sb = new StringBuffer("PinCode{");
sb.append("value='").append(value).append('\'');
sb.append('}');
return sb.toString();
}
}
package com.example;
import com.example.Pincode;
import java.net.URI;
import java.security.SecureRandom;
import java.util.Random;
public class PinCodeFactory {
private Random random;
public PinCodeFactory () {
random = new SecureRandom();
}
public PinCode aNewPinCode () {
final String payload = String.format("%06d", random.nextInt(1_000_000));
final PinCode pinCode = new PinCode(payload);
return pinCode;
}
protected void setGenerator (final Random generator) {
this.random = generator;
}
}
package com.example;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.function.Consumer;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class PinCodeFactoryTest {
private PinCodeFactory pinCodeFactory;
@Before
public void setUp () {
pinCodeFactory = new PinCodeFactory();
pinCodeFactory.setGenerator(new Random(1L));
}
@Test
public void there_are_no_repeated_with_the_given_seed () {
final int desiredProofSize = 100;
verifyAll(pinCodes(desiredProofSize), pinCodes -> assertThat(pinCodes.size(), is(desiredProofSize)));
}
@Test
public void the_numbers_are_left_padded_with_zeros () {
verifyEachOf(pinCodes(100), pincode -> assertThat(pincode.length(), is(6)));
}
@Test
public void the_numbers_do_not_contain_spaces () {
verifyEachOf(pinCodes(100), pincode -> assertThat(pincode.contains(" "), is(false)));
}
public void verifyEachOf (final Collection<String> pincodes, final Consumer<String> assertion) {
for (final String pincode : pincodes) {
assertion.accept(pincode);
}
}
public void verifyAll (final Collection<String> pincodes, final Consumer<Collection<String>> assertion) {
assertion.accept(pincodes);
}
private Set<String> pinCodes (final int desiredProofSize) {
return Stream.generate(() -> pinCodeFactory.aNewPinCode().value)
.limit(desiredProofSize)
.collect(Collectors.toSet());
}
}
@pierre-jean
Copy link

If you want, you can maybe change this:

        private HashSet<String> generatePinCodes (final int desiredProofSize) {
        final HashSet<String> pincodes = new HashSet<>();
        for (int i = 0; i < desiredProofSize; i++) {
            final String pincode = pinCodeFactory.aNewPinCode().value;
            pincodes.add(pincode);
        }
        return pincodes;
}

to

    private HashSet<String> generatePinCodes (final int desiredProofSize) {
    return Stream.generate(() -> pinCodeFactory.aNewPinCode().value)
                .limit(desiredProofSize)
                .collect(Collectors.toSet());
}

If your java level is above 1.8.
I don't know if it does improve clarity so much though (expected avoided a useless counter i) but that's the only thing I can suggest from that code.

@alvarogarcia7
Copy link
Author

Cool trick, @pierre-jean :) 👍

@johnhearn
Copy link

Maybe the test would be stronger if it matched a regex pattern instead of just checking for length and the existence of spaces?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment