From 3976f08dd08ca5c09ca8b477a1be727e8ab4dc69 Mon Sep 17 00:00:00 2001 From: Yassine Date: Fri, 7 Jan 2022 14:48:44 +0100 Subject: [PATCH 1/5] Ajout de la javadoc pour les classes generateurs + interface Generator --- .../java/org/atlanmod/testing/Generator.java | 21 ++++++++++++++++++ .../java/org/atlanmod/testing/Verifier.java | 2 +- .../generator/IntegerBoundaryGenerator.java | 2 +- .../generator/RandomBooleanGenerator.java | 13 +++++++++-- .../generator/RandomByteGenerator.java | 14 ++++++++++-- .../generator/RandomCharGenerator.java | 13 +++++++++-- .../generator/RandomIntegerGenerator.java | 22 ++++++++++++++----- .../generator/RandomStringGenerator.java | 15 ++++++++++--- .../generator/RandomStringOfIntGenerator.java | 14 ++++++++---- .../generator/StringBoundaryGenerator.java | 8 +------ 10 files changed, 97 insertions(+), 27 deletions(-) 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..1a60b6cd 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,31 @@ */ package org.atlanmod.testing; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +/** + * + * 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/Verifier.java b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java index 067686da..0d356226 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java @@ -150,7 +150,7 @@ private static Optional> getGeneratorsForConstructor (Constructo } /** - *Generate a new instance of a constructor and return it. + * 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 */ 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..9bc92c8c 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,7 +9,6 @@ import org.atlanmod.commons.Guards; import org.atlanmod.testing.Generator; - import java.util.Arrays; public class IntegerBoundaryGenerator implements Generator { @@ -18,6 +17,7 @@ public class IntegerBoundaryGenerator implements Generator { private int index = 0; public IntegerBoundaryGenerator(){ + this(new int[]{Integer.MIN_VALUE, 0, Integer.MAX_VALUE}); } 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..aadabdd1 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 @@ -11,11 +11,18 @@ import java.util.Random; +/** + * + * Generator class for values of the type Boolean. + * + */ public class RandomBooleanGenerator implements Generator { @Override /** - * Generate a boolean. + * Generates a random boolean value. + * + * @return a boolean value */ public Boolean generate() { Random r = new Random(); @@ -25,7 +32,9 @@ public Boolean generate() { @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}; 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..50eba848 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 @@ -11,22 +11,32 @@ import java.io.UnsupportedEncodingException; import java.util.Random; +/** + * + * Generator class for values of the type Byte. + * + */ public class RandomByteGenerator implements Generator { @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); + 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}; 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..bf942fb1 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 @@ -11,10 +11,17 @@ import java.util.Random; +/** + * + * Generator class for values of the type Char. + * + */ public class RandomCharGenerator implements Generator { @Override /** - *Generate a char. + * Generates random Char values, alphabets or numerical characters . + * + * @return a single char value */ public Character generate() { Random random = new Random(); @@ -29,7 +36,9 @@ public Character generate() { @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}; 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..ea2850ce 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 @@ -6,32 +6,44 @@ * this distribution, and is available at https://www.eclipse.org/legal/epl-2.0/ */ package org.atlanmod.testing.generator; +import org.atlanmod.testing.EqualsVerifier; import org.atlanmod.testing.Generator; +import org.atlanmod.testing.SerializationVerifier; +import org.atlanmod.testing.Verifier; import java.util.Random; +/** + * + * Generator class for values of the type Integer. + */ public class RandomIntegerGenerator implements Generator { - @Override /** - *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; } @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}; + Class[] types={ Integer.class, int.class }; return types; } } 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..d4a79718 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 @@ -10,12 +10,19 @@ import java.util.Random; +/** + * + * Generator class for values of the type String. + * + */ public class RandomStringGenerator implements Generator { - @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; @@ -34,7 +41,9 @@ 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}; 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..19e400fd 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,15 +8,19 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; +/** + * * Generator class for String values composed of only numerical characters. + * + */ public class RandomStringOfIntGenerator implements Generator { - @Override /** - *Generate a string. + * Generates random String values composed of numerical characters. + * + * @return a single String value */ public String generate() { Random random = new Random(); @@ -32,7 +36,9 @@ 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}; 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..64ecb682 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 @@ -35,11 +35,5 @@ 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()); - } - } + } From 55df2539d8c51fb05a022b43ea9ce97e8147739a Mon Sep 17 00:00:00 2001 From: ibou07 Date: Fri, 7 Jan 2022 15:07:37 +0100 Subject: [PATCH 2/5] javadoc --- .../testing/SerializationVerifier.java | 31 ++++++- .../java/org/atlanmod/testing/Verifier.java | 80 ++++++++++++------- .../generator/RandomIntegerGenerator.java | 5 +- .../generator/StringBoundaryGenerator.java | 12 +-- 4 files changed, 87 insertions(+), 41 deletions(-) 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..debddb80 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/SerializationVerifier.java @@ -13,17 +13,40 @@ 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 +58,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); 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..dca00f61 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java @@ -38,18 +38,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 +61,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 +72,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 +80,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 +114,7 @@ public Object generate() { } return list; } + @Override public Class[] types() { List listTypes = new ArrayList<>(); @@ -120,28 +127,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 +167,14 @@ 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,32 @@ 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 +219,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/RandomIntegerGenerator.java b/commons-testing/src/main/java/org/atlanmod/testing/generator/RandomIntegerGenerator.java index 9790062e..df78b613 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 @@ -21,8 +21,9 @@ public Integer generate() { int max= 20; Random r = new Random(); int value = r.nextInt((max - min) + 1); - boolean bool = r.nextBoolean(); - // if(!bool) value=-1*value; + boolean booleen; + booleen = r.nextBoolean(); + if(!booleen) value=-1*value; return value; } 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..f87d9985 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 @@ -17,7 +17,7 @@ public class StringBoundaryGenerator implements Generator { private int index; public StringBoundaryGenerator() { - this(new String[] {"monsieur", "yassine", "el", "kamel", ""}); + this(new String[]{"monsieur", "yassine", "el", "kamel", ""}); } public StringBoundaryGenerator(String[] values) { @@ -34,12 +34,4 @@ public String generate() { 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()); - } - } -} +} \ No newline at end of file From 93f80af100ccd8ce0e9ab1f53cb3be807cd64f25 Mon Sep 17 00:00:00 2001 From: ibou07 <72793309+ibou07@users.noreply.github.com> Date: Fri, 7 Jan 2022 16:08:12 +0100 Subject: [PATCH 3/5] Update RandomIntegerGenerator.java --- .../generator/RandomIntegerGenerator.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) 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 df78b613..ea2850ce 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 @@ -6,33 +6,44 @@ * this distribution, and is available at https://www.eclipse.org/legal/epl-2.0/ */ package org.atlanmod.testing.generator; +import org.atlanmod.testing.EqualsVerifier; import org.atlanmod.testing.Generator; +import org.atlanmod.testing.SerializationVerifier; +import org.atlanmod.testing.Verifier; import java.util.Random; +/** + * + * Generator class for values of the type Integer. + */ public class RandomIntegerGenerator implements Generator { - @Override /** - *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 booleen; - booleen = r.nextBoolean(); - if(!booleen) value=-1*value; + boolean bool = r.nextBoolean(); + return value; } @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}; + Class[] types={ Integer.class, int.class }; return types; } } From 6ed4dd5ca49091334b4469a94dd6bfa416fbfd4e Mon Sep 17 00:00:00 2001 From: ibou07 <72793309+ibou07@users.noreply.github.com> Date: Fri, 7 Jan 2022 16:17:48 +0100 Subject: [PATCH 4/5] Update Verifier.java --- .../java/org/atlanmod/testing/Verifier.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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 dca00f61..6a03b97c 100644 --- a/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java +++ b/commons-testing/src/main/java/org/atlanmod/testing/Verifier.java @@ -81,7 +81,7 @@ 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. @@ -95,7 +95,7 @@ public static void registerGenerator(Generator 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 @@ -128,14 +128,14 @@ 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 @@ -168,11 +168,12 @@ private static Optional> getGeneratorsForConstructor(Constructor /** * 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 @@ -197,7 +198,8 @@ public static Object generateConstructor(Constructor construc) { * 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 From 0668ae2f765e3a5079722f70919f10cd5b8870ec Mon Sep 17 00:00:00 2001 From: ibou07 Date: Fri, 7 Jan 2022 17:32:26 +0100 Subject: [PATCH 5/5] fix codacy problems --- .../java/org/atlanmod/testing/Generator.java | 5 ---- .../testing/SerializationVerifier.java | 3 +-- .../java/org/atlanmod/testing/Verifier.java | 1 - .../generator/IntegerBoundaryGenerator.java | 24 +++++++++++++++---- .../generator/RandomBooleanGenerator.java | 9 +++---- .../generator/RandomByteGenerator.java | 9 +++---- .../generator/RandomCharGenerator.java | 10 ++++---- .../generator/RandomIntegerGenerator.java | 15 +++--------- .../generator/RandomStringGenerator.java | 7 +++--- .../generator/RandomStringOfIntGenerator.java | 7 +++--- .../generator/StringBoundaryGenerator.java | 24 +++++++++++++++---- 11 files changed, 59 insertions(+), 55 deletions(-) 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 1a60b6cd..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,11 +7,6 @@ */ package org.atlanmod.testing; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - /** * * An object able to randomly generate values of different data 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 debddb80..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,7 +8,6 @@ package org.atlanmod.testing; import org.atlanmod.commons.reflect.MoreReflection; - import java.io.*; import java.util.function.Function; import java.util.stream.Stream; @@ -72,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 6a03b97c..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; 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 9bc92c8c..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 @@ -10,14 +10,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 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 aadabdd1..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,7 +8,6 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; /** @@ -17,6 +16,7 @@ * */ public class RandomBooleanGenerator implements Generator { + private Random random = new Random(); @Override /** @@ -25,9 +25,7 @@ public class RandomBooleanGenerator implements Generator { * @return a boolean value */ public Boolean generate() { - Random r = new Random(); - boolean bool = r.nextBoolean(); - return bool; + return random.nextBoolean(); } @Override @@ -37,7 +35,6 @@ public Boolean 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 50eba848..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,8 +7,6 @@ */ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - -import java.io.UnsupportedEncodingException; import java.util.Random; /** @@ -17,6 +15,7 @@ * */ public class RandomByteGenerator implements Generator { + private Random random = new Random(); @Override /** @@ -25,9 +24,8 @@ public class RandomByteGenerator implements Generator { * @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]; } @@ -39,7 +37,6 @@ public Byte 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 bf942fb1..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,7 +8,6 @@ package org.atlanmod.testing.generator; import org.atlanmod.testing.Generator; - import java.util.Random; /** @@ -17,6 +16,8 @@ * */ public class RandomCharGenerator implements Generator { + private Random random = new Random(); + @Override /** * Generates random Char values, alphabets or numerical characters . @@ -24,14 +25,12 @@ public class RandomCharGenerator implements Generator { * @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 @@ -41,7 +40,6 @@ public Character 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 ea2850ce..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 @@ -6,11 +6,7 @@ * this distribution, and is available at https://www.eclipse.org/legal/epl-2.0/ */ package org.atlanmod.testing.generator; -import org.atlanmod.testing.EqualsVerifier; import org.atlanmod.testing.Generator; -import org.atlanmod.testing.SerializationVerifier; -import org.atlanmod.testing.Verifier; - import java.util.Random; /** @@ -18,7 +14,7 @@ * Generator class for values of the type Integer. */ public class RandomIntegerGenerator implements Generator { - + private Random random = new Random(); /** * Generates a single integer at a time * Produced values are random using class java.util.Random @@ -29,11 +25,7 @@ public class RandomIntegerGenerator implements Generator { public Integer generate() { int min= 0; int max= 20; - Random r = new Random(); - int value = r.nextInt((max - min) + 1); - boolean bool = r.nextBoolean(); - - return value; + return random.nextInt((max - min) + 1); } @Override @@ -43,7 +35,6 @@ public Integer 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 d4a79718..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,8 +6,8 @@ * 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; /** @@ -16,6 +16,7 @@ * */ public class RandomStringGenerator implements Generator { + private Random random = new Random(); /** * Generates random String values containing a mix of numerical characters and alphabets. @@ -24,7 +25,6 @@ public class RandomStringGenerator implements Generator { */ @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++) { @@ -46,7 +46,6 @@ public String 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 19e400fd..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 @@ -14,7 +14,8 @@ * * Generator class for String values composed of only numerical characters. * */ -public class RandomStringOfIntGenerator implements Generator { +public class RandomStringOfIntGenerator implements Generator { + private Random random = new Random(); @Override /** @@ -23,7 +24,6 @@ public class RandomStringOfIntGenerator implements Generator { * @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++) { @@ -41,7 +41,6 @@ public String 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 f87d9985..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,13 +9,16 @@ 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", ""}); } @@ -25,13 +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]; + return new Class[]{String.class}; } } \ No newline at end of file