|
| 1 | +Migrate test sample files for $ARGUMENTS from the old location (`java-checks/src/test/files/checks/`) to the new location. |
| 2 | + |
| 3 | +Before starting, ask the user whether the sample is **compiling** or **non-compiling**: |
| 4 | +- **Compiling** samples go to: `java-checks-test-sources/default/src/main/java/checks/` |
| 5 | +- **Non-compiling** samples go to: `java-checks-test-sources/default/src/main/files/non-compiling/checks/` |
| 6 | + |
| 7 | +Follow these steps for each `*CheckTest.java` file being migrated: |
| 8 | + |
| 9 | +## 1. Identify the test file and its sample files |
| 10 | + |
| 11 | +Find the test class at `java-checks/src/test/java/org/sonar/java/checks/<CheckName>CheckTest.java`. Read it and identify ALL `.onFile("src/test/files/checks/...")` references. Each such reference points to an old sample file that needs migrating. |
| 12 | + |
| 13 | +## 2. Move each sample file to the new location with the correct name |
| 14 | + |
| 15 | +For each old sample file: |
| 16 | + |
| 17 | +- The **primary** sample (the main one used in the `test()` method) should be renamed to `<CheckName>CheckSample.java` in the destination directory. |
| 18 | +- **Variant** samples (used in other test methods, e.g. with custom config or different Java versions) should follow the convention `<CheckName>CheckSample_<variant>.java` or `<CheckName>Check_<variant>.java`, matching whatever variant suffix the old file had (e.g. `Custom`, `no_version`, `java8`). |
| 19 | +- The destination depends on the user's choice: |
| 20 | + - **Compiling**: `java-checks-test-sources/default/src/main/java/checks/` |
| 21 | + - **Non-compiling**: `java-checks-test-sources/default/src/main/files/non-compiling/checks/` |
| 22 | + |
| 23 | +Use `git mv` to move the file, then rename it if the filename changed. |
| 24 | + |
| 25 | +## 3. Add `package checks;` to the sample file (compiling samples only) |
| 26 | + |
| 27 | +For **compiling** samples: Insert `package checks;` as the very first line of the moved sample file, followed by a blank line before any existing content. |
| 28 | + |
| 29 | +For **non-compiling** samples: Do NOT add a package declaration. |
| 30 | + |
| 31 | +In both cases, this is the ONLY content modification to make to the sample. Do NOT rename classes, fix compilation errors, update imports, or make any other changes. Leave those for the user. |
| 32 | + |
| 33 | +## 4. Update the test file |
| 34 | + |
| 35 | +In the `*CheckTest.java` file: |
| 36 | + |
| 37 | +For **compiling** samples: |
| 38 | +- Add the import if not already present: |
| 39 | + ```java |
| 40 | + import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; |
| 41 | + ``` |
| 42 | +- Replace each old-style `.onFile("src/test/files/checks/<OldName>.java")` with: |
| 43 | + ```java |
| 44 | + .onFile(mainCodeSourcesPath("checks/<NewName>.java")) |
| 45 | + ``` |
| 46 | + |
| 47 | +For **non-compiling** samples: |
| 48 | +- Add the import if not already present: |
| 49 | + ```java |
| 50 | + import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath; |
| 51 | + ``` |
| 52 | +- Replace each old-style `.onFile("src/test/files/checks/<OldName>.java")` with: |
| 53 | + ```java |
| 54 | + .onFile(nonCompilingTestSourcesPath("checks/<NewName>.java")) |
| 55 | + ``` |
| 56 | + |
| 57 | +In both cases, `<NewName>` is the new filename chosen in step 2. |
| 58 | + |
| 59 | +## 5. Verify |
| 60 | + |
| 61 | +Run the specific test to verify nothing is broken: |
| 62 | +``` |
| 63 | +mvn test -pl java-checks -Dtest=<CheckName>CheckTest -Dsurefire.failIfNoSpecifiedTests=false |
| 64 | +``` |
| 65 | + |
| 66 | +If the test fails, report the failure to the user but do NOT attempt to fix the sample file content. |
| 67 | + |
| 68 | +## Example: migrating `LambdaTypeParameterCheck` |
| 69 | + |
| 70 | +**Old sample** at `java-checks/src/test/files/checks/LambdaTypeParameterCheck.java`: |
| 71 | +```java |
| 72 | +import java.util.function.BiConsumer; |
| 73 | +import java.util.function.Consumer; |
| 74 | + |
| 75 | +abstract class A { |
| 76 | + public void foo() { |
| 77 | + foo((String s) -> s.length()); // Compliant |
| 78 | + foo(s -> {return s.length();}); // Noncompliant {{Specify a type for: 's'}} |
| 79 | +// ^ |
| 80 | + } |
| 81 | + |
| 82 | + abstract void foo(Consumer<String> s); |
| 83 | + abstract void foo(BiConsumer<String, Object> bc); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +**New sample** at `java-checks-test-sources/default/src/main/java/checks/LambdaTypeParameterCheckSample.java`: |
| 88 | +```java |
| 89 | +package checks; |
| 90 | + |
| 91 | +import java.util.function.BiConsumer; |
| 92 | +import java.util.function.Consumer; |
| 93 | + |
| 94 | +abstract class LambdaTypeParameterCheckSample { |
| 95 | + public void foo() { |
| 96 | + foo((String s) -> s.length()); // Compliant |
| 97 | + foo(s -> {return s.length();}); // Noncompliant {{Specify a type for: 's'}} |
| 98 | +// ^ |
| 99 | + } |
| 100 | + |
| 101 | + abstract void foo(Consumer<String> s); |
| 102 | + abstract void foo(BiConsumer<String, Object> bc); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Note: `package checks;` was added as the first line. The class was renamed from `A` to `LambdaTypeParameterCheckSample` by the user after migration — the skill only adds the package declaration. |
| 107 | + |
| 108 | +**Old test** `LambdaTypeParameterCheckTest.java`: |
| 109 | +```java |
| 110 | +import org.junit.jupiter.api.Test; |
| 111 | +import org.sonar.java.checks.verifier.CheckVerifier; |
| 112 | + |
| 113 | +class LambdaTypeParameterCheckTest { |
| 114 | + @Test |
| 115 | + void test() { |
| 116 | + CheckVerifier.newVerifier() |
| 117 | + .onFile("src/test/files/checks/LambdaTypeParameterCheck.java") |
| 118 | + .withCheck(new LambdaTypeParameterCheck()) |
| 119 | + .verifyIssues(); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +**New test** `LambdaTypeParameterCheckTest.java`: |
| 125 | +```java |
| 126 | +import org.junit.jupiter.api.Test; |
| 127 | +import org.sonar.java.checks.verifier.CheckVerifier; |
| 128 | + |
| 129 | +import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; |
| 130 | + |
| 131 | +class LambdaTypeParameterCheckTest { |
| 132 | + @Test |
| 133 | + void test() { |
| 134 | + CheckVerifier.newVerifier() |
| 135 | + .onFile(mainCodeSourcesPath("checks/LambdaTypeParameterCheckSample.java")) |
| 136 | + .withCheck(new LambdaTypeParameterCheck()) |
| 137 | + .verifyIssues(); |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
0 commit comments