Skip to content

Commit

Permalink
Bael 2434 (eugenp#6635)
Browse files Browse the repository at this point in the history
* BAEL-2434

* BAEL-2434

* Minor alterations to reflect changes in the article

* Fixing Spel expression and removing extra code that is not referenced anymore in the article

* Moved the files from spring-core to spring-static-resources. Renamed ResourceUtil to ResourceReader.

* Changes due to change of article outline.

* Changes due to change of article outline.

* Removing Converter from article
  • Loading branch information
cscib authored and ashleyfrieze committed Jun 18, 2019
1 parent b2df125 commit 5488905
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spring-static-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@
<artifactId>handlebars</artifactId>
<version>${handlebars.version}</version>
</dependency>

<!-- commons.io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -208,6 +215,9 @@

<!-- Maven plugins -->
<yuicompressor-maven-plugin.version>1.5.1</yuicompressor-maven-plugin.version>

<!-- commons.io -->
<commons.io.version>2.5</commons.io.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.loadresourceasstring;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoadResourceConfig {

@Bean
public String resourceString() {
return ResourceReader.readFileToString("resource.txt");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.loadresourceasstring;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;

import static java.nio.charset.StandardCharsets.UTF_8;


public class ResourceReader {

public static String readFileToString(String path) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(path);
return asString(resource);
}

public static String asString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
1 change: 1 addition & 0 deletions spring-static-resources/src/main/resources/resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a resource text file. This file will be loaded as a resource and use its contents as a string.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.loadresourceasstring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.util.FileCopyUtils;

import java.io.InputStreamReader;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = LoadResourceConfig.class)
public class LoadResourceAsStringIntegrationTest {

private static final String EXPECTED_RESOURCE_VALUE = "This is a resource text file. This file will be loaded as a " + "resource and use its contents as a string.";

@Value("#{T(com.baeldung.loadresourceasstring.ResourceReader).readFileToString('classpath:resource.txt')}")
private String resourceStringUsingSpel;

@Autowired
@Qualifier("resourceString")
private String resourceString;

@Autowired
private ResourceLoader resourceLoader;

@Test
public void givenUsingResourceLoadAndFileCopyUtils_whenConvertingAResourceToAString_thenCorrect() {
Resource resource = resourceLoader.getResource("classpath:resource.txt");
assertEquals(EXPECTED_RESOURCE_VALUE, ResourceReader.asString(resource));
}

@Test
public void givenUsingResourceStringBean_whenConvertingAResourceToAString_thenCorrect() {
assertEquals(EXPECTED_RESOURCE_VALUE, resourceString);
}

@Test
public void givenUsingSpel_whenConvertingAResourceToAString_thenCorrect() {
assertEquals(EXPECTED_RESOURCE_VALUE, resourceStringUsingSpel);
}




}

0 comments on commit 5488905

Please sign in to comment.