From 29c0fd837133da0c32dcdb3aba0ece6afefc7161 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 11 Feb 2017 14:00:52 +0200 Subject: [PATCH] minor formatting work --- .../com/baeldung/mdc/TransactionFactory.java | 22 ++-- .../java/com/baeldung/mdc/TransferDemo.java | 8 +- .../controller/JBossLoggingController.java | 1 - .../java/com/baeldung/mdc/log4j/Demo.java | 2 +- .../java/com/baeldung/mdc/log4j2/Demo.java | 2 +- .../java/com/baeldung/mdc/slf4j/Demo.java | 2 +- .../rxjava/RxJavaBackpressureTest.java | 121 ++++++------------ .../com/baeldung/rxjava/RxJavaTesting.java | 53 +++----- .../main/java/org/baeldung/Application.java | 2 +- .../MultipleLoginApplication.java | 10 +- .../MultipleLoginSecurityConfig.java | 76 ++--------- .../org/baeldung/voter/XmlSecurityConfig.java | 4 +- 12 files changed, 99 insertions(+), 204 deletions(-) diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java index ec1887eea6..0904e4603f 100644 --- a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java +++ b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -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; + } + } diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java index daf256007c..259e9a8c5c 100644 --- a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java +++ b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java @@ -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); } diff --git a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java index b024f3ec81..e581c45cd3 100644 --- a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java +++ b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java @@ -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 diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java index f9a210606f..665168452a 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java @@ -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(); diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java index 3f7c1d37d5..78c48c2a83 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java @@ -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(); diff --git a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java index 98db698f47..de890f9f5d 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java @@ -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(); diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java index 33f94a9c6f..8a495650b3 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java @@ -18,154 +18,113 @@ public class RxJavaBackpressureTest { @Test public void givenColdObservable_shouldNotThrowException() { - //given + // given TestSubscriber 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 testSubscriber = new TestSubscriber<>(); PublishSubject source = PublishSubject. 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> testSubscriber = new TestSubscriber<>(); PublishSubject source = PublishSubject. 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> testSubscriber = new TestSubscriber<>(); PublishSubject source = PublishSubject. 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 testSubscriber = new TestSubscriber<>(); PublishSubject source = PublishSubject. 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 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 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); } } diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java index 67f6e1c2b8..91c8ed540e 100644 --- a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java +++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java @@ -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 letters = Arrays.asList("A", "B", "C", "D", "E"); List results = new ArrayList<>(); - Observable observable = Observable - .from(letters) - .zipWith(Observable.range(1, Integer.MAX_VALUE), - (string, index) -> index + "-" + string); + Observable 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 letters = Arrays.asList("A", "B", "C", "D", "E"); TestSubscriber subscriber = new TestSubscriber<>(); - Observable observable = Observable - .from(letters) - .zipWith(Observable.range(1, Integer.MAX_VALUE), - ((string, index) -> index + "-" + string)); + Observable 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 letters = Arrays.asList("A", "B", "C", "D", "E"); TestSubscriber subscriber = new TestSubscriber<>(); + Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)).concatWith(Observable.error(new RuntimeException("error in Observable"))); - Observable 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 letters = Arrays.asList("A", "B", "C", "D", "E"); TestScheduler scheduler = new TestScheduler(); TestSubscriber subscriber = new TestSubscriber<>(); Observable tick = Observable.interval(1, TimeUnit.SECONDS, scheduler); - Observable observable = Observable.from(letters) - .zipWith(tick, (string, index) -> index + "-" + string); + Observable 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")); } } - diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java index 072a6ca4fe..03de5897f5 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java @@ -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); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java index 23fda0547f..836336eb71 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java @@ -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); + } } \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java index d7dbf17723..8327e7e5d3 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java @@ -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(); } } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java index bee1381778..8041585f42 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java @@ -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();