Skip to content

Commit 182ab21

Browse files
feat: adds revisions and testing to bulk sample generator mode
1 parent 2a8b8c9 commit 182ab21

File tree

4 files changed

+324
-37
lines changed

4 files changed

+324
-37
lines changed

lesson_09/types/types_app/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,52 @@ Example:
6969
---
7070

7171
*Need help? Check the full documentation in the parent directory or run the tests with `./gradlew :types_app:test`*
72+
73+
## 🧪 Testing
74+
75+
### Running Tests
76+
77+
Execute all tests for the types application:
78+
79+
```bash
80+
cd /workspaces/code-society-25-2/lesson_09/types
81+
./gradlew test
82+
```
83+
84+
### Test Suites
85+
86+
#### Core Functionality Tests (`Lesson9Test.java`)
87+
- **DataProvider Configuration**: Validates that each provider is properly configured
88+
- **Data Parsing**: Tests type conversion and data processing
89+
- **File Loading**: Ensures JSON files can be loaded and parsed correctly
90+
- **Provider Uniqueness**: Verifies that provider names are unique
91+
92+
#### Bulk Mode Tests (`BulkModeTest.java`)
93+
- **Multi-Provider Processing**: Tests bulk generation with multiple DataProviders
94+
- **Case-Insensitive Arguments**: Verifies `--bulk`, `--BULK`, `--BuLk` all work
95+
- **Edge Case Handling**: Tests with empty provider lists and no arguments
96+
- **File Generation Integration**: Tests actual file creation using temporary directories
97+
- **Mode Separation**: Ensures bulk and single-provider modes work independently
98+
99+
### Test Coverage Details
100+
101+
The test suite covers:
102+
-**Argument Parsing**: All command-line argument combinations
103+
-**File Generation**: Both bulk and single-provider file creation
104+
-**Error Handling**: Graceful handling of edge cases
105+
-**Output Verification**: Console messages and user feedback
106+
-**Integration**: End-to-end workflow testing
107+
-**Data Type Accuracy**: Generated data matches provider specifications
108+
109+
### Quality Assurance
110+
111+
Run complete quality checks:
112+
```bash
113+
./gradlew check # Tests + formatting + linting
114+
./gradlew spotlessApply # Apply code formatting
115+
./gradlew spotlessCheck # Verify formatting compliance
116+
```
117+
118+
---
119+
120+
*Need help? Check the full documentation in the parent directory or run the tests with `./gradlew :types_app:test`*

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/Lesson9.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ public Lesson9(List<DataProvider> dataProviders) {
2424
this.dataProviders = dataProviders;
2525
}
2626

27-
private void generateFileFor(DataProvider provider) throws Exception {
28-
String path = getDataPath();
29-
var fileGenerator = new SampleFileGenerator();
30-
31-
// Use the provider-aware method to ensure correct data types
32-
fileGenerator.createTestFileForProvider(path, provider);
33-
34-
System.out.println("Generated file for provider: " + provider.getProviderName());
35-
}
36-
3727
public static void main(String[] args) {
3828
var application = new SpringApplication(Lesson9.class);
3929
// This removes the Spring Boot startup logs to make the output cleaner
@@ -50,8 +40,11 @@ public void run(String... args) throws Exception {
5040
System.out.println(" Running Bulk Mode ");
5141
System.out.println("==============================\n");
5242

43+
String path = getDataPath();
44+
var fileGenerator = new SampleFileGenerator();
45+
5346
for (DataProvider provider : dataProviders) {
54-
generateFileFor(provider);
47+
fileGenerator.generateSampleFileForProvider(path, provider);
5548
}
5649

5750
System.out.println("\n============ Done =============");

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/generator/SampleFileGenerator.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.codedifferently.lesson9.generator;
22

3-
// ENHANCEMENT: Added DataProvider import to support provider-aware generation
43
import 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;
611
import com.google.gson.GsonBuilder;
712
import java.io.File;
813
import java.io.FileWriter;
@@ -14,19 +19,7 @@
1419
import java.util.List;
1520
import 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. */
3023
public 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

Comments
 (0)