![JAR search and dependency download from the Maven repository](/logo.png)
autofixture.generators.InetAddressGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of autofixturegenerator Show documentation
Show all versions of autofixturegenerator Show documentation
An attempt to reimplement core features of a popular .NET anonymous value generator - AutoFixture - in
Java
The newest version!
package autofixture.generators;
import autofixture.exceptions.ObjectCreationException;
import autofixture.interfaces.FixtureContract;
import autofixture.interfaces.InstanceGenerator;
import autofixture.interfaces.InstanceType;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
public class InetAddressGenerator implements InstanceGenerator {
public static final int NUMBER_OF_BYTES_IN_IP_ADDRESS = 4;
private final Random random = new Random();
@Override
public boolean appliesTo(final InstanceType instanceType) {
return instanceType.isRawTypeAssignableFrom(InetAddress.class);
}
@SuppressWarnings("unchecked")
@Override
public T next(final InstanceType instanceType, final FixtureContract fixture) {
final byte[] bytes = new byte[NUMBER_OF_BYTES_IN_IP_ADDRESS];
random.nextBytes(bytes);
final InetAddress address;
try {
address = Inet4Address.getByAddress(bytes);
} catch (final UnknownHostException e) {
throw new ObjectCreationException(instanceType, e);
}
return (T) address;
}
@Override
public T nextEmpty(final InstanceType instanceType, final FixtureContract fixture) {
return next(instanceType, fixture);
}
@Override
public void setOmittingAutoProperties(final boolean isOn) {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy