11package com .codedifferently .lesson9 .generator ;
22
3- // ENHANCEMENT: Added DataProvider import to support provider-aware generation
43import com .codedifferently .lesson9 .dataprovider .DataProvider ;
5- import com .codedifferently .lesson9 .generator .Generators .*;
4+ import com .codedifferently .lesson9 .generator .Generators .BooleanValueGenerator ;
5+ import com .codedifferently .lesson9 .generator .Generators .DoubleValueGenerator ;
6+ import com .codedifferently .lesson9 .generator .Generators .FloatValueGenerator ;
7+ import com .codedifferently .lesson9 .generator .Generators .IntValueGenerator ;
8+ import com .codedifferently .lesson9 .generator .Generators .LongValueGenerator ;
9+ import com .codedifferently .lesson9 .generator .Generators .ShortValueGenerator ;
10+ import com .codedifferently .lesson9 .generator .Generators .StringValueGenerator ;
611import com .google .gson .GsonBuilder ;
712import java .io .File ;
813import java .io .FileWriter ;
1419import java .util .List ;
1520import java .util .Map ;
1621
17- /**
18- * A class to generate sample files with random data.
19- *
20- * <p>ENHANCEMENT SUMMARY: - Added provider-aware generation capability for test compatibility -
21- * Fixed critical bugs in row generation logic - Modernized code with Java 14+ switch expressions -
22- * Improved type safety with proper generic usage
23- *
24- * <p>CHANGES MADE: 1. NEW FEATURE: Provider-aware generation via createTestFileForProvider() 2. BUG
25- * FIX: Fixed double row generation in createSampleData() 3. BUG FIX: Fixed ignored parameter in
26- * createRow() method 4. ENHANCEMENT: Modern switch expressions in generateValueForType() 5. TYPE
27- * SAFETY: Added Class<?> wildcards for better generics 6. BACKWARD COMPATIBILITY: All original
28- * methods preserved
29- */
22+ /** A class to generate a sample file with random data. */
3023public class SampleFileGenerator {
3124
3225 private static final ValueGenerator [] GENERATORS = {
@@ -40,8 +33,8 @@ public class SampleFileGenerator {
4033 };
4134
4235 /**
43- * ORIGINAL METHOD: Create a test file with random sample data. This method uses shuffled
44- * generators to create random data types for each column. Preserved for backward compatibility.
36+ * Create a test file with random sample data. This method uses shuffled generators to create
37+ * random data types for each column. Preserved for backward compatibility.
4538 *
4639 * @param path the path to the directory where the file will be created
4740 * @param providerName the name of the provider
@@ -53,9 +46,9 @@ public void createTestFile(String path, String providerName) {
5346 }
5447
5548 /**
56- * NEW METHOD: Create a test file with sample data that matches the DataProvider's expected types.
57- * This is the enhanced version that ensures test compatibility by generating data according to
58- * the provider's type specifications.
49+ * Create a test file with sample data that matches the DataProvider's expected types. This is the
50+ * enhanced version that ensures test compatibility by generating data according to the provider's
51+ * type specifications.
5952 *
6053 * @param path the path to the directory where the file will be created
6154 * @param provider the DataProvider to generate data for
@@ -72,9 +65,6 @@ private List<ValueGenerator> getShuffledGenerators() {
7265 return generators ;
7366 }
7467
75- // BUG FIX: Fixed the double-generation bug in this method
76- // BEFORE: Created row twice per iteration (unused variable + actual add)
77- // AFTER: Clean single row generation per iteration
7868 private ArrayList <Map <String , String >> createSampleData (List <ValueGenerator > generators ) {
7969 var rows = new ArrayList <Map <String , String >>();
8070 for (var i = 0 ; i < 10 ; ++i ) {
@@ -96,9 +86,6 @@ private ArrayList<Map<String, String>> createSampleDataForProvider(DataProvider
9686 return rows ;
9787 }
9888
99- // BUG FIX: Fixed the ignored parameter bug in this method
100- // BEFORE: Ignored 'generators' parameter and used static GENERATORS array
101- // AFTER: Properly uses the passed-in generators parameter
10289 private Map <String , String > createRow (List <ValueGenerator > generators ) {
10390 var row = new LinkedHashMap <String , String >();
10491 for (int i = 0 ; i < generators .size (); ++i ) { // Fixed: use parameter size
@@ -144,6 +131,19 @@ private String generateValueForType(Class<?> type) { // Wildcard - best practice
144131 };
145132 }
146133
134+ /**
135+ * Generate a file for a specific DataProvider. This method handles the complete file generation
136+ * process including path setup.
137+ *
138+ * @param path the path to the directory where the file will be created
139+ * @param provider the DataProvider to generate data for
140+ */
141+ public void generateSampleFileForProvider (String path , DataProvider provider ) {
142+ // Use the provider-aware method to ensure correct data types
143+ createTestFileForProvider (path , provider );
144+ System .out .println ("Generated file for provider: " + provider .getProviderName ());
145+ }
146+
147147 private void saveToJsonFile (
148148 String path , String providerName , ArrayList <Map <String , String >> rows ) {
149149 var file = new File (path + File .separator + providerName + ".json" );
0 commit comments