Skip to content

Commit

Permalink
#630 Fix tests for Java 17
Browse files Browse the repository at this point in the history
- New Mockito Version
- setting of line.separator change in Java 17
- Fix new line formats of test files
  • Loading branch information
svennissel committed May 9, 2022
1 parent 45f153c commit ac12eef
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 58 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/olv.java-conventions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dependencies {
implementation 'io.dropwizard.metrics:metrics-core:3.2.2'
implementation 'com.alibaba:fastjson:1.2.76'
testImplementation 'org.testng:testng:6.8.8'
testImplementation 'org.mockito:mockito-core:1.9.0'
testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation 'org.assertj:assertj-core:3.6.2'
testImplementation 'uk.co.datumedge:hamcrest-json:0.2'
testImplementation 'org.awaitility:awaitility:2.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -23,6 +23,7 @@
import pl.otros.logview.api.model.Note;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.Level;

Expand Down Expand Up @@ -54,11 +55,11 @@ public class LogDataListPersistanceVer2 implements LogDataListPersistance {

/*
* (non-Javadoc)
*
*
* @see pl.otros.logview.api.LogDataListPersistance#saveLogsList(java.io.OutputStream, java.util.List)
*/
public void saveLogsList(OutputStream out, List<LogData> list) throws IOException {
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));

ArrayList<String> saveMapOrder = getSaveMapOrder();
for (String string : saveMapOrder) {
Expand Down Expand Up @@ -112,7 +113,7 @@ private Map<String, String> toMap(LogData logData) {
}
try {
p.store(bout, null);
mdc = bout.toString();
mdc = bout.toString().replace("\r", "");//Convert to unix line separator format
mdc = mdc.substring(mdc.indexOf("\n") + 1);
} catch (IOException e) {
LOGGER.error(String.format("Can't save LogData (id=%d) properties: %s", logData.getId(), e.getMessage()));
Expand Down Expand Up @@ -148,17 +149,17 @@ private ArrayList<String> getSaveMapOrder() {

/*
* (non-Javadoc)
*
*
* @see pl.otros.logview.api.LogDataListPersistance#loadLogsList(java.io.InputStream)
*/
public List<LogData> loadLogsList(InputStream in) throws IOException {
BufferedReader bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
BufferedReader bin = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
String line = bin.readLine();
String[] split = line.split(FIELD_SEPERATOR_TO_SPLIT);
HashMap<String, Integer> fieldMapping = new HashMap<>();
for (int i = 0; i < split.length; i++) {
String string = split[i];
fieldMapping.put(string, Integer.valueOf(i));
fieldMapping.put(string, i);
}

ArrayList<LogData> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2011 Krzysztof Otrebski
*
*
* Licensed 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
*
*
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
*
* 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.
Expand All @@ -25,33 +25,37 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;

import static org.testng.AssertJUnit.assertEquals;

//TODO check assert order
public class PersistanceVer2Test {

private ArrayList<LogData> list;
private final String result = "ID|TIMESTAMP|MESSAGE|CLASS|METHOD|LEVEL|LOGGER|THREAD|MDC|NDC|FILE|LINE|LOG_SOURCE|NOTE|MARKED|MARKED_COLOR|\n"
+ "0|0|message\\nno \\P0\\P|class|method|INFO|LN|Thread|a=l0\\n|ndc|File|123|file:/a.txt|Note|true|Red|\n" //
+ "1|1|message\\nno \\P1\\P|class|method|INFO|LN|Thread|a=l1\\n|ndc|File|123|file:/a.txt|Note|false||\n" //
+ "2|2|message\\nno \\P2\\P|class|method|INFO|LN|Thread|a=l2\\n|ndc|File|123|file:/a.txt|Note|true|Black|\n" //
+ "3|3|message\\nno \\P3\\P|class|method|INFO|LN|Thread|a=l3\\n|ndc|File|123|file:/a.txt|Note|false||\n" //
+ "4|4|message\\nno \\P4\\P|class|method|INFO|LN|Thread|a=l4\\n|ndc|File|123|file:/a.txt|Note|true|Green|\n";

private final String result = """
ID|TIMESTAMP|MESSAGE|CLASS|METHOD|LEVEL|LOGGER|THREAD|MDC|NDC|FILE|LINE|LOG_SOURCE|NOTE|MARKED|MARKED_COLOR|
0|0|message\\nno \\P0\\P|class|method|INFO|LN|Thread|a=l0\\n|ndc|File|123|file:/a.txt|Note|true|Red|
1|1|message\\nno \\P1\\P|class|method|INFO|LN|Thread|a=l1\\n|ndc|File|123|file:/a.txt|Note|false||
2|2|message\\nno \\P2\\P|class|method|INFO|LN|Thread|a=l2\\n|ndc|File|123|file:/a.txt|Note|true|Black|
3|3|message\\nno \\P3\\P|class|method|INFO|LN|Thread|a=l3\\n|ndc|File|123|file:/a.txt|Note|false||
4|4|message\\nno \\P4\\P|class|method|INFO|LN|Thread|a=l4\\n|ndc|File|123|file:/a.txt|Note|true|Green|
""";

private static final String EMPTY_RESULT = "ID|TIMESTAMP|MESSAGE|CLASS|METHOD|LEVEL|LOGGER|THREAD|MDC|NDC|FILE|LINE|LOG_SOURCE|NOTE|MARKED|MARKED_COLOR|\n";
private LogDataListPersistanceVer2 p;

@BeforeMethod
public void prepare() {
public void prepare() {
p = new LogDataListPersistanceVer2();
LogData[] datas = new LogData[5];
list = new ArrayList<>();
for (int i = 0; i < datas.length; i++) {
for (int i = 0; i < 5; i++) {
LogData ld = new LogData();
ld.setLevel(Level.INFO);
ld.setId(i);
Expand All @@ -75,18 +79,16 @@ public void prepare() {
properties.put("a", "l" + i);
ld.setProperties(properties);

datas[i] = ld;
list.add(ld);
}
}

@Test
public void testSaveLogsListVer2() throws IOException {
System.setProperty("line.separator", "\n");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
p.saveLogsList(bout, list);
String s = new String(bout.toByteArray(), "UTF-8");
Assert.assertEquals(result,s);
String s = bout.toString(StandardCharsets.UTF_8);
Assert.assertEquals(result, s);

}

Expand All @@ -95,14 +97,14 @@ public void testSaveEmpty() throws IOException {
list.clear();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
p.saveLogsList(bout, list);
String s = new String(bout.toByteArray(), "UTF-8");
String s = bout.toString(StandardCharsets.UTF_8);
Assert.assertEquals(EMPTY_RESULT, s);
}

@Test
public void testLoadLogsListVer2() throws IOException {

ByteArrayInputStream bin = new ByteArrayInputStream(result.getBytes("UTF-8"));
ByteArrayInputStream bin = new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8));
List<LogData> loadLogsListVer2 = p.loadLogsList(bin);
Assert.assertEquals(5, loadLogsListVer2.size());
for (int i = 0; i < list.size(); i++) {
Expand All @@ -123,7 +125,7 @@ public void testParseLogData() {
fieldMapping.put(LogDataListPersistanceVer2.HEADER_MESSAGE, i++);
fieldMapping.put(LogDataListPersistanceVer2.HEADER_METHOD, i++);
fieldMapping.put(LogDataListPersistanceVer2.HEADER_THREAD, i++);
fieldMapping.put(LogDataListPersistanceVer2.HEADER_TIMESTAMP, i++);
fieldMapping.put(LogDataListPersistanceVer2.HEADER_TIMESTAMP, i);

String[] line = { "3", "a.b.Class", "SEVERE", "logger", "message!\\n\\P\\S", "myMethod", "thread-1", "1000" };
LogData ld = p.parseLogData(line, fieldMapping);
Expand All @@ -145,6 +147,7 @@ public void testEsape() {

assertEquals(expected, result);
}

@Test
public void testUnEsape() {
String s = "ala ma kota \\nkot ma \\Sal\\Pe";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright 2011 Krzysztof Otrebski
*
*
* Licensed 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
*
*
* <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
*
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pl.otros.logview.gui.renderers;

import com.google.common.base.Joiner;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
Expand All @@ -24,30 +23,31 @@ public void before() {
}

@Test
public void testAbbreviatePackageUsingMappings(){
public void testAbbreviatePackageUsingMappings() {
//given
SortedMap<String, String> map = underTest.toMap("com.company={D}\ncom={E}\ncom.company.package={C}");

//when
final String actual = underTest.abbreviatePackageUsingMappings("com.company.package.Class", map);

//then
assertEquals(actual,"{C}.Class");
assertEquals(actual, "{C}.Class");
}

@Test
public void testAbbreviatePackageUsingMappingsMappingNotFound(){
public void testAbbreviatePackageUsingMappingsMappingNotFound() {
//given
SortedMap<String, String> map = underTest.toMap("com.company={D}\ncom={E}\ncom.company.package={C}");

//when
final String actual = underTest.abbreviatePackageUsingMappings("co.company.package.Class", map);

//then
assertEquals(actual,"co.company.package.Class");
assertEquals(actual, "co.company.package.Class");
}

@Test
public void testToMap() throws Exception {
public void testToMap() {
final SortedMap<String, String> treeMap = underTest.toMap("a.b.c.d={D}\na.b.c.e={E}\na.b.c={C}\na.b={B}");

//then
Expand All @@ -59,46 +59,46 @@ public void testToMap() throws Exception {
}

@Test
public void testToMapIfConfIsNull() throws Exception {
public void testToMapIfConfIsNull() {
final Map<String, String> treeMap = underTest.toMap(null);

//then
assertEquals(treeMap.size(), 0);
}

@Test
public void testIsNullSafe() throws Exception {
public void testIsNullSafe() {
underTest.getTableCellRendererComponent(Mockito.mock(JTable.class), null, false, false, 0, 0);
}

@DataProvider(name = "abbreviations")
public Object[][] abbreviations(){
return new Object[][] {
new Object[]{"com.company.package.Class",26,"com.company.package.Class"},
new Object[]{"com.company.package.Class",25,"com.company.package.Class"},
new Object[]{"com.company.package.Class",24,"c.company.package.Class"},
new Object[]{"com.company.package.Class",13,"c.c.p.Class"},
new Object[]{"com.company.package.Class",11,"c.c.p.Class"},
new Object[]{"com.company.package.Class",5,"c.c.p.Class"},
new Object[]{"Class",8,"Class"},
new Object[]{"Class",3,"Class"},
new Object[]{"",3,""},
new Object[]{"com.package.Dot.",1,"c.p.D."},
new Object[]{"com.package.Dot.",9,"c.p.Dot."},
new Object[]{".com.package.Dot.",10,".c.p.Dot."},
public Object[][] abbreviations() {
return new Object[][]{
new Object[]{ "com.company.package.Class", 26, "com.company.package.Class" },
new Object[]{ "com.company.package.Class", 25, "com.company.package.Class" },
new Object[]{ "com.company.package.Class", 24, "c.company.package.Class" },
new Object[]{ "com.company.package.Class", 13, "c.c.p.Class" },
new Object[]{ "com.company.package.Class", 11, "c.c.p.Class" },
new Object[]{ "com.company.package.Class", 5, "c.c.p.Class" },
new Object[]{ "Class", 8, "Class" },
new Object[]{ "Class", 3, "Class" },
new Object[]{ "", 3, "" },
new Object[]{ "com.package.Dot.", 1, "c.p.D." },
new Object[]{ "com.package.Dot.", 9, "c.p.Dot." },
new Object[]{ ".com.package.Dot.", 10, ".c.p.Dot." },
};
}

@Test(dataProvider= "abbreviations")
public void testAbbreviatePackagesToSingleLetter(String clazz,int width, String expected) {
@Test(dataProvider = "abbreviations")
public void testAbbreviatePackagesToSingleLetter(String clazz, int width, String expected) {
//given
final FontMetrics fontMetrics = Mockito.mock(FontMetrics.class);
Mockito.when(fontMetrics.stringWidth(Matchers.anyString())).thenAnswer(invocation -> ((String)invocation.getArguments()[0]).length());
Mockito.when(fontMetrics.stringWidth(Mockito.anyString())).thenAnswer(invocation -> ((String) invocation.getArguments()[0]).length());

//when
final String actual = underTest.abbreviatePackagesToSingleLetter(clazz, width, fontMetrics);

//then
assertEquals(actual,expected);
assertEquals(actual, expected);
}
}

0 comments on commit ac12eef

Please sign in to comment.