Skip to content

Commit

Permalink
Added unit tests for reading excel core_properties_set
Browse files Browse the repository at this point in the history
* Set the locale to en-US for tests, as tests break on non en-US machines
  • Loading branch information
breucode committed May 6, 2020
1 parent d14993f commit 2d71369
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 27 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<configuration>
<argLine>${add.opens}</argLine>
<argLine>-Djava.awt.headless=true</argLine>
<argLine>-Duser.language=en -Duser.region=US</argLine>
</configuration>
</plugin>

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/poiji/bind/mapping/PropertyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ private static void setPropertyValueOnTarget(String propertyName, POIXMLProperti
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getRevision(), targetObject);
break;
default:
ReflectUtil.setFieldData(targetField, poixmlProperties.getCustomProperties().getProperty(propertyName).getLpwstr(), targetObject);
if (poixmlProperties.getCustomProperties().getProperty(propertyName) != null) {
ReflectUtil.setFieldData(targetField, poixmlProperties.getCustomProperties().getProperty(propertyName).getLpwstr(), targetObject);
}

break;
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/test/java/com/poiji/deserialize/metadata/CorePropertyTest.java

This file was deleted.

76 changes: 76 additions & 0 deletions src/test/java/com/poiji/deserialize/metadata/PropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.poiji.deserialize.metadata;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.metadata.model.CorePropertyEntity;
import com.poiji.exception.PoijiExcelType;
import com.poiji.option.PoijiOptions;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class PropertyTest {

@Test
public void readProperties() {
CorePropertyEntity deserializedProperties = Poiji.fromExcelProperties(
new File("src/test/resources/core_properties_set.xlsx"),
CorePropertyEntity.class);

assertExcelProperties(deserializedProperties, 1588771680000L);
}

@Test
public void readPropertiesInputStream() throws FileNotFoundException {
CorePropertyEntity deserializedProperties = Poiji.fromExcelProperties(new FileInputStream(
new File("src/test/resources/core_properties_set.xlsx")),
PoijiExcelType.XLSX,
CorePropertyEntity.class);

assertExcelProperties(deserializedProperties, 1588771680000L);
}

@Test
public void readCorePropertiesWithPassword() {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().password("testPassword").build();
CorePropertyEntity deserializedProperties = Poiji.fromExcelProperties(
new File("src/test/resources/core_properties_set_password.xlsx"),
CorePropertyEntity.class, options);

assertExcelProperties(deserializedProperties, 1588772003000L);
}

@Test
public void readCorePropertiesInputStreamWithPassword() throws FileNotFoundException {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().password("testPassword").build();
CorePropertyEntity deserializedProperties = Poiji.fromExcelProperties(new FileInputStream(
new File("src/test/resources/core_properties_set_password.xlsx")),
PoijiExcelType.XLSX,
CorePropertyEntity.class, options);

assertExcelProperties(deserializedProperties, 1588772003000L);
}

private void assertExcelProperties(CorePropertyEntity deserializedProperties, long modified) {
assertThat(deserializedProperties.getTitle(), is("TestTitle"));
assertThat(deserializedProperties.getCategory(), is("TestCategory"));
assertThat(deserializedProperties.getContentStatus(), is("TestStatus"));
assertThat(deserializedProperties.getCreator(), is("TestAuthor"));
assertThat(deserializedProperties.getCreated(), is(new Date(1588689638000L)));
assertThat(deserializedProperties.getDescription(), is("TestDescription"));
assertThat(deserializedProperties.getKeywords(), is("TestKeywords"));
assertThat(deserializedProperties.getLastPrinted(), is(new Date(1588768892000L)));
assertThat(deserializedProperties.getModified(), is(new Date(modified)));
assertThat(deserializedProperties.getSubject(), is("TestSubject"));
assertThat(deserializedProperties.getCustomProperty(), is("customValue"));
//Only testing, if field is null, as newer Excel versions don't set this field any more.
// An older excel version is required to set this field.
assertThat(deserializedProperties.getRevision(), is(nullValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,81 @@ public class CorePropertyEntity {
@ExcelProperty
private Date modified;

@ExcelProperty
private String category;

@ExcelProperty
private String contentStatus;

@ExcelProperty
private Date created;

@ExcelProperty
private String creator;

@ExcelProperty
private String description;

@ExcelProperty
private String keywords;

@ExcelProperty
private Date lastPrinted;

@ExcelProperty
private String subject;

@ExcelProperty
private String revision;

@ExcelProperty
private String customProperty;

public String getTitle() {
return title;
}

public Date getModified() {
return modified;
}

public String getCategory() {
return category;
}

public String getContentStatus() {
return contentStatus;
}

public Date getCreated() {
return created;
}

public String getCreator() {
return creator;
}

public String getDescription() {
return description;
}

public String getKeywords() {
return keywords;
}

public Date getLastPrinted() {
return lastPrinted;
}

public String getSubject() {
return subject;
}

public String getRevision() {
return revision;
}

public String getCustomProperty() {
return customProperty;
}
}
Binary file removed src/test/resources/corePropertiesSet.xlsx
Binary file not shown.
Binary file added src/test/resources/core_properties_set.xlsx
Binary file not shown.
Binary file modified src/test/resources/core_properties_set_password.xlsx
Binary file not shown.

0 comments on commit 2d71369

Please sign in to comment.