From 5e1faf0124a7e8d6887c7bf9f6d8d3e2bc3c9e74 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Fri, 8 Sep 2023 23:17:55 +0200 Subject: [PATCH] Coverage result api --- .../fugerit/java/core/io/line/LineReader.java | 3 +- .../fugerit/java/core/io/line/LineWriter.java | 3 +- .../core/util/result/AbstractPagedResult.java | 9 +- .../core/util/result/DefaultPagedResult.java | 12 +- .../java/core/util/result/PageInfo.java | 4 +- .../java/core/util/result/PagedResult.java | 8 - .../core/util/result/VirtualPageCache.java | 11 +- .../java/core/io/line/TestLineIOUtils.java | 123 +++++++++++++ .../java/core/lang/helpers/TestExHandler.java | 41 +++++ .../java/core/util/result/TestResult.java | 169 ++++++++++++++++++ 10 files changed, 352 insertions(+), 31 deletions(-) create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/io/line/TestLineIOUtils.java create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/lang/helpers/TestExHandler.java create mode 100644 fj-core/src/test/java/test/org/fugerit/java/core/util/result/TestResult.java diff --git a/fj-core/src/main/java/org/fugerit/java/core/io/line/LineReader.java b/fj-core/src/main/java/org/fugerit/java/core/io/line/LineReader.java index 150dd2b3..bf8fe815 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/io/line/LineReader.java +++ b/fj-core/src/main/java/org/fugerit/java/core/io/line/LineReader.java @@ -25,6 +25,7 @@ The Apache Software Foundation (http://www.apache.org/). */ package org.fugerit.java.core.io.line; +import java.io.Closeable; import java.io.IOException; /* @@ -42,7 +43,7 @@ The Apache Software Foundation (http://www.apache.org/). * @author Morozko * */ -public interface LineReader { +public interface LineReader extends Closeable { /* *

diff --git a/fj-core/src/main/java/org/fugerit/java/core/io/line/LineWriter.java b/fj-core/src/main/java/org/fugerit/java/core/io/line/LineWriter.java index 3fd437b3..cfaf343b 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/io/line/LineWriter.java +++ b/fj-core/src/main/java/org/fugerit/java/core/io/line/LineWriter.java @@ -25,6 +25,7 @@ The Apache Software Foundation (http://www.apache.org/). */ package org.fugerit.java.core.io.line; +import java.io.Closeable; import java.io.IOException; /* @@ -42,7 +43,7 @@ The Apache Software Foundation (http://www.apache.org/). * @author Morozko * */ -public interface LineWriter { +public interface LineWriter extends Closeable { /* *

diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/result/AbstractPagedResult.java b/fj-core/src/main/java/org/fugerit/java/core/util/result/AbstractPagedResult.java index 3f79a1fc..e5e5d44d 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/result/AbstractPagedResult.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/result/AbstractPagedResult.java @@ -1,7 +1,6 @@ package org.fugerit.java.core.util.result; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -21,8 +20,6 @@ public abstract class AbstractPagedResult extends BasicResult implements Page private int currentPage; private List pageElements; - - private Map info; protected AbstractPagedResult() { super( RESULT_CODE_OK ); @@ -36,11 +33,7 @@ protected AbstractPagedResult(int perPage, long elementCount, int currentPage, i this.currentPage = currentPage; this.pageElements = pageElements; this.pageCount = pageCount; - this.info = new HashMap(); } - - - /* *

The position of the first element of the current pages ( (currentPage-1) * perPage )

@@ -138,7 +131,7 @@ public Iterator getPageCountIterator() { @Override public Map getInfo() { - return info; + return this.getInfoMap(); } @Override diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/result/DefaultPagedResult.java b/fj-core/src/main/java/org/fugerit/java/core/util/result/DefaultPagedResult.java index f2aec8f1..a2ad5b3e 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/result/DefaultPagedResult.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/result/DefaultPagedResult.java @@ -16,6 +16,16 @@ public class DefaultPagedResult extends AbstractPagedResult implements Se private String virtualKey; + /** + * Creates a new PagedResult + * + * @param the parameter type + * @param perPage the number of element per page + * @param elementCount the total element count + * @param currentPage the current element + * @param pageElements the content of the current page + * @return the paged result + */ public static PagedResult newPagedResult( int perPage, long elementCount, int currentPage, List pageElements ) { int pageCount = calcPageCount( elementCount, perPage ); AbstractPagedResult result = new DefaultPagedResult( perPage, elementCount, currentPage, pageCount, pageElements, perPage, currentPage, null ); @@ -28,7 +38,7 @@ public static PagedResult newPagedResult( int perPage, long elementCount return result; } - protected static PagedResult newPagedResult( int resultCode ) { + public static PagedResult newPagedResult( int resultCode ) { DefaultPagedResult result = new DefaultPagedResult( -1, -1, -1, -1, null, -1, -1, null ); result.setResultCode( resultCode ); return result; diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/result/PageInfo.java b/fj-core/src/main/java/org/fugerit/java/core/util/result/PageInfo.java index 630858ca..4a24ae61 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/result/PageInfo.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/result/PageInfo.java @@ -21,8 +21,8 @@ public class PageInfo implements Serializable { public PageInfo(int number, int size) { super(); - this.number = number; - this.size = size; + this.setNumber(number); + this.setSize(size); } public int getNumber() { diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/result/PagedResult.java b/fj-core/src/main/java/org/fugerit/java/core/util/result/PagedResult.java index b8dbb7bb..e06ffe4c 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/result/PagedResult.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/result/PagedResult.java @@ -83,14 +83,6 @@ public interface PagedResult extends Result { */ public Iterator getPageCountIterator(); - /** - * Result code for this page - * - * @return result code - */ - @Override - public int getResultCode(); - /** * Additional info of this page. * diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/result/VirtualPageCache.java b/fj-core/src/main/java/org/fugerit/java/core/util/result/VirtualPageCache.java index 4c3c8443..ad31cd2f 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/result/VirtualPageCache.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/result/VirtualPageCache.java @@ -69,7 +69,6 @@ class CacheWrapper { public CacheWrapper(PagedResult page) { super(); - this.page = page; this.cacheTime = System.currentTimeMillis(); } @@ -80,17 +79,9 @@ public CacheWrapper(PagedResult page) { public PagedResult getPage() { return page; } - - public void setPage(PagedResult page) { - this.page = page; - } - + public long getCacheTime() { return cacheTime; } - public void setCacheTime(long cacheTime) { - this.cacheTime = cacheTime; - } - } diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/io/line/TestLineIOUtils.java b/fj-core/src/test/java/test/org/fugerit/java/core/io/line/TestLineIOUtils.java new file mode 100644 index 00000000..a0d7dc56 --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/io/line/TestLineIOUtils.java @@ -0,0 +1,123 @@ +package test.org.fugerit.java.core.io.line; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringWriter; +import java.util.List; + +import org.fugerit.java.core.io.line.LineIOUtils; +import org.fugerit.java.core.io.line.LineReader; +import org.fugerit.java.core.io.line.LineWriter; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestLineIOUtils { + + private static final String TEST_FILE_PATH = "src/test/resources/core/db/base-db-conn.properties"; + + private static final int EXPECTED_SIZE = 9; + + @Test + public void testLineReadStream() throws IOException { + try ( InputStream is = new FileInputStream( TEST_FILE_PATH ) ) { + List lines = LineIOUtils.readLines( is ); + Assert.assertEquals( EXPECTED_SIZE , lines.size() ); + } + } + + @Test + public void testLineReadReader() throws IOException { + try ( Reader is = new FileReader( TEST_FILE_PATH ) ) { + List lines = LineIOUtils.readLines( is ); + Assert.assertEquals( EXPECTED_SIZE , lines.size() ); + } + } + + @Test + public void testLineReadFile() throws IOException { + List lines = LineIOUtils.readLines( new File( TEST_FILE_PATH ) ); + lines.stream().forEach( s -> log.info( "current line -> {}", s ) ); + String[] array = LineIOUtils.toLines(lines); + Assert.assertEquals( EXPECTED_SIZE , array.length ); + } + + private int testLineReader( LineReader reader ) throws IOException { + String line = null; + int count = 0; + do { + line = reader.readLine(); + if ( line != null ) { + count ++; + } + } while ( line != null ); + return count; + } + + @Test + public void testCreateLineReader() throws IOException { + try ( Reader is = new FileReader( TEST_FILE_PATH ); + LineReader reader = LineIOUtils.createLineReader( is ) ) { + int count = this.testLineReader(reader); + Assert.assertEquals( EXPECTED_SIZE , count ); + } + } + + @Test + public void testCreateLineBufferedReader() throws IOException { + try ( Reader is = new BufferedReader( new FileReader( TEST_FILE_PATH ) ); + LineReader reader = LineIOUtils.createLineReader( is ) ) { + int count = this.testLineReader(reader); + Assert.assertEquals( EXPECTED_SIZE , count ); + } + } + + @Test + public void testCreateLineReaderStream() throws IOException { + try ( InputStream is = new FileInputStream( TEST_FILE_PATH ); + LineReader reader = LineIOUtils.createLineReader( is ) ) { + int count = this.testLineReader(reader); + Assert.assertEquals( EXPECTED_SIZE , count ); + } + } + + private static final String[] TEST_LINES = { "test1", "test2", "test3" }; + + private int testLineWriter( LineWriter writer ) { + int count = 0; + for ( String line : TEST_LINES ) { + writer.println(line); + count++; + } + writer.print("END"); + writer.println(); + return count; + } + + @Test + public void testCreateLineWriter() throws IOException { + try ( StringWriter w = new StringWriter(); + LineWriter writer = LineIOUtils.createLineWriter( w ) ) { + int count = this.testLineWriter(writer); + Assert.assertEquals( TEST_LINES.length , count ); + } + } + + @Test + public void testCreateLineWriterStream() throws IOException { + try ( ByteArrayOutputStream w = new ByteArrayOutputStream(); + LineWriter writer = LineIOUtils.createLineWriter( w ) ) { + int count = this.testLineWriter(writer); + Assert.assertEquals( TEST_LINES.length , count ); + } + } + +} diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/lang/helpers/TestExHandler.java b/fj-core/src/test/java/test/org/fugerit/java/core/lang/helpers/TestExHandler.java new file mode 100644 index 00000000..7b81502b --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/lang/helpers/TestExHandler.java @@ -0,0 +1,41 @@ +package test.org.fugerit.java.core.lang.helpers; + +import java.io.IOException; + +import org.fugerit.java.core.lang.helpers.ExHandler; +import org.fugerit.java.core.lang.helpers.Result; +import org.fugerit.java.core.lang.helpers.ResultExHandler; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestExHandler { + + private boolean worker( ExHandler handler ) { + handler.fatal( new IOException( "fatal" ) ); + handler.error( new IOException( "error" ) ); + handler.warning( new IOException( "warning" ) ); + return handler != null; + } + + @Test + public void testResultExHandler() { + ResultExHandler handler = new ResultExHandler(); + handler.setResult( new Result() ); + log.info( "test : {}", handler.getResult() ); + boolean ok = this.worker(handler); + Assert.assertTrue( ok ); + } + + @Test + public void testResultExHandlerAlt() { + org.fugerit.java.core.util.result.ResultExHandler handler = new org.fugerit.java.core.util.result.ResultExHandler(); + handler.setResult( new Result() ); + log.info( "test : {}", handler.getResult() ); + boolean ok = this.worker(handler); + Assert.assertTrue( ok ); + } + +} diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/util/result/TestResult.java b/fj-core/src/test/java/test/org/fugerit/java/core/util/result/TestResult.java new file mode 100644 index 00000000..f3735ca4 --- /dev/null +++ b/fj-core/src/test/java/test/org/fugerit/java/core/util/result/TestResult.java @@ -0,0 +1,169 @@ +package test.org.fugerit.java.core.util.result; + +import java.util.Arrays; +import java.util.List; + +import org.fugerit.java.core.util.result.AbstractPagedResult; +import org.fugerit.java.core.util.result.BasicResult; +import org.fugerit.java.core.util.result.DefaultPagedResult; +import org.fugerit.java.core.util.result.DefaultVirtualFinder; +import org.fugerit.java.core.util.result.PageInfo; +import org.fugerit.java.core.util.result.PagedResult; +import org.fugerit.java.core.util.result.Result; +import org.fugerit.java.core.util.result.ResultHelper; +import org.fugerit.java.core.util.result.VirtualPageCache; +import org.junit.Assert; +import org.junit.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TestResult { + + private static Integer[] DATA_SET = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + private static List DATA = Arrays.asList( DATA_SET ); + + private boolean testPagedResultWorker( PagedResult result ) { + if ( result.isResultOk() ) { + log.info( "info : {}", result.getInfo() ); + log.info( "isSupportVirtualPaging : {}", result.isSupportVirtualPaging() ); + log.info( "getOffset : {}", result.getOffset() ); + log.info( "getCurrentPage : {}", result.getCurrentPage() ); + log.info( "getCurrentPageSize : {}", result.getCurrentPageSize() ); + log.info( "getElementCount : {}", result.getElementCount() ); + log.info( "getPageCount : {}", result.getPageCount() ); + log.info( "getPerPage : {}", result.getPerPage() ); + log.info( "getRealCurrentPage : {}", result.getRealCurrentPage() ); + log.info( "getRealPerPage : {}", result.getRealPerPage() ); + log.info( "isFirstPage : {}", result.isFirstPage() ); + log.info( "isLastPage : {}", result.isLastPage() ); + log.info( "isFullResult : {}", result.isFullResult() ); + log.info( "getVirtualSearchKey : {}", result.getVirtualSearchKey() ); + result.getPageCountIterator().forEachRemaining( p -> log.info( "page it : {}", p ) ); + result.getPageElementsList().stream().forEach( e -> log.info( "element list : {}", e ) ); + result.getPageElements().forEachRemaining( e -> log.info( "element it : {}", e ) ); + } + return this.testResultWorker(result); + } + + private boolean testResultWorker( Result result ) { + log.info( "result : {}, info : ", result, result.getInfoMap() ); + result.setResultCode( result.getResultCode() ); + return result.isResultOk(); + } + + @Test + public void testBasicResult() { + BasicResult result = new BasicResult( Result.RESULT_CODE_OK ); + boolean ok = this.testResultWorker(result); + Assert.assertTrue( ok ); + } + + @Test + public void testPagedResultVirtual() { + PagedResult result = DefaultPagedResult.newPagedResult( 10, DATA.size(), 0, DATA ); + boolean ok = this.testPagedResultWorker(result); + Assert.assertTrue( ok ); + ok = this.testPagedResultWorker( result.getVirtualPage( 0 ) ); + Assert.assertTrue( ok ); + } + + @Test + public void testPagedResult() { + PagedResult result = DefaultPagedResult.newPagedResult( 3, DATA.size(), 0, DATA, 3, DATA.size(), "test" ); + boolean ok = this.testPagedResultWorker(result); + Assert.assertTrue( ok ); + } + + private int testPagedResultFullWorker( int perPage, List data ) { + int currentPage = 0; + log.info( "total size : {}", data.size() ); + while ( currentPage*perPage < data.size() ) { + int fromIndex = currentPage*perPage; + int toIndex = (currentPage+1)*perPage; + List createList = data.subList( fromIndex, Math.min( data.size() , toIndex ) ); + PagedResult result = DefaultPagedResult.newPagedResult(perPage, createList.size(), currentPage, createList); + currentPage++; + log.info( "current page : {}", currentPage ); + result.getPageElementsList().stream().forEach( e -> log.info( "element : {}", e ) ); + } + return currentPage; + } + + @Test + public void testPagedResultFull3() { + int perPage = 3; + int currentPage = this.testPagedResultFullWorker(perPage, DATA); + Assert.assertEquals( 4 , currentPage ); + } + + @Test + public void testPagedResultFull5() { + int perPage = 5; + int currentPage = this.testPagedResultFullWorker(perPage, DATA); + Assert.assertEquals( 2 , currentPage ); + } + + @Test + public void testPagedResultAlt() { + PagedResult result = DefaultPagedResult.newPagedResult( 3, DATA.size(), 1, DATA ); + boolean ok = this.testPagedResultWorker(result); + Assert.assertTrue( ok ); + } + + @Test + public void testPagedResultKo() { + PagedResult result = DefaultPagedResult.newPagedResult( Result.RESULT_CODE_KO ); + boolean ok = this.testPagedResultWorker(result); + Assert.assertFalse( ok ); + } + + @Test + public void testConstants() { + Assert.assertEquals( Integer.valueOf( -1 ) , PagedResult.ELEMENT_COUNT_UNAVAILABLE ); + Assert.assertFalse( AbstractPagedResult.DEFAULT_SUPPORT_VIRTUAL_PAGING ); + } + + @Test + public void testVirtualFinder() { + DefaultVirtualFinder finder = new DefaultVirtualFinder( 5, 0, 10, 0, "TEST" ); + log.info( "finder.getSearchVirtualKey : {}", finder.getSearchVirtualKey() ); + log.info( "finder.getCurrentPage : {}", finder.getCurrentPage() ); + log.info( "finder.getPerPage : {}", finder.getPerPage() ); + log.info( "finder.getRealCurrentPage : {}", finder.getRealCurrentPage() ); + log.info( "finder.getRealPerPage : {}", finder.getRealPerPage() ); + log.info( "finder.isVirtualPagingUsed : {}", finder.isVirtualPagingUsed() ); + log.info( "finder : {}", finder ); + Assert.assertTrue( finder != null ); + } + + @Test + public void testVirtualFinderFail() { + DefaultVirtualFinder finder = new DefaultVirtualFinder( 5, 0 ); + log.info( "finder.isVirtualPagingUsed : {}", finder.isVirtualPagingUsed() ); + Assert.assertThrows( UnsupportedOperationException.class , () -> finder.getSearchVirtualKey() ); + } + + @Test + public void testVirtualCache() { + VirtualPageCache cache = new VirtualPageCache<>(); + int perPage = 3; + int elementCount = DATA.size(); + cache.addPageToCache( DefaultPagedResult.newPagedResult( perPage, elementCount, 0, DATA, 3, DATA.size(), "test" ) ); + Assert.assertNull( cache.getCachedPage( new DefaultVirtualFinder(perPage, elementCount, 3, DATA.size(), "test" ) ) ); + } + + @Test + public void testResultHelper() { + Assert.assertEquals( 1 , ResultHelper.createList( "a" ).size() ); + } + + @Test + public void testPageInfo() { + PageInfo info = new PageInfo( 1 , 10 ); + Assert.assertEquals( 1 , info.getNumber() ); + Assert.assertEquals( 10 , info.getSize() ); + } + +}