Skip to content

Commit

Permalink
@W-14633315@: Converted pmd-cataloger tests to junit 5 and normalized…
Browse files Browse the repository at this point in the history
… test dependencies+tasks
  • Loading branch information
stephen-carter-at-sf committed Dec 14, 2023
1 parent 415d5c8 commit ee4d6fc
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 250 deletions.
34 changes: 33 additions & 1 deletion cli-messaging/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import java.awt.Desktop

plugins {
java
jacoco
}

version = "1.0"
Expand All @@ -16,16 +19,45 @@ dependencies {
}
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.google.guava:guava:31.1-jre")

testImplementation("org.hamcrest:hamcrest:2.2")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.2")
}

tasks.getByName<Test>("test") {
tasks.test {
// Use JUnit 5
useJUnitPlatform()

testLogging {
events("passed", "skipped", "failed")
}
// Run tests in multiple threads
maxParallelForks = Runtime.getRuntime().availableProcessors()/2 + 1

// Report is always generated after test runs
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
}

tasks.register("showCoverageReport") {
group = "verification"
dependsOn(tasks.jacocoTestReport)
doLast {
Desktop.getDesktop().browse(File("$buildDir/reports/jacoco/test/html/index.html").toURI())
}
}

tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = BigDecimal("0.70") // TODO: We should aim to increase this
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"lint-typescript": "eslint ./src --ext .ts --max-warnings 0",
"test": "./gradlew test jacocoTestCoverageVerification && nyc mocha --timeout 10000 --retries 5 \"./test/**/*.test.ts\"",
"test-quiet": "cross-env SFGE_LOGGING=false ./gradlew test jacocoTestCoverageVerification && nyc mocha --timeout 10000 --retries 5 \"./test/**/*.test.ts\"",
"test-cli-messaging": "./gradlew cli-messaging:test",
"test-cli-messaging": "./gradlew cli-messaging:test cli-messaging:jacocoTestCoverageVerification",
"test-pmd-cataloger": "./gradlew pmd-cataloger:test pmd-cataloger:jacocoTestCoverageVerification",
"test-sfge": "./gradlew sfge:test sfge:jacocoTestCoverageVerification",
"test-sfge-quiet": "cross-env SFGE_LOGGING=false ./gradlew sfge:test sfge:jacocoTestCoverageVerification",
Expand Down
45 changes: 34 additions & 11 deletions pmd-cataloger/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.awt.Desktop

plugins {
java
application
Expand Down Expand Up @@ -58,21 +60,23 @@ dependencies {
implementation ("com.googlecode.json-simple:json-simple:1.1.1") {
exclude("junit")
}
implementation("com.google.code.gson:gson:2.3")
implementation("com.google.guava:guava:28.0-jre")
testImplementation("org.mockito:mockito-core:1.+")
testImplementation("junit", "junit", "4.12")
testImplementation("org.hamcrest:hamcrest:2.1")
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.google.guava:guava:31.1-jre")

testImplementation("org.mockito:mockito-core:5.2.0")
testImplementation("org.hamcrest:hamcrest:2.2")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.2")

// Used in unit tests
testImplementation(files("$buildDir/../../test/test-jars/apex/testjar-categories-and-rulesets-1.jar"))
}

configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
java.sourceCompatibility = JavaVersion.VERSION_1_8

application {
mainClassName = "sfdc.sfdx.scanner.pmd.Main"
mainClass.set("sfdc.sfdx.scanner.pmd.Main");
}

// Running the cli locally needs the dist exploded, so just do that
Expand All @@ -87,18 +91,37 @@ tasks.named("assemble") {
}

tasks.test {
finalizedBy(tasks.jacocoTestReport) // Report is always generated after test runs.
// Use JUnit 5
useJUnitPlatform()

testLogging {
events("passed", "skipped", "failed")
}

// Run tests in multiple threads
maxParallelForks = Runtime.getRuntime().availableProcessors()/2 + 1

// Report is always generated after test runs
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
}

tasks.register("showCoverageReport") {
group = "verification"
dependsOn(tasks.jacocoTestReport)
doLast {
Desktop.getDesktop().browse(File("$buildDir/reports/jacoco/test/html/index.html").toURI())
}
}

tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.80".toBigDecimal()
minimum = BigDecimal("0.80")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package sfdc.sfdx.scanner.pmd;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static sfdc.sfdx.scanner.TestConstants.*;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static sfdc.sfdx.scanner.TestConstants.APEX;
import static sfdc.sfdx.scanner.TestConstants.JAVA;
import static sfdc.sfdx.scanner.TestConstants.SOMECAT_XML_FILE;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.salesforce.messaging.EventKey;
import com.salesforce.messaging.MessagePassableException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Unit test for {@link LanguageXmlFileMapping}
*/
public class LanguageXmlFileMappingTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

private LanguageXmlFileMapping languageXmlFileMapping;

@Before
@BeforeEach
public void setup() {
languageXmlFileMapping = new LanguageXmlFileMapping();
}
Expand Down Expand Up @@ -122,10 +121,9 @@ private void testCollision(String collidingPath) {

languageXmlFileMapping.addPathsForLanguage(Arrays.asList(xmlContainer1), APEX);

thrown.expect(new MessagePassableExceptionMatcher(EventKey.ERROR_EXTERNAL_DUPLICATE_XML_PATH,
new String[] { collidingPath, jar2, jar1 }));

languageXmlFileMapping.addPathsForLanguage(Arrays.asList(xmlContainer2), APEX);
MessagePassableException ex = assertThrows(MessagePassableException.class, () -> languageXmlFileMapping.addPathsForLanguage(Arrays.asList(xmlContainer2), APEX));
assertThat(ex.getEventKey(), is(EventKey.ERROR_EXTERNAL_DUPLICATE_XML_PATH));
assertThat(ex.getArgs(), is(new String[] { collidingPath, jar2, jar1 }));
}

private void setupCategoriesAndRulesets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package sfdc.sfdx.scanner.pmd;

import static org.junit.Assert.*;

import org.junit.Test;
import com.salesforce.messaging.MessagePassableException;
import com.salesforce.messaging.EventKey;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.List;
import java.util.Map;

import com.salesforce.messaging.EventKey;
import com.salesforce.messaging.MessagePassableException;
import org.junit.jupiter.api.Test;

public class MainArgsHandlingTest {

final Main main = new Main();
Expand All @@ -24,13 +26,13 @@ public void verifyHappyCase() {
final Map<String, List<String>> stringListMap = main.parseArguments(args);

// Validate
assertEquals("Unexpected number of items in parsed map", 1, stringListMap.size());
assertTrue("Language not found in parsed map", stringListMap.containsKey(language));
assertEquals(1, stringListMap.size(), "Unexpected number of items in parsed map");
assertTrue(stringListMap.containsKey(language), "Language not found in parsed map");

final List<String> parsedPaths = stringListMap.get(language);
assertEquals("Unexpected number of paths in parsed map", paths.length, parsedPaths.size());
assertEquals(paths.length, parsedPaths.size(), "Unexpected number of paths in parsed map");
for (String path : paths) {
assertTrue("Path not found in parsed map: " + path, parsedPaths.contains(path));
assertTrue(parsedPaths.contains(path), "Path not found in parsed map: " + path);
}
}

Expand Down Expand Up @@ -73,8 +75,8 @@ private void testParseArgForErrorHandling(String[] args, String expectedArgForMe
main.parseArguments(args);
fail(failureMessage);
} catch (MessagePassableException e) {
assertEquals("Unexpected eventKey on exception", EventKey.ERROR_INTERNAL_MAIN_INVALID_ARGUMENT, e.getEventKey());
assertEquals("Unexpected arg list on exception", expectedArgForMessage, e.getArgs()[0]);
assertEquals(EventKey.ERROR_INTERNAL_MAIN_INVALID_ARGUMENT, e.getEventKey(), "Unexpected eventKey on exception");
assertEquals(expectedArgForMessage, e.getArgs()[0], "Unexpected arg list on exception");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package sfdc.sfdx.scanner.pmd;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.salesforce.messaging.CliMessager;
import com.salesforce.messaging.EventKey;
import com.salesforce.messaging.MessagePassableException;
import com.salesforce.messaging.Message;
import com.salesforce.messaging.CliMessager;

import java.util.List;

import static org.mockito.Mockito.*;
import com.salesforce.messaging.MessagePassableException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MainMessagesTest {

@Before
@After
@BeforeEach
@AfterEach
public void clearMessages() {
CliMessager.getInstance().resetMessages();
}
Expand All @@ -38,12 +42,12 @@ public void verifySfdxScannerExceptionsToMessages() {

// Validate
final List<Message> messages = getMessages();
assertEquals("Unexpected count of messages", 1, messages.size());
assertEquals(1, messages.size(), "Unexpected count of messages");
final Message actualMessage = messages.get(0);

// Validate message
assertEquals("Unexpected eventKey in message", expectedEventKey.getMessageKey(), actualMessage.getMessageKey());
assertEquals("Unexpected args in message", actualMessage.getArgs().get(0), expectedArgs[0]);
assertEquals(expectedEventKey.getMessageKey(), actualMessage.getMessageKey(), "Unexpected eventKey in message");
assertEquals(actualMessage.getArgs().get(0), expectedArgs[0], "Unexpected args in message");
}

@Test
Expand All @@ -56,13 +60,13 @@ public void verifyAnyThrowableAddedToMessages() {

// Validate
List<Message> messages = getMessages();
assertEquals("Unexpected count of messages", 1, messages.size());
assertEquals(1, messages.size(), "Unexpected count of messages");
final Message actualMessage = messages.get(0);

// Validate message
assertEquals("Unexpected eventKey in message when handling uncaught exception", EventKey.ERROR_INTERNAL_UNEXPECTED.getMessageKey(), actualMessage.getMessageKey());
assertEquals(EventKey.ERROR_INTERNAL_UNEXPECTED.getMessageKey(), actualMessage.getMessageKey(), "Unexpected eventKey in message when handling uncaught exception");
final String actualLog = actualMessage.getInternalLog();
assertTrue("log field of message should contain message from actual exception", actualLog.contains(exception.getMessage()));
assertTrue(actualLog.contains(exception.getMessage()), "log field of message should contain message from actual exception");
}

private Main.Dependencies setupMockToThrowException(Exception exception) {
Expand Down

This file was deleted.

Loading

0 comments on commit ee4d6fc

Please sign in to comment.