Do not declare class level variables in authenticators. Use static and final.
+ +
+public class BasicAuthenticator extends AbstractApplicationAuthenticator
+ implements LocalApplicationAuthenticator {
+
+ private int number; // Noncompliant
+
+}
+
+
+
+
+public class BasicAuthenticator extends AbstractApplicationAuthenticator
+ implements LocalApplicationAuthenticator {
+
+ private static final long serialVersionUID = 123; // Compliant
+}
+
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassLevelVariablesInServicesCheck.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassLevelVariablesInServicesCheck.json
new file mode 100644
index 0000000..d2212a9
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassLevelVariablesInServicesCheck.json
@@ -0,0 +1,9 @@
+{
+ "title": "Do not declare class level variables in authenticators. Use static and final.",
+ "type": "Bug",
+ "status": "ready",
+ "tags": [
+ "bugs"
+ ],
+ "defaultSeverity": "Critical"
+}
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.html b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.html
new file mode 100644
index 0000000..700535b
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.html
@@ -0,0 +1,33 @@
+
+
+Don’t use the word ‘Class’ as part of class names or ‘Enum’ as part of enum class names.
+ +
+class myClass { }
+class testclass{ }
+enum sampleenum{ }
+enum SampleEnum{ }
+
+
+
+class student { }
+enum Season{ }
+
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.json
new file mode 100644
index 0000000..a88f700
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/ClassNameCheck.json
@@ -0,0 +1,9 @@
+{
+ "title": "Don’t use the word ‘Class’ as part of class names or ‘Enum’ as part of enum class names.",
+ "type": "Bug",
+ "status": "ready",
+ "tags": [
+ "bugs"
+ ],
+ "defaultSeverity": "Critical"
+}
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.html b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.html
new file mode 100644
index 0000000..ce488c2
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.html
@@ -0,0 +1,29 @@
+
+
+For a comment having WSO2, should be followed by 'LLC.' and not 'Inc.'.
+ ++ // WSO2 Inc. ++ +
+WSO2 LLC. +diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.json new file mode 100644 index 0000000..db6b56c --- /dev/null +++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/IncInComments.json @@ -0,0 +1,9 @@ +{ + "title": "WSO2 should not be followed by Inc.", + "type": "Bug", + "status": "ready", + "tags": [ + "bugs" + ], + "defaultSeverity": "Critical" +} diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.html b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.html new file mode 100644 index 0000000..1a5eacc --- /dev/null +++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.html @@ -0,0 +1,46 @@ + + +
endTenantFlow() is missing in the finally block.
+ +
+try // Noncompliant
+{
+ startTenantFlow();
+ int i;
+
+} catch(Exception e) {
+
+} finally {
+
+}
+
+
+
+try {
+ startTenantFlow();
+ int i;
+
+} catch(Exception e) {
+
+} finally {
+ endTenantFlow();
+}
+
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.json
new file mode 100644
index 0000000..6649f01
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/MissingEndTenantFlowCheck.json
@@ -0,0 +1,9 @@
+{
+ "title": "endTenantFlow() is missing in the finally block.",
+ "type": "Bug",
+ "status": "ready",
+ "tags": [
+ "bugs"
+ ],
+ "defaultSeverity": "Critical"
+}
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.html b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.html
new file mode 100644
index 0000000..6070bf0
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.html
@@ -0,0 +1,52 @@
+
+
+startTenantFlow() should be the first line in a try-block. endTenantFlow() should be in a finally block.
+ +
+void foo(){
+ startTenantFlow(); // Noncompliant
+ endTenantFlow(); // Noncompliant
+
+ try {
+ String myString;
+ int myNum;
+ startTenantFlow(); // Noncompliant
+ } catch(Exception e) {
+ endTenantFlow(); // Noncompliant
+
+ } finally {
+
+ }
+}
+
+
+
+
+try {
+ startTenantFlow();
+ int i;
+
+} catch(Exception e) {
+
+} finally {
+ endTenantFlow();
+}
+
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.json
new file mode 100644
index 0000000..3aebc60
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/TenantFlowCheck.json
@@ -0,0 +1,9 @@
+{
+ "title": "startTenantFlow() should be the first line in a try-block. endTenantFlow() should be in a finally block.",
+ "type": "Bug",
+ "status": "ready",
+ "tags": [
+ "bugs"
+ ],
+ "defaultSeverity": "Critical"
+}
diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.html b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.html
new file mode 100644
index 0000000..d13624c
--- /dev/null
+++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.html
@@ -0,0 +1,34 @@
+
+
+Don’t use one letter variable names (except ‘e’ for exceptions, ‘i, j’ in for loops)
+ ++int i; +String l; +boolean p; +float k; +char a; ++ +
+int number; +String name; +diff --git a/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.json b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.json new file mode 100644 index 0000000..fea42f5 --- /dev/null +++ b/sonar-plugin/plugin/src/main/resources/org/wso2/code/quality/sonar/plugin/rules/descriptions/VariableLengthCheck.json @@ -0,0 +1,9 @@ +{ + "title": "Don’t use one letter variable names (except ‘e’ for exceptions, ‘i, j’ in for loops)", + "type": "Bug", + "status": "ready", + "tags": [ + "bugs" + ], + "defaultSeverity": "Critical" +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/FileCheckRegistrarTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/FileCheckRegistrarTest.java new file mode 100644 index 0000000..fa6974a --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/FileCheckRegistrarTest.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.code.quality.sonar.plugin; + +import org.junit.jupiter.api.Test; +import org.sonar.plugins.java.api.CheckRegistrar; + +import static org.assertj.core.api.Assertions.assertThat; + +class FileCheckRegistrarTest { + + @Test + void checkNumberRules() { + + CheckRegistrar.RegistrarContext context = new CheckRegistrar.RegistrarContext(); + FileCheckRegistrar registrar = new FileCheckRegistrar(); + registrar.register(context); + + assertThat(context.checkClasses()).hasSize(6); + } + +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesDefinitionImplTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesDefinitionImplTest.java new file mode 100644 index 0000000..dc2650c --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesDefinitionImplTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.code.quality.sonar.plugin; + +import org.junit.jupiter.api.Test; +import org.sonar.api.server.rule.RulesDefinition.Param; +import org.sonar.api.server.rule.RulesDefinition.Repository; +import org.sonar.api.server.rule.RulesDefinition.Rule; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.wso2.code.quality.sonar.plugin.util.PluginConstants.WSO2_REPOSITORY_KEY; +import static org.wso2.code.quality.sonar.plugin.util.PluginConstants.WSO2_REPOSITORY_NAME; + +class RulesDefinitionImplTest { + + @Test + void test() { + + RulesDefinitionImpl rulesDefinitionImpl = new RulesDefinitionImpl(); + org.sonar.api.server.rule.RulesDefinition.Context context = new org.sonar.api.server.rule.RulesDefinition.Context(); + rulesDefinitionImpl.define(context); + Repository repository = context.repository(WSO2_REPOSITORY_KEY); + + assert repository != null; + assertThat(repository.name()).isEqualTo(WSO2_REPOSITORY_NAME); + assertThat(repository.language()).isEqualTo("java"); + assertThat(repository.rules()).hasSize(RulesList.getChecks().size()); + assertThat(repository.rules().stream().filter(Rule::template)).isEmpty(); + assertAllRuleParametersHaveDescription(repository); + } + + private static void assertAllRuleParametersHaveDescription(Repository repository) { + + for (Rule rule : repository.rules()) { + for (Param param : rule.params()) { + assertThat(param.description()).as("description for " + param.key()).isNotEmpty(); + } + } + } + +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesPluginTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesPluginTest.java new file mode 100644 index 0000000..3922264 --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/RulesPluginTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.code.quality.sonar.plugin; + +import org.junit.jupiter.api.Test; + +import org.sonar.api.Plugin; +import org.sonar.api.SonarEdition; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarRuntime; +import org.sonar.api.utils.Version; + +import static org.assertj.core.api.Assertions.assertThat; + +class RulesPluginTest { + + @Test + void testName() { + + Plugin.Context context = new Plugin.Context(new MockedSonarRuntime()); + new RulesPlugin().define(context); + assertThat(context.getExtensions()).hasSize(2); + } + + private static class MockedSonarRuntime implements SonarRuntime { + + @Override + public Version getApiVersion() { + + return Version.create(7, 9); + } + + @Override + public SonarProduct getProduct() { + + return SonarProduct.SONARQUBE; + } + + @Override + public SonarQubeSide getSonarQubeSide() { + + return SonarQubeSide.SCANNER; + } + + @Override + public SonarEdition getEdition() { + + return SonarEdition.COMMUNITY; + } + } + +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassLevelVariablesInServicesCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassLevelVariablesInServicesCheckTest.java new file mode 100644 index 0000000..dda2fac --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassLevelVariablesInServicesCheckTest.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.code.quality.sonar.plugin.custom.check; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.sonar.java.checks.verifier.FilesUtils; +import org.wso2.code.quality.sonar.plugin.custom.check.authenticator.ClassLevelVariablesInServicesCheck; + +class ClassLevelVariablesInServicesCheckTest { + + @Test + void test() { + + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/ClassLevelVariablesInServicesCheck.java") + .withCheck(new ClassLevelVariablesInServicesCheck()) + .withClassPath(FilesUtils.getClassPath("target/test-jars")) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassNameCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassNameCheckTest.java new file mode 100644 index 0000000..4fab654 --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/ClassNameCheckTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.code.quality.sonar.plugin.custom.check; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.wso2.code.quality.sonar.plugin.custom.check.variable.classname.ClassNameCheck; + +class ClassNameCheckTest { + + @Test + void test() { + + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/ClassNameCheck.java") + .withCheck(new ClassNameCheck()) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/IncInCommentsCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/IncInCommentsCheckTest.java new file mode 100644 index 0000000..e744afe --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/IncInCommentsCheckTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.code.quality.sonar.plugin.custom.check; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.wso2.code.quality.sonar.plugin.custom.check.licenseheader.IncInCommentsCheck; + +class IncInCommentsCheckTest { + + @Test + void test() { + + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/IncInCommentsCheck.java") + .withCheck(new IncInCommentsCheck()) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/MissingEndTenantFlowCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/MissingEndTenantFlowCheckTest.java new file mode 100644 index 0000000..895491a --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/MissingEndTenantFlowCheckTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.code.quality.sonar.plugin.custom.check; + + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.wso2.code.quality.sonar.plugin.custom.check.tenantflow.MissingEndTenantFlowCheck; + +class MissingEndTenantFlowCheckTest { + + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/MissingEndTenantFlowCheck.java") + .withCheck(new MissingEndTenantFlowCheck()) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/TenantFlowCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/TenantFlowCheckTest.java new file mode 100644 index 0000000..114aeed --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/TenantFlowCheckTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.code.quality.sonar.plugin.custom.check; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.wso2.code.quality.sonar.plugin.custom.check.tenantflow.TenantFlowCheck; + +class TenantFlowCheckTest { + + @Test + void test() { + + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/TenantFlowCheck.java") + .withCheck(new TenantFlowCheck()) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/VariableLengthCheckTest.java b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/VariableLengthCheckTest.java new file mode 100644 index 0000000..49bcc52 --- /dev/null +++ b/sonar-plugin/plugin/src/test/java/org/wso2/code/quality/sonar/plugin/custom/check/VariableLengthCheckTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.code.quality.sonar.plugin.custom.check; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; +import org.wso2.code.quality.sonar.plugin.custom.check.variable.VariableLengthCheck; + +class VariableLengthCheckTest { + + @Test + void test() { + + CheckVerifier.newVerifier() + .onFile("src/test/sonar/rules/test/files/VariableLengthCheck.java") + .withCheck(new VariableLengthCheck()) + .verifyIssues(); + } +} diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassLevelVariablesInServicesCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassLevelVariablesInServicesCheck.java new file mode 100644 index 0000000..c0217e1 --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassLevelVariablesInServicesCheck.java @@ -0,0 +1,56 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +interface SecondInterface extends MyInterface { + + int number; + static final long serialVersionUID; +} + +@WSO2SpecificService +class WSO2SpecificServiceClass { + + private int number; // Noncompliant + private static final long serialVersionUID = 123; +} + +class TestClass implements MyInterface { + private int number; // Noncompliant + private static final long serialVersionUID = 123; +} + +class TestClass2 extends TestClass { + private int number; // Noncompliant + private static final long serialVersionUID = 123; +} + +class TestClass3 extends TestClass2 { + private int number; // Noncompliant + private static final long serialVersionUID = 123; +} + +@WSO2SpecificService +interface MyInterface { + + int number; + static final long serialVersionUID; +} + +public class SecondClass implements MyInterface { + + private int number; // Noncompliant + private static final long serialVersionUID = 123; + + @WSO2SpecificService + boolean myMethod() { + return true; + } +} + +@interface WSO2SpecificService{ } + +class Registry extends SecondClass { + private int number; // Noncompliant + private static final long serialVersionUID = 123; +} diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassNameCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassNameCheck.java new file mode 100644 index 0000000..64088a9 --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/ClassNameCheck.java @@ -0,0 +1,15 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +class student { } // Compliant + +class myClass { } // Noncompliant + +class testclass extends myclass{ } // Noncompliant + +enum sampleenum{ } // Noncompliant + +enum SampleEnum{ } // Noncompliant + +enum Season{ } // Compliant diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/IncInCommentsCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/IncInCommentsCheck.java new file mode 100644 index 0000000..dac1342 --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/IncInCommentsCheck.java @@ -0,0 +1,19 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +// foo + +// Noncompliant@+1 +// Noncompliant@+1 {{Remove usage of this "WSO2 Inc." comment. Replace it with LLC.}} +// wso2 inc. + +// Noncompliant@+1 +// WSO2 Inc. + +// Noncompliant@+1 +// [WSO2 Inc.] + +// WSO2 LLC +// wso2 llc +// WSo2 LLC diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/MissingEndTenantFlowCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/MissingEndTenantFlowCheck.java new file mode 100644 index 0000000..0f32cf8 --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/MissingEndTenantFlowCheck.java @@ -0,0 +1,61 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +class myClass { + + int num; + + void startTenantFlow() { + // Do nothing. + } + + void endTenantFlow() { + // Do nothing. + } + + void foo() { + // Do nothing. + } + + void mainMethod() { + + foo(); + endTenantFlow(); + + try // Noncompliant + { + startTenantFlow(); + int i; + + + } catch (Exception e) { + + } finally { + } + + try { + String myString; + int myNum; + startTenantFlow(); + + } catch (Exception e) { + + } finally { + endTenantFlow(); // Compliant + } + + try // Noncompliant + { + String myString; + int myNum; + startTenantFlow(); + + } catch (Exception e) { + + } finally { + int i; + } + + } +} diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/TenantFlowCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/TenantFlowCheck.java new file mode 100644 index 0000000..bb48fcf --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/TenantFlowCheck.java @@ -0,0 +1,71 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +class myClass { + + int num; + + void startTenantFlow(){ + // Do nothing. + } + + void endTenantFlow(){ + // Do nothing. + } + + void mainMethod() { + + startTenantFlow(); // Noncompliant + endTenantFlow(); // Noncompliant + + try { + startTenantFlow(); + int i; + + + } catch(Exception e) { + + } finally { + endTenantFlow(); + } + + try { + endTenantFlow(); // Noncompliant + + } catch(Exception e) { + startTenantFlow(); // Noncompliant + endTenantFlow(); // Noncompliant + + } finally { + endTenantFlow(); + } + + } + + void anotherMethod() { + + try { + startTenantFlow(); + int i; + + } catch(Exception e) { + startTenantFlow(); // Noncompliant + + } finally { + endTenantFlow(); + } + + try { + String myString; + int myNum; + startTenantFlow(); // Noncompliant + + } catch(Exception e) { + + } finally { + endTenantFlow(); + } + + } +} diff --git a/sonar-plugin/plugin/src/test/sonar/rules/test/files/VariableLengthCheck.java b/sonar-plugin/plugin/src/test/sonar/rules/test/files/VariableLengthCheck.java new file mode 100644 index 0000000..25f5eb9 --- /dev/null +++ b/sonar-plugin/plugin/src/test/sonar/rules/test/files/VariableLengthCheck.java @@ -0,0 +1,46 @@ +/** + *This file is the sample code against we run our unit test. + *It is placed src/test/sonar/rules/test/files in order to not be part of the maven compilation. + **/ +class myClass { + + int a; // Noncompliant + boolean b; // Noncompliant + char c; // Noncompliant + + float aa; // Compliant + int bb; // Compliant + boolean cc; // Compliant + + void foo() { + for (int i = 0; i < 10; i++) { + // Do something. + } + } + + void foo() { + int s = 3; // Noncompliant + int sb; + + for (int i = 0; i < 10; i++) { + int ab; + int a; // Noncompliant + + for (int z = 0; z < 10; z++) { + // Do something. + int zz; + int z; // Noncompliant + } + } + } + + void foo() { + try { + + } catch (Exception e) { + + } finally { + + } + } +}