类org.springframework.core.io.ClassRelativeResourceLoader源码实例Demo

下面列出了怎么用org.springframework.core.io.ClassRelativeResourceLoader的API类实例代码及写法,或者点击链接到github查看源代码。

@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();
	}
}
 
@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();
}
 
@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();
	}
}
 
@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();
}
 
@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();
	}
}
 
@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();
}
 
源代码7 项目: sofa-lookout   文件: LookoutAllBootstrap.java
/**
 * 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);
    }
}
 
 同包方法