Skip to content

Commit

Permalink
various test cases #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sombra-dmytro-mula committed Aug 29, 2019
1 parent 079d3bb commit 9eb728b
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/sombrainc/excelorm/Excelorm.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static <E> E read(Sheet sheet, Class<E> targetClass, CellIndexTracker tra
}

/**
* To load the the data into POJO object based on excel doc and selected sheet name.
* Loads the the data into POJO object based on excel doc and selected sheet name.
*
* @param docInputStream input stream to the excel file
* @param sheetName selected Apache POI sheet
Expand All @@ -64,10 +64,10 @@ public static <E> E read(Sheet sheet, Class<E> targetClass, CellIndexTracker tra
*/
public static <E> E read(InputStream docInputStream, String sheetName, Class<E> targetClass) {
if (docInputStream == null) {
throw new NullPointerException("InputSteam could not be null");
throw new POIRuntimeException("InputSteam could not be null");
}
if (StringUtils.isNullOrEmpty(sheetName)) {
throw new NullPointerException("Sheet name could not be null");
throw new POIRuntimeException("Sheet name could not be null");
}
try (Workbook wb = WorkbookFactory.create(docInputStream)) {
Sheet sheet = wb.getSheet(sheetName);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/sombrainc/excelorm/e2/impl/Bind.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sombrainc.excelorm.e2.impl;

import com.sombrainc.excelorm.utils.StringUtils;
import lombok.Data;
import lombok.experimental.Accessors;

Expand All @@ -17,8 +16,8 @@ public class Bind {
private Function<BindField, Boolean> until;

public Bind(String field, String cell) {
this.field = StringUtils.requireNotBlank(field);
this.initialCell = StringUtils.requireNotBlank(cell);
this.field = field;
this.initialCell = cell;
}

public Bind filter(final Function<BindField, Boolean> filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ protected T go() {
try {
return execute();
} catch (RuntimeException ex) {
if (ex instanceof POIRuntimeException) {
throw ex;
}
throw new POIRuntimeException(ex);
} finally {
if (workbook != null) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/sombrainc/excelorm/e2/impl/MiddleExecutor.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.sombrainc.excelorm.e2.impl;

import com.sombrainc.excelorm.exception.IncorrectRangeException;
import com.sombrainc.excelorm.exception.POIRuntimeException;
import com.sombrainc.excelorm.exception.TypeIsNotSupportedException;
import com.sombrainc.excelorm.utils.ReflectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;

import static com.sombrainc.excelorm.utils.ExcelUtils.getOrCreateCell;
import static com.sombrainc.excelorm.utils.ExcelUtils.readGenericValueFromSheet;
import static com.sombrainc.excelorm.utils.ReflectionUtils.getInstance;
import static com.sombrainc.excelorm.utils.TypesUtils.isPureObject;
Expand All @@ -25,12 +25,13 @@ protected MiddleExecutor(EReaderContext context) {
super(context);
}

protected<R> R readForSingleObject(List<Pair<Bind, CellRangeAddress>> pairOfFiels, Class<R> aClass,
protected<R> R readForSingleObject(List<Pair<Bind, CellRangeAddress>> pairOfFields, Class<R> aClass,
FormulaEvaluator formulaEvaluator) {
validate(pairOfFields);
final R instance = getInstance(aClass);
final Field[] allFields = FieldUtils.getAllFields(aClass);
for (Field field : allFields) {
final Pair<Bind, CellRangeAddress> pair = pairOfFiels.stream()
final Pair<Bind, CellRangeAddress> pair = pairOfFields.stream()
.filter(p -> p.getKey().getField().equals(field.getName())).findFirst().orElse(null);
if (pair == null) {
continue;
Expand All @@ -49,6 +50,14 @@ protected<R> R readForSingleObject(List<Pair<Bind, CellRangeAddress>> pairOfFiel
return instance;
}

private void validate(List<Pair<Bind, CellRangeAddress>> list) {
for (Pair<Bind, CellRangeAddress> pair : list) {
if (StringUtils.isBlank(pair.getKey().getField())) {
throw new POIRuntimeException("Field name could not be empty");
}
}
}

private<R> void readSingleFieldAsCollection(FormulaEvaluator formulaEvaluator, R instance,
Field field, Pair<Bind, CellRangeAddress> pair) {
final Collection<Object> collection = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sombrainc.excelorm.e2.impl.CoreExecutor;
import com.sombrainc.excelorm.e2.impl.MiddleExecutor;
import com.sombrainc.excelorm.exception.IncorrectRangeException;
import com.sombrainc.excelorm.exception.POIRuntimeException;
import com.sombrainc.excelorm.exception.TypeIsNotSupportedException;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.poi.ss.usermodel.Cell;
Expand Down Expand Up @@ -61,9 +62,12 @@ public List<T> execute() {
}

private void validate() {
if (target.aClass == null) {
throw new POIRuntimeException("Generic class is null");
}
if (!isPureObject(target.aClass)) {
if (target.binds.isEmpty()) {
throw new TypeIsNotSupportedException("You should explicitly map the object fields");
throw new TypeIsNotSupportedException("Check if the mapped object is supported or define the object fields to be bind");
}
if (!isVector(obtainRange(target.range))) {
throw new IncorrectRangeException("For user custom object the range should be on the same column/row");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.sombrainc.excelorm.e2.impl.BindField;
import com.sombrainc.excelorm.e2.impl.CoreActions;
import com.sombrainc.excelorm.e2.impl.CoreExecutor;
import com.sombrainc.excelorm.e2.impl.MiddleExecutor;
import org.apache.poi.ss.usermodel.Cell;

import java.util.Map;
import java.util.Optional;
Expand All @@ -20,11 +18,11 @@ protected void validateOnPureObjects(MapHolder holder) {
validateOnPureObject(holder.getValueClass(), "Value object is not supported.");
}

protected<K1, V1> boolean isUntilByKeyReached(MapHolder<K1, V1> holder, BindField keyCell) {
protected <K1, V1> boolean isUntilByKeyReached(MapHolder<K1, V1> holder, BindField keyCell) {
return Optional.ofNullable(holder.getKeyUntil()).map(func -> func.apply(keyCell)).orElse(false);
}

protected<K1, V1> boolean filterByKey(MapHolder<K1, V1> holder, BindField keyCell) {
protected <K1, V1> boolean filterByKey(MapHolder<K1, V1> holder, BindField keyCell) {
return Optional.ofNullable(holder.getKeyFilter()).map(func -> !func.apply(keyCell)).orElse(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public Map<K, List<V>> execute() {
continue;
}
final K key = readRequestedType(evaluator, keyCell, holder.getKeyMapper(), holder.getKeyClass());

if (!isVector(keyRange) || !isVector(valueRange) || isSameVector(keyRange, valueRange)) {
final V value = readRequestedType(evaluator, new BindField(toCell(valueIterator.next()), evaluator),
holder.getValueMapper(), holder.getValueClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SinglePick<T> pick(String cell) {
}

public SinglePick<T> binds(Bind... binds) {
this.binds = Arrays.asList(Objects.requireNonNull(binds));
this.binds = Arrays.asList(binds);
return new SinglePick<>(this.getEReaderContext(), aClass, cell, mapper, this.binds);
}

Expand Down
Binary file removed src/main/resources/test.xlsx
Binary file not shown.
6 changes: 3 additions & 3 deletions src/test/java/com/sombrainc/excelorm/ReadViaStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public void readUsingInputStreamTest() {
Assert.assertTrue(model.getD1BoolObject());
}

@Test(expectedExceptions = NullPointerException.class)
@Test(expectedExceptions = POIRuntimeException.class)
public void readUsingInputStreamExceptionTest() {
ModelReader.getModel(null, Model1.class, "position");
}

@Test(expectedExceptions = NullPointerException.class)
@Test(expectedExceptions = POIRuntimeException.class)
public void readUsingInputStreamSheetInNullTest() {
ModelReader.getModel(ReadViaStreamTest.class.getResourceAsStream(ModelReader.PATH), Model1.class, null);
}

@Test(expectedExceptions = NullPointerException.class)
@Test(expectedExceptions = POIRuntimeException.class)
public void readUsingInputStreamSheetInEmptyTest() {
ModelReader.getModel(ReadViaStreamTest.class.getResourceAsStream(ModelReader.PATH), Model1.class, " ");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.sombrainc.excelorm.e2;
package com.sombrainc.excelorm.e2.list;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sombrainc.excelorm.e2.dto.UserDTO;
import com.sombrainc.excelorm.e2.impl.Bind;
import com.sombrainc.excelorm.e2.utils.EFilters;
import com.sombrainc.excelorm.exception.POIRuntimeException;
import com.sombrainc.excelorm.exception.TypeIsNotSupportedException;
import com.sombrainc.excelorm.utils.Comparisons;
import com.sombrainc.excelorm.utils.Jackson;
import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand All @@ -30,6 +33,17 @@ public void numberAsText() {
});
}

public void empty() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<String> value = e2
.listOf(String.class)
.pick("B8")
.filter(field -> !isNotBlank(field))
.go();
Assert.assertEquals(value, new ArrayList<>());
});
}

public void numberAsTextObject() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<UserDTO> value = e2
Expand Down Expand Up @@ -97,10 +111,6 @@ public void customObjectWithTwoListInsideFilterUntilMap() {
});
}

public static void main(String[] args) {
Stream.of(1,2,3,4,5,6).filter(integer -> integer > 3).forEach(System.out::println);
}

public void integer() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<Integer> value = e2
Expand Down Expand Up @@ -145,4 +155,6 @@ public void longFilter() {
});
}



}
124 changes: 124 additions & 0 deletions src/test/java/com/sombrainc/excelorm/e2/list/ListTestFailures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.sombrainc.excelorm.e2.list;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sombrainc.excelorm.e2.dto.UserDTO;
import com.sombrainc.excelorm.e2.impl.Bind;
import com.sombrainc.excelorm.e2.utils.EFilters;
import com.sombrainc.excelorm.exception.POIRuntimeException;
import com.sombrainc.excelorm.exception.TypeIsNotSupportedException;
import com.sombrainc.excelorm.utils.Comparisons;
import com.sombrainc.excelorm.utils.Jackson;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.sombrainc.excelorm.e2.utils.EFilters.isNotBlank;
import static com.sombrainc.excelorm.utils.ModelReader.executeForE2;

@Test
public class ListTestFailures {
private static final String DEFAULT_SHEET = "e2Single";

@Test(expectedExceptions = TypeIsNotSupportedException.class)
public void ofSet() {
executeForE2(DEFAULT_SHEET, e2 -> {
e2
.listOf(Set.class)
.pick("B48:B50")
.go();
});
}

@Test(expectedExceptions = TypeIsNotSupportedException.class)
public void ofMap() {
executeForE2(DEFAULT_SHEET, e2 -> {
e2
.listOf(Map.class)
.pick("B48:B50")
.go();
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void ofNull() {
executeForE2(DEFAULT_SHEET, e2 -> {
e2
.listOf(null)
.pick("B48:B50")
.go();
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void pickIsNull() {
executeForE2(DEFAULT_SHEET, e2 -> {
final List<String> go = e2
.listOf(String.class)
.pick(null)
.go();
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void pickIsIncorrect() {
executeForE2(DEFAULT_SHEET, e2 -> {
e2
.listOf(String.class)
.pick("null")
.go();
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void customObjectFieldIsNull() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<UserDTO> value = e2
.listOf(UserDTO.class)
.binds(
new Bind(null, "M24:O24").filter(isNotBlank()),
new Bind("listOfIntAsStr", "M24:O24").filter(isNotBlank())
)
.pick("M24:M29")
.filter(EFilters::isNotBlank)
.go();
Comparisons.compareLists(value, Jackson.parseTo(new TypeReference<List<UserDTO>>() {
}, "/json/e2/list/customObjectWithTwoListInside.json"));
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void customObjectCellIsNull() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<UserDTO> value = e2
.listOf(UserDTO.class)
.binds(
new Bind("test", "M24:O24").filter(isNotBlank()),
new Bind("listOfIntAsStr", null).filter(isNotBlank())
)
.pick("M24:M29")
.filter(EFilters::isNotBlank)
.go();
Comparisons.compareLists(value, Jackson.parseTo(new TypeReference<List<UserDTO>>() {
}, "/json/e2/list/customObjectWithTwoListInside.json"));
});
}

@Test(expectedExceptions = POIRuntimeException.class)
public void customObjectCellIsIncorrect() {
executeForE2(DEFAULT_SHEET, e2 -> {
List<UserDTO> value = e2
.listOf(UserDTO.class)
.binds(
new Bind("test", "M24:O24").filter(isNotBlank()),
new Bind("listOfIntAsStr", "A-").filter(isNotBlank())
)
.pick("M24:M29")
.filter(EFilters::isNotBlank)
.go();
Comparisons.compareLists(value, Jackson.parseTo(new TypeReference<List<UserDTO>>() {
}, "/json/e2/list/customObjectWithTwoListInside.json"));
});
}
}
3 changes: 0 additions & 3 deletions src/test/java/com/sombrainc/excelorm/utils/ModelReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public static synchronized <E> E getModel(Class<E> modelClass, String sheetName)
public static synchronized void executeForE2(String sheetName, Consumer<EReader> reader) {
try (XSSFWorkbook wb = new XSSFWorkbook(new File(ModelReader.class.getResource(PATH).getFile()))) {
EReader eReader = new EReader(wb.getSheet(sheetName));
XSSFSheet position = wb.getSheet("position");
double numericCellValue = position.getRow(0).getCell(0).getNumericCellValue();
// do actual test
reader.accept(eReader);
} catch (InvalidFormatException | IOException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 9eb728b

Please sign in to comment.