Skip to content

Commit

Permalink
initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-gannaway-sonarsource committed Feb 27, 2024
1 parent ea0d0f7 commit 6ca831d
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions: read-all
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 17
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: mvn test -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=SonarSource-Demos_junit-test-coverage-example_a1392355-783c-4434-ab85-06e7549a55ee -Dsonar.projectName='junit-test-coverage-example'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ buildNumber.properties
# Eclipse m2e generated files
# Eclipse Core
.project
.settings/

# JDT-specific (Eclipse Java Development Tools)
.classpath
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>sonarsource</groupId>
<artifactId>sonarsource-demos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sonarsource-demos</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.coverage.jacoco.xmlReportPaths>./target/site/jacoco-unit-test-coverage-report/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions src/main/java/sonarsource/sonarsource_demos/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sonarsource.sonarsource_demos;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
17 changes: 17 additions & 0 deletions src/main/java/sonarsource/sonarsource_demos/CoverageMetrics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sonarsource.sonarsource_demos;

public class CoverageMetrics {
/**
* Showing how SonarQube calculates Test Coverage % metrics
*
* @param i
* @return
*/
public float f(int i) {
int k = 0; /* default */
if (i != 0) {
k = 1;
}
return (float)i/(k+1);
}
}
38 changes: 38 additions & 0 deletions src/test/java/sonarsource/sonarsource_demos/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sonarsource.sonarsource_demos;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sonarsource.sonarsource_demos;

import junit.framework.TestCase;

public class CoverageMetricsTest extends TestCase {

public void testF() {
CoverageMetrics c = new CoverageMetrics();
assertEquals(0.5, c.f(1), 0.0);
}

}

0 comments on commit 6ca831d

Please sign in to comment.