Skip to content

Migrate tests to JUnit5 #181

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
25 changes: 8 additions & 17 deletions src/test/java/com/cloudbees/syslog/SyslogMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@

import java.util.Calendar;
import java.util.TimeZone;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class SyslogMessageTest {
class SyslogMessageTest {

@Test
public void testRfc5425Format() {
void testRfc5425Format() {
// GIVEN
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
Expand All @@ -39,7 +38,6 @@ public void testRfc5425Format() {
System.out.println(SyslogMessage.rfc3339DateFormat.format(cal.getTime()));
System.out.println(cal.getTimeInMillis());


SyslogMessage message = new SyslogMessage()
.withTimestamp(cal.getTimeInMillis())
.withAppName("my_app")
Expand All @@ -58,8 +56,7 @@ public void testRfc5425Format() {
}

@Test
public void testRfc5424Format() {

void testRfc5424Format() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(2013, Calendar.DECEMBER, 5, 10, 30, 5);
Expand All @@ -68,7 +65,6 @@ public void testRfc5424Format() {
System.out.println(SyslogMessage.rfc3339DateFormat.format(cal.getTime()));
System.out.println(cal.getTimeInMillis());


SyslogMessage message = new SyslogMessage()
.withTimestamp(cal.getTimeInMillis())
.withAppName("my_app")
Expand All @@ -82,11 +78,10 @@ public void testRfc5424Format() {
String expected = "<14>1 2013-12-05T10:30:05.000Z myserver.example.com my_app - - - a syslog message";

assertThat(actual, is(expected));

}

@Test
public void testRfc5424FormatWithStructuredData() {
void testRfc5424FormatWithStructuredData() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(2013, Calendar.DECEMBER, 5, 10, 30, 5);
Expand All @@ -95,7 +90,6 @@ public void testRfc5424FormatWithStructuredData() {
System.out.println(SyslogMessage.rfc3339DateFormat.format(cal.getTime()));
System.out.println(cal.getTimeInMillis());


SyslogMessage message = new SyslogMessage()
.withTimestamp(cal.getTimeInMillis())
.withAppName("my_app")
Expand All @@ -110,17 +104,16 @@ public void testRfc5424FormatWithStructuredData() {
String expected = "<14>1 2013-12-05T10:30:05.000Z myserver.example.com my_app - - [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"] a syslog message";

assertThat(actual, is(expected));

message.withSDElement(new SDElement("examplePriority@32473", new SDParam("class", "high")));
actual = message.toRfc5424SyslogMessage();
expected = "<14>1 2013-12-05T10:30:05.000Z myserver.example.com my_app - - [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"][examplePriority@32473 class=\"high\"] a syslog message";

assertThat(actual, is(expected));
}

@Test
public void testRfc3164Format() {

void testRfc3164Format() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.set(2013, Calendar.DECEMBER, 5, 10, 30, 5);
Expand All @@ -129,7 +122,6 @@ public void testRfc3164Format() {
System.out.println(SyslogMessage.rfc3339DateFormat.format(cal.getTime()));
System.out.println(cal.getTimeInMillis());


SyslogMessage message = new SyslogMessage()
.withTimestamp(cal.getTimeInMillis())
.withAppName("my_app")
Expand All @@ -143,6 +135,5 @@ public void testRfc3164Format() {
String expected = "<14>Dec 05 10:30:05 myserver.example.com my_app: a syslog message";

assertThat(actual, is(expected));

}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
/*
* Copyright (c) 2010-2013 the original author or authors
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
*/
package com.cloudbees.syslog.integration.jul;

import com.cloudbees.syslog.sender.UdpSyslogMessageSender;
import org.junit.Test;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class SyslogHandlerTest {
class SyslogHandlerTest {

@Test
public void test(){
void test() {
Logger logger = Logger.getLogger(getClass().getName());
logger.setLevel(Level.FINEST);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < THREADS_COUNT; i++) {
final String prefix = "thread-" + i + "msg-";

Runnable command = new Runnable() {
@Override
public void run() {
for (int j = 0; j < ITERATION_COUNT; j++) {
try {
messageSender.sendMessage(prefix + j);
Thread.sleep(random.nextInt(3));
} catch (IOException | InterruptedException e) {
System.err.println("ERROR in " + prefix);
e.printStackTrace();
break;
}
Runnable command = () -> {
for (int j = 0; j < ITERATION_COUNT; j++) {
try {
messageSender.sendMessage(prefix + j);
Thread.sleep(random.nextInt(3));
} catch (IOException | InterruptedException e) {
System.err.println("ERROR in " + prefix);
e.printStackTrace();
break;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
import com.cloudbees.syslog.MessageFormat;
import com.cloudbees.syslog.Severity;
import com.cloudbees.syslog.SyslogMessage;
import org.junit.Ignore;
import org.junit.Test;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class TcpSyslogMessageSenderTest {
class TcpSyslogMessageSenderTest {

// @Ignore
// @Disabled
@Test
public void send() throws Exception {
void send() throws Exception {
TcpSyslogMessageSender messageSender = new TcpSyslogMessageSender();
messageSender.setDefaultMessageHostname("mysecretkey");
messageSender.setDefaultAppName("myapp");
Expand All @@ -44,10 +43,9 @@ public void send() throws Exception {
messageSender.sendMessage("unit test message over tcp éèà " + getClass() + " - " + new Timestamp(System.currentTimeMillis()));
}

@Ignore
@Disabled
@Test
public void send2() throws Exception {

void send2() throws Exception {
SyslogMessage msg = new SyslogMessage()
.withAppName("my-app")
.withFacility(Facility.USER)
Expand All @@ -67,11 +65,9 @@ public void send2() throws Exception {
messageSender.sendMessage(msg);
}


@Ignore
@Disabled
@Test
public void sendOverSSL() throws Exception {

void sendOverSSL() throws Exception {
SyslogMessage msg = new SyslogMessage()
.withAppName("my-app")
.withFacility(Facility.USER)
Expand All @@ -91,12 +87,11 @@ public void sendOverSSL() throws Exception {
messageSender.sendMessage(msg);
}


/**
* https://github.com/CloudBees-community/syslog-java-client/issues/19
*/
@Test
public void test_bug19_NullPointerException_In_ToString(){
void test_bug19_NullPointerException_In_ToString(){
TcpSyslogMessageSender tcpSyslogMessageSender = new TcpSyslogMessageSender();
tcpSyslogMessageSender.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ public static void main(String[] args) throws Exception {
for (int i = 0; i < THREADS_COUNT; i++) {
final String prefix = "thread-" + i + "-udp-msg-";

Runnable command = new Runnable() {
@Override
public void run() {
for (int j = 0; j < ITERATION_COUNT; j++) {
try {
messageSender.sendMessage(prefix + j);
} catch (IOException e) {
System.err.println("ERROR in " + prefix);
e.printStackTrace();
break;
}
Runnable command = () -> {
for (int j = 0; j < ITERATION_COUNT; j++) {
try {
messageSender.sendMessage(prefix + j);
} catch (IOException e) {
System.err.println("ERROR in " + prefix);
e.printStackTrace();
break;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

import com.cloudbees.syslog.Facility;
import com.cloudbees.syslog.Severity;
import org.junit.Test;

import java.sql.Timestamp;
import org.junit.jupiter.api.Test;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class UpdSyslogMessageSenderTest {
class UpdSyslogMessageSenderTest {

// @Ignore
// @Disabled
@Test
public void send() throws Exception {
void send() throws Exception {
UdpSyslogMessageSender messageSender = new UdpSyslogMessageSender();
messageSender.setDefaultMessageHostname("mysecretkey");
messageSender.setDefaultAppName("myapp");
Expand All @@ -39,6 +39,4 @@ public void send() throws Exception {
messageSender.setSyslogServerPort(18977);
messageSender.sendMessage("unit test message éèà " + getClass() + " - " + new Timestamp(System.currentTimeMillis()));
}


}
26 changes: 12 additions & 14 deletions src/test/java/com/cloudbees/syslog/util/CachingReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
package com.cloudbees.syslog.util;

import org.hamcrest.Matchers;
import org.junit.Test;

import org.junit.jupiter.api.Test;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;

class CachingReferenceTest {

public class CachingReferenceTest {
/**
* Test that the locks are properly released.
*/
@Test
public void test_return_value() {

CachingReference<String> cachingReference = new CachingReference<String>(5, TimeUnit.SECONDS) {
void test_return_value() {
CachingReference<String> cachingReference = new CachingReference<>(5, TimeUnit.SECONDS) {
@Nullable
@Override
protected String newObject() {
Expand All @@ -39,27 +40,24 @@ protected String newObject() {
};

String actual = cachingReference.get();
assertThat(actual, Matchers.equalTo("value"));
assertThat(actual, equalTo("value"));
}

/**
* Test that the locks are properly released.
*/
@Test(expected = MyRuntimeException.class)
public void test_throw_exception_in_get_object() {

CachingReference<String> cachingReference = new CachingReference<String>(5, TimeUnit.SECONDS) {
@Test
void test_throw_exception_in_get_object() {
CachingReference<String> cachingReference = new CachingReference<>(5, TimeUnit.SECONDS) {
@Nullable
@Override
protected String newObject() {
throw new MyRuntimeException();
}
};

cachingReference.get();
assertThrows(MyRuntimeException.class, cachingReference::get);
}


private static class MyRuntimeException extends RuntimeException {
public MyRuntimeException() {
super();
Expand Down