Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
11863ab2c3
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.comparable;
|
||||
|
||||
public class Player implements Comparable<Player> {
|
||||
|
||||
private int ranking;
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
public Player(int ranking, String name, int age) {
|
||||
this.ranking = ranking;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getRanking() {
|
||||
return ranking;
|
||||
}
|
||||
|
||||
public void setRanking(int ranking) {
|
||||
this.ranking = ranking;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Player otherPlayer) {
|
||||
return (this.getRanking() - otherPlayer.getRanking());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.comparable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerSorter {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Player> footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 20);
|
||||
Player player2 = new Player(67, "Roger", 22);
|
||||
Player player3 = new Player(45, "Steven", 24);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
Collections.sort(footballTeam);
|
||||
System.out.println("After Sorting : " + footballTeam);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
public class Player {
|
||||
|
||||
private int ranking;
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
public Player(int ranking, String name, int age) {
|
||||
this.ranking = ranking;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getRanking() {
|
||||
return ranking;
|
||||
}
|
||||
|
||||
public void setRanking(int ranking) {
|
||||
this.ranking = ranking;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class PlayerAgeComparator implements Comparator<Player> {
|
||||
|
||||
@Override
|
||||
public int compare(Player firstPlayer, Player secondPlayer) {
|
||||
return (firstPlayer.getAge() - secondPlayer.getAge());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerAgeSorter {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Player> footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 22);
|
||||
Player player2 = new Player(67, "Roger", 20);
|
||||
Player player3 = new Player(45, "Steven", 24);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
//Instance of PlayerAgeComparator
|
||||
PlayerAgeComparator playerComparator = new PlayerAgeComparator();
|
||||
Collections.sort(footballTeam, playerComparator);
|
||||
System.out.println("After Sorting by age : " + footballTeam);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class PlayerRankingComparator implements Comparator<Player> {
|
||||
|
||||
@Override
|
||||
public int compare(Player firstPlayer, Player secondPlayer) {
|
||||
return (firstPlayer.getRanking() - secondPlayer.getRanking());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerRankingSorter {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Player> footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 22);
|
||||
Player player2 = new Player(67, "Roger", 20);
|
||||
Player player3 = new Player(45, "Steven", 40);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
//Instance of PlayerRankingComparator
|
||||
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
|
||||
Collections.sort(footballTeam, playerComparator);
|
||||
System.out.println("After Sorting by ranking : " + footballTeam);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,7 @@ public class PolygonFactory {
|
|||
if(numberOfSides == 5) {
|
||||
return new Pentagon();
|
||||
}
|
||||
if(numberOfSides == 4) {
|
||||
if(numberOfSides == 7) {
|
||||
return new Heptagon();
|
||||
}
|
||||
else if(numberOfSides == 8) {
|
||||
|
@ -19,4 +19,4 @@ public class PolygonFactory {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.comparable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ComparableUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingComparable_thenSortedList() {
|
||||
List<Player> footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 20);
|
||||
Player player2 = new Player(67, "Roger", 22);
|
||||
Player player3 = new Player(45, "Steven", 24);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
Collections.sort(footballTeam);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "Steven");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 67);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ComparatorUnitTest {
|
||||
|
||||
List<Player> footballTeam;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 20);
|
||||
Player player2 = new Player(67, "Roger", 22);
|
||||
Player player3 = new Player(45, "Steven", 24);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRankingComparator_thenSortedList() {
|
||||
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
|
||||
Collections.sort(footballTeam, playerComparator);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "Steven");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 67);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingAgeComparator_thenSortedList() {
|
||||
PlayerAgeComparator playerComparator = new PlayerAgeComparator();
|
||||
Collections.sort(footballTeam, playerComparator);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "John");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 45);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.comparator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java8ComparatorUnitTest {
|
||||
|
||||
List<Player> footballTeam;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
footballTeam = new ArrayList<Player>();
|
||||
Player player1 = new Player(59, "John", 22);
|
||||
Player player2 = new Player(67, "Roger", 20);
|
||||
Player player3 = new Player(45, "Steven", 24);
|
||||
footballTeam.add(player1);
|
||||
footballTeam.add(player2);
|
||||
footballTeam.add(player3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparing_UsingLambda_thenSorted() {
|
||||
System.out.println("************** Java 8 Comaparator **************");
|
||||
Comparator<Player> byRanking = (Player player1, Player player2) -> player1.getRanking() - player2.getRanking();
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
Collections.sort(footballTeam, byRanking);
|
||||
System.out.println("After Sorting : " + footballTeam);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "Steven");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 67);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenComparing_UsingComparatorComparing_thenSorted() {
|
||||
System.out.println("********* Comaparator.comparing method *********");
|
||||
System.out.println("********* byRanking *********");
|
||||
Comparator<Player> byRanking = Comparator.comparing(Player::getRanking);
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
Collections.sort(footballTeam, byRanking);
|
||||
System.out.println("After Sorting : " + footballTeam);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "Steven");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 67);
|
||||
|
||||
System.out.println("********* byAge *********");
|
||||
Comparator<Player> byAge = Comparator.comparing(Player::getAge);
|
||||
|
||||
System.out.println("Before Sorting : " + footballTeam);
|
||||
Collections.sort(footballTeam, byAge);
|
||||
System.out.println("After Sorting : " + footballTeam);
|
||||
assertEquals(footballTeam.get(0)
|
||||
.getName(), "Roger");
|
||||
assertEquals(footballTeam.get(2)
|
||||
.getRanking(), 45);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,5 @@
|
|||
- [Introduction to Java Logging](http://www.baeldung.com/java-logging-intro)
|
||||
- [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback)
|
||||
- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode)
|
||||
- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java)
|
||||
- [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback)
|
||||
- [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders)
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
* @SpringJUnitConfig(SpringJUnitConfigTest.Config.class) is equivalent to:
|
||||
*
|
||||
* @ExtendWith(SpringExtension.class)
|
||||
* @ContextConfiguration(classes = SpringJUnitConfigTest.Config.class )
|
||||
*
|
||||
*/
|
||||
@SpringJUnitConfig(SpringJUnitConfigTest.Config.class)
|
||||
public class SpringJUnitConfigTest {
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
void givenAppContext_WhenInjected_ThenItShouldNotBeNull() {
|
||||
assertNotNull(applicationContext);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.jupiter;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* @SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class) is equivalent to:
|
||||
*
|
||||
* @ExtendWith(SpringExtension.class)
|
||||
* @WebAppConfiguration
|
||||
* @ContextConfiguration(classes = SpringJUnitWebConfigTest.Config.class )
|
||||
*
|
||||
*/
|
||||
@SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class)
|
||||
public class SpringJUnitWebConfigTest {
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
|
||||
@Test
|
||||
void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() {
|
||||
assertNotNull(webAppContext);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package org.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue