minor formatting work
This commit is contained in:
parent
67fcde1009
commit
29c0fd8371
|
@ -7,15 +7,15 @@ import java.util.UUID;
|
|||
|
||||
public class TransactionFactory {
|
||||
|
||||
private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"};
|
||||
private static long nextId = 1;
|
||||
|
||||
public Transfer newInstance() {
|
||||
String transactionId = String.valueOf( nextId++ );
|
||||
String owner = NAMES[ (int) floor(random()*NAMES.length) ];
|
||||
long amount = (long) (random()*1500 + 500);
|
||||
Transfer tx = new Transfer(transactionId, owner, amount);
|
||||
return tx;
|
||||
}
|
||||
|
||||
private static final String[] NAMES = { "John", "Susan", "Marc", "Samantha" };
|
||||
private static long nextId = 1;
|
||||
|
||||
public Transfer newInstance() {
|
||||
String transactionId = String.valueOf(nextId++);
|
||||
String owner = NAMES[(int) floor(random() * NAMES.length)];
|
||||
long amount = (long) (random() * 1500 + 500);
|
||||
Transfer tx = new Transfer(transactionId, owner, amount);
|
||||
return tx;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,11 +18,11 @@ public class TransferDemo {
|
|||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Transfer tx = transactionFactory.newInstance();
|
||||
|
||||
//Runnable task = new Log4JRunnable(tx);
|
||||
//Runnable task = new Log4J2Runnable(tx);
|
||||
|
||||
// Runnable task = new Log4JRunnable(tx);
|
||||
// Runnable task = new Log4J2Runnable(tx);
|
||||
Runnable task = new Slf4jRunnable(tx);
|
||||
|
||||
|
||||
executor.submit(task);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import com.baeldung.ndc.Investment;
|
||||
import com.baeldung.ndc.service.InvestmentService;
|
||||
|
||||
|
||||
@RestController
|
||||
public class JBossLoggingController {
|
||||
@Autowired
|
||||
|
|
|
@ -17,7 +17,7 @@ public class Demo {
|
|||
TransactionFactory transactionFactory = new TransactionFactory();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Transfer tx = transactionFactory.newInstance();
|
||||
Runnable task = new Log4JRunnable(tx);
|
||||
Runnable task = new Log4JRunnable(tx);
|
||||
executor.submit(task);
|
||||
}
|
||||
executor.shutdown();
|
||||
|
|
|
@ -21,7 +21,7 @@ public class Demo {
|
|||
TransactionFactory transactionFactory = new TransactionFactory();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Transfer tx = transactionFactory.newInstance();
|
||||
Runnable task = new Log4J2Runnable(tx);
|
||||
Runnable task = new Log4J2Runnable(tx);
|
||||
executor.submit(task);
|
||||
}
|
||||
executor.shutdown();
|
||||
|
|
|
@ -21,7 +21,7 @@ public class Demo {
|
|||
TransactionFactory transactionFactory = new TransactionFactory();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Transfer tx = transactionFactory.newInstance();
|
||||
Runnable task = new Slf4jRunnable(tx);
|
||||
Runnable task = new Slf4jRunnable(tx);
|
||||
executor.submit(task);
|
||||
}
|
||||
executor.shutdown();
|
||||
|
|
|
@ -18,154 +18,113 @@ public class RxJavaBackpressureTest {
|
|||
|
||||
@Test
|
||||
public void givenColdObservable_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
|
||||
//when
|
||||
Observable
|
||||
.range(1, 1_000_000)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// when
|
||||
Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent();
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenBackpressureNotDefined_shouldTrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
PublishSubject<Integer> source = PublishSubject.<Integer> create();
|
||||
|
||||
source
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
source.observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
//when
|
||||
IntStream
|
||||
.range(0, 1_000_000)
|
||||
.forEach(source::onNext);
|
||||
// when
|
||||
IntStream.range(0, 1_000_000).forEach(source::onNext);
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent();
|
||||
testSubscriber.assertError(MissingBackpressureException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenWindowIsDefined_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Observable<Integer>> testSubscriber = new TestSubscriber<>();
|
||||
PublishSubject<Integer> source = PublishSubject.<Integer> create();
|
||||
|
||||
//when
|
||||
source
|
||||
.window(500)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// when
|
||||
source.window(500).observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
IntStream
|
||||
.range(0, 1_000)
|
||||
.forEach(source::onNext);
|
||||
IntStream.range(0, 1_000).forEach(source::onNext);
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenBufferIsDefined_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<List<Integer>> testSubscriber = new TestSubscriber<>();
|
||||
PublishSubject<Integer> source = PublishSubject.<Integer> create();
|
||||
|
||||
//when
|
||||
source
|
||||
.buffer(1024)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// when
|
||||
source.buffer(1024).observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
IntStream
|
||||
.range(0, 1_000)
|
||||
.forEach(source::onNext);
|
||||
IntStream.range(0, 1_000).forEach(source::onNext);
|
||||
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenSkippingOperationIsDefined_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
PublishSubject<Integer> source = PublishSubject.<Integer> create();
|
||||
|
||||
//when
|
||||
// when
|
||||
source.sample(100, TimeUnit.MILLISECONDS)
|
||||
// .throttleFirst(100, TimeUnit.MILLISECONDS)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// .throttleFirst(100, TimeUnit.MILLISECONDS)
|
||||
.observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
IntStream
|
||||
.range(0, 1_000)
|
||||
.forEach(source::onNext);
|
||||
IntStream.range(0, 1_000).forEach(source::onNext);
|
||||
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenOnBackpressureBufferDefined_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
|
||||
//when
|
||||
Observable
|
||||
.range(1, 1_000_000)
|
||||
.onBackpressureBuffer(16, () -> {}, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST)
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// when
|
||||
Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> {
|
||||
}, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHotObservable_whenOnBackpressureDropDefined_shouldNotThrowException() {
|
||||
//given
|
||||
// given
|
||||
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
|
||||
|
||||
//when
|
||||
Observable
|
||||
.range(1, 1_000_000)
|
||||
.onBackpressureDrop()
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe(testSubscriber);
|
||||
// when
|
||||
Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.computation()).subscribe(testSubscriber);
|
||||
|
||||
//then
|
||||
// then
|
||||
testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS);
|
||||
assertTrue(testSubscriber
|
||||
.getOnErrorEvents()
|
||||
.size() == 0);
|
||||
assertTrue(testSubscriber.getOnErrorEvents().size() == 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,22 +13,18 @@ import java.util.concurrent.TimeUnit;
|
|||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
||||
public class RxJavaTesting {
|
||||
@Test
|
||||
public void givenObservable_whenZip_shouldAssertBlockingInASameThread() {
|
||||
//given
|
||||
// given
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
List<String> results = new ArrayList<>();
|
||||
Observable<String> observable = Observable
|
||||
.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE),
|
||||
(string, index) -> index + "-" + string);
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), (string, index) -> index + "-" + string);
|
||||
|
||||
//when
|
||||
// when
|
||||
observable.subscribe(results::add);
|
||||
|
||||
//then
|
||||
// then
|
||||
assertThat(results, notNullValue());
|
||||
assertThat(results, hasSize(5));
|
||||
assertThat(results, hasItems("1-A", "2-B", "3-C", "4-D", "5-E"));
|
||||
|
@ -36,19 +32,16 @@ public class RxJavaTesting {
|
|||
|
||||
@Test
|
||||
public void givenObservable_whenZip_shouldAssertOnTestSubscriber() {
|
||||
//given
|
||||
// given
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
TestSubscriber<String> subscriber = new TestSubscriber<>();
|
||||
|
||||
Observable<String> observable = Observable
|
||||
.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE),
|
||||
((string, index) -> index + "-" + string));
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string));
|
||||
|
||||
//when
|
||||
// when
|
||||
observable.subscribe(subscriber);
|
||||
|
||||
//then
|
||||
// then
|
||||
subscriber.assertCompleted();
|
||||
subscriber.assertNoErrors();
|
||||
subscriber.assertValueCount(5);
|
||||
|
@ -57,52 +50,45 @@ public class RxJavaTesting {
|
|||
|
||||
@Test
|
||||
public void givenTestObserver_whenExceptionWasThrowsOnObservable_observerShouldGetError() {
|
||||
//given
|
||||
// given
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
TestSubscriber<String> subscriber = new TestSubscriber<>();
|
||||
|
||||
Observable<String> observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)).concatWith(Observable.error(new RuntimeException("error in Observable")));
|
||||
|
||||
Observable<String> observable = Observable
|
||||
.from(letters)
|
||||
.zipWith(Observable.range(1, Integer.MAX_VALUE),
|
||||
((string, index) -> index + "-" + string))
|
||||
.concatWith(Observable.error(new RuntimeException("error in Observable")));
|
||||
|
||||
//when
|
||||
// when
|
||||
observable.subscribe(subscriber);
|
||||
|
||||
//then
|
||||
// then
|
||||
subscriber.assertError(RuntimeException.class);
|
||||
subscriber.assertNotCompleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObservableThatEmitsEventPerSecond_whenUseAdvanceByTime_shouldEmitEventPerSecond() {
|
||||
//given
|
||||
// given
|
||||
List<String> letters = Arrays.asList("A", "B", "C", "D", "E");
|
||||
TestScheduler scheduler = new TestScheduler();
|
||||
TestSubscriber<String> subscriber = new TestSubscriber<>();
|
||||
Observable<Long> tick = Observable.interval(1, TimeUnit.SECONDS, scheduler);
|
||||
|
||||
Observable<String> observable = Observable.from(letters)
|
||||
.zipWith(tick, (string, index) -> index + "-" + string);
|
||||
Observable<String> observable = Observable.from(letters).zipWith(tick, (string, index) -> index + "-" + string);
|
||||
|
||||
observable.subscribeOn(scheduler)
|
||||
.subscribe(subscriber);
|
||||
observable.subscribeOn(scheduler).subscribe(subscriber);
|
||||
|
||||
//expect
|
||||
// expect
|
||||
subscriber.assertNoValues();
|
||||
subscriber.assertNotCompleted();
|
||||
|
||||
//when
|
||||
// when
|
||||
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
|
||||
|
||||
//then
|
||||
// then
|
||||
subscriber.assertNoErrors();
|
||||
subscriber.assertValueCount(1);
|
||||
subscriber.assertValues("0-A");
|
||||
|
||||
//when
|
||||
// when
|
||||
scheduler.advanceTimeTo(6, TimeUnit.SECONDS);
|
||||
subscriber.assertCompleted();
|
||||
subscriber.assertNoErrors();
|
||||
|
@ -110,4 +96,3 @@ public class RxJavaTesting {
|
|||
assertThat(subscriber.getOnNextEvents(), hasItems("0-A", "1-B", "2-C", "3-D", "4-E"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.springframework.context.annotation.FilterType;
|
|||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*")})
|
||||
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*") })
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
|
|
|
@ -14,9 +14,9 @@ public class MultipleLoginApplication extends SpringBootServletInitializer {
|
|||
public static void main(String[] args) {
|
||||
SpringApplication.run(MultipleLoginApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(MultipleLoginApplication.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(MultipleLoginApplication.class);
|
||||
}
|
||||
}
|
|
@ -21,14 +21,8 @@ public class MultipleLoginSecurityConfig {
|
|||
@Bean
|
||||
public UserDetailsService userDetailsService() throws Exception {
|
||||
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
|
||||
manager.createUser(User.withUsername("user")
|
||||
.password("userPass")
|
||||
.roles("USER")
|
||||
.build());
|
||||
manager.createUser(User.withUsername("admin")
|
||||
.password("adminPass")
|
||||
.roles("ADMIN")
|
||||
.build());
|
||||
manager.createUser(User.withUsername("user").password("userPass").roles("USER").build());
|
||||
manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build());
|
||||
return manager;
|
||||
}
|
||||
|
||||
|
@ -42,37 +36,16 @@ public class MultipleLoginSecurityConfig {
|
|||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("admin")
|
||||
.password("admin")
|
||||
.roles("ADMIN");
|
||||
auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/admin*")
|
||||
.authorizeRequests()
|
||||
.anyRequest()
|
||||
.hasRole("ADMIN")
|
||||
// log in
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/loginAdmin")
|
||||
.loginProcessingUrl("/admin_login")
|
||||
.failureUrl("/loginAdmin?error=loginError")
|
||||
.defaultSuccessUrl("/adminPage")
|
||||
// logout
|
||||
.and()
|
||||
.logout()
|
||||
.logoutUrl("/admin_logout")
|
||||
.logoutSuccessUrl("/protectedLinks")
|
||||
.deleteCookies("JSESSIONID")
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.accessDeniedPage("/403")
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
http.antMatcher("/admin*").authorizeRequests().anyRequest().hasRole("ADMIN")
|
||||
// log in
|
||||
.and().formLogin().loginPage("/loginAdmin").loginProcessingUrl("/admin_login").failureUrl("/loginAdmin?error=loginError").defaultSuccessUrl("/adminPage")
|
||||
// logout
|
||||
.and().logout().logoutUrl("/admin_logout").logoutSuccessUrl("/protectedLinks").deleteCookies("JSESSIONID").and().exceptionHandling().accessDeniedPage("/403").and().csrf().disable();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,36 +59,15 @@ public class MultipleLoginSecurityConfig {
|
|||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("user")
|
||||
.password("user")
|
||||
.roles("USER");
|
||||
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
|
||||
}
|
||||
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/user*")
|
||||
.authorizeRequests()
|
||||
.anyRequest()
|
||||
.hasRole("USER")
|
||||
// log in
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/loginUser")
|
||||
.loginProcessingUrl("/user_login")
|
||||
.failureUrl("/loginUser?error=loginError")
|
||||
.defaultSuccessUrl("/userPage")
|
||||
// logout
|
||||
.and()
|
||||
.logout()
|
||||
.logoutUrl("/user_logout")
|
||||
.logoutSuccessUrl("/protectedLinks")
|
||||
.deleteCookies("JSESSIONID")
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.accessDeniedPage("/403")
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
http.antMatcher("/user*").authorizeRequests().anyRequest().hasRole("USER")
|
||||
// log in
|
||||
.and().formLogin().loginPage("/loginUser").loginProcessingUrl("/user_login").failureUrl("/loginUser?error=loginError").defaultSuccessUrl("/userPage")
|
||||
// logout
|
||||
.and().logout().logoutUrl("/user_logout").logoutSuccessUrl("/protectedLinks").deleteCookies("JSESSIONID").and().exceptionHandling().accessDeniedPage("/403").and().csrf().disable();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ import org.springframework.context.annotation.ImportResource;
|
|||
/**
|
||||
* Created by ambrusadrianz on 09/10/2016.
|
||||
*/
|
||||
//@Configuration
|
||||
//@ImportResource({ "classpath:spring-security-custom-voter.xml" })
|
||||
// @Configuration
|
||||
// @ImportResource({ "classpath:spring-security-custom-voter.xml" })
|
||||
public class XmlSecurityConfig {
|
||||
public XmlSecurityConfig() {
|
||||
super();
|
||||
|
|
Loading…
Reference in New Issue