diff --git a/commons-testing/src/main/java/org/atlanmod/testing/Generator.java b/commons-testing/src/main/java/org/atlanmod/testing/Generator.java index 1d168eae..b905d74b 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/Generator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/Generator.java @@ -7,10 +7,26 @@ */ package org.atlanmod.testing; - +/** + * + * An object able to randomly generate values of different data types + * + * @param the type of the values to be generated + */ public interface Generator { + /** + * function responsible for generating values + * + * @return single value of the type T + */ T generate() ; + + /** + * Returns all of the data types the current class is able to generate. + * + * @return an array of class types. + */ Class[] types(); } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java b/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java index 64b406d7..2c1432f4 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java @@ -8,22 +8,44 @@ package org.atlanmod.testing; import org.atlanmod.commons.reflect.MoreReflection; - import java.io.*; import java.util.function.Function; import java.util.stream.Stream; - - +/** + * Verifies that the {@code serializable()} interface of a class was correctly implemented, checking that: + * + * - An object is serializable and serialize it by converting it into a sequence (stream) of bytes + * - Deserialize by reading the stream of bytes and convert it back into a Java object + * - The resulting object is equals to the object we want to test. + * + * @param + */ public class SerializationVerifier { private Class type; private Object[] arguments; private static final long serialVersionUID = 1623437; + /** + * Creates an instance of {@code SerializationVerifier} for class {@code type}. + * + * @param type the class to be verified. + */ SerializationVerifier(Class type) { this.type = type; } + /** + * Arguments are used to create a reference instance of class {@code type}. The reference instance is used + * to be serialized,deserialized in another object and compare it to the resulting object. + * + * The types and the length of {@code arguments} must match either a visible constructor or a static + * method (a factory) that returns an instance of {@code type}. + * + * + * @param arguments an array of instances of {@link Object}. + * @return the current verifier. + */ public SerializationVerifier withArguments(Object... arguments) { this.arguments = arguments; return this; @@ -35,6 +57,10 @@ private static Class[] mapToClasses(Object[] objects) { .toArray(Class[]::new); } + /** + * Verifies the implementation of interface {@code serializable()}. + * + */ public void check() throws IOException, ClassNotFoundException { Class[] argumentTypes = mapToClasses(arguments); Function instantiator = MoreReflection.getInstantiator(type, argumentTypes); @@ -45,7 +71,7 @@ public void check() throws IOException, ClassNotFoundException { oos.writeObject(object); //deserialiser object ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(boos.toByteArray())); - Object object2 = (Object) ois.readObject(); + Object object2 = ois.readObject(); assertIsEqual(object,object2); } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java index 067686da..4ad2c6a1 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java @@ -9,7 +9,6 @@ import org.atlanmod.commons.Throwables; import org.atlanmod.testing.generator.*; - import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -38,18 +37,18 @@ * */ public class Verifier { - private static final Map,Generator> generators= new HashMap<>(); + private static final Map, Generator> generators = new HashMap<>(); private static Generator stringGenerator = new RandomStringGenerator(); private static Generator integerGenerator = new RandomIntegerGenerator(); private static Generator charGenerator = new RandomCharGenerator(); - //private static Generator byteGenerator= new RandomByteGenerator(); + private static Generator byteGenerator= new RandomByteGenerator(); private static Generator booleanGenerator = new RandomBooleanGenerator(); static { registerGenerator(integerGenerator); registerGenerator(stringGenerator); registerGenerator(charGenerator); - //registerGenerator(byteGenerator); + registerGenerator(byteGenerator); registerGenerator(booleanGenerator); } @@ -61,7 +60,7 @@ private Verifier() { * Creates a {@link EqualsVerifier} for class {@code type}. * * @param type the class whose {@code equals()} method will be verified. - * @param the actual class of the class {@type}. + * @param the actual class of the class {@type}. * @return an instance of {@link EqualsVerifier}. */ public static EqualsVerifier verifyEqualsOf(Class type) { @@ -72,7 +71,7 @@ public static EqualsVerifier verifyEqualsOf(Class type) { * Creates a {@link SerializationVerifier} for class {@code type}. * * @param type the class whose {@code serialize()} method will be verified. - * @param the actual class of the class {@type}. + * @param the actual class of the class {@type}. * @return an instance of {@link SerializationVerifier}. */ public static SerializationVerifier verifySerialization(Class type) { @@ -80,25 +79,31 @@ public static SerializationVerifier verifySerializat } /** + * Register a new generator by adding it in the hashmap generators. + * * Register a new generator for a specific type. + * * @param generator the generator of the target class. - * @param the target class to generate. + * @param the target class to generate. */ - public static void registerGenerator(Generator generator) { - for (Class type :generator.types() ) { + public static void registerGenerator(Generator generator) { + for (Class type : generator.types()) { generators.put(type, generator); } } /** - *creation of an array generator from his simple generator. - * @param gen the simple generator + * Return a {@link Generator} that generates an array of type {@code arrayType} + * + * creation of an array generator from his simple generator. + * + * @param gen the simple generator * @param arrayType the class of the array generator we want to create * @return An array generator */ - public static Generator createArrayGenerator(Generator gen,Class arrayType) { - Random random =new Random(); - int length= random.nextInt(10) + 1; + public static Generator createArrayGenerator(Generator gen, Class arrayType) { + Random random = new Random(); + int length = random.nextInt(10) + 1; return new Generator() { @Override public Object generate() { @@ -108,6 +113,7 @@ public Object generate() { } return list; } + @Override public Class[] types() { List listTypes = new ArrayList<>(); @@ -120,28 +126,38 @@ public Class[] types() { } /** + * Returns the appropriate generators for each of {@code constr} parameters + * + * For each of {@code constr} parameters : + * - if the parameter is a singular value and its generator is available : add generator to the list {@code generate}. + * - if the parameter is an array and a generator for singular value is available : + * call to createArrayGenerator, new array generator is added to the list {@code generate} + * - if the parameter is a singular value or array but the generator for singular value is not available : return an empty instance of {@link Optional} + * + * * Provide an optional list which contains the specific generator of each parameter of the constructor + * * @param constr the constructor to be verified * @return An optional list of generator */ - private static Optional> getGeneratorsForConstructor (Constructor constr) { - if(constr.getParameters().length==0) { + private static Optional> getGeneratorsForConstructor(Constructor constr) { + if (constr.getParameters().length == 0) { return Optional.of(Collections.emptyList()); } List generate = new ArrayList<>(); for (Class type : constr.getParameterTypes()) { Generator newgen = generators.get(type); - if (newgen==null && type.isArray()) { + if (newgen == null && type.isArray()) { Class arrayType = type.getComponentType(); newgen = generators.get(arrayType); - if (newgen!=null) { + if (newgen != null) { //Creer le nouveau generateur d'array à partir de newgen et l'ajouter à generators Generator newGenerator = createArrayGenerator(newgen, arrayType); registerGenerator(newGenerator); newgen = newGenerator; } } - if(newgen==null) { + if (newgen == null) { return Optional.empty(); } generate.add(newgen); @@ -150,7 +166,15 @@ private static Optional> getGeneratorsForConstructor (Constructo } /** - *Generate a new instance of a constructor and return it. + * Return a new instance of the constructor {@code construc} + * + * Get the list of generators of the different parameters of this constructor using {@code getGeneratorsForConstructor}. + * Then call each of these generators to generate a value and add it to a list of objects. + * Create a new instance of the constructor {@code construc} using the generated values. + * + * + * Generate a new instance of a constructor and return it. + * * @param construc the constructor we want to generate * @return A new instance of a constructor */ @@ -161,26 +185,33 @@ public static Object generateConstructor(Constructor construc) { generatedArguments.add(gen.generate()); } try { - return construc.newInstance(generatedArguments.toArray()); - } catch (InstantiationException | IllegalAccessException |InvocationTargetException e ) { + return construc.newInstance(generatedArguments.toArray()); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); return e.getCause().getClass(); } } /** + * Get all the constructor of the class {@code klass} + * For each constructor : + * - Get the list of generators of the different parameters using {@code getGeneratorsForConstructor} + * - If the list is not empty then generate the values and create a new instance of the constructor. + * + * * Generate a new instance of the constructors of a class and put them in a list. + * * @param klass the class we want to generate all constructors * @return A list of objects of type klass */ - public static List generateConstructorsOfClass(Class klass) { + public static List generateConstructorsOfClass(Class klass) { if (klass.getName().equals(Integer.class.getName())) { registerGenerator(new RandomStringOfIntGenerator()); } List listConstructors = new ArrayList<>(); for (Constructor each : klass.getConstructors()) { Optional> optionalGeneratorList = getGeneratorsForConstructor(each); - if(optionalGeneratorList.isPresent()) { + if (optionalGeneratorList.isPresent()) { listConstructors.add(generateConstructor(each)); } } @@ -189,8 +220,4 @@ public static List generateConstructorsOfClass(Class klass) { } return listConstructors; } - - public static void main(String[] args) { - generateConstructorsOfClass(String.class); - } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/IntegerBoundaryGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/IntegerBoundaryGenerator.java index 43abf677..5f0aa65d 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/IntegerBoundaryGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/IntegerBoundaryGenerator.java @@ -9,13 +9,17 @@ import org.atlanmod.commons.Guards; import org.atlanmod.testing.Generator; - import java.util.Arrays; +import java.util.Random; +/** + * + * Generator class for bound values of the type Integer. + */ public class IntegerBoundaryGenerator implements Generator { private int[] values; - private int index = 0; + private Random random = new Random(); public IntegerBoundaryGenerator(){ this(new int[]{Integer.MIN_VALUE, 0, Integer.MAX_VALUE}); @@ -26,13 +30,25 @@ public IntegerBoundaryGenerator(int[] values) { this.values = Arrays.copyOfRange(values, 0, values.length) ; } + /** + * Generates a single integer at a time + * Produced values are random using class java.util.Random + * + * @return a single integer value + */ @Override public Integer generate() { - return this.values[index % values.length]; + int index = random.nextInt(values.length); + return this.values[index]; } + /** + * Returns all of the variation of the integer data type the current class is able to generate. + * + * @return an array of class types. + */ @Override public Class[] types() { - return new Class[0]; + return new Class[]{Integer.class, int.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomBooleanGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomBooleanGenerator.java index 0f43c1c7..2806867e 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomBooleanGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomBooleanGenerator.java @@ -8,27 +8,33 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; +/** + * + * Generator class for values of the type Boolean. + * + */ public class RandomBooleanGenerator implements Generator { + private Random random = new Random(); @Override /** - * Generate a boolean. + * Generates a random boolean value. + * + * @return a boolean value */ public Boolean generate() { - Random r = new Random(); - boolean bool = r.nextBoolean(); - return bool; + return random.nextBoolean(); } @Override /** - * return an array of class which contains the boolean class. + * Returns all of the variation of the boolean data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={Boolean.class}; - return types; + return new Class[]{Boolean.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomByteGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomByteGenerator.java index 35d691ee..3aa0fc8e 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomByteGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomByteGenerator.java @@ -7,29 +7,36 @@ */ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - -import java.io.UnsupportedEncodingException; import java.util.Random; +/** + * + * Generator class for values of the type Byte. + * + */ public class RandomByteGenerator implements Generator { + private Random random = new Random(); @Override /** - *Generate a byte. + * Generates random Byte values. + * + * @return a single Byte value */ public Byte generate() { - Random rd = new Random(); byte[] arr = new byte[7]; - rd.nextBytes(arr); + random.nextBytes(arr); + return arr[2]; } @Override /** - * return an array of class which contains the byte class. + * Returns all of the variation of the Byte data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={Byte.class,byte.class}; - return types; + return new Class[]{Byte.class, byte.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomCharGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomCharGenerator.java index fb7eeea0..b12c9e04 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomCharGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomCharGenerator.java @@ -8,31 +8,38 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; +/** + * + * Generator class for values of the type Char. + * + */ public class RandomCharGenerator implements Generator { + private Random random = new Random(); + @Override /** - *Generate a char. + * Generates random Char values, alphabets or numerical characters . + * + * @return a single char value */ public Character generate() { - Random random = new Random(); int randomInt = random.nextInt(10) + 48; int randomUpperCaseAlphabet = random.nextInt(26) + 65; int randomLowerCaseAlphabet = random.nextInt(26) + 97; int[] possibleValues = {randomInt, randomLowerCaseAlphabet, randomUpperCaseAlphabet}; int choice = random.nextInt(3); - char generatedChar = (char)possibleValues[choice]; - return generatedChar; + return (char)possibleValues[choice]; } @Override /** - *return an array of class which contains the Character class. + * Returns all of the variation of the Char data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={Character.class}; - return types; + return new Class[]{Character.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomIntegerGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomIntegerGenerator.java index 9790062e..0740e036 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomIntegerGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomIntegerGenerator.java @@ -7,31 +7,34 @@ */ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; +/** + * + * Generator class for values of the type Integer. + */ public class RandomIntegerGenerator implements Generator { - - @Override + private Random random = new Random(); /** - *Generate an integer. + * Generates a single integer at a time + * Produced values are random using class java.util.Random + * + * @return a single integer value */ + @Override public Integer generate() { int min= 0; int max= 20; - Random r = new Random(); - int value = r.nextInt((max - min) + 1); - boolean bool = r.nextBoolean(); - // if(!bool) value=-1*value; - return value; + return random.nextInt((max - min) + 1); } @Override /** - *return an array of class which contains the integer and int class. + * Returns all of the variation of the integer data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={Integer.class,int.class}; - return types; + return new Class[]{Integer.class, int.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringGenerator.java index 54654389..a46ab166 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringGenerator.java @@ -6,18 +6,25 @@ * this distribution, and is available at https://www.eclipse.org/legal/epl-2.0/ */ package org.atlanmod.testing.generator; -import org.atlanmod.testing.Generator; +import org.atlanmod.testing.Generator; import java.util.Random; +/** + * + * Generator class for values of the type String. + * + */ public class RandomStringGenerator implements Generator { + private Random random = new Random(); - @Override /** - *Generate a string. + * Generates random String values containing a mix of numerical characters and alphabets. + * + * @return a single String value */ + @Override public String generate() { - Random random = new Random(); int length= random.nextInt(10)+1; StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { @@ -34,10 +41,11 @@ public String generate() { @Override /** - *return an array of class which contains the string class. + * Returns all of the variations of the String data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={String.class}; - return types; + return new Class[]{String.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringOfIntGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringOfIntGenerator.java index 0764f4ff..f9b8e2d6 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringOfIntGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomStringOfIntGenerator.java @@ -8,18 +8,22 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; -public class RandomStringOfIntGenerator implements Generator { - +/** + * * Generator class for String values composed of only numerical characters. + * + */ +public class RandomStringOfIntGenerator implements Generator { + private Random random = new Random(); @Override /** - *Generate a string. + * Generates random String values composed of numerical characters. + * + * @return a single String value */ public String generate() { - Random random = new Random(); int length= random.nextInt(10)+1; StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { @@ -32,10 +36,11 @@ public String generate() { @Override /** - *return an array of class which contains the string class. + * Returns all of the variation of the String data type the current class is able to generate. + * + * @return an array of class types. */ public Class[] types() { - Class[] types={String.class}; - return types; + return new Class[]{String.class}; } } diff --git a/commons-testing/src/main/java/org/atlanmod/testing/generator/StringBoundaryGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/StringBoundaryGenerator.java index d25cbebc..8bc8fed0 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/generator/StringBoundaryGenerator.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/generator/StringBoundaryGenerator.java @@ -9,15 +9,18 @@ import org.atlanmod.commons.Guards; import org.atlanmod.testing.Generator; - import java.util.Arrays; +import java.util.Random; +/** + * + * Generator class for bound values of the type Integer. + */ public class StringBoundaryGenerator implements Generator { private String[] values; - private int index; - + private Random random = new Random(); public StringBoundaryGenerator() { - this(new String[] {"monsieur", "yassine", "el", "kamel", ""}); + this(new String[]{"monsieur", "yassine", "el", "kamel", ""}); } public StringBoundaryGenerator(String[] values) { @@ -25,21 +28,24 @@ public StringBoundaryGenerator(String[] values) { this.values = Arrays.copyOfRange(values, 0, values.length); } + /** + * Generates random String values containing a mix of numerical characters and alphabets. + * + * @return a single String value + */ @Override public String generate() { - return this.values[index % values.length]; + int index = random.nextInt(values.length); + return this.values[index]; } + /** + * Returns all of the variations of the String data type the current class is able to generate. + * + * @return an array of class types. + */ @Override public Class[] types() { - return new Class[0]; - } - - public static void main(String[] args) { - int i; - StringBoundaryGenerator ibou = new StringBoundaryGenerator(); - for (i = 0; i < 5; i++) { - System.out.println(ibou.generate()); - } + return new Class[]{String.class}; } -} +} \ No newline at end of file