Java源码示例:org.springframework.core.io.ClassRelativeResourceLoader

示例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();
	}
}
 
示例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();
}
 
示例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();
	}
}
 
示例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();
}
 
示例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();
	}
}
 
示例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();
}
 
示例7
/**
 * 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);
    }
}