Skip to content

Commit

Permalink
Merge pull request #35 from fugerit-org/feature/issue_31_coverage_80
Browse files Browse the repository at this point in the history
Coverage result api
  • Loading branch information
fugerit79 authored Sep 8, 2023
2 parents 89b35fe + 5e1faf0 commit 3a371cb
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand All @@ -42,7 +43,7 @@ The Apache Software Foundation (http://www.apache.org/).
* @author Morozko
*
*/
public interface LineReader {
public interface LineReader extends Closeable {

/*
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand All @@ -42,7 +43,7 @@ The Apache Software Foundation (http://www.apache.org/).
* @author Morozko
*
*/
public interface LineWriter {
public interface LineWriter extends Closeable {

/*
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,8 +20,6 @@ public abstract class AbstractPagedResult<T> extends BasicResult implements Page
private int currentPage;

private List<T> pageElements;

private Map<String, Object> info;

protected AbstractPagedResult() {
super( RESULT_CODE_OK );
Expand All @@ -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<String, Object>();
}




/*
* <p>The position of the first element of the current pages ( (currentPage-1) * perPage )</p>
Expand Down Expand Up @@ -138,7 +131,7 @@ public Iterator<Integer> getPageCountIterator() {

@Override
public Map<String, Object> getInfo() {
return info;
return this.getInfoMap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public class DefaultPagedResult<T> extends AbstractPagedResult<T> implements Se

private String virtualKey;

/**
* Creates a new PagedResult
*
* @param <T> 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 <T> PagedResult<T> newPagedResult( int perPage, long elementCount, int currentPage, List<T> pageElements ) {
int pageCount = calcPageCount( elementCount, perPage );
AbstractPagedResult<T> result = new DefaultPagedResult<T>( perPage, elementCount, currentPage, pageCount, pageElements, perPage, currentPage, null );
Expand All @@ -28,7 +38,7 @@ public static <T> PagedResult<T> newPagedResult( int perPage, long elementCount
return result;
}

protected static <T> PagedResult<T> newPagedResult( int resultCode ) {
public static <T> PagedResult<T> newPagedResult( int resultCode ) {
DefaultPagedResult<T> result = new DefaultPagedResult<T>( -1, -1, -1, -1, null, -1, -1, null );
result.setResultCode( resultCode );
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ public interface PagedResult<T> extends Result {
*/
public Iterator<Integer> getPageCountIterator();

/**
* Result code for this page
*
* @return result code
*/
@Override
public int getResultCode();

/**
* Additional info of this page.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class CacheWrapper<T> {

public CacheWrapper(PagedResult<T> page) {
super();
this.page = page;
this.cacheTime = System.currentTimeMillis();
}

Expand All @@ -80,17 +79,9 @@ public CacheWrapper(PagedResult<T> page) {
public PagedResult<T> getPage() {
return page;
}

public void setPage(PagedResult<T> page) {
this.page = page;
}


public long getCacheTime() {
return cacheTime;
}

public void setCacheTime(long cacheTime) {
this.cacheTime = cacheTime;
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> lines = LineIOUtils.readLines( is );
Assert.assertEquals( EXPECTED_SIZE , lines.size() );
}
}

@Test
public void testLineReadFile() throws IOException {
List<String> 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 );
}
}

}
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Loading

0 comments on commit 3a371cb

Please sign in to comment.