/** * Create a new ResourceScriptSource for the given resource. * @param resource the Resource to load the script from (using UTF-8 encoding) */ public ResourceScriptSource(Resource resource) { Assert.notNull(resource, "Resource must not be null"); this.resource = new EncodedResource(resource, "UTF-8"); }
/** * Set the encoding used for reading the script resource. * <p>The default value for regular Resources is "UTF-8". * A {@code null} value implies the platform default. */ public void setEncoding(@Nullable String encoding) { this.resource = new EncodedResource(this.resource.getResource(), encoding); }
/** * Load bean definitions from the specified properties file. * @param resource the resource descriptor for the properties file * @param prefix a filter within the keys in the map: e.g. 'beans.' * (can be empty or {@code null}) * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ public int loadBeanDefinitions(Resource resource, @Nullable String prefix) throws BeanDefinitionStoreException { return loadBeanDefinitions(new EncodedResource(resource), prefix); }
/** * Load bean definitions from the specified properties file, * using all property keys (i.e. not filtering by prefix). * @param resource the resource descriptor for the properties file * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors * @see #loadBeanDefinitions(org.springframework.core.io.Resource, String) */ @Override public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException { return loadBeanDefinitions(new EncodedResource(resource), null); }
/** * Load bean definitions from the specified Groovy script or XML file. * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds * of resources will be parsed as Groovy scripts. * @param resource the resource descriptor for the Groovy script or XML file * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException { return loadBeanDefinitions(new EncodedResource(resource)); }
/** * Load bean definitions from the specified XML file. * @param resource the resource descriptor for the XML file * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ @Override public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException { return loadBeanDefinitions(new EncodedResource(resource)); }
/** * {@inheritDoc} * @see #execute(DataSource) */ @Override public void populate(Connection connection) throws ScriptException { Assert.notNull(connection, "Connection must not be null"); for (Resource script : this.scripts) { EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding); ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter); } }
/** * Execute the given SQL script using default settings for statement * separators, comment delimiters, and exception handling flags. * <p>Statement separators and comments will be removed before executing * individual statements within the supplied script. * <p><strong>Warning</strong>: this method does <em>not</em> release the * provided {@link Connection}. * @param connection the JDBC connection to use to execute the script; already * configured and ready to use * @param resource the resource to load the SQL script from; encoded with the * current platform's default encoding * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String) * @see #DEFAULT_STATEMENT_SEPARATOR * @see #DEFAULT_COMMENT_PREFIX * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException { executeSqlScript(connection, new EncodedResource(resource)); }
/** * Create a PropertySource having the given name based on Properties * loaded from the given encoded resource. */ public ResourcePropertySource(String name, Resource resource) throws IOException { super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = getNameForResource(resource); }
/** * Create a PropertySource based on Properties loaded from the given resource. * The name of the PropertySource will be generated based on the * {@link Resource#getDescription() description} of the given resource. */ public ResourcePropertySource(Resource resource) throws IOException { super(getNameForResource(resource), PropertiesLoaderUtils.loadProperties(new EncodedResource(resource))); this.resourceName = null; }
@Override public void setAsText(String text) throws IllegalArgumentException { this.resourceEditor.setAsText(text); Resource resource = (Resource) this.resourceEditor.getValue(); try { setValue(resource != null ? new EncodedResource(resource).getReader() : null); } catch (IOException ex) { throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex); } }
@Test public void equalsWithDifferentEncoding() { EncodedResource er1 = new EncodedResource(resource, UTF8); EncodedResource er2 = new EncodedResource(resource, UTF16); assertNotEquals(er1, er2); }
@Test public void equalsWithSameCharset() { EncodedResource er1 = new EncodedResource(resource, UTF8_CS); EncodedResource er2 = new EncodedResource(resource, UTF8_CS); assertEquals(er1, er2); }
@Test public void equalsWithDifferentCharset() { EncodedResource er1 = new EncodedResource(resource, UTF8_CS); EncodedResource er2 = new EncodedResource(resource, UTF16_CS); assertNotEquals(er1, er2); }
@Test public void equalsWithEncodingAndCharset() { EncodedResource er1 = new EncodedResource(resource, UTF8); EncodedResource er2 = new EncodedResource(resource, UTF8_CS); assertNotEquals(er1, er2); }
@Test public void equalsWithSameEncoding() { EncodedResource er1 = new EncodedResource(resource, UTF8); EncodedResource er2 = new EncodedResource(resource, UTF8); assertEquals(er1, er2); }
private String readScript(String path) throws Exception { EncodedResource resource = new EncodedResource(new ClassPathResource(path, getClass())); return ScriptUtils.readScript(resource); }
@Test public void equalsWithNullOtherObject() { assertFalse(new EncodedResource(resource).equals(null)); }
@Test public void resourceInjection() throws IOException { System.setProperty("logfile", "do_not_delete_me.txt"); try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) { ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class); Resource resource = new ClassPathResource("do_not_delete_me.txt"); assertEquals(resource, resourceInjectionBean.resource); assertEquals(resource.getURL(), resourceInjectionBean.url); assertEquals(resource.getURI(), resourceInjectionBean.uri); assertEquals(resource.getFile(), resourceInjectionBean.file); assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()), FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream)); assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()), FileCopyUtils.copyToString(resourceInjectionBean.reader)); } finally { System.getProperties().remove("logfile"); } }
@Test public void testRefToSingleton() throws Exception { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); reader.loadBeanDefinitions(new EncodedResource(REFTYPES_CONTEXT, "ISO-8859-1")); TestBean jen = (TestBean) xbf.getBean("jenny"); TestBean dave = (TestBean) xbf.getBean("david"); TestBean jenks = (TestBean) xbf.getBean("jenks"); ITestBean davesJen = dave.getSpouse(); ITestBean jenksJen = jenks.getSpouse(); assertTrue("1 jen instance", davesJen == jenksJen); assertTrue("1 jen instance", davesJen == jen); }