org.springframework.core.io.ClassRelativeResourceLoader Java Examples

The following examples show how to use org.springframework.core.io.ClassRelativeResourceLoader. 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
Source File: EmbeddedDatabaseBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: EmbeddedDatabaseBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: EmbeddedDatabaseBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: EmbeddedDatabaseBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: EmbeddedDatabaseBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: EmbeddedDatabaseBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: LookoutAllBootstrap.java    From sofa-lookout with 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);
    }
}