[BAEL-3392] Formatted code examples for [BAEL-3392]

This commit is contained in:
Martin van Wingerden
2019-11-01 09:05:12 +01:00
parent db85c8f275
commit 0e23f2e682
20519 changed files with 1642357 additions and 0 deletions
@@ -0,0 +1,37 @@
package com.baeldung.Lazy;
import com.baeldung.lazy.AppConfig;
import com.baeldung.lazy.Country;
import com.baeldung.lazy.Region;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class LazyAnnotationUnitTest {
@Test
public void givenLazyAnnotation_whenConfigClass_thenLazyAll() {
// Add @Lazy to AppConfig.class while testing
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
ctx.getBean(Region.class);
ctx.getBean(Country.class);
}
@Test
public void givenLazyAnnotation_whenAutowire_thenLazyBean() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Region region = ctx.getBean(Region.class);
region.getCityInstance();
}
@Test
public void givenLazyAnnotation_whenBeanConfig_thenLazyBean() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
ctx.getBean(Region.class);
}
}
@@ -0,0 +1,59 @@
package com.baeldung.applicationcontext;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Locale;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
public class ClasspathXmlApplicationContextIntegrationTest {
@Test
public void testBasicUsage() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
Student student = (Student) context.getBean("student");
assertThat(student.getNo(), equalTo(15));
assertThat(student.getName(), equalTo("Tom"));
Student sameStudent = context.getBean("student", Student.class);// do not need cast class
assertThat(sameStudent.getNo(), equalTo(15));
assertThat(sameStudent.getName(), equalTo("Tom"));
}
@Test
public void testRegisterShutdownHook() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
context.registerShutdownHook();
}
@Test
public void testInternationalization() {
MessageSource resources = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-internationalization.xml");
String enHello = resources.getMessage("hello", null, "Default", Locale.ENGLISH);
String enYou = resources.getMessage("you", null, Locale.ENGLISH);
String enThanks = resources.getMessage("thanks", new Object[] { enYou }, Locale.ENGLISH);
assertThat(enHello, equalTo("hello"));
assertThat(enThanks, equalTo("thank you"));
String chHello = resources.getMessage("hello", null, "Default", Locale.SIMPLIFIED_CHINESE);
String chYou = resources.getMessage("you", null, Locale.SIMPLIFIED_CHINESE);
String chThanks = resources.getMessage("thanks", new Object[] { chYou }, Locale.SIMPLIFIED_CHINESE);
assertThat(chHello, equalTo("你好"));
assertThat(chThanks, equalTo("谢谢你"));
}
@Test
public void testApplicationContextAware() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml");
Teacher teacher = context.getBean("teacher", Teacher.class);
List<Course> courses = teacher.getCourses();
assertThat(courses.size(), equalTo(1));
assertThat(courses.get(0).getName(), equalTo("math"));
}
}
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Introduction to Java Servlets](http://www.baeldung.com/intro-to-servlets)
- [Intro to the Spring ClassPathXmlApplicationContext](http://www.baeldung.com/spring-classpathxmlapplicationcontext)
@@ -0,0 +1,120 @@
package com.baeldung.classpathfileaccess;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
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.ResourceUtils;
/**
* Test class illustrating various methods of accessing a file from the classpath using Resource.
* @author tritty
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class SpringResourceIntegrationTest {
/**
* Resource loader instance for lazily loading resources.
*/
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private ApplicationContext appContext;
/**
* Injecting resource
*/
@Value("classpath:data/employees.dat")
private Resource resourceFile;
/**
* Data in data/employee.dat
*/
private static final String EMPLOYEES_EXPECTED = "Joe Employee,Jan Employee,James T. Employee";
@Test
public void whenResourceLoader_thenReadSuccessful() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:data/employees.dat");
final String employees = new String(Files.readAllBytes(resource.getFile()
.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenApplicationContext_thenReadSuccessful() throws IOException {
final Resource resource = appContext.getResource("classpath:data/employees.dat");
final String employees = new String(Files.readAllBytes(resource.getFile()
.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenAutowired_thenReadSuccessful() throws IOException {
final String employees = new String(Files.readAllBytes(resourceFile.getFile()
.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenResourceUtils_thenReadSuccessful() throws IOException {
final File employeeFile = loadEmployeesWithSpringInternalClass();
final String employees = new String(Files.readAllBytes(employeeFile.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenResourceAsStream_thenReadSuccessful() throws IOException {
final InputStream resource = new ClassPathResource("data/employees.dat").getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
final String employees = reader.lines()
.collect(Collectors.joining("\n"));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
}
@Test
public void whenResourceAsFile_thenReadSuccessful() throws IOException {
final File resource = new ClassPathResource("data/employees.dat").getFile();
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenClassPathResourceWithAbsoultePath_thenReadSuccessful() throws IOException {
final File resource = new ClassPathResource("/data/employees.dat", this.getClass()).getFile();
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenClassPathResourceWithRelativePath_thenReadSuccessful() throws IOException {
final File resource = new ClassPathResource("../../../data/employees.dat", SpringResourceIntegrationTest.class).getFile();
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
public File loadEmployeesWithSpringInternalClass() throws FileNotFoundException {
return ResourceUtils.getFile("classpath:data/employees.dat");
}
}
@@ -0,0 +1,16 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class ApplicationContextTestResourceNameType {
@Bean(name = "namedFile")
public File namedFile() {
File namedFile = new File("namedFile.txt");
return namedFile;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class ApplicationContextTestResourceQualifier {
@Bean(name = "defaultFile")
public File defaultFile() {
File defaultFile = new File("defaultFile.txt");
return defaultFile;
}
@Bean(name = "namedFile")
public File namedFile() {
File namedFile = new File("namedFile.txt");
return namedFile;
}
}
@@ -0,0 +1,33 @@
package com.baeldung.lombok;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class ApologizeServiceAutowiringIntegrationTest {
private final static String TRANSLATED = "TRANSLATED";
@Autowired
private ApologizeService apologizeService;
@Autowired
private Translator translator;
@Test
public void apologizeWithTranslatedMessage() {
when(translator.translate("sorry")).thenReturn(TRANSLATED);
assertEquals(TRANSLATED, apologizeService.apologize());
}
}
@@ -0,0 +1,21 @@
package com.baeldung.lombok;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ApologizeServiceIntegrationTest {
private final static String MESSAGE = "MESSAGE";
private final static String TRANSLATED = "TRANSLATED";
@Test
public void apologizeWithCustomTranslatedMessage() {
Translator translator = mock(Translator.class);
ApologizeService apologizeService = new ApologizeService(translator, MESSAGE);
when(translator.translate(MESSAGE)).thenReturn(TRANSLATED);
assertEquals(TRANSLATED, apologizeService.apologize());
}
}
@@ -0,0 +1,31 @@
package com.baeldung.lombok;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class FarewellAutowiringIntegrationTest {
@Autowired
private FarewellService farewellService;
@Autowired
private Translator translator;
@Test
public void sayByeWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("bye")).thenReturn(translated);
assertEquals(translated, farewellService.farewell());
}
}
@@ -0,0 +1,20 @@
package com.baeldung.lombok;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FarewellServiceIntegrationTest {
private final static String TRANSLATED = "TRANSLATED";
@Test
public void sayByeWithTranslatedMessage() {
Translator translator = mock(Translator.class);
when(translator.translate("bye")).thenReturn(TRANSLATED);
FarewellService farewellService = new FarewellService(translator);
assertEquals(TRANSLATED, farewellService.farewell());
}
}
@@ -0,0 +1,37 @@
package com.baeldung.lombok;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class GreetingServiceIntegrationTest {
@Autowired
private GreetingService greetingService;
@Autowired
private Translator translator;
@Test
public void greetWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("hello")).thenReturn(translated);
assertEquals(translated, greetingService.greet());
}
@Test(expected = NullPointerException.class)
public void throwWhenInstantiated() {
GreetingService greetingService = new GreetingService();
greetingService.greet();
}
}
@@ -0,0 +1,17 @@
package com.baeldung.lombok;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import static org.mockito.Mockito.mock;
@Configuration
@ComponentScan("com.baeldung.lombok")
class TestConfig {
@Bean
public Translator mockTranslator() {
return mock(Translator.class);
}
}
@@ -0,0 +1,31 @@
package com.baeldung.lombok;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class ThankingServiceAutowiringIntegrationTest {
@Autowired
private ThankingService thankingService;
@Autowired
private Translator translator;
@Test
public void thankWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("thank you")).thenReturn(translated);
assertEquals(translated, thankingService.thank());
}
}
@@ -0,0 +1,20 @@
package com.baeldung.lombok;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ThankingServiceIntegrationTest {
private final static String TRANSLATED = "TRANSLATED";
@Test
public void thankWithTranslatedMessage() {
Translator translator = mock(Translator.class);
when(translator.translate("thank you")).thenReturn(TRANSLATED);
ThankingService thankingService = new ThankingService(translator);
assertEquals(TRANSLATED, thankingService.thank());
}
}
@@ -0,0 +1,30 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceNameType.class)
public class FieldResourceInjectionIntegrationTest {
@Resource(name = "namedFile")
private File defaultFile;
@Test
public void givenResourceAnnotation_WhenOnField_ThenDependencyValid() {
assertNotNull(defaultFile);
assertEquals("namedFile.txt", defaultFile.getName());
}
}
@@ -0,0 +1,45 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceQualifier.class)
public class MethodByQualifierResourceIntegrationTest {
private File arbDependency;
private File anotherArbDependency;
@Test
public void givenResourceQualifier_WhenSetter_ThenValidDependencies() {
assertNotNull(arbDependency);
assertEquals("namedFile.txt", arbDependency.getName());
assertNotNull(anotherArbDependency);
assertEquals("defaultFile.txt", anotherArbDependency.getName());
}
@Resource
@Qualifier("namedFile")
public void setArbDependency(File arbDependency) {
this.arbDependency = arbDependency;
}
@Resource
@Qualifier("defaultFile")
public void setAnotherArbDependency(File anotherArbDependency) {
this.anotherArbDependency = anotherArbDependency;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceNameType.class)
public class MethodByTypeResourceIntegrationTest {
private File defaultFile;
@Resource
protected void setDefaultFile(File defaultFile) {
this.defaultFile = defaultFile;
}
@Test
public void givenResourceAnnotation_WhenSetter_ThenValidDependency() {
assertNotNull(defaultFile);
assertEquals("namedFile.txt", defaultFile.getName());
}
}
@@ -0,0 +1,34 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceNameType.class)
public class MethodResourceInjectionIntegrationTest {
private File defaultFile;
@Resource(name = "namedFile")
protected void setDefaultFile(File defaultFile) {
this.defaultFile = defaultFile;
}
@Test
public void givenResourceAnnotation_WhenSetter_ThenDependencyValid() {
assertNotNull(defaultFile);
assertEquals("namedFile.txt", defaultFile.getName());
}
}
@@ -0,0 +1,29 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceNameType.class)
public class NamedResourceIntegrationTest {
@Resource(name = "namedFile")
private File testFile;
@Test
public void givenResourceAnnotation_WhenOnField_THEN_DEPENDENCY_Found() {
assertNotNull(testFile);
assertEquals("namedFile.txt", testFile.getName());
}
}
@@ -0,0 +1,42 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceQualifier.class)
public class QualifierResourceInjectionIntegrationTest {
@Resource
@Qualifier("defaultFile")
private File dependency1;
@Resource
@Qualifier("namedFile")
private File dependency2;
@Test
public void givenResourceAnnotation_WhenField_ThenDependency1Valid() {
assertNotNull(dependency1);
assertEquals("defaultFile.txt", dependency1.getName());
}
@Test
public void givenResourceQualifier_WhenField_ThenDependency2Valid() {
assertNotNull(dependency2);
assertEquals("namedFile.txt", dependency2.getName());
}
}
@@ -0,0 +1,33 @@
package com.baeldung.resource;
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.annotation.Resource;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
classes = ApplicationContextTestResourceNameType.class)
public class SetterResourceInjectionIntegrationTest {
private File defaultFile;
@Resource
protected void setDefaultFile(File defaultFile) {
this.defaultFile = defaultFile;
}
@Test
public void givenResourceAnnotation_WhenOnSetter_THEN_MUST_INJECT_Dependency() {
assertNotNull(defaultFile);
assertEquals("namedFile.txt", defaultFile.getName());
}
}
@@ -0,0 +1,63 @@
package com.baeldung.scope;
import com.baeldung.scope.prototype.PrototypeBean;
import com.baeldung.scope.singletone.SingletonFunctionBean;
import com.baeldung.scope.singletone.SingletonLookupBean;
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
import com.baeldung.scope.singletone.SingletonProviderBean;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class)
public class PrototypeBeanInjectionIntegrationTest {
@Test
public void givenPrototypeInjection_WhenObjectFactory_ThenNewInstanceReturn() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SingletonObjectFactoryBean firstContext = context.getBean(SingletonObjectFactoryBean.class);
SingletonObjectFactoryBean secondContext = context.getBean(SingletonObjectFactoryBean.class);
PrototypeBean firstInstance = firstContext.getPrototypeInstance();
PrototypeBean secondInstance = secondContext.getPrototypeInstance();
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
}
@Test
public void givenPrototypeInjection_WhenLookup_ThenNewInstanceReturn() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SingletonLookupBean firstContext = context.getBean(SingletonLookupBean.class);
SingletonLookupBean secondContext = context.getBean(SingletonLookupBean.class);
PrototypeBean firstInstance = firstContext.getPrototypeBean();
PrototypeBean secondInstance = secondContext.getPrototypeBean();
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
}
@Test
public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SingletonProviderBean firstContext = context.getBean(SingletonProviderBean.class);
SingletonProviderBean secondContext = context.getBean(SingletonProviderBean.class);
PrototypeBean firstInstance = firstContext.getPrototypeInstance();
PrototypeBean secondInstance = secondContext.getPrototypeInstance();
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
}
}
@@ -0,0 +1,34 @@
package com.baeldung.scope;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.config.scope.AppConfigFunctionBean;
import com.baeldung.scope.prototype.PrototypeBean;
import com.baeldung.scope.singletone.SingletonFunctionBean;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfigFunctionBean.class)
public class PrototypeFunctionBeanIntegrationTest {
@Test
public void givenPrototypeInjection_WhenFunction_ThenNewInstanceReturn() {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfigFunctionBean.class);
SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class);
SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class);
PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1");
PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2");
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
}
}
@@ -0,0 +1,19 @@
package com.baeldung.springbean;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.springbean.domain.Company;
public class SpringBeanIntegrationTest {
@Test
public void whenUsingIoC_thenDependenciesAreInjected() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Company company = context.getBean("company", Company.class);
assertEquals("High Street", company.getAddress().getStreet());
assertEquals(1000, company.getAddress().getNumber());
}
}
@@ -0,0 +1,100 @@
package com.baeldung.streamutils;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.util.StreamUtils;
import static com.baeldung.streamutils.CopyStream.getStringFromInputStream;
public class CopyStreamIntegrationTest {
@Test
public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
StreamUtils.copy(in, out);
assertTrue(outputFile.exists());
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(inputFileContent, outputFileContent);
}
@Test
public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
StreamUtils.copyRange(in, out, 1, 10);
assertTrue(outputFile.exists());
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(inputFileContent.substring(1, 11), outputFileContent);
}
@Test
public void whenCopyStringToOutputStream_thenCorrect() throws IOException {
String string = "Should be copied to OutputStream.";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
OutputStream out = new FileOutputStream("src/test/resources/output.txt");
StreamUtils.copy(string, StandardCharsets.UTF_8, out);
assertTrue(outputFile.exists());
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(outputFileContent, string);
}
@Test
public void whenCopyInputStreamToString_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
InputStream is = new FileInputStream(inputFileName);
String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8);
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
assertEquals(inputFileContent, content);
}
@Test
public void whenCopyByteArrayToOutputStream_thenCorrect() throws IOException {
String outputFileName = "src/test/resources/output.txt";
String string = "Should be copied to OutputStream.";
byte[] byteArray = string.getBytes();
OutputStream out = new FileOutputStream("src/test/resources/output.txt");
StreamUtils.copy(byteArray, out);
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(outputFileContent, string);
}
@Test
public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
InputStream in = new FileInputStream(inputFileName);
byte[] out = StreamUtils.copyToByteArray(in);
String content = new String(out);
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
assertEquals(inputFileContent, content);
}
}
@@ -0,0 +1 @@
Joe Employee,Jan Employee,James T. Employee
+1
View File
@@ -0,0 +1 @@
This file is merely for testing.
@@ -0,0 +1 @@
Should be copied to OutputStream.