Skip to content

Commit

Permalink
Add include/exclude attributes to FileFormatSource
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 16, 2024
1 parent ff461dc commit 7682e8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/test/java/io/github/spannm/jackcess/PropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,8 @@ void testModifyProperties(TestDb testDb) throws Exception {
}

@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource
@FileFormatSource(exclude = "GENERIC_JET4")
void testCreateDbProperties(FileFormat fileFormat) throws Exception {
if (fileFormat == FileFormat.GENERIC_JET4) {
// weirdo format, no properties
return;
}

UUID u1 = UUID.randomUUID();
UUID u2 = UUID.randomUUID();

Expand Down
10 changes: 6 additions & 4 deletions src/test/java/io/github/spannm/jackcess/impl/JetFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.github.spannm.jackcess.test.AbstractBaseTest;
import io.github.spannm.jackcess.test.TestDb;
import io.github.spannm.jackcess.test.TestUtil;
import io.github.spannm.jackcess.test.source.FileFormatSource;
import io.github.spannm.jackcess.test.source.TestDbReadOnlySource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -69,10 +70,11 @@ void testFileFormat1(TestDb testDb) throws Exception {
}
}

@Test
void testFileFormat2() throws Exception {
try (Database db = TestUtil.open(FileFormat.GENERIC_JET4, new File(DIR_TEST_DATA, "adox_jet4.mdb"))) {
assertEquals(FileFormat.GENERIC_JET4, db.getFileFormat());
@ParameterizedTest(name = "[{index}] {0}")
@FileFormatSource(include = "GENERIC_JET4")
void testFileFormat2(FileFormat ff) throws Exception {
try (Database db = TestUtil.open(ff, new File(DIR_TEST_DATA, "adox_jet4.mdb"))) {
assertEquals(ff, db.getFileFormat());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.platform.commons.support.AnnotationSupport;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -23,18 +26,40 @@
@ArgumentsSource(FileFormatArgumentsProvider.class)
public @interface FileFormatSource {

/**
* Optional names of enum constants to include.<br>
* If specified, the names must match existing enum constants otherwise an {@link IllegalArgumentException} is thrown.<br>
* If not specified, all enum constants are taken into consideration.
*/
String[] include() default {};

/**
* Optional names of enum constants to exclude.<br>
* If used, the names must match existing enum constants otherwise an {@link IllegalArgumentException} is thrown.
*/
String[] exclude() default {};

static class FileFormatArgumentsProvider implements ArgumentsProvider {

/**
* Defines currently supported database file formats that are neither read-only nor {@value FileFormat#MSISAM} (MS Money).
*/
private static final FileFormat[] FILE_FORMATS_WRITE = Arrays.stream(FileFormat.values())
private static final List<FileFormat> FILE_FORMATS_WRITE = Arrays.stream(FileFormat.values())
.filter(ff -> !DatabaseImpl.getFileFormatDetails(ff).getFormat().READ_ONLY && ff != FileFormat.MSISAM)
.toArray(FileFormat[]::new);
.collect(Collectors.toList());

@Override
public Stream<Arguments> provideArguments(ExtensionContext context) {
return Arrays.stream(FILE_FORMATS_WRITE).map(Arguments::of);
public Stream<Arguments> provideArguments(ExtensionContext _context) {
FileFormatSource src = _context.getElement().map(elem -> AnnotationSupport.findAnnotation(elem, FileFormatSource.class).get()).orElse(null);
List<FileFormat> include = Arrays.stream(src.include()).map(FileFormat::valueOf).collect(Collectors.toList());
if (include.isEmpty()) {
include.addAll(FILE_FORMATS_WRITE);
}
List<FileFormat> exclude = Arrays.stream(src.exclude()).map(FileFormat::valueOf).collect(Collectors.toList());

return include.stream()
.filter(ff -> !exclude.contains(ff))
.map(Arguments::of);
}

}
Expand Down

0 comments on commit 7682e8f

Please sign in to comment.