Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wjsrobertson committed Mar 27, 2016
2 parents 96fa45c + c3eddae commit 80b4fea
Show file tree
Hide file tree
Showing 36 changed files with 663 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.class
target
target/*
**/target
**/target/*
*.jar
hs_err_pid*

Expand Down
6 changes: 6 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junitVersion}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.unitsheet</groupId>
<artifactId>unitsheet-api</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions all/src/main/assembly/release.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
33 changes: 33 additions & 0 deletions all/src/test/java/org/unitsheet/SpreadsheetRuleReadColumnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.unitsheet;

import org.junit.Rule;
import org.junit.Test;
import org.unitsheet.annotations.ReadCell;
import org.unitsheet.annotations.ReadColumn;
import org.unitsheet.annotations.Workbook;
import org.unitsheet.junit.SpreadsheetRule;

import java.util.List;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.unitsheet.utils.Collections.listOf;

@Workbook("classpath:spreadsheets/SumOfColumn.ods")
public class SpreadsheetRuleReadColumnTest {

@Rule
public SpreadsheetRule spreadsheetRule = new SpreadsheetRule();

@ReadColumn(from = "B2", to = "B6")
private List<Integer> listOfNumbers;

@Test
public void checkAllNumbersGetRead() {
List<Integer> expcected = listOf(100, 200, 300, 400, 500);
assertThat(listOfNumbers)
.hasSize(5)
.isEqualTo(expcected);
}

}
4 changes: 3 additions & 1 deletion all/src/test/java/org/unitsheet/SpreadsheetRunnerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.unitsheet;

import org.junit.Ignore;
import org.unitsheet.annotations.ReadCell;
import org.unitsheet.annotations.Workbook;
import org.unitsheet.annotations.ForceWorkbookType;
Expand All @@ -11,8 +12,9 @@
import static org.junit.Assert.assertEquals;

@RunWith(SpreadsheetRunner.class)
@Workbook("classpath:spreadsheets/AverageOfTwoIntegers.ods")
@Workbook("classpath:spreadsheets/AverageOfTwoIntegers.xls")
@ForceWorkbookType(XlsType.class)
@Ignore
public class SpreadsheetRunnerTest {

@ReadCell("C2")
Expand Down
Binary file not shown.
35 changes: 35 additions & 0 deletions api/src/main/java/org/unitsheet/api/adapter/CellInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public CellInfo(String sheetName, String name, Integer row, Integer column) {
this.column = column;
}

public static Builder builder() {
return new Builder();
}

public String getSheetName() {
return sheetName;
}
Expand All @@ -29,4 +33,35 @@ public Integer getRow() {
public Integer getColumn() {
return column;
}

public static class Builder {
private String sheetName;
private String name;
private Integer row;
private Integer column;

public CellInfo build() {
return new CellInfo(sheetName, name, row, column);
}

public Builder withSheetName(String sheetName) {
this.sheetName = sheetName;
return this;
}

public Builder withName(String name) {
this.name = name;
return this;
}

public Builder withRow(Integer row) {
this.row = row;
return this;
}

public Builder withColumn(Integer column) {
this.column = column;
return this;
}
}
}
29 changes: 29 additions & 0 deletions api/src/main/java/org/unitsheet/api/adapter/ColumnInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.unitsheet.api.adapter;

// TODO - use Builder
public class ColumnInfo {

private final String sheetName;

private final CellInfo fromCellInfo;

private final CellInfo toCellInfo;

public ColumnInfo(String sheetName, CellInfo fromCellInfo, CellInfo toCellInfo) {
this.sheetName = sheetName;
this.fromCellInfo = fromCellInfo;
this.toCellInfo = toCellInfo;
}

public String getSheetName() {
return sheetName;
}

public CellInfo getFromCellInfo() {
return fromCellInfo;
}

public CellInfo getToCellInfo() {
return toCellInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface SpreadsheetAdapter {
*/
public Object getCellValue(CellInfo cellInfo);

public List<Object> getRange(String startCellName, String endCellName, String sheetName);
public List<Object> getColumn(ColumnInfo columnInfo);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.unitsheet.api.exceptions;

public class InvalidColumnRangeException extends UnitSheetException {

public InvalidColumnRangeException() {
}

public InvalidColumnRangeException(String message) {
super(message);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.unitsheet.api.exceptions;

public class UnitSheetException extends RuntimeException {

public UnitSheetException() {
}

public UnitSheetException(String message) {
super(message);
}

public UnitSheetException(String message, Throwable cause) {
super(message, cause);
}

public UnitSheetException(Throwable cause) {
super(cause);
}

public UnitSheetException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/org/unitsheet/TestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.io.*;

import static org.unitsheet.utils.ArgumentChecks.checkNotNull;
import static org.unitsheet.utils.Paths.fileExtension;
import static org.unitsheet.utils.FilePaths.fileExtension;

public class TestManager {

Expand Down
80 changes: 51 additions & 29 deletions core/src/main/java/org/unitsheet/TestObjectFieldPopulator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package org.unitsheet;

import org.unitsheet.annotations.ReadCell;
import org.unitsheet.annotations.ReadColumn;
import org.unitsheet.api.adapter.CellInfo;
import org.unitsheet.api.adapter.ColumnInfo;
import org.unitsheet.types.ObjectConverter;
import org.unitsheet.api.adapter.SpreadsheetAdapter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import static java.util.Arrays.asList;
import static org.unitsheet.utils.ReflectionUtils.getGenericTypeClass;
import static org.unitsheet.utils.ReflectionUtils.getObjectFieldsInOrder;
import static org.unitsheet.utils.ReflectionUtils.setObjectFieldValue;

public class TestObjectFieldPopulator {

Expand All @@ -26,48 +33,63 @@ public void populateFieldsWithValuesFromSpreadsheet(Object testObject) {
SortedSet<Field> fields = getObjectFieldsInOrder(testObject);

for (Field field : fields) {
ReadCell annotation = field.getAnnotation(ReadCell.class);
if (annotation != null) {
String cellName = annotation.value();
String sheetName = annotation.sheet();

CellInfo cellInfo = new CellInfo(sheetName, cellName, null, null);

Object cellValue = spreadsheet.getCellValue(cellInfo);
Object fieldValue = objectConverter.convertType(cellValue, field.getType());
ReadCell readCell = field.getAnnotation(ReadCell.class);
if (readCell != null) {
handleReadCellAnnotation(testObject, field, readCell);
}

try {
setObjectFieldValue(field, testObject, fieldValue);
} catch (IllegalAccessException | SecurityException e) {
throw new RuntimeException(); // TODO - handle me
}
ReadColumn readColumn = field.getAnnotation(ReadColumn.class);
if (readColumn != null) {
handleReadColumnAnnotation(testObject, field, readColumn);
}
}

}

private SortedSet<Field> getObjectFieldsInOrder(Object object) {
Class<?> clazz = object.getClass();
SortedSet<Field> fields = new TreeSet<>((Field x, Field y) -> x.getName().compareTo(y.getName()));
fields.addAll(asList(clazz.getFields()));
fields.addAll(asList(clazz.getDeclaredFields()));
private void handleReadColumnAnnotation(Object testObject, Field field, ReadColumn readColumn) {
String sheetName = readColumn.sheet();
String from = readColumn.from();
String to = readColumn.to();

return fields;
}
CellInfo fromCellInfo = CellInfo.builder().withName(from).withSheetName(sheetName).build();
CellInfo toCellInfo = CellInfo.builder().withName(to).withSheetName(sheetName).build();

ColumnInfo columnInfo = new ColumnInfo(sheetName, fromCellInfo, toCellInfo);

private void setObjectFieldValue(Field field, Object object, Object value)
throws IllegalAccessException, SecurityException {
List<Object> column = spreadsheet.getColumn(columnInfo);
List<Object> results = new ArrayList<>();

boolean accessibleField = field.isAccessible();
if (!accessibleField) {
field.setAccessible(true);
Class<?> collectionGenericTypeClass = getGenericTypeClass(field);

for (Object o : column) {
Object value = objectConverter.convertType(o, collectionGenericTypeClass);
results.add(value);
}

try {
setObjectFieldValue(field, testObject, results);
} catch (IllegalAccessException | SecurityException e) {
throw new RuntimeException(); // TODO - handle me
}
}

field.set(object, value);
private void handleReadCellAnnotation(Object testObject, Field field, ReadCell readCell) {
Object cellValue = resolveCellValue(readCell);
Object fieldValue = objectConverter.convertType(cellValue, field.getType());

if (!accessibleField) {
field.setAccessible(false);
try {
setObjectFieldValue(field, testObject, fieldValue);
} catch (IllegalAccessException | SecurityException e) {
throw new RuntimeException(); // TODO - handle me
}
}

private Object resolveCellValue(ReadCell annotation) {
String cellName = annotation.value();
String sheetName = annotation.sheet();

CellInfo cellInfo = CellInfo.builder().withName(cellName).withSheetName(sheetName).build();

return spreadsheet.getCellValue(cellInfo);
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/org/unitsheet/annotations/ReadCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
public @interface ReadCell {

String sheet() default "";

String name() default "";

String value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Range {
public @interface ReadColumn {
String sheet() default "";
String name() default "";
String value();

String from() default "";

String to() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.unitsheet.api.adapter.CellInfo;
import org.unitsheet.api.adapter.ColumnInfo;
import org.unitsheet.api.adapter.SpreadsheetAdapter;

import java.util.List;
Expand Down Expand Up @@ -42,9 +43,11 @@ public Object getCellValue(CellInfo cellInfo) {

private Sheet getSheetWithFirstByDefault(String sheetName) {
int sheetIndex = 0;

if (isNotEmpty(sheetName)) {
sheetIndex = workbook.getSheetIndex(sheetName);
}

return workbook.getSheetAt(sheetIndex);
}

Expand All @@ -67,7 +70,7 @@ public Object getCellValue(Cell cell) {
}

@Override
public List<Object> getRange(String startCellName, String endCellName, String sheetName) {
public List<Object> getColumn(ColumnInfo columnInfo) {
return null;
}

Expand Down
Loading

0 comments on commit 80b4fea

Please sign in to comment.