Search in sources :

Example 1 with ClassPathScanner

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner in project flyway by flyway.

the class ClassUtilsSmallTest method addDirectoryToClasspath.

/**
     * Tests dynamically adding a directory to the classpath.
     */
@Test
public void addDirectoryToClasspath() throws Exception {
    assertFalse(new ClassPathResource("pkg/runtime.conf", getClassLoader()).exists());
    String folder = new ClassPathResource("dynamic", getClassLoader()).getLocationOnDisk();
    ClassUtils.addJarOrDirectoryToClasspath(folder);
    assertTrue(new ClassPathResource("pkg/runtime.conf", getClassLoader()).exists());
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:pkg"), "run", ".conf");
    assertEquals("pkg/runtime.conf", resources[0].getLocation());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 2 with ClassPathScanner

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner in project flyway by flyway.

the class ClassUtilsSmallTest method addJarToClasspath.

/**
     * Tests dynamically adding a jar file to the classpath.
     */
@Test
public void addJarToClasspath() throws Exception {
    assertFalse(new ClassPathResource("db/migration/V1__Initial_structure.sql.sql", getClassLoader()).exists());
    assertFalse(ClassUtils.isPresent("org.flywaydb.sample.migration.V1_2__Another_user", getClassLoader()));
    String jar = new ClassPathResource("flyway-sample.jar", getClassLoader()).getLocationOnDisk();
    assertTrue(new File(jar).isFile());
    ClassUtils.addJarOrDirectoryToClasspath(jar);
    assertTrue(new ClassPathResource("db/migration/V1__Initial_structure.sql", getClassLoader()).exists());
    assertTrue(ClassUtils.isPresent("org.flywaydb.sample.migration.V1_2__Another_user", getClassLoader()));
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:db/migration"), "V1__", ".sql");
    assertEquals("db/migration/V1__Initial_structure.sql", resources[0].getLocation());
    Class<?>[] classes = new ClassPathScanner(getClassLoader()).scanForClasses(new Location("classpath:org/flywaydb/sample/migration"), SpringJdbcMigration.class);
    assertEquals("org.flywaydb.sample.migration.V1_2__Another_user", classes[0].getName());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) File(java.io.File) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 3 with ClassPathScanner

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner in project flyway by flyway.

the class ClassUtilsSmallTest method addDirectoryToClasspathDefaultPackage.

/**
     * Tests dynamically adding a directory to the default package of classpath.
     */
@Test
public void addDirectoryToClasspathDefaultPackage() throws Exception {
    assertFalse(new ClassPathResource("runtime.conf", getClassLoader()).exists());
    String folder = new ClassPathResource("dynamic/pkg2", getClassLoader()).getLocationOnDisk();
    ClassUtils.addJarOrDirectoryToClasspath(folder);
    assertTrue(new ClassPathResource("funtime.properties", getClassLoader()).exists());
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:"), "fun", ".properties");
    assertEquals("funtime.properties", resources[1].getLocation());
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 4 with ClassPathScanner

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner in project che by eclipse.

the class ResourcesFinder method findResources.

/**
     * Finds script resources in configured {@link FlywayConfiguration#getLocations()}.
     *
     * @param configuration
     *         flyway configuration to find scripts
     * @return found scripts or an empty list if nothing found
     * @throws IOException
     *         when any io error occurs during scripts look up
     */
Map<Location, List<Resource>> findResources(FlywayConfiguration configuration) throws IOException {
    final String prefix = configuration.getSqlMigrationPrefix();
    final String suffix = configuration.getSqlMigrationSuffix();
    final ClassPathScanner cpScanner = new ClassPathScanner(configuration.getClassLoader());
    final FileSystemScanner fsScanner = new FileSystemScanner();
    final Map<Location, List<Resource>> resources = new HashMap<>();
    for (String rawLocation : configuration.getLocations()) {
        final Location location = new Location(rawLocation);
        if (location.isClassPath()) {
            resources.put(location, newArrayList(cpScanner.scanForResources(location, prefix, suffix)));
        } else {
            resources.put(location, newArrayList(fsScanner.scanForResources(location, prefix, suffix)));
        }
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) FileSystemScanner(org.flywaydb.core.internal.util.scanner.filesystem.FileSystemScanner) Location(org.flywaydb.core.internal.util.Location)

Example 5 with ClassPathScanner

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner in project flyway by flyway.

the class ClassUtilsSmallTest method addJarToClasspathNoDirectoryEntries.

/**
     * Tests dynamically adding a jar file to the classpath.
     */
@Test
public void addJarToClasspathNoDirectoryEntries() throws Exception {
    assertTrue(new ClassPathResource("db/migration/V1_11__Create_tbl_bob.sql", getClassLoader()).exists());
    Resource[] resources = new ClassPathScanner(getClassLoader()).scanForResources(new Location("classpath:db/migration"), "V1_11", ".sql");
    Class[] classes = new ClassPathScanner(getClassLoader()).scanForClasses(new Location("classpath:db/migration"), JdbcMigration.class);
    assertEquals("db/migration/V1_11__Create_tbl_bob.sql", resources[0].getLocation());
    assertEquals(0, classes.length);
}
Also used : Resource(org.flywaydb.core.internal.util.scanner.Resource) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) ClassPathScanner(org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathScanner (org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner)5 Resource (org.flywaydb.core.internal.util.scanner.Resource)4 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)4 Test (org.junit.Test)4 AfterClass (org.junit.AfterClass)2 BeforeClass (org.junit.BeforeClass)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 File (java.io.File)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Location (org.flywaydb.core.internal.util.Location)1 FileSystemScanner (org.flywaydb.core.internal.util.scanner.filesystem.FileSystemScanner)1