-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from thiyagu-7/sheetname
Supporting configuring Excel sheet via annotation
- Loading branch information
Showing
10 changed files
with
296 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.poiji.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to set the sheet name to be processed. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Documented | ||
public @interface ExcelSheet { | ||
/** | ||
* Specifies the name of the sheet that is to be processed. | ||
* @return the sheet name. | ||
*/ | ||
String value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/poiji/bind/mapping/SheetNameExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.poiji.bind.mapping; | ||
|
||
import com.poiji.annotation.ExcelSheet; | ||
import com.poiji.exception.PoijiException; | ||
import com.poiji.option.PoijiOptions; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Utility class to extract the sheet name. | ||
*/ | ||
class SheetNameExtractor { | ||
/** | ||
* Extracts the sheet name from either the annotated value {@link ExcelSheet} from the model class or from the sheet name set | ||
* in the Poiji Options. Returns Optional.empty when sheet name is not configured at either place. | ||
* @param type The class instance of the object model. | ||
* @param options The Poiji options. | ||
* @param <T> The type of the object model. | ||
* @return an Optional sheet name | ||
* @throws PoijiException when the configured sheet name in the PoijiOptions and the annotated sheet name do not match. | ||
*/ | ||
public static <T> Optional<String> getSheetName(Class<T> type, PoijiOptions options) { | ||
String annotatedSheetName = null; | ||
String configuredSheetName = options.getSheetName(); | ||
|
||
if (type.isAnnotationPresent(ExcelSheet.class)) { | ||
ExcelSheet excelSheet = type.getAnnotation(ExcelSheet.class); | ||
annotatedSheetName = excelSheet.value(); | ||
} | ||
if (annotatedSheetName != null && configuredSheetName != null | ||
&& !annotatedSheetName.equals(options.getSheetName())) { | ||
throw new PoijiException(String.format("The configured sheet name in PoijiOptions (%s) and the annotated sheet name " | ||
+ "(%s) do not match", configuredSheetName, annotatedSheetName)); | ||
} | ||
|
||
if (annotatedSheetName != null) { | ||
return Optional.of(annotatedSheetName); | ||
} | ||
return Optional.ofNullable(configuredSheetName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/com/poiji/deserialize/ReadExcelByAnnotatedSheetNameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.poiji.deserialize; | ||
|
||
import com.poiji.bind.Poiji; | ||
import com.poiji.deserialize.model.Student; | ||
import com.poiji.exception.PoijiException; | ||
import com.poiji.option.PoijiOptions; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static com.poiji.util.Data.unmarshallingStudents; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* Test for Reading excel files using annotated sheet name | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class ReadExcelByAnnotatedSheetNameTest { | ||
private String path; | ||
private List<Student> expectedStudents; | ||
private PoijiOptions options; | ||
private Class<?> expectedException; | ||
|
||
public ReadExcelByAnnotatedSheetNameTest(String path, List<Student> expectedStudents, PoijiOptions options, | ||
Class<?> expectedException) { | ||
this.path = path; | ||
this.expectedStudents = expectedStudents; | ||
this.options = options; | ||
this.expectedException = expectedException; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{index}: ({0})={1}") | ||
public static Iterable<Object[]> queries() { | ||
return Arrays.asList(new Object[][]{ | ||
{"src/test/resources/student.xlsx", unmarshallingStudents(), null, null}, | ||
{"src/test/resources/student.xls", unmarshallingStudents(), null, null}, | ||
{"src/test/resources/student.xlsx", unmarshallingStudents(), PoijiOptions.PoijiOptionsBuilder.settings() | ||
.sheetName("Sheet1") | ||
.build(), | ||
PoijiException.class}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void shouldReadAnnotatedSheetNameFromStudent() { | ||
|
||
try { | ||
List<Student> actualStudents; | ||
if (options == null) { | ||
actualStudents = Poiji.fromExcel(new File(path), Student.class); | ||
} else { | ||
actualStudents = Poiji.fromExcel(new File(path), Student.class, options); | ||
} | ||
|
||
assertThat(actualStudents, notNullValue()); | ||
assertThat(actualStudents.size(), not(0)); | ||
assertThat(actualStudents.size(), is(expectedStudents.size())); | ||
|
||
Student actualStudent1 = actualStudents.get(0); | ||
Student actualStudent2 = actualStudents.get(1); | ||
|
||
Student expectedStudent1 = expectedStudents.get(0); | ||
Student expectedStudent2 = expectedStudents.get(1); | ||
|
||
assertEquals(actualStudent1, expectedStudent1); | ||
assertEquals(actualStudent2, expectedStudent2); | ||
} catch (Exception e) { | ||
if (expectedException == null) { | ||
fail(e.getMessage()); | ||
} else { | ||
assertThat(e, instanceOf(expectedException)); | ||
assertEquals("The configured sheet name in PoijiOptions (Sheet1) and the annotated sheet name " | ||
+ "(Sheet2) do not match", e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.