Skip to content

Commit 4362aab

Browse files
author
Grzegorz Piwowarek
committed
Add missing files
1 parent 02dda99 commit 4362aab

28 files changed

Lines changed: 2332 additions & 0 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
line 1
2+
a second line
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.DayOfWeek;
4+
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class UseLocalDateTest {
11+
12+
UseLocalDate useLocalDate = new UseLocalDate();
13+
14+
@Test
15+
public void givenValues_whenUsingFactoryOf_thenLocalDate(){
16+
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString());
17+
}
18+
19+
@Test
20+
public void givenString_whenUsingParse_thenLocalDate(){
21+
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
22+
}
23+
24+
@Test
25+
public void whenUsingClock_thenLocalDate(){
26+
Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock());
27+
}
28+
29+
@Test
30+
public void givenDate_whenUsingPlus_thenNextDay(){
31+
Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now()));
32+
}
33+
34+
@Test
35+
public void givenDate_whenUsingMinus_thenPreviousDay(){
36+
Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now()));
37+
}
38+
39+
@Test
40+
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){
41+
Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
42+
}
43+
44+
@Test
45+
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){
46+
Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth());
47+
}
48+
49+
@Test
50+
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){
51+
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
52+
}
53+
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalTime;
5+
import java.time.Month;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class UseLocalDateTimeTest {
11+
12+
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
13+
14+
@Test
15+
public void givenString_whenUsingParse_thenLocalDateTime(){
16+
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
17+
Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalTime;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class UseLocalTimeTest {
9+
10+
UseLocalTime useLocalTime = new UseLocalTime();
11+
12+
@Test
13+
public void givenValues_whenUsingFactoryOf_thenLocalTime(){
14+
Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString());
15+
}
16+
17+
@Test
18+
public void givenString_whenUsingParse_thenLocalTime(){
19+
Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
20+
}
21+
22+
@Test
23+
public void givenTime_whenAddHour_thenLocalTime(){
24+
Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString());
25+
}
26+
27+
@Test
28+
public void getHourFromLocalTime(){
29+
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1)));
30+
}
31+
32+
@Test
33+
public void getLocalTimeWithMinuteSetToValue(){
34+
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20));
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDate;
4+
import java.time.Period;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class UsePeriodTest {
10+
UsePeriod usingPeriod=new UsePeriod();
11+
12+
@Test
13+
public void givenPeriodAndLocalDate_thenCalculateModifiedDate(){
14+
Period period = Period.ofDays(1);
15+
LocalDate localDate = LocalDate.parse("2007-05-10");
16+
Assert.assertEquals(localDate.plusDays(1),usingPeriod.modifyDates(localDate, period));
17+
}
18+
19+
@Test
20+
public void givenDates_thenGetPeriod(){
21+
LocalDate localDate1 = LocalDate.parse("2007-05-10");
22+
LocalDate localDate2 = LocalDate.parse("2007-05-15");
23+
24+
Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2));
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
import java.time.ZonedDateTime;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class UseZonedDateTimeTest {
11+
12+
UseZonedDateTime zonedDateTime=new UseZonedDateTime();
13+
14+
@Test
15+
public void givenZoneId_thenZonedDateTime(){
16+
ZoneId zoneId=ZoneId.of("Europe/Paris");
17+
ZonedDateTime zonedDatetime=zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
18+
Assert.assertEquals(zoneId,ZoneId.from(zonedDatetime));
19+
}
20+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.doublecolon;
2+
3+
import com.baeldung.doublecolon.function.TriFunction;
4+
import org.junit.After;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
import java.util.Comparator;
11+
import java.util.List;
12+
import java.util.function.BiFunction;
13+
14+
import static com.baeldung.doublecolon.ComputerUtils.*;
15+
16+
public class TestComputerUtils {
17+
18+
@Before
19+
public void setup() {
20+
}
21+
22+
@After
23+
public void tearDown() {
24+
}
25+
26+
@Test
27+
public void testConstructorReference() {
28+
29+
Computer c1 = new Computer(2015, "white");
30+
Computer c2 = new Computer(2009, "black");
31+
Computer c3 = new Computer(2014, "black");
32+
33+
BiFunction<Integer, String, Computer> c4Function = Computer::new;
34+
Computer c4 = c4Function.apply(2013, "white");
35+
BiFunction<Integer, String, Computer> c5Function = Computer::new;
36+
Computer c5 = c5Function.apply(2010, "black");
37+
BiFunction<Integer, String, Computer> c6Function = Computer::new;
38+
Computer c6 = c6Function.apply(2008, "black");
39+
40+
List<Computer> inventory = Arrays.asList(c1, c2, c3, c4, c5, c6);
41+
42+
List<Computer> blackComputer = filter(inventory, blackPredicate);
43+
Assert.assertEquals("The black Computers are: ", blackComputer.size(), 4);
44+
45+
List<Computer> after2010Computer = filter(inventory, after2010Predicate);
46+
Assert.assertEquals("The Computer bought after 2010 are: ", after2010Computer.size(), 3);
47+
48+
List<Computer> before2011Computer = filter(inventory, c -> c.getAge() < 2011);
49+
Assert.assertEquals("The Computer bought before 2011 are: ", before2011Computer.size(), 3);
50+
51+
inventory.sort(Comparator.comparing(Computer::getAge));
52+
53+
Assert.assertEquals("Oldest Computer in inventory", c6, inventory.get(0));
54+
55+
}
56+
57+
@Test
58+
public void testStaticMethodReference() {
59+
60+
Computer c1 = new Computer(2015, "white", 35);
61+
Computer c2 = new Computer(2009, "black", 65);
62+
TriFunction<Integer, String, Integer, Computer> c6Function = Computer::new;
63+
Computer c3 = c6Function.apply(2008, "black", 90);
64+
65+
List<Computer> inventory = Arrays.asList(c1, c2, c3);
66+
inventory.forEach(ComputerUtils::repair);
67+
68+
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
69+
}
70+
71+
@Test
72+
public void testInstanceMethodArbitraryObjectParticularType() {
73+
74+
Computer c1 = new Computer(2015, "white", 35);
75+
Computer c2 = new MacbookPro(2009, "black", 65);
76+
List<Computer> inventory = Arrays.asList(c1, c2);
77+
inventory.forEach(Computer::turnOnPc);
78+
79+
}
80+
81+
@Test
82+
public void testSuperMethodReference() {
83+
84+
final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new;
85+
final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black", 100);
86+
Double initialValue = new Double(999.99);
87+
final Double actualValue = macbookPro.calculateValue(initialValue);
88+
Assert.assertEquals(766.659, actualValue, 0.0);
89+
}
90+
91+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.encoderdecoder;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.io.UnsupportedEncodingException;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
import java.net.URLDecoder;
13+
import java.net.URLEncoder;
14+
import java.nio.charset.StandardCharsets;
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
20+
21+
public class EncoderDecoder {
22+
23+
private static final String URL = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
24+
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoder.class);
25+
26+
private String encodeValue(String value) {
27+
String encoded = null;
28+
try {
29+
encoded = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
30+
} catch (UnsupportedEncodingException e) {
31+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
32+
}
33+
return encoded;
34+
}
35+
36+
37+
private String decode(String value) {
38+
String decoded = null;
39+
try {
40+
decoded = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
41+
} catch (UnsupportedEncodingException e) {
42+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
43+
}
44+
return decoded;
45+
}
46+
47+
@Test
48+
public void givenURL_whenAnalyze_thenCorrect() throws Exception {
49+
URL url = new URL(URL);
50+
51+
Assert.assertThat(url.getProtocol(), CoreMatchers.is("http"));
52+
Assert.assertThat(url.getHost(), CoreMatchers.is("www.baeldung.com"));
53+
Assert.assertThat(url.getQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253"));
54+
}
55+
56+
@Test
57+
public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
58+
Map<String, String> requestParams = new HashMap<>();
59+
requestParams.put("key1", "value 1");
60+
requestParams.put("key2", "value@!$2");
61+
requestParams.put("key3", "value%3");
62+
63+
String encodedQuery = requestParams.keySet().stream()
64+
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
65+
.collect(Collectors.joining("&"));
66+
String encodedURL = "http://www.baeldung.com?" + encodedQuery;
67+
68+
Assert.assertThat(URL, CoreMatchers.is(encodedURL));
69+
}
70+
71+
@Test
72+
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
73+
URL url = new URL(URL);
74+
String query = url.getQuery();
75+
76+
String decodedQuery = Arrays.stream(query.split("&"))
77+
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
78+
.collect(Collectors.joining("&"));
79+
80+
Assert.assertEquals(
81+
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)