Java Code Examples for org.springframework.core.io.ClassRelativeResourceLoader

The following examples show how to use org.springframework.core.io.ClassRelativeResourceLoader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void createSameSchemaTwiceWithoutUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql").build();

	try {
		new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
		.addScripts("db-schema-without-dropping.sql").build();

		fail("Should have thrown a ScriptStatementFailedException");
	}
	catch (ScriptStatementFailedException e) {
		// expected
	}
	finally {
		db1.shutdown();
	}
}
 
Example 2
@Test
public void createSameSchemaTwiceWithGeneratedUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();

	JdbcTemplate template1 = new JdbcTemplate(db1);
	assertNumRowsInTestTable(template1, 1);
	template1.update("insert into T_TEST (NAME) values ('Sam')");
	assertNumRowsInTestTable(template1, 2);

	EmbeddedDatabase db2 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();
	assertDatabaseCreated(db2);

	db1.shutdown();
	db2.shutdown();
}
 
Example 3
@Test
public void createSameSchemaTwiceWithoutUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql").build();

	try {
		new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
		.addScripts("db-schema-without-dropping.sql").build();

		fail("Should have thrown a ScriptStatementFailedException");
	}
	catch (ScriptStatementFailedException e) {
		// expected
	}
	finally {
		db1.shutdown();
	}
}
 
Example 4
@Test
public void createSameSchemaTwiceWithGeneratedUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();

	JdbcTemplate template1 = new JdbcTemplate(db1);
	assertNumRowsInTestTable(template1, 1);
	template1.update("insert into T_TEST (NAME) values ('Sam')");
	assertNumRowsInTestTable(template1, 2);

	EmbeddedDatabase db2 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();
	assertDatabaseCreated(db2);

	db1.shutdown();
	db2.shutdown();
}
 
Example 5
@Test
public void createSameSchemaTwiceWithoutUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql").build();

	try {
		new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
		.addScripts("db-schema-without-dropping.sql").build();

		fail("Should have thrown a ScriptStatementFailedException");
	}
	catch (ScriptStatementFailedException e) {
		// expected
	}
	finally {
		db1.shutdown();
	}
}
 
Example 6
@Test
public void createSameSchemaTwiceWithGeneratedUniqueDbNames() throws Exception {
	EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();

	JdbcTemplate template1 = new JdbcTemplate(db1);
	assertNumRowsInTestTable(template1, 1);
	template1.update("insert into T_TEST (NAME) values ('Sam')");
	assertNumRowsInTestTable(template1, 2);

	EmbeddedDatabase db2 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
	.addScripts("db-schema-without-dropping.sql", "db-test-data.sql")//
	.generateUniqueName(true)//
	.build();
	assertDatabaseCreated(db2);

	db1.shutdown();
	db2.shutdown();
}
 
Example 7
Source Project: sofa-lookout   Source File: LookoutAllBootstrap.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * copy configs from classpath to external fileSystem
 *
 * @param bootName
 * @throws IOException
 */
private static void copyConfigFiles(String bootName) throws IOException {
    Path tempDirectory = Files.createTempDirectory(bootName + "-configs");
    JSONObject configs = new JSONObject();
    configs.put("configDir", tempDirectory.toAbsolutePath().toString());
    String configsStr = configs.toJSONString();
    System.setProperty("sofaark.configs", configsStr);
    LOGGER.info("set system property sofaark.configs = {}", configsStr);

    // We now use ResourcePatternResolver from spring-core to help us find all properties in a directory
    Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(
        new ClassRelativeResourceLoader(LookoutAllBootstrap.class)).getResources(
        "classpath:app-configs/*/*.properties");
    for (Resource resource : resources) {
        String uri = resource.getURI().toString();
        int slashIndex1 = uri.lastIndexOf('/');
        int slashIndex0 = uri.lastIndexOf('/', slashIndex1 - 1);
        String app = uri.substring(slashIndex0 + 1, slashIndex1);
        String configName = uri.substring(slashIndex1 + 1);

        Path appConfigPath = tempDirectory.resolve(app);
        appConfigPath.toFile().mkdirs();
        Path targetPath = appConfigPath.resolve(configName);
        LOGGER.info("copy {} to {}", uri, targetPath);
        Files.copy(resource.getInputStream(), targetPath);
    }
}