Skip to content

fix: update repo with calculator app and GHA + CircleCI #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 2.1
orbs:
codecov: codecov/codecov@3

jobs:
build:
docker:
- image: cimg/openjdk:18.0.2
steps:
- checkout
- run:
name: Install dependencies
command: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- run:
name: Run tests and collect coverage
command: mvn -B test
- codecov/upload

workflow:
version: 2.1
build-test:
jobs:
- build
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Workflow for Codecov example-java-maven
on: [push, pull_request]
jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up JDK 18
uses: actions/setup-java@v1
with:
java-version: 18
- name: Install dependencies
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- name: Run tests and collect coverage
run: mvn -B test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
target/
org.jacoco.examples.maven.java.iml
.idea
66 changes: 10 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,17 @@
# [Codecov][1] Java Maven Example
# [Codecov](https://codecov.io) Java Maven Example
[![codecov](https://codecov.io/github/codecov/example-java-maven/branch/main/graph/badge.svg?token=ob1cArXXM6)](https://app.codecov.io/github/codecov/example-java-maven)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bi.8713187.xyz%2Fcodecov%2Fexample-java-maven.svg?type=shield)](https://app.fossa.com/projects/git%2Bi.8713187.xyz%2Fcodecov%2Fexample-java-maven?ref=badge_shield)

## Guide
### Travis Setup
This example repository shows how Codecov can be integrated with a simple java-maven project. It uses **GitHub Actions** and **CircleCI** as CI/CD providers and **Jacoco** as the coverage provider.

Add to your `.travis.yml` file.
```yml
language: java
For more information, please see the links below.

# Cobertura is not supported in JDK11 so you must downgrade the JDK that Travis uses if you want to use Cobertura with Travis.
# See https://github.com/cobertura/cobertura/issues/381
jdk:
- openjdk8

script: "mvn cobertura:cobertura"

after_success:
- bash <(curl -s https://codecov.io/bash)
```
### Produce Coverage Reports
#### Add Cobertura plugin
```xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
</configuration>
</plugin>
```
Call `mvn cobertura:cobertura` during your CI tests, [see here](https://github.com/codecov/example-java-maven/blob/master/.travis.yml#L4)

## Caveats
### Private Repos
Add to your `.travis.yml` file.
```yml
after_success:
- bash <(curl -s https://codecov.io/bash) -t uuid-repo-token
```
## Support
### FAQ
- Q: Is there a Gradle example?<br/>A: **Yes**, enter [codecov/example-gradle][2]
- Q: Is there a Android example?<br/>A: **Yes**, enter [codecov/example-android][3]
- Q: Is there a JaCoCo example?<br/>A: **Yes**, enter [codecov/example-java][4]

1. More documentation at https://docs.codecov.io
2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml

We are happy to help if you have any questions. Please contact email our Support at [[email protected]](mailto:[email protected])

[1]: https://codecov.io/
[2]: https://github.com/codecov/example-gradle
[3]: https://github.com/codecov/example-android
[4]: https://github.com/codecov/example-java
## Links
- [Quick Start](https://docs.codecov.com/docs/quick-start)
- [GitHub Tutorial](https://docs.codecov.com/docs/github-tutorial)
- [Community Boards](https://community.codecov.io)
- [Support](https://codecov.io/support)
- [Documentation](https://docs.codecov.io)


## License
Expand Down
73 changes: 31 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
Marc R. Hoffmann - initial API and implementation
-->

<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">
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>io.codecov</groupId>
<artifactId>example-java</artifactId>
<version>1.0</version>

<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.examples.maven.java</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Cobertura Maven plug-in example for Java project</name>
<url>http://cobertura.github.io/cobertura/</url>
<name>Codecov example Java repository</name>
<url>http://github.com/codecov/example-java</url>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
</configuration>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</build>

</project>
23 changes: 23 additions & 0 deletions src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package calculator;

public class Calculator {
public static double add(double x, double y) {
return x + y;
}

public static double subtract(double x, double y) {
return x - y;
}

public static double multiply(double x, double y) {
return x * y;
}

public static double divide(double x, double y) {
if (y == 0) {
System.out.println("Cannot divide by 0");
return 0;
}
return x / y;
}
}
13 changes: 0 additions & 13 deletions src/main/java/org/jacoco/examples/maven/java/HelloWorld.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/test/java/calculator/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package calculator;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CalculatorTest {
private static final double DELTA = 0.001;

@Test
public void testAdd() {
assertEquals(Calculator.add(1, 2), 3.0, DELTA);
assertEquals(Calculator.add(1.0, 2.0), 3.0, DELTA);
assertEquals(Calculator.add(0, 2.0), 2.0, DELTA);
assertEquals(Calculator.add(2.0, 0), 2.0, DELTA);
assertEquals(Calculator.add(-4, 2.0), -2.0, DELTA);
}

@Test
public void testSubtract() {
assertEquals(Calculator.subtract(1, 2), -1.0, DELTA);
assertEquals(Calculator.subtract(2, 1), 1.0, DELTA);
assertEquals(Calculator.subtract(1.0, 2.0), -1.0, DELTA);
assertEquals(Calculator.subtract(0, 2.0), -2.0, DELTA);
assertEquals(Calculator.subtract(2.0, 0), 2.0, DELTA);
assertEquals(Calculator.subtract(-4, 2.0), -6.0, DELTA);
}

@Test
public void testMultiply() {
assertEquals(Calculator.multiply(1, 2), 2.0, DELTA);
assertEquals(Calculator.multiply(1.0, 2.0), 2.0, DELTA);
assertEquals(Calculator.multiply(0, 2.0), 0.0, DELTA);
assertEquals(Calculator.multiply(2.0, 0), 0.0, DELTA);
assertEquals(Calculator.multiply(-4, 2.0), -8.0, DELTA);
}

@Test
public void testDivide() {
assertEquals(Calculator.divide(1, 2), 0.5, DELTA);
assertEquals(Calculator.divide(1.0, 2.0), 0.5, DELTA);
assertEquals(Calculator.divide(0, 2.0), 0, DELTA);
assertEquals(Calculator.divide(-4, 2.0), -2.0, DELTA);
// assertEquals(Calculator.divide(2.0, 0), 0.0, DELTA);
}
}
22 changes: 0 additions & 22 deletions src/test/java/org/jacoco/examples/maven/java/HelloWorldTest.java

This file was deleted.