Skip to content

Commit

Permalink
Merge pull request #28 from scottescue/issue/22/support-multi-package…
Browse files Browse the repository at this point in the history
…-scanning

Adding support for scanning multiple packages for JPA entities
  • Loading branch information
scottescue committed Oct 7, 2016
2 parents 1f07ac7 + ea1d494 commit 5218595
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
Expand Up @@ -20,24 +20,23 @@ public abstract class ScanningEntityManagerBundle<T extends Configuration> exten
/**
* @param path string with package containing JPA entities (classes annotated with {@code @Entity}
* annotation) e.g. {@code com.my.application.directory.entities}
* @param paths any additional strings with packages containing JPA entities
*/
protected ScanningEntityManagerBundle(String path) {
this(path,
new EntityManagerFactoryFactory(),
new SharedEntityManagerFactory());
protected ScanningEntityManagerBundle(String path, String... paths) {
this(new EntityManagerFactoryFactory(), new SharedEntityManagerFactory(), path, paths);
}

@VisibleForTesting
ScanningEntityManagerBundle(String path,
EntityManagerFactoryFactory entityManagerFactoryFactory,
SharedEntityManagerFactory sharedEntityManagerFactory) {
super(findEntityClassesFromDirectory(path), entityManagerFactoryFactory, sharedEntityManagerFactory);
ScanningEntityManagerBundle(EntityManagerFactoryFactory entityManagerFactoryFactory,
SharedEntityManagerFactory sharedEntityManagerFactory,
String path, String... paths) {
super(findEntityClassesFromDirectory(path, paths), entityManagerFactoryFactory, sharedEntityManagerFactory);
}

private static ImmutableList<Class<?>> findEntityClassesFromDirectory(String path) {
private static ImmutableList<Class<?>> findEntityClassesFromDirectory(String path, String... paths) {
@SuppressWarnings("unchecked")
final AnnotationAcceptingListener asl = new AnnotationAcceptingListener(Entity.class);
final PackageNamesScanner scanner = new PackageNamesScanner(new String[]{path}, true);
final PackageNamesScanner scanner = new PackageNamesScanner(merge(path, paths), true);

while (scanner.hasNext()) {
final String next = scanner.next();
Expand All @@ -55,4 +54,11 @@ private static ImmutableList<Class<?>> findEntityClassesFromDirectory(String pat

return builder.build();
}

private static String[] merge(String arg, String... args) {
String[] combinedPaths = new String[args.length + 1];
combinedPaths[0] = arg;
System.arraycopy(args, 0, combinedPaths, 1, args.length);
return combinedPaths;
}
}
@@ -1,9 +1,10 @@
package com.scottescue.dropwizard.entitymanager;

import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg.FakeEntity1;
import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg.FakeEntity2;
import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg.deep.DeepFakeEntity;
import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg.deep.deeper.DeeperFakeEntity;
import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg2.FakeEntity2;
import com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg3.FakeEntity3;
import io.dropwizard.Configuration;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.setup.Environment;
Expand All @@ -12,7 +13,9 @@
import static org.assertj.core.api.Assertions.assertThat;

public class ScanningEntityManagerBundleTest {

@Test
@SuppressWarnings("unchecked")
public void testFindEntityClassesFromDirectory() {
String packageWithEntities = "com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg";
ScanningEntityManagerBundle bundle = new ScanningEntityManagerBundle(packageWithEntities) {
Expand All @@ -26,9 +29,33 @@ public PooledDataSourceFactory getDataSourceFactory(Configuration configuration)
}
};

assertThat(bundle.getEntities()).containsOnly(
FakeEntity1.class,
DeepFakeEntity.class,
DeeperFakeEntity.class);
}

@Test
@SuppressWarnings("unchecked")
public void testFindEntityClassesFromMultipleDirectories() {
String root = "com.scottescue.dropwizard.entitymanager.entity.fake.entities.";

ScanningEntityManagerBundle bundle = new ScanningEntityManagerBundle(
root.concat("pckg"), root.concat("pckg2"), root.concat("pckg3")) {
@Override
public void run(Object o, Environment environment) throws Exception {
}

@Override
public PooledDataSourceFactory getDataSourceFactory(Configuration configuration) {
return null;
}
};

assertThat(bundle.getEntities()).containsOnly(
FakeEntity1.class,
FakeEntity2.class,
FakeEntity3.class,
DeepFakeEntity.class,
DeeperFakeEntity.class);
}
Expand Down
@@ -1,4 +1,4 @@
package com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg;
package com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg2;

import javax.persistence.Entity;

Expand Down
@@ -0,0 +1,8 @@
package com.scottescue.dropwizard.entitymanager.entity.fake.entities.pckg3;

import javax.persistence.Entity;

@Entity
public class FakeEntity3 {

}

0 comments on commit 5218595

Please sign in to comment.