Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
		
						commit
						22a59aa8fb
					
				| @ -14,3 +14,4 @@ | ||||
| - [Bubble Sort in Java](http://www.baeldung.com/java-bubble-sort) | ||||
| - [Introduction to JGraphT](http://www.baeldung.com/jgrapht) | ||||
| - [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm) | ||||
| - [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance) | ||||
|  | ||||
| @ -0,0 +1,59 @@ | ||||
| package com.baeldung.algorithms.prime; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.LinkedList; | ||||
| import java.util.List; | ||||
| import java.util.stream.Collectors; | ||||
| import java.util.stream.IntStream; | ||||
| 
 | ||||
| public class PrimeGenerator { | ||||
|     public static List<Integer> sieveOfEratosthenes(int n) { | ||||
|         final boolean prime[] = new boolean[n + 1]; | ||||
|         Arrays.fill(prime, true); | ||||
| 
 | ||||
|         for (int p = 2; p * p <= n; p++) { | ||||
|             if (prime[p]) { | ||||
|                 for (int i = p * 2; i <= n; i += p) | ||||
|                     prime[i] = false; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         final List<Integer> primes = new LinkedList<>(); | ||||
|         for (int i = 2; i <= n; i++) { | ||||
|             if (prime[i]) | ||||
|                 primes.add(i); | ||||
|         } | ||||
|         return primes; | ||||
|     } | ||||
| 
 | ||||
|     public static List<Integer> primeNumbersBruteForce(int max) { | ||||
|         final List<Integer> primeNumbers = new LinkedList<Integer>(); | ||||
|         for (int i = 2; i <= max; i++) { | ||||
|             if (isPrimeBruteForce(i)) { | ||||
|                 primeNumbers.add(i); | ||||
|             } | ||||
|         } | ||||
|         return primeNumbers; | ||||
|     } | ||||
| 
 | ||||
|     private static boolean isPrimeBruteForce(int x) { | ||||
|         for (int i = 2; i < x; i++) { | ||||
|             if (x % i == 0) { | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     public static List<Integer> primeNumbersTill(int max) { | ||||
|         return IntStream.rangeClosed(2, max) | ||||
|             .filter(x -> isPrime(x)) | ||||
|             .boxed() | ||||
|             .collect(Collectors.toList()); | ||||
|     } | ||||
| 
 | ||||
|     private static boolean isPrime(int x) { | ||||
|         return IntStream.rangeClosed(2, (int) (Math.sqrt(x))) | ||||
|             .allMatch(n -> x % n != 0); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.algorithms.prime; | ||||
| 
 | ||||
| import static com.baeldung.algorithms.prime.PrimeGenerator.*; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import org.junit.Test; | ||||
| import static org.junit.Assert.*; | ||||
| 
 | ||||
| public class PrimeGeneratorTest { | ||||
|     @Test | ||||
|     public void whenBruteForced_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = primeNumbersBruteForce(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenOptimized_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = primeNumbersTill(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSieveOfEratosthenes_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = sieveOfEratosthenes(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3
									
								
								apache-spark/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								apache-spark/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| ### Relevant articles | ||||
| 
 | ||||
| - [Introduction to Apache Spark](http://www.baeldung.com/apache-spark) | ||||
| @ -65,6 +65,24 @@ | ||||
| 				<groupId>org.springframework.boot</groupId> | ||||
| 				<artifactId>spring-boot-maven-plugin</artifactId> | ||||
| 			</plugin> | ||||
| 			<plugin> | ||||
| 				<groupId>org.apache.maven.plugins</groupId> | ||||
| 				<artifactId>maven-surefire-plugin</artifactId> | ||||
| 				<version>${maven-surefire-plugin.version}</version> | ||||
| 				<configuration> | ||||
| 					<forkCount>3</forkCount> | ||||
| 					<reuseForks>true</reuseForks> | ||||
| 					<excludes> | ||||
| 						<exclude>**/*IntegrationTest.java</exclude> | ||||
| 						<exclude>**/*LongRunningUnitTest.java</exclude> | ||||
| 						<exclude>**/*ManualTest.java</exclude> | ||||
| 						<exclude>**/JdbcTest.java</exclude> | ||||
| 						<exclude>**/*LiveTest.java</exclude> | ||||
| 					</excludes> | ||||
| 
 | ||||
| 				</configuration> | ||||
| 			</plugin> | ||||
| 
 | ||||
| 		</plugins> | ||||
| 	</build> | ||||
| 
 | ||||
| @ -107,4 +125,5 @@ | ||||
| 	</pluginRepositories> | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| </project> | ||||
|  | ||||
| @ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; | ||||
| 
 | ||||
| @RunWith(SpringRunner.class) | ||||
| @SpringBootTest | ||||
| public class CasSecuredAppApplicationTests { | ||||
| public class CasSecuredAppApplicationIntegrationTest { | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void contextLoads() { | ||||
| @ -0,0 +1,35 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.time.Instant; | ||||
| import java.time.LocalDate; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows different ways of converting java.util.Date into java.time.LocalDate. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateConverter { | ||||
| 
 | ||||
|     public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) { | ||||
|         return dateToConvert.toInstant() | ||||
|             .atZone(ZoneId.systemDefault()) | ||||
|             .toLocalDate(); | ||||
|     } | ||||
| 
 | ||||
|     public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) { | ||||
|         return new java.sql.Date(dateToConvert.getTime()).toLocalDate(); | ||||
|     } | ||||
| 
 | ||||
|     public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) { | ||||
|         return Instant.ofEpochMilli(dateToConvert.getTime()) | ||||
|             .atZone(ZoneId.systemDefault()) | ||||
|             .toLocalDate(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,35 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.time.Instant; | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows different ways of converting java.util.Date into java.time.LocalDateTime.  | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateTimeConverter { | ||||
| 
 | ||||
|     public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) { | ||||
|         return dateToConvert.toInstant() | ||||
|             .atZone(ZoneId.systemDefault()) | ||||
|             .toLocalDateTime(); | ||||
|     } | ||||
| 
 | ||||
|     public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) { | ||||
|         return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime(); | ||||
|     } | ||||
| 
 | ||||
|     public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) { | ||||
|         return Instant.ofEpochMilli(dateToConvert.getTime()) | ||||
|             .atZone(ZoneId.systemDefault()) | ||||
|             .toLocalDateTime(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,27 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows different ways of converting java.time.LocalDateTime into java.util.Date.  | ||||
|  *   | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class LocalDateTimeToDateConverter { | ||||
| 
 | ||||
|     public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) { | ||||
|         return java.sql.Timestamp.valueOf(dateToConvert); | ||||
|     } | ||||
| 
 | ||||
|     public static Date convertToDateViaInstant(LocalDateTime dateToConvert) { | ||||
|         return java.util.Date.from(dateToConvert.atZone(ZoneId.systemDefault()) | ||||
|             .toInstant()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.time.LocalDate; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows different ways of converting java.time.LocalDate into java.util.Date.  | ||||
|  *   | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class LocalDateToDateConverter { | ||||
| 
 | ||||
|     public static Date convertToDateViaSqlDate(LocalDate dateToConvert) { | ||||
|         return java.sql.Date.valueOf(dateToConvert); | ||||
|     } | ||||
| 
 | ||||
|     public static Date convertToDateViaInstant(LocalDate dateToConvert) { | ||||
|         return java.util.Date.from(dateToConvert.atStartOfDay() | ||||
|             .atZone(ZoneId.systemDefault()) | ||||
|             .toInstant()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,59 @@ | ||||
| package com.baeldung.prime; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.LinkedList; | ||||
| import java.util.List; | ||||
| import java.util.stream.Collectors; | ||||
| import java.util.stream.IntStream; | ||||
| 
 | ||||
| public class PrimeGenerator { | ||||
|     public static List<Integer> sieveOfEratosthenes(int n) { | ||||
|         final boolean prime[] = new boolean[n + 1]; | ||||
|         Arrays.fill(prime, true); | ||||
| 
 | ||||
|         for (int p = 2; p * p <= n; p++) { | ||||
|             if (prime[p]) { | ||||
|                 for (int i = p * 2; i <= n; i += p) | ||||
|                     prime[i] = false; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         final List<Integer> primes = new LinkedList<>(); | ||||
|         for (int i = 2; i <= n; i++) { | ||||
|             if (prime[i]) | ||||
|                 primes.add(i); | ||||
|         } | ||||
|         return primes; | ||||
|     } | ||||
| 
 | ||||
|     public static List<Integer> primeNumbersBruteForce(int max) { | ||||
|         final List<Integer> primeNumbers = new LinkedList<Integer>(); | ||||
|         for (int i = 2; i <= max; i++) { | ||||
|             if (isPrimeBruteForce(i)) { | ||||
|                 primeNumbers.add(i); | ||||
|             } | ||||
|         } | ||||
|         return primeNumbers; | ||||
|     } | ||||
| 
 | ||||
|     private static boolean isPrimeBruteForce(int x) { | ||||
|         for (int i = 2; i < x; i++) { | ||||
|             if (x % i == 0) { | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     public static List<Integer> primeNumbersTill(int max) { | ||||
|         return IntStream.rangeClosed(2, max) | ||||
|             .filter(x -> isPrime(x)) | ||||
|             .boxed() | ||||
|             .collect(Collectors.toList()); | ||||
|     } | ||||
| 
 | ||||
|     private static boolean isPrime(int x) { | ||||
|         return IntStream.rangeClosed(2, (int) (Math.sqrt(x))) | ||||
|             .allMatch(n -> x % n != 0); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,72 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDate; | ||||
| import java.time.temporal.ChronoField; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * JUnits for {@link DateToLocalDateConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaInstant() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDate.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDate.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaSqlDate() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDate.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,78 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.temporal.ChronoField; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * JUnits for {@link DateToLocalDateTimeConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateTimeConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10, 8, 20); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDateTime.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); | ||||
|         assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); | ||||
|         assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10, 8, 20); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDateTime.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); | ||||
|         assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); | ||||
|         assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10, 8, 20); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDateTime.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); | ||||
|         assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); | ||||
|         assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,61 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * JUnits for {@link LocalDateTimeToDateConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class LocalDateTimeToDateConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() { | ||||
|         // given | ||||
|         LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20); | ||||
| 
 | ||||
|         // when | ||||
|         Date date = LocalDateTimeToDateConverter.convertToDateViaInstant(dateToConvert); | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, calendar.get(Calendar.YEAR)); | ||||
|         assertEquals(10, calendar.get(Calendar.MONTH)); | ||||
|         assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH)); | ||||
|         assertEquals(8, calendar.get(Calendar.HOUR)); | ||||
|         assertEquals(20, calendar.get(Calendar.MINUTE)); | ||||
|         assertEquals(0, calendar.get(Calendar.SECOND)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaSqlTimestamp() { | ||||
|         // given | ||||
|         LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20); | ||||
| 
 | ||||
|         // when | ||||
|         Date date = LocalDateTimeToDateConverter.convertToDateViaSqlTimestamp(dateToConvert); | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, calendar.get(Calendar.YEAR)); | ||||
|         assertEquals(10, calendar.get(Calendar.MONTH)); | ||||
|         assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH)); | ||||
|         assertEquals(8, calendar.get(Calendar.HOUR)); | ||||
|         assertEquals(20, calendar.get(Calendar.MINUTE)); | ||||
|         assertEquals(0, calendar.get(Calendar.SECOND)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,55 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDate; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| /** | ||||
|  *  | ||||
|  * JUnits for {@link LocalDateToDateConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class LocalDateToDateConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaInstant() { | ||||
|         // given | ||||
|         LocalDate dateToConvert = LocalDate.of(2010, 11, 10); | ||||
| 
 | ||||
|         // when | ||||
|         Date date = LocalDateToDateConverter.convertToDateViaInstant(dateToConvert); | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, calendar.get(Calendar.YEAR)); | ||||
|         assertEquals(10, calendar.get(Calendar.MONTH)); | ||||
|         assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertViaSqlDate() { | ||||
|         // given | ||||
|         LocalDate dateToConvert = LocalDate.of(2010, 11, 10); | ||||
| 
 | ||||
|         // when | ||||
|         Date date = LocalDateToDateConverter.convertToDateViaSqlDate(dateToConvert); | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, calendar.get(Calendar.YEAR)); | ||||
|         assertEquals(10, calendar.get(Calendar.MONTH)); | ||||
|         assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.prime; | ||||
| 
 | ||||
| import static com.baeldung.prime.PrimeGenerator.*; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import org.junit.Test; | ||||
| import static org.junit.Assert.*; | ||||
| 
 | ||||
| public class PrimeGeneratorTest { | ||||
|     @Test | ||||
|     public void whenBruteForced_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = primeNumbersBruteForce(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenOptimized_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = primeNumbersTill(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSieveOfEratosthenes_returnsSuccessfully() { | ||||
|         final List<Integer> primeNumbers = sieveOfEratosthenes(20); | ||||
|         assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.java9.datetime; | ||||
| 
 | ||||
| import java.time.LocalDate; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows a way to convert java.util.Date into java.time.LocalDate with new Java 1.9. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateConverter { | ||||
| 
 | ||||
|     public static LocalDate convertToLocalDate(Date dateToConvert) { | ||||
|         return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,22 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.java9.datetime; | ||||
| 
 | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.ZoneId; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * Class which shows a way to convert java.util.Date into java.time.LocalDateTime with new Java 1.9. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateTimeConverter { | ||||
| 
 | ||||
|     public static LocalDateTime convertToLocalDateTime(Date dateToConvert) { | ||||
|         return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,41 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.java9.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDate; | ||||
| import java.time.temporal.ChronoField; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.java9.datetime.DateToLocalDateConverter; | ||||
| 
 | ||||
| /** | ||||
|  * JUnits for {@link DateToLocalDateConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010WhenConvertToLocalDate() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDate localDateTime = DateToLocalDateConverter.convertToLocalDate(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDateTime.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,43 @@ | ||||
| /** | ||||
|  *  | ||||
|  */ | ||||
| package com.baeldung.java9.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.temporal.ChronoField; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.java9.datetime.DateToLocalDateTimeConverter; | ||||
| 
 | ||||
| /** | ||||
|  * JUnits for {@link DateToLocalDateTimeConverter} class. | ||||
|  *  | ||||
|  * @author abialas | ||||
|  * | ||||
|  */ | ||||
| public class DateToLocalDateTimeConverterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() { | ||||
|         // given | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.set(2010, 10, 10, 8, 20); | ||||
|         Date dateToConvert = calendar.getTime(); | ||||
| 
 | ||||
|         // when | ||||
|         LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTime(dateToConvert); | ||||
| 
 | ||||
|         // then | ||||
|         assertEquals(2010, localDateTime.get(ChronoField.YEAR)); | ||||
|         assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR)); | ||||
|         assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH)); | ||||
|         assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY)); | ||||
|         assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -30,3 +30,4 @@ | ||||
| - [Guide to Volatile Keyword in Java](http://www.baeldung.com/java-volatile) | ||||
| - [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent) | ||||
| - [Semaphores in Java](http://www.baeldung.com/java-semaphore) | ||||
| - [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread) | ||||
|  | ||||
| @ -0,0 +1,52 @@ | ||||
| package com.baeldung.concurrent.stopping; | ||||
| 
 | ||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||
| 
 | ||||
| public class ControlSubThread implements Runnable { | ||||
| 
 | ||||
|     private Thread worker; | ||||
|     private int interval = 100; | ||||
|     private AtomicBoolean running = new AtomicBoolean(false); | ||||
|     private AtomicBoolean stopped = new AtomicBoolean(true); | ||||
| 
 | ||||
| 
 | ||||
|     public ControlSubThread(int sleepInterval) { | ||||
|         interval = sleepInterval; | ||||
|     } | ||||
| 
 | ||||
|     public void start() { | ||||
|         worker = new Thread(this); | ||||
|         worker.start(); | ||||
|     } | ||||
| 
 | ||||
|     public void stop() { | ||||
|         running.set(false); | ||||
|     } | ||||
| 
 | ||||
|     public void interrupt() { | ||||
|         running.set(false); | ||||
|         worker.interrupt(); | ||||
|     } | ||||
| 
 | ||||
|     boolean isRunning() { | ||||
|         return running.get(); | ||||
|     } | ||||
| 
 | ||||
|     boolean isStopped() { | ||||
|         return stopped.get(); | ||||
|     } | ||||
| 
 | ||||
|     public void run() { | ||||
|         running.set(true); | ||||
|         stopped.set(false); | ||||
|         while (running.get()) { | ||||
|             try { | ||||
|                 Thread.sleep(interval); | ||||
|             } catch (InterruptedException e) { | ||||
|                 // no-op, just loop again | ||||
|             } | ||||
|             // do something | ||||
|         } | ||||
|         stopped.set(true); | ||||
|     } | ||||
| } | ||||
| @ -1,24 +1,21 @@ | ||||
| package com.baeldung.concurrent.runnable; | ||||
| 
 | ||||
| import java.util.concurrent.Callable; | ||||
| import java.util.concurrent.ExecutionException; | ||||
| import java.util.concurrent.ExecutorService; | ||||
| import java.util.concurrent.Executors; | ||||
| import java.util.concurrent.Future; | ||||
| 
 | ||||
| import org.apache.commons.lang3.RandomUtils; | ||||
| import org.junit.After; | ||||
| import org.junit.AfterClass; | ||||
| import org.junit.Before; | ||||
| import org.junit.BeforeClass; | ||||
| import org.junit.Test; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| 
 | ||||
| public class RunnableVsThreadTest { | ||||
| public class RunnableVsThreadLiveTest { | ||||
| 
 | ||||
| 	private static Logger log =  | ||||
| 	  LoggerFactory.getLogger(RunnableVsThreadTest.class); | ||||
| 	  LoggerFactory.getLogger(RunnableVsThreadLiveTest.class); | ||||
| 
 | ||||
| 	private static ExecutorService executorService; | ||||
| 	 | ||||
| @ -77,9 +74,7 @@ public class RunnableVsThreadTest { | ||||
| 	public void givenACallableAsLambda_whenSubmitToES_thenResult()  | ||||
| 	  throws Exception { | ||||
| 		 | ||||
| 		Future<Integer> future = executorService.submit(() -> { | ||||
| 		  return RandomUtils.nextInt(0, 100); | ||||
| 		}); | ||||
| 		Future<Integer> future = executorService.submit(() -> RandomUtils.nextInt(0, 100)); | ||||
| 		 | ||||
| 		log.info("Result from callable: {}", future.get()); | ||||
| 	} | ||||
| @ -99,7 +94,7 @@ class SimpleThread extends Thread{ | ||||
| 	 | ||||
| 	private String message; | ||||
| 	 | ||||
| 	public SimpleThread(String message) { | ||||
| 	SimpleThread(String message) { | ||||
| 		this.message = message; | ||||
| 	} | ||||
| 	 | ||||
| @ -116,7 +111,7 @@ class SimpleRunnable implements Runnable { | ||||
| 	 | ||||
| 	private String message; | ||||
| 	 | ||||
| 	public SimpleRunnable(String message) { | ||||
| 	SimpleRunnable(String message) { | ||||
| 		this.message = message; | ||||
| 	} | ||||
| 	 | ||||
| @ -0,0 +1,50 @@ | ||||
| package com.baeldung.concurrent.stopping; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import static org.junit.Assert.assertFalse; | ||||
| import static org.junit.Assert.assertTrue; | ||||
| 
 | ||||
| public class StopThreadTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenStoppedThreadIsStopped() throws InterruptedException { | ||||
| 
 | ||||
|         int interval = 100; | ||||
| 
 | ||||
|         ControlSubThread controlSubThread = new ControlSubThread(interval); | ||||
|         controlSubThread.start(); | ||||
| 
 | ||||
|         // Give things a chance to get set up | ||||
|         Thread.sleep(interval); | ||||
|         assertTrue(controlSubThread.isRunning()); | ||||
|         assertFalse(controlSubThread.isStopped()); | ||||
| 
 | ||||
|         // Stop it and make sure the flags have been reversed | ||||
|         controlSubThread.stop(); | ||||
|         Thread.sleep(interval); | ||||
|         assertTrue(controlSubThread.isStopped()); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenInterruptedThreadIsStopped() throws InterruptedException { | ||||
| 
 | ||||
|         int interval = 5000; | ||||
| 
 | ||||
|         ControlSubThread controlSubThread = new ControlSubThread(interval); | ||||
|         controlSubThread.start(); | ||||
| 
 | ||||
|         // Give things a chance to get set up | ||||
|         Thread.sleep(100); | ||||
|         assertTrue(controlSubThread.isRunning()); | ||||
|         assertFalse(controlSubThread.isStopped()); | ||||
| 
 | ||||
|         // Stop it and make sure the flags have been reversed | ||||
|         controlSubThread.interrupt(); | ||||
| 
 | ||||
|         // Wait less than the time we would normally sleep, and make sure we exited. | ||||
|         Thread.sleep(interval/10); | ||||
|         assertTrue(controlSubThread.isStopped()); | ||||
|     } | ||||
| } | ||||
| @ -114,4 +114,6 @@ | ||||
| - [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer) | ||||
| - [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int) | ||||
| - [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns) | ||||
| 
 | ||||
| - [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) | ||||
| - [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static) | ||||
| - [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array) | ||||
|  | ||||
| @ -216,6 +216,13 @@ | ||||
| 		    <artifactId>spring-web</artifactId> | ||||
| 		    <version>4.3.4.RELEASE</version> | ||||
| 		</dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.sun</groupId> | ||||
|             <artifactId>tools</artifactId> | ||||
|             <version>1.8.0</version> | ||||
|             <scope>system</scope> | ||||
|             <systemPath>${java.home}/../lib/tools.jar</systemPath> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <build> | ||||
|  | ||||
| @ -0,0 +1,138 @@ | ||||
| package com.baeldung.breakcontinue; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.HashMap; | ||||
| import java.util.Iterator; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Map.Entry; | ||||
| 
 | ||||
| /** | ||||
|  * @author Santosh | ||||
|  * | ||||
|  */ | ||||
| public class BreakContinue { | ||||
| 
 | ||||
|     public static int unlabeledBreak() { | ||||
|         String searchName = "Wilson"; | ||||
|         int counter = 0; | ||||
|         List<String> names = Arrays.asList("John", "Peter", "Robert", "Wilson", "Anthony", "Donald", "Richard"); | ||||
| 
 | ||||
|         for (String name : names) { | ||||
|             counter++; | ||||
|             if (name.equalsIgnoreCase(searchName)) { | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return counter; | ||||
|     } | ||||
| 
 | ||||
|     public static int unlabeledBreakNestedLoops() { | ||||
|         String searchName = "Wilson"; | ||||
|         int counter = 0; | ||||
|         Map<String, List<String>> nameMap = new HashMap<>(); | ||||
|         nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson")); | ||||
|         nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold")); | ||||
|         nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan")); | ||||
| 
 | ||||
|         Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet() | ||||
|             .iterator(); | ||||
|         Entry<String, List<String>> entry = null; | ||||
|         List<String> names = null; | ||||
|         while (iterator.hasNext()) { | ||||
|             entry = iterator.next(); | ||||
|             names = entry.getValue(); | ||||
|             for (String name : names) { | ||||
|                 if (name.equalsIgnoreCase(searchName)) { | ||||
|                     counter++; | ||||
|                     break; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return counter; | ||||
|     } | ||||
| 
 | ||||
|     public static int labeledBreak() { | ||||
|         String searchName = "Wilson"; | ||||
|         int counter = 0; | ||||
|         Map<String, List<String>> nameMap = new HashMap<>(); | ||||
|         nameMap.put("Grade1", Arrays.asList("John", "Peter", "Robert", "Wilson")); | ||||
|         nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Richard", "Arnold")); | ||||
|         nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Stephen", "Ryan")); | ||||
| 
 | ||||
|         Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet() | ||||
|             .iterator(); | ||||
|         Entry<String, List<String>> entry = null; | ||||
|         List<String> names = null; | ||||
|         compare:  | ||||
|         while (iterator.hasNext()) { | ||||
|             entry = iterator.next(); | ||||
|             names = entry.getValue(); | ||||
|             for (String name : names) { | ||||
|                 if (name.equalsIgnoreCase(searchName)) { | ||||
|                     counter++; | ||||
|                     break compare; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return counter; | ||||
|     } | ||||
| 
 | ||||
|     public static int unlabeledContinue() { | ||||
|         String searchName = "Wilson"; | ||||
|         int counter = 0; | ||||
|         Map<String, List<String>> nameMap = new HashMap<>(); | ||||
|         nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson")); | ||||
|         nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold")); | ||||
|         nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan")); | ||||
| 
 | ||||
|         Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet() | ||||
|             .iterator(); | ||||
|         Entry<String, List<String>> entry = null; | ||||
|         List<String> names = null; | ||||
|         while (iterator.hasNext()) { | ||||
|             entry = iterator.next(); | ||||
|             names = entry.getValue(); | ||||
|             for (String name : names) { | ||||
|                 if (!name.equalsIgnoreCase(searchName)) { | ||||
|                     continue; | ||||
|                 } | ||||
| 
 | ||||
|                 counter++; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return counter; | ||||
|     } | ||||
| 
 | ||||
|     public static int labeledContinue() { | ||||
|         String searchName = "Wilson"; | ||||
|         int counter = 0; | ||||
|         Map<String, List<String>> nameMap = new HashMap<>(); | ||||
|         nameMap.put("Grade1", Arrays.asList("John", "Wilson", "Robert", "Wilson")); | ||||
|         nameMap.put("Grade2", Arrays.asList("Anthony", "Donald", "Wilson", "Arnold")); | ||||
|         nameMap.put("Grade3", Arrays.asList("Wilson", "Michael", "Wilson", "Ryan")); | ||||
| 
 | ||||
|         Iterator<Entry<String, List<String>>> iterator = nameMap.entrySet() | ||||
|             .iterator(); | ||||
|         Entry<String, List<String>> entry = null; | ||||
|         List<String> names = null; | ||||
|         compare:  | ||||
|         while (iterator.hasNext()) { | ||||
|             entry = iterator.next(); | ||||
|             names = entry.getValue(); | ||||
|             for (String name : names) { | ||||
|                 if (name.equalsIgnoreCase(searchName)) { | ||||
|                     counter++; | ||||
|                     continue compare; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return counter; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										9
									
								
								core-java/src/main/java/com/baeldung/javac/Positive.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								core-java/src/main/java/com/baeldung/javac/Positive.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import java.lang.annotation.*; | ||||
| 
 | ||||
| @Documented | ||||
| @Retention(RetentionPolicy.CLASS) | ||||
| @Target({ElementType.PARAMETER}) | ||||
| public @interface Positive { | ||||
| } | ||||
| @ -0,0 +1,123 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import com.sun.source.tree.MethodTree; | ||||
| import com.sun.source.tree.VariableTree; | ||||
| import com.sun.source.util.*; | ||||
| import com.sun.tools.javac.api.BasicJavacTask; | ||||
| import com.sun.tools.javac.code.TypeTag; | ||||
| import com.sun.tools.javac.tree.JCTree; | ||||
| import com.sun.tools.javac.tree.TreeMaker; | ||||
| import com.sun.tools.javac.util.Context; | ||||
| import com.sun.tools.javac.util.Name; | ||||
| import com.sun.tools.javac.util.Names; | ||||
| 
 | ||||
| import javax.tools.JavaCompiler; | ||||
| import java.util.*; | ||||
| import java.util.stream.Collectors; | ||||
| 
 | ||||
| import static com.sun.tools.javac.util.List.nil; | ||||
| 
 | ||||
| /** | ||||
|  * A {@link JavaCompiler javac} plugin which inserts {@code >= 0} checks into resulting {@code *.class} files | ||||
|  * for numeric method parameters marked by {@link Positive} | ||||
|  */ | ||||
| public class SampleJavacPlugin implements Plugin { | ||||
| 
 | ||||
|     public static final String NAME = "MyPlugin"; | ||||
| 
 | ||||
|     private static Set<String> TARGET_TYPES = new HashSet<>(Arrays.asList( | ||||
|         // Use only primitive types for simplicity | ||||
|         byte.class.getName(), short.class.getName(), char.class.getName(),  | ||||
|         int.class.getName(), long.class.getName(), float.class.getName(), double.class.getName())); | ||||
| 
 | ||||
|     @Override | ||||
|     public String getName() { | ||||
|         return NAME; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void init(JavacTask task, String... args) { | ||||
|         Context context = ((BasicJavacTask) task).getContext(); | ||||
|         task.addTaskListener(new TaskListener() { | ||||
|             @Override | ||||
|             public void started(TaskEvent e) { | ||||
|             } | ||||
| 
 | ||||
|             @Override | ||||
|             public void finished(TaskEvent e) { | ||||
|                 if (e.getKind() != TaskEvent.Kind.PARSE) { | ||||
|                     return; | ||||
|                 } | ||||
|                 e.getCompilationUnit() | ||||
|                     .accept(new TreeScanner<Void, Void>() { | ||||
|                         @Override | ||||
|                         public Void visitMethod(MethodTree method, Void v) { | ||||
|                             List<VariableTree> parametersToInstrument = method.getParameters() | ||||
|                                 .stream() | ||||
|                                 .filter(SampleJavacPlugin.this::shouldInstrument) | ||||
|                                 .collect(Collectors.toList()); | ||||
|                             if (!parametersToInstrument.isEmpty()) { | ||||
|                                 // There is a possible case that more than one argument is marked by @Positive, | ||||
|                                 // as the checks are added to the method's body beginning, we process parameters RTL | ||||
|                                 // to ensure correct order. | ||||
|                                 Collections.reverse(parametersToInstrument); | ||||
|                                 parametersToInstrument.forEach(p -> addCheck(method, p, context)); | ||||
|                             } | ||||
|                             // There is a possible case that there is a nested class declared in a method's body, | ||||
|                             // hence, we want to proceed with method body AST as well. | ||||
|                             return super.visitMethod(method, v); | ||||
|                         } | ||||
|                     }, null); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     private boolean shouldInstrument(VariableTree parameter) { | ||||
|         return TARGET_TYPES.contains(parameter.getType().toString()) | ||||
|           && parameter.getModifiers().getAnnotations() | ||||
|             .stream() | ||||
|             .anyMatch(a -> Positive.class.getSimpleName().equals(a.getAnnotationType().toString())); | ||||
|     } | ||||
| 
 | ||||
|     private void addCheck(MethodTree method, VariableTree parameter, Context context) { | ||||
|         JCTree.JCIf check = createCheck(parameter, context); | ||||
|         JCTree.JCBlock body = (JCTree.JCBlock) method.getBody(); | ||||
|         body.stats = body.stats.prepend(check); | ||||
|     } | ||||
| 
 | ||||
|     private static JCTree.JCIf createCheck(VariableTree parameter, Context context) { | ||||
|         TreeMaker factory = TreeMaker.instance(context); | ||||
|         Names symbolsTable = Names.instance(context); | ||||
|          | ||||
|         return factory.at(((JCTree) parameter).pos) | ||||
|           .If(factory.Parens(createIfCondition(factory, symbolsTable, parameter)), | ||||
|             createIfBlock(factory, symbolsTable, parameter), | ||||
|             null); | ||||
|     } | ||||
| 
 | ||||
|     private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) { | ||||
|         Name parameterId = symbolsTable.fromString(parameter.getName().toString()); | ||||
|         return factory.Binary(JCTree.Tag.LE,  | ||||
|           factory.Ident(parameterId),  | ||||
|           factory.Literal(TypeTag.INT, 0)); | ||||
|     } | ||||
| 
 | ||||
|     private static JCTree.JCBlock createIfBlock(TreeMaker factory, Names symbolsTable, VariableTree parameter) { | ||||
|         String parameterName = parameter.getName().toString(); | ||||
|         Name parameterId = symbolsTable.fromString(parameterName); | ||||
|          | ||||
|         String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",  | ||||
|           parameterName, parameter.getType(), Positive.class.getSimpleName()); | ||||
|         String errorMessageSuffix = "' for it"; | ||||
|          | ||||
|         return factory.Block(0, com.sun.tools.javac.util.List.of( | ||||
|           factory.Throw( | ||||
|             factory.NewClass(null, nil(),  | ||||
|               factory.Ident(symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())), | ||||
|                 com.sun.tools.javac.util.List.of(factory.Binary(JCTree.Tag.PLUS,  | ||||
|                   factory.Binary(JCTree.Tag.PLUS, factory.Literal(TypeTag.CLASS, errorMessagePrefix),  | ||||
|                     factory.Ident(parameterId)),  | ||||
|                     factory.Literal(TypeTag.CLASS, errorMessageSuffix))), null)))); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1 @@ | ||||
| com.baeldung.javac.SampleJavacPlugin | ||||
| @ -0,0 +1,39 @@ | ||||
| package com.baeldung.breakcontinue; | ||||
| 
 | ||||
| import static com.baeldung.breakcontinue.BreakContinue.labeledBreak; | ||||
| import static com.baeldung.breakcontinue.BreakContinue.labeledContinue; | ||||
| import static com.baeldung.breakcontinue.BreakContinue.unlabeledBreak; | ||||
| import static com.baeldung.breakcontinue.BreakContinue.unlabeledBreakNestedLoops; | ||||
| import static com.baeldung.breakcontinue.BreakContinue.unlabeledContinue; | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| public class BreakContinueTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenUnlabeledBreak_ThenEqual() { | ||||
|         assertEquals(4, unlabeledBreak()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenUnlabeledBreakNestedLoops_ThenEqual() { | ||||
|         assertEquals(2, unlabeledBreakNestedLoops()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenLabeledBreak_ThenEqual() { | ||||
|         assertEquals(1, labeledBreak()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenUnlabeledContinue_ThenEqual() { | ||||
|         assertEquals(5, unlabeledContinue()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenLabeledContinue_ThenEqual() { | ||||
|         assertEquals(3, labeledContinue()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,42 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| public class SampleJavacPluginIntegrationTest { | ||||
| 
 | ||||
|     private static final String CLASS_TEMPLATE = | ||||
|             "package com.baeldung.javac;\n" + | ||||
|             "\n" + | ||||
|             "public class Test {\n" + | ||||
|             "    public static %1$s service(@Positive %1$s i) {\n" + | ||||
|             "        return i;\n" + | ||||
|             "    }\n" + | ||||
|             "}\n" + | ||||
|             ""; | ||||
| 
 | ||||
|     private TestCompiler compiler = new TestCompiler(); | ||||
|     private TestRunner   runner   = new TestRunner(); | ||||
| 
 | ||||
|     @Test(expected = IllegalArgumentException.class) | ||||
|     public void givenInt_whenNegative_thenThrowsException() throws Throwable { | ||||
|         compileAndRun(double.class,-1); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = IllegalArgumentException.class) | ||||
|     public void givenInt_whenZero_thenThrowsException() throws Throwable { | ||||
|         compileAndRun(int.class,0); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenInt_whenPositive_thenSuccess() throws Throwable { | ||||
|         assertEquals(1, compileAndRun(int.class, 1)); | ||||
|     } | ||||
| 
 | ||||
|     private Object compileAndRun(Class<?> argumentType, Object argument) throws Throwable { | ||||
|         String qualifiedClassName = "com.baeldung.javac.Test"; | ||||
|         byte[] byteCode = compiler.compile(qualifiedClassName, String.format(CLASS_TEMPLATE, argumentType.getName())); | ||||
|         return runner.run(byteCode, qualifiedClassName, "service", new Class[] {argumentType}, argument); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,26 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import javax.tools.SimpleJavaFileObject; | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.OutputStream; | ||||
| import java.net.URI; | ||||
| 
 | ||||
| /** Holds compiled byte code in a byte array */ | ||||
| public class SimpleClassFile extends SimpleJavaFileObject { | ||||
| 
 | ||||
|     private ByteArrayOutputStream out; | ||||
| 
 | ||||
|     public SimpleClassFile(URI uri) { | ||||
|         super(uri, Kind.CLASS); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public OutputStream openOutputStream() throws IOException { | ||||
|         return out = new ByteArrayOutputStream(); | ||||
|     } | ||||
| 
 | ||||
|     public byte[] getCompiledBinaries() { | ||||
|         return out.toByteArray(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,34 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import javax.tools.*; | ||||
| import java.net.URI; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** Adapts {@link SimpleClassFile} to the {@link JavaCompiler} */ | ||||
| public class SimpleFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> { | ||||
| 
 | ||||
|     private final List<SimpleClassFile> compiled = new ArrayList<>(); | ||||
| 
 | ||||
|     public SimpleFileManager(StandardJavaFileManager delegate) { | ||||
|         super(delegate); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public JavaFileObject getJavaFileForOutput(Location location, | ||||
|                                                String className, | ||||
|                                                JavaFileObject.Kind kind, | ||||
|                                                FileObject sibling) | ||||
|     { | ||||
|         SimpleClassFile result = new SimpleClassFile(URI.create("string://" + className)); | ||||
|         compiled.add(result); | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return  compiled binaries processed by the current class | ||||
|      */ | ||||
|     public List<SimpleClassFile> getCompiled() { | ||||
|         return compiled; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,23 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import javax.tools.SimpleJavaFileObject; | ||||
| import java.net.URI; | ||||
| 
 | ||||
| /** Exposes given test source to the compiler. */ | ||||
| public class SimpleSourceFile extends SimpleJavaFileObject { | ||||
| 
 | ||||
|     private final String content; | ||||
| 
 | ||||
|     public SimpleSourceFile(String qualifiedClassName, String testSource) { | ||||
|         super(URI.create(String.format("file://%s%s", | ||||
|                                        qualifiedClassName.replaceAll("\\.", "/"), | ||||
|                                        Kind.SOURCE.extension)), | ||||
|               Kind.SOURCE); | ||||
|         content = testSource; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public CharSequence getCharContent(boolean ignoreEncodingErrors) { | ||||
|         return content; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										35
									
								
								core-java/src/test/java/com/baeldung/javac/TestCompiler.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								core-java/src/test/java/com/baeldung/javac/TestCompiler.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import javax.tools.JavaCompiler; | ||||
| import javax.tools.ToolProvider; | ||||
| import java.io.StringWriter; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import static java.util.Arrays.asList; | ||||
| import static java.util.Collections.singletonList; | ||||
| 
 | ||||
| public class TestCompiler { | ||||
|     public byte[] compile(String qualifiedClassName, String testSource) { | ||||
|         StringWriter output = new StringWriter(); | ||||
| 
 | ||||
|         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | ||||
|         SimpleFileManager fileManager = new SimpleFileManager(compiler.getStandardFileManager( | ||||
|                 null, | ||||
|                 null, | ||||
|                 null | ||||
|         )); | ||||
|         List<SimpleSourceFile> compilationUnits = singletonList(new SimpleSourceFile(qualifiedClassName, testSource)); | ||||
|         List<String> arguments = new ArrayList<>(); | ||||
|         arguments.addAll(asList("-classpath", System.getProperty("java.class.path"), | ||||
|                                 "-Xplugin:" + SampleJavacPlugin.NAME)); | ||||
|         JavaCompiler.CompilationTask task = compiler.getTask(output, | ||||
|                                                              fileManager, | ||||
|                                                              null, | ||||
|                                                              arguments, | ||||
|                                                              null, | ||||
|                                                              compilationUnits); | ||||
|         task.call(); | ||||
|         return fileManager.getCompiled().iterator().next().getCompiledBinaries(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										41
									
								
								core-java/src/test/java/com/baeldung/javac/TestRunner.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								core-java/src/test/java/com/baeldung/javac/TestRunner.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,41 @@ | ||||
| package com.baeldung.javac; | ||||
| 
 | ||||
| import java.lang.reflect.InvocationTargetException; | ||||
| import java.lang.reflect.Method; | ||||
| 
 | ||||
| public class TestRunner { | ||||
| 
 | ||||
|     public Object run(byte[] byteCode, | ||||
|                       String qualifiedClassName, | ||||
|                       String methodName, | ||||
|                       Class<?>[] argumentTypes, | ||||
|                       Object... args) | ||||
|             throws Throwable | ||||
|     { | ||||
|         ClassLoader classLoader = new ClassLoader() { | ||||
|             @Override | ||||
|             protected Class<?> findClass(String name) throws ClassNotFoundException { | ||||
|                 return defineClass(name, byteCode, 0, byteCode.length); | ||||
|             } | ||||
|         }; | ||||
|         Class<?> clazz; | ||||
|         try { | ||||
|             clazz = classLoader.loadClass(qualifiedClassName); | ||||
|         } catch (ClassNotFoundException e) { | ||||
|             throw new RuntimeException("Can't load compiled test class", e); | ||||
|         } | ||||
| 
 | ||||
|         Method method; | ||||
|         try { | ||||
|             method = clazz.getMethod(methodName, argumentTypes); | ||||
|         } catch (NoSuchMethodException e) { | ||||
|             throw new RuntimeException("Can't find the 'main()' method in the compiled test class", e); | ||||
|         } | ||||
| 
 | ||||
|         try { | ||||
|             return method.invoke(null, args); | ||||
|         } catch (InvocationTargetException e) { | ||||
|             throw e.getCause(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										137
									
								
								core-java/src/test/java/com/baeldung/stack/StackUnitTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								core-java/src/test/java/com/baeldung/stack/StackUnitTest.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,137 @@ | ||||
| package com.baeldung.stack; | ||||
| 
 | ||||
| import static org.hamcrest.CoreMatchers.equalTo; | ||||
| import static org.junit.Assert.*; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.ListIterator; | ||||
| import java.util.Stack; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| public class StackUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenStackIsCreated_thenItHasSize0() { | ||||
|         Stack intStack = new Stack(); | ||||
|         assertEquals(0, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenEmptyStack_whenElementIsPushed_thenStackSizeisIncreased() { | ||||
|         Stack intStack = new Stack(); | ||||
|         intStack.push(1); | ||||
|         assertEquals(1, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenEmptyStack_whenMultipleElementsArePushed_thenStackSizeisIncreased() { | ||||
|         Stack intStack = new Stack(); | ||||
|         List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); | ||||
|         boolean result = intStack.addAll(intList); | ||||
|         assertTrue(result); | ||||
|         assertEquals(7, intList.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.pop(); | ||||
|         assertTrue(intStack.isEmpty()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.peek(); | ||||
|         assertEquals(1, intStack.search(5)); | ||||
|         assertEquals(1, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         assertEquals(1, intStack.search(5)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         int indexOf = intStack.indexOf(5); | ||||
|         assertEquals(0, indexOf); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.push(5); | ||||
|         intStack.push(5); | ||||
|         int lastIndexOf = intStack.lastIndexOf(5); | ||||
|         assertEquals(2, lastIndexOf); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenElementOnStack_whenRemoveElementIsInvoked_thenElementIsRemoved() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.push(5); | ||||
|         intStack.removeElement(5); | ||||
|         assertEquals(1, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenElementOnStack_whenRemoveElementAtIsInvoked_thenElementIsRemoved() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.push(7); | ||||
|         intStack.removeElementAt(1); | ||||
|         assertEquals(-1, intStack.search(7)); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenElementsOnStack_whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         intStack.push(5); | ||||
|         intStack.push(7); | ||||
|         intStack.removeAllElements(); | ||||
|         assertTrue(intStack.isEmpty()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); | ||||
|         intStack.addAll(intList); | ||||
|         intStack.add(500); | ||||
|         intStack.removeAll(intList); | ||||
|         assertEquals(1, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenElementsOnStack_whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() { | ||||
|         Stack<Integer> intStack = new Stack(); | ||||
|         List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); | ||||
|         intStack.addAll(intList); | ||||
|         intStack.removeIf(element -> element < 6); | ||||
|         assertEquals(2, intStack.size()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() { | ||||
|         Stack<Integer> intStack = new Stack<>(); | ||||
|         List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7); | ||||
|         intStack.addAll(intList); | ||||
|         ListIterator<Integer> it = intStack.listIterator(); | ||||
|         Stack<Integer> result = new Stack(); | ||||
|         while(it.hasNext()) { | ||||
|             result.push(it.next()); | ||||
|         } | ||||
| 
 | ||||
|         assertThat(result, equalTo(intStack)); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,141 @@ | ||||
| package com.baeldung.string.formatter; | ||||
| 
 | ||||
| import java.util.Calendar; | ||||
| import java.util.Formatter; | ||||
| import java.util.GregorianCalendar; | ||||
| import static org.junit.Assert.assertEquals; | ||||
| import static org.junit.Assert.assertFalse; | ||||
| import static org.junit.Assert.assertTrue; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| public class StringFormatterExampleTests { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenFormatSpecifierForCalendar_thenGotExpected() { | ||||
|         //Syntax of Format Specifiers for Date/Time Representation | ||||
|         Calendar c = new GregorianCalendar(2017, 11, 10); | ||||
|         String s = String.format("The date is: %1$tm %1$te,%1$tY", c); | ||||
| 
 | ||||
|         assertEquals("The date is: 12 10,2017", s); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenGeneralConversion_thenConvertedString() { | ||||
|         //General Conversions | ||||
|         String s = String.format("The correct answer is %s", false); | ||||
|         assertEquals("The correct answer is false", s); | ||||
| 
 | ||||
|         s = String.format("The correct answer is %b", null); | ||||
|         assertEquals("The correct answer is false", s); | ||||
| 
 | ||||
|         s = String.format("The correct answer is %B", true); | ||||
|         assertEquals("The correct answer is TRUE", s); | ||||
|     } | ||||
|      | ||||
|     @Test | ||||
|     public void givenString_whenCharConversion_thenConvertedString() {         | ||||
|         //Character Conversions | ||||
|         String s = String.format("The correct answer is %c", 'a'); | ||||
|         assertEquals("The correct answer is a", s); | ||||
| 
 | ||||
|         s = String.format("The correct answer is %c", null); | ||||
|         assertEquals("The correct answer is null", s); | ||||
| 
 | ||||
|         s = String.format("The correct answer is %C", 'b'); | ||||
|         assertEquals("The correct answer is B", s); | ||||
|          | ||||
|         s = String.format("The valid unicode character: %c", 0x0400); | ||||
|         assertTrue(Character.isValidCodePoint(0x0400)); | ||||
|         assertEquals("The valid unicode character: Ѐ", s); | ||||
|     } | ||||
|      | ||||
|     @Test(expected = java.util.IllegalFormatCodePointException.class) | ||||
|     public void givenString_whenIllegalCodePointForConversion_thenError() {         | ||||
|         String s = String.format("The valid unicode character: %c", 0x11FFFF); | ||||
|         assertFalse(Character.isValidCodePoint(0x11FFFF)); | ||||
|         assertEquals("The valid unicode character: Ā", s); | ||||
|     } | ||||
|      | ||||
|     @Test | ||||
|     public void givenString_whenNumericIntegralConversion_thenConvertedString() { | ||||
|         //Numeric Integral Conversions | ||||
|         String s = String.format("The number 25 in decimal = %d", 25); | ||||
|         assertEquals("The number 25 in decimal = 25", s); | ||||
| 
 | ||||
|         s = String.format("The number 25 in octal = %o", 25); | ||||
|         assertEquals("The number 25 in octal = 31", s); | ||||
| 
 | ||||
|         s = String.format("The number 25 in hexadecimal = %x", 25); | ||||
|         assertEquals("The number 25 in hexadecimal = 19", s); | ||||
|     } | ||||
|      | ||||
|     @Test | ||||
|     public void givenString_whenNumericFloatingConversion_thenConvertedString() {         | ||||
|         //Numeric Floating-point Conversions | ||||
|         String s = String.format("The computerized scientific format of 10000.00 " | ||||
|                 + "= %e", 10000.00); | ||||
|         assertEquals("The computerized scientific format of 10000.00 = 1.000000e+04", s); | ||||
| 
 | ||||
|         s = String.format("The decimal format of 10.019 = %f", 10.019); | ||||
|         assertEquals("The decimal format of 10.019 = 10.019000", s); | ||||
|     } | ||||
|      | ||||
|     @Test | ||||
|     public void givenString_whenLineSeparatorConversion_thenConvertedString() {         | ||||
|         //Line Separator Conversion | ||||
|         String s = String.format("First Line %nSecond Line"); | ||||
|         assertEquals("First Line \n" | ||||
|                 + "Second Line", s); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenSpecifyFlag_thenGotFormattedString() { | ||||
|         //Without left-justified flag | ||||
|         String s = String.format("Without left justified flag: %5d", 25); | ||||
|         assertEquals("Without left justified flag:    25", s); | ||||
| 
 | ||||
|         //Using left-justified flag | ||||
|         s = String.format("With left justified flag: %-5d", 25); | ||||
|         assertEquals("With left justified flag: 25   ", s); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenSpecifyPrecision_thenGotExpected() { | ||||
| 
 | ||||
|         //Precision | ||||
|         String s = String.format("Output of 25.09878 with Precision 2: %.2f", 25.09878); | ||||
|         assertEquals("Output of 25.09878 with Precision 2: 25.10", s); | ||||
| 
 | ||||
|         s = String.format("Output of general conversion type with Precision 2: %.2b", true); | ||||
|         assertEquals("Output of general conversion type with Precision 2: tr", s); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenString_whenSpecifyArgumentIndex_thenGotExpected() { | ||||
|         Calendar c = new GregorianCalendar(2017, 11, 10); | ||||
|         //Argument_Index | ||||
|         String s = String.format("The date is: %1$tm %1$te,%1$tY", c); | ||||
|         assertEquals("The date is: 12 10,2017", s); | ||||
| 
 | ||||
|         s = String.format("The date is: %1$tm %<te,%<tY", c); | ||||
|         assertEquals("The date is: 12 10,2017", s); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenAppendable_whenCreateFormatter_thenFormatterWorksOnAppendable() { | ||||
|         //Using String Formatter with Appendable | ||||
|         StringBuilder sb = new StringBuilder(); | ||||
|         Formatter formatter = new Formatter(sb); | ||||
|         formatter.format("I am writting to a %1$s Instance.", sb.getClass()); | ||||
| 
 | ||||
|         assertEquals("I am writting to a class java.lang.StringBuilder Instance.", sb.toString()); | ||||
|     } | ||||
|      | ||||
|     @Test | ||||
|     public void givenString_whenNoArguments_thenExpected() { | ||||
|         //Using String Formatter without arguments | ||||
|         String s = String.format("John scored 90%% in Fall semester"); | ||||
|         assertEquals("John scored 90% in Fall semester", s); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,43 @@ | ||||
| package com.baeldung.stringpool; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| import static org.assertj.core.api.Assertions.*; | ||||
| 
 | ||||
| public class StringPoolUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() { | ||||
|         String constantString1 = "Baeldung"; | ||||
|         String constantString2 = "Baeldung"; | ||||
| 
 | ||||
|         assertThat(constantString1).isSameAs(constantString2); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() { | ||||
|         String newString1 = new String("Baeldung"); | ||||
|         String newString2 = new String("Baeldung"); | ||||
| 
 | ||||
|         assertThat(newString1).isNotSameAs(newString2); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() { | ||||
|         String constantString = "Baeldung"; | ||||
|         String newString = new String("Baeldung"); | ||||
| 
 | ||||
|         assertThat(constantString).isNotSameAs(newString); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() { | ||||
|         String constantString = "interned Baeldung"; | ||||
|         String newString = new String("interned Baeldung"); | ||||
| 
 | ||||
|         assertThat(constantString).isNotSameAs(newString); | ||||
| 
 | ||||
|         String internedString = newString.intern(); | ||||
| 
 | ||||
|         assertThat(constantString).isSameAs(internedString); | ||||
|     } | ||||
| } | ||||
| @ -2,4 +2,4 @@ | ||||
| This is a sample project for the [deeplearning4j](https://deeplearning4j.org) library. | ||||
| 
 | ||||
| ### Relevant Articles: | ||||
| - [A Guide to deeplearning4j](http://www.baeldung.com/a-guide-to-deeplearning4j/) | ||||
| - [A Guide to deeplearning4j](http://www.baeldung.com/deeplearning4j) | ||||
|  | ||||
| @ -1,4 +1,3 @@ | ||||
| ### Relevant Articles: | ||||
| - [Introduction to Drools](http://www.baeldung.com/drools) | ||||
| - [Drools Using Rules from Excel Files](http://www.baeldung.com/drools-excel) | ||||
| - [Drools Using Rules from Excel Files](http://www.baeldung.com/drools-excel) | ||||
|  | ||||
| @ -1,38 +0,0 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
| 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
| 
 | ||||
| 	<modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
| 	<artifactId>drools-backward-chaining</artifactId> | ||||
| 	<version>1.0</version> | ||||
| 	<name>drools-backward-chaining</name> | ||||
| 
 | ||||
| 	<parent> | ||||
| 		<groupId>com.baeldung</groupId> | ||||
| 		<artifactId>parent-modules</artifactId> | ||||
| 		<version>1.0.0-SNAPSHOT</version> | ||||
| 	</parent> | ||||
| 
 | ||||
| 	<properties> | ||||
| 		<runtime.version>6.4.0.Final</runtime.version> | ||||
| 	</properties> | ||||
| 
 | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>org.kie</groupId> | ||||
| 			<artifactId>kie-api</artifactId> | ||||
| 			<version>${runtime.version}</version> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>org.drools</groupId> | ||||
| 			<artifactId>drools-core</artifactId> | ||||
| 			<version>${runtime.version}</version> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>org.drools</groupId> | ||||
| 			<artifactId>drools-decisiontables</artifactId> | ||||
| 			<version>${runtime.version}</version> | ||||
| 		</dependency> | ||||
| 	</dependencies> | ||||
| </project> | ||||
| @ -1,28 +0,0 @@ | ||||
| package com.baeldung; | ||||
| 
 | ||||
| import org.kie.api.KieServices; | ||||
| import org.kie.api.runtime.KieContainer; | ||||
| import org.kie.api.runtime.KieSession; | ||||
| 
 | ||||
| import com.baeldung.model.Beatle; | ||||
| 
 | ||||
| public class BackwardChainingBeatles { | ||||
|     public static void main(String[] args) { | ||||
|          | ||||
|             KieServices ks = KieServices.Factory.get(); | ||||
|             KieContainer kContainer = ks.getKieClasspathContainer(); | ||||
|             KieSession kSession = kContainer.newKieSession("ksession-backward-chaining"); | ||||
|             // drools session base on the xml configuration (<strong>kmodule.xml</strong>) | ||||
| 
 | ||||
|             // graph population | ||||
|             kSession.insert(new Beatle("Starr", "drums")); | ||||
|             kSession.insert(new Beatle("McCartney", "bass")); | ||||
|             kSession.insert(new Beatle("Lennon", "guitar")); | ||||
|             kSession.insert(new Beatle("Harrison", "guitar")); | ||||
| 
 | ||||
|             kSession.insert("Ringo"); // invoke the rule that calls the query implentation of backward chaining | ||||
|             kSession.fireAllRules(); // invoke all the rules | ||||
|          | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -1,33 +0,0 @@ | ||||
| package com.baeldung.model; | ||||
| 
 | ||||
| import org.kie.api.definition.type.Position; | ||||
| 
 | ||||
| public class Beatle { | ||||
| 
 | ||||
|     @Position(0) | ||||
|     private String lastName; | ||||
|     @Position(1) | ||||
|     private String instrument; | ||||
| 
 | ||||
|     public Beatle(String lastName, String instrument) { | ||||
|         this.lastName = lastName; | ||||
|         this.instrument = instrument; | ||||
|     } | ||||
| 
 | ||||
|     public String getInstrument() { | ||||
|         return instrument; | ||||
|     } | ||||
| 
 | ||||
|     public void setInstrument(String instrument) { | ||||
|         this.instrument = instrument; | ||||
|     } | ||||
| 
 | ||||
|     public String getLastName() { | ||||
|         return lastName; | ||||
|     } | ||||
| 
 | ||||
|     public void setLastName(String lastName) { | ||||
|         this.lastName = lastName; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -1,6 +0,0 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> | ||||
|     <kbase name="backward_chaining" packages="backward_chaining"> | ||||
|         <ksession name="ksession-backward-chaining"/> | ||||
|     </kbase> | ||||
| </kmodule> | ||||
| @ -1,3 +0,0 @@ | ||||
| groupId=com.baeldung.drools | ||||
| artifactId=DroosBackwardChaining | ||||
| version=1.0.0-SNAPSHOT | ||||
| @ -1,44 +0,0 @@ | ||||
| package com.baeldung | ||||
| 
 | ||||
| import com.baeldung.model.Beatle; | ||||
| 
 | ||||
| 
 | ||||
| query whichBeatle(String lastName, String instrument)  | ||||
|   Beatle(lastName, instrument;) | ||||
|   or  | ||||
|   (Beatle(lastName, null;)  | ||||
|     and  | ||||
|    whichBeatle(null, instrument;)) //recursive call to the function that allows to search in a derivation tree structure | ||||
| end | ||||
| 
 | ||||
| rule "Ringo" | ||||
|   when | ||||
|     String(this == "Ringo") | ||||
|     whichBeatle("Starr", "drums";) | ||||
|   then | ||||
|     System.out.println("The beatle is Ringo Starr"); | ||||
| end | ||||
| 
 | ||||
| rule "Paul" | ||||
|   when | ||||
|     String(this == "Paul") | ||||
|     whichBeatle("McCartney", "bass";) | ||||
|   then | ||||
|     System.out.println("The beatle is Paul McCartney"); | ||||
| end | ||||
| 
 | ||||
| rule "John" | ||||
|   when | ||||
|     String(this == "John") | ||||
|     whichBeatle("Lennon", "guitar";) | ||||
|   then | ||||
|     System.out.println("The beatle is John Lennon"); | ||||
| end | ||||
| 
 | ||||
| rule "George" | ||||
|   when | ||||
|     String(this == "George") | ||||
|     whichBeatle("Harrison", "guitar";) | ||||
|   then | ||||
|     System.out.println("The beatle is George Harrison"); | ||||
| end | ||||
| @ -1,57 +0,0 @@ | ||||
| package com.baeldung.test; | ||||
| 
 | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.junit.runners.JUnit4; | ||||
| import org.kie.api.KieServices; | ||||
| import org.kie.api.runtime.KieContainer; | ||||
| import org.kie.api.runtime.KieSession; | ||||
| 
 | ||||
| import com.baeldung.drools.model.Fact; | ||||
| import com.baeldung.drools.model.Result; | ||||
| 
 | ||||
| import static junit.framework.TestCase.assertEquals; | ||||
| 
 | ||||
| @RunWith(value = JUnit4.class) | ||||
| public class BackwardChainingTest { | ||||
|     private Result result; | ||||
|     private KieServices ks; | ||||
|     private KieContainer kContainer; | ||||
|     private KieSession ksession; | ||||
| 
 | ||||
|     @Before | ||||
|     public void before() { | ||||
|         result = new Result(); | ||||
|         ks = KieServices.Factory.get(); | ||||
|         kContainer = ks.getKieClasspathContainer(); | ||||
|         ksession = kContainer.newKieSession("ksession-backward-chaining"); | ||||
|         ksession.setGlobal("result", result); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() { | ||||
| 
 | ||||
|         ksession.setGlobal("result", result); | ||||
|         ksession.insert(new Fact("Asia", "Planet Earth")); | ||||
|         ksession.insert(new Fact("China", "Asia")); | ||||
|         ksession.insert(new Fact("Great Wall of China", "China")); | ||||
| 
 | ||||
|         ksession.fireAllRules(); | ||||
|          | ||||
|         // Assert Decision one | ||||
|         assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenChinaIsNotGiven_ThenWallOfChinaDoesNotBelongToPlanetEarth() { | ||||
|         ksession.insert(new Fact("Asia", "Planet Earth")); | ||||
|         // ksession.insert(new Location("China", "Asia")); // not provided to force Decision two | ||||
|         ksession.insert(new Fact("Great Wall of China", "China")); | ||||
|          | ||||
|         ksession.fireAllRules(); | ||||
|          | ||||
|         // Assert Decision two | ||||
|         assertEquals(result.getValue(), "Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth"); | ||||
|     } | ||||
| } | ||||
| @ -1,24 +1,19 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>com.baeldung</groupId> | ||||
|     <artifactId>drools</artifactId> | ||||
|     <version>1.0.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
| 
 | ||||
|     <properties> | ||||
|         <http-component-version>4.4.6</http-component-version> | ||||
|         <drools-version>7.1.0.Beta2</drools-version> | ||||
|         <apache-poi-version>3.13</apache-poi-version> | ||||
|     </properties> | ||||
| 	<properties> | ||||
| 		<http-component-version>4.4.6</http-component-version> | ||||
| 		<drools-version>7.4.1.Final</drools-version> | ||||
| 		<apache-poi-version>3.13</apache-poi-version> | ||||
| 	</properties> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
| @ -68,4 +63,25 @@ | ||||
| 
 | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <build> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <groupId>org.apache.maven.plugins</groupId> | ||||
|                 <artifactId>maven-surefire-plugin</artifactId> | ||||
|                 <version>${maven-surefire-plugin.version}</version> | ||||
|                 <configuration> | ||||
|                     <forkCount>3</forkCount> | ||||
|                     <reuseForks>true</reuseForks> | ||||
|                     <excludes> | ||||
|                         <exclude>**/*IntegrationTest.java</exclude> | ||||
|                         <exclude>**/*LongRunningUnitTest.java</exclude> | ||||
|                         <exclude>**/*ManualTest.java</exclude> | ||||
|                         <exclude>**/JdbcTest.java</exclude> | ||||
|                         <exclude>**/*LiveTest.java</exclude> | ||||
|                     </excludes> | ||||
|                 </configuration> | ||||
|             </plugin> | ||||
|         </plugins> | ||||
|     </build> | ||||
| 
 | ||||
| </project> | ||||
| @ -1,9 +1,8 @@ | ||||
| package com.baeldung.drools; | ||||
| package com.baeldung.drools.backward_chaining; | ||||
| 
 | ||||
| import org.kie.api.KieServices; | ||||
| import org.kie.api.runtime.KieContainer; | ||||
| import org.kie.api.runtime.KieSession; | ||||
| 
 | ||||
| import com.baeldung.drools.config.DroolsBeanFactory; | ||||
| import com.baeldung.drools.model.Fact; | ||||
| import com.baeldung.drools.model.Result; | ||||
| 
 | ||||
| @ -11,23 +10,21 @@ public class BackwardChaining { | ||||
|     public static void main(String[] args) { | ||||
|         Result result = new BackwardChaining().backwardChaining(); | ||||
|         System.out.println(result.getValue()); | ||||
|         result.getFacts().stream().forEach(System.out::println); | ||||
|         result.getFacts() | ||||
|             .stream() | ||||
|             .forEach(System.out::println); | ||||
|     } | ||||
| 
 | ||||
|     public Result backwardChaining() { | ||||
|         Result result = new Result(); | ||||
|         KieServices ks = KieServices.Factory.get(); | ||||
|         KieContainer kContainer = ks.getKieClasspathContainer(); | ||||
|         KieSession ksession = kContainer.newKieSession("ksession-backward-chaining"); | ||||
|         KieSession ksession = new DroolsBeanFactory().getKieSession(); | ||||
|         ksession.setGlobal("result", result); | ||||
|         ksession.insert(new Fact("Asia", "Planet Earth")); | ||||
| //        ksession.insert(new Fact("China", "Asia")); | ||||
|         ksession.insert(new Fact("China", "Asia")); | ||||
|         ksession.insert(new Fact("Great Wall of China", "China")); | ||||
| 
 | ||||
|         ksession.fireAllRules(); | ||||
| 
 | ||||
|         return result; | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -3,7 +3,6 @@ package com.baeldung.drools.config; | ||||
| import org.drools.decisiontable.DecisionTableProviderImpl; | ||||
| import org.kie.api.KieServices; | ||||
| import org.kie.api.builder.*; | ||||
| import org.kie.api.io.KieResources; | ||||
| import org.kie.api.io.Resource; | ||||
| import org.kie.api.runtime.KieContainer; | ||||
| import org.kie.api.runtime.KieSession; | ||||
| @ -22,7 +21,7 @@ public class DroolsBeanFactory { | ||||
| 
 | ||||
|     private  KieFileSystem getKieFileSystem() throws IOException{ | ||||
|         KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); | ||||
|         List<String> rules=Arrays.asList("SuggestApplicant.drl","Product_rules.xls"); | ||||
|         List<String> rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls"); | ||||
|         for(String rule:rules){ | ||||
|             kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); | ||||
|         } | ||||
| @ -56,9 +55,11 @@ public class DroolsBeanFactory { | ||||
|         getKieRepository(); | ||||
|         KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); | ||||
| 
 | ||||
|         kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/BackwardChaining.drl")); | ||||
|         kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl")); | ||||
|         kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); | ||||
|          | ||||
|          | ||||
|         KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); | ||||
|         kb.buildAll(); | ||||
|         KieModule kieModule = kb.getKieModule(); | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| package com.baeldung | ||||
| package com.baeldung.drools.rules | ||||
| 
 | ||||
| import com.baeldung.drools.model.Fact; | ||||
| 
 | ||||
| @ -19,13 +19,6 @@ then | ||||
| 	result.setValue("Decision one taken: Great Wall of China BELONGS TO Planet Earth"); | ||||
| end | ||||
| 
 | ||||
| rule "Great Wall of China DOES NOT BELONG TO of Planet Earth" | ||||
| when | ||||
|     not belongsTo("Great Wall of China", "Planet Earth";) | ||||
| then | ||||
|     result.setValue("Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth"); | ||||
| end | ||||
| 
 | ||||
| rule "print all facts" | ||||
| when | ||||
|     belongsTo(element, place;) | ||||
| @ -0,0 +1,36 @@ | ||||
| package com.baeldung.drools.backward_chaining; | ||||
| 
 | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.kie.api.runtime.KieSession; | ||||
| 
 | ||||
| import com.baeldung.drools.config.DroolsBeanFactory; | ||||
| import com.baeldung.drools.model.Fact; | ||||
| import com.baeldung.drools.model.Result; | ||||
| 
 | ||||
| import static junit.framework.TestCase.assertEquals; | ||||
| 
 | ||||
| public class BackwardChainingTest { | ||||
|     private Result result; | ||||
|     private KieSession ksession; | ||||
|   | ||||
|     @Before | ||||
|     public void before() { | ||||
|         result = new Result(); | ||||
|         ksession = new DroolsBeanFactory().getKieSession(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() { | ||||
| 
 | ||||
|         ksession.setGlobal("result", result); | ||||
|         ksession.insert(new Fact("Asia", "Planet Earth")); | ||||
|         ksession.insert(new Fact("China", "Asia")); | ||||
|         ksession.insert(new Fact("Great Wall of China", "China")); | ||||
| 
 | ||||
|         ksession.fireAllRules(); | ||||
|          | ||||
|         // Assert Decision one | ||||
|         assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth"); | ||||
|     } | ||||
| } | ||||
| @ -16,32 +16,26 @@ import wildfly.beans.UserBeanLocal; | ||||
|  * Servlet implementation class TestEJBServlet | ||||
|  */ | ||||
| public class TestEJBServlet extends HttpServlet { | ||||
| 	private static final long serialVersionUID = 1L; | ||||
| 
 | ||||
| 	@EJB | ||||
| 	private UserBeanLocal userBean; | ||||
|     @EJB | ||||
|     private UserBeanLocal userBean; | ||||
| 
 | ||||
| 	protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||||
| 			throws ServletException, IOException { | ||||
| 		List<User> users = userBean.getUsers(); | ||||
|     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         List<User> users = userBean.getUsers(); | ||||
| 
 | ||||
| 		PrintWriter out = response.getWriter(); | ||||
|         PrintWriter out = response.getWriter(); | ||||
| 
 | ||||
| 		out.println("<html>"); | ||||
| 		out.println("<head><title>Users</title></head>"); | ||||
| 		out.println("<body>"); | ||||
| 		out.println("<center><h1>List of users:</h1>"); | ||||
| 		out.println("<table border=\"1\" align=\"center\" style=\"width:50%\">"); | ||||
| 		for (User user : users) { | ||||
| 			out.println("<tr>"); | ||||
| 			out.print("<td>" + user.getUsername() + "</td>"); | ||||
| 			out.print("<td>" + user.getEmail() + "</td>"); | ||||
| 			out.println("</tr>"); | ||||
| 		} | ||||
| 	} | ||||
|         out.println("<html>"); | ||||
|         out.println("<body>"); | ||||
|         for (User user : users) { | ||||
|             out.print(user.getUsername()); | ||||
|             out.print(" " + user.getEmail() + " <br>"); | ||||
|         } | ||||
|         out.println("</body>"); | ||||
|         out.println("</html>"); | ||||
|     } | ||||
| 
 | ||||
| 	protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||||
| 			throws ServletException, IOException { | ||||
| 		doGet(request, response); | ||||
| 	} | ||||
|     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||||
|         doGet(request, response); | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										3
									
								
								guava-modules/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								guava-modules/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| 
 | ||||
| ## Guava Modules | ||||
| 
 | ||||
| @ -4,13 +4,14 @@ | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>com.baeldung</groupId> | ||||
|     <artifactId>guava18</artifactId> | ||||
|     <artifactId>guava-18</artifactId> | ||||
|     <version>0.1.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|         <relativePath>../</relativePath>         | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
| @ -4,13 +4,14 @@ | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>com.baeldung</groupId> | ||||
|     <artifactId>guava19</artifactId> | ||||
|     <artifactId>guava-19</artifactId> | ||||
|     <version>0.1.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|         <relativePath>../</relativePath> | ||||
|     </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
| @ -4,13 +4,14 @@ | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <artifactId>guava21</artifactId> | ||||
|     <artifactId>guava-21</artifactId> | ||||
|     <version>1.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-modules</artifactId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|         <relativePath>../</relativePath>         | ||||
|      </parent> | ||||
| 
 | ||||
|     <dependencies> | ||||
							
								
								
									
										17
									
								
								guest/spring-security/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								guest/spring-security/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | ||||
| ## Building | ||||
| 
 | ||||
| To build the module, use Maven's `package` goal: | ||||
| 
 | ||||
| ``` | ||||
| mvn clean package | ||||
| ``` | ||||
| 
 | ||||
| ## Running | ||||
| 
 | ||||
| To run the application, use Spring Boot's `run` goal: | ||||
| 
 | ||||
| ``` | ||||
| mvn spring-boot:run | ||||
| ``` | ||||
| 
 | ||||
| The application will be accessible at [http://localhost:8080/](http://localhost:8080/) | ||||
							
								
								
									
										77
									
								
								guest/spring-security/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								guest/spring-security/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,77 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>com.stackify.guest</groupId> | ||||
|     <artifactId>spring-security</artifactId> | ||||
|     <version>1.0-SNAPSHOT</version> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>org.springframework.boot</groupId> | ||||
|         <artifactId>spring-boot-starter-parent</artifactId> | ||||
|         <version>2.0.0.M6</version> | ||||
|         <relativePath/> | ||||
|     </parent> | ||||
| 
 | ||||
|     <name>spring-security</name> | ||||
|     <description>Spring Security Sample Project</description> | ||||
| 
 | ||||
|     <dependencies> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-web</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-thymeleaf</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.thymeleaf</groupId> | ||||
|             <artifactId>thymeleaf-spring5</artifactId> | ||||
|             <version>3.0.8.RELEASE</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-security</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.springframework.boot</groupId> | ||||
|             <artifactId>spring-boot-starter-jdbc</artifactId> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>com.h2database</groupId> | ||||
|             <artifactId>h2</artifactId> | ||||
|             <scope>runtime</scope> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <repositories> | ||||
|         <repository> | ||||
|             <id>spring-milestones</id> | ||||
|             <name>Spring Milestones</name> | ||||
|             <url>https://repo.spring.io/milestone</url> | ||||
|             <snapshots> | ||||
|                 <enabled>false</enabled> | ||||
|             </snapshots> | ||||
|         </repository> | ||||
|     </repositories> | ||||
|     <pluginRepositories> | ||||
|         <pluginRepository> | ||||
|             <id>spring-milestones</id> | ||||
|             <name>Spring Milestones</name> | ||||
|             <url>https://repo.spring.io/milestone</url> | ||||
|             <snapshots> | ||||
|                 <enabled>false</enabled> | ||||
|             </snapshots> | ||||
|         </pluginRepository> | ||||
|     </pluginRepositories> | ||||
| 
 | ||||
|     <properties> | ||||
|         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
|         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||||
|         <java.version>1.8</java.version> | ||||
|     </properties> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,15 @@ | ||||
| package com.stackify.guest.springsecurity; | ||||
| 
 | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.springframework.context.annotation.ComponentScan; | ||||
| 
 | ||||
| @SpringBootApplication | ||||
| @ComponentScan(basePackages = {"com.stackify.guest.springsecurity"}) | ||||
| public class Application { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         SpringApplication.run(Application.class, args); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,16 @@ | ||||
| package com.stackify.guest.springsecurity.config; | ||||
| 
 | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
| 
 | ||||
| @Configuration | ||||
| public class WebMvcConfiguration implements WebMvcConfigurer { | ||||
| 
 | ||||
|     @Override | ||||
|     public void addViewControllers(ViewControllerRegistry registry) { | ||||
|         registry.addViewController("/customLogin").setViewName("customLogin"); | ||||
|         registry.addViewController("/loginSuccess").setViewName("index"); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,40 @@ | ||||
| package com.stackify.guest.springsecurity.config; | ||||
| 
 | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||||
| import org.springframework.security.core.userdetails.UserDetailsService; | ||||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||||
| import org.springframework.security.provisioning.JdbcUserDetailsManager; | ||||
| 
 | ||||
| import javax.sql.DataSource; | ||||
| 
 | ||||
| @EnableWebSecurity | ||||
| public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | ||||
| 
 | ||||
|     @Bean | ||||
|     public UserDetailsService jdbcUserDetailsService(DataSource dataSource) { | ||||
|         JdbcUserDetailsManager manager = new JdbcUserDetailsManager(); | ||||
|         manager.setDataSource(dataSource); | ||||
|         return manager; | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public PasswordEncoder passwordEncoder() { | ||||
|         return new BCryptPasswordEncoder(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void configure(HttpSecurity http) throws Exception { | ||||
|         http.authorizeRequests() | ||||
|                 .antMatchers("/css/**").permitAll() | ||||
|                 .anyRequest().authenticated() | ||||
|                 .and().formLogin() | ||||
|                 .loginPage("/customLogin") | ||||
|                 .defaultSuccessUrl("/loginSuccess", true) | ||||
|                 .permitAll(); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										2
									
								
								guest/spring-security/src/main/resources/data.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								guest/spring-security/src/main/resources/data.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| INSERT INTO users VALUES ('jill', '$2a$04$qUlqAEEYF1YvrpJMosodoewgL6aO.qgHytl2k5L7kdXEWnJsFdxvq', TRUE); | ||||
| INSERT INTO authorities VALUES ('jill', 'USERS'); | ||||
							
								
								
									
										10
									
								
								guest/spring-security/src/main/resources/schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								guest/spring-security/src/main/resources/schema.sql
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| CREATE TABLE users ( | ||||
|   username VARCHAR(256) PRIMARY KEY, | ||||
|   password VARCHAR(256), | ||||
|   enabled  BOOLEAN | ||||
| ); | ||||
| 
 | ||||
| CREATE TABLE authorities ( | ||||
|   username  VARCHAR(256) REFERENCES users (username), | ||||
|   authority VARCHAR(256) | ||||
| ); | ||||
| @ -0,0 +1,3 @@ | ||||
| .bad-login { | ||||
|   color: red; | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| <!DOCTYPE html> | ||||
| <html xmlns="http://www.w3.org/1999/xhtml" | ||||
|       xmlns:th="http://www.thymeleaf.org"> | ||||
| <head> | ||||
|     <link rel="stylesheet" href="/css/styles.css"> | ||||
| </head> | ||||
| <body> | ||||
| <form th:action="@{/customLogin}" method="post"> | ||||
|     <fieldset> | ||||
|         <label for="username">Login:</label> | ||||
|         <input id="username" name="username"> | ||||
|         <label for="password">Password:</label> | ||||
|         <input id="password" name="password" type="password"> | ||||
|     </fieldset> | ||||
|     <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/> | ||||
|     <input type="submit" value="Login"> | ||||
|     <div th:if="${param.error}" class="bad-login">Bad login or password.</div> | ||||
|     <div th:if="${param.logout}">Log out successful.</div> | ||||
| </form> | ||||
| </body> | ||||
| </html> | ||||
Some files were not shown because too many files have changed in this diff Show More
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user