Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
+46
-1
@@ -97,6 +97,14 @@
|
||||
<version>8.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dbdoclet</groupId>
|
||||
<artifactId>herold</artifactId>
|
||||
<version>6.1.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/src/test/resources/jars/herold.jar</systemPath>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.jtidy</groupId>
|
||||
<artifactId>jtidy</artifactId>
|
||||
@@ -132,7 +140,8 @@
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
@@ -141,6 +150,42 @@
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.tidy.Tidy;
|
||||
|
||||
public class ApacheFOPConvertHTMLTest {
|
||||
public class ApacheFOPConvertHTMLIntegrationTest {
|
||||
private String inputFile = "src/test/resources/input.html";
|
||||
private String style = "src/test/resources/xhtml2fo.xsl";
|
||||
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
||||
+1
-1
@@ -30,7 +30,7 @@ import org.dbdoclet.trafo.script.Script;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class ApacheFOPHeroldTest {
|
||||
public class ApacheFOPHeroldLiveTest {
|
||||
private String[] inputUrls = {// @formatter:off
|
||||
"http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
|
||||
"http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
|
||||
@@ -0,0 +1,119 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.lang.Integer.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StreamFeaturesTest {
|
||||
|
||||
public static class TakeAndDropWhileTest {
|
||||
|
||||
public Stream<String> getStreamAfterTakeWhileOperation() {
|
||||
return Stream
|
||||
.iterate("", s -> s + "s")
|
||||
.takeWhile(s -> s.length() < 10);
|
||||
}
|
||||
|
||||
public Stream<String> getStreamAfterDropWhileOperation() {
|
||||
return Stream
|
||||
.iterate("", s -> s + "s")
|
||||
.takeWhile(s -> s.length() < 10)
|
||||
.dropWhile(s -> !s.contains("sssss"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTakeWhileOperation() {
|
||||
List<String> list = getStreamAfterTakeWhileOperation().collect(Collectors.toList());
|
||||
|
||||
assertEquals(10, list.size());
|
||||
|
||||
assertEquals("", list.get(0));
|
||||
assertEquals("ss", list.get(2));
|
||||
assertEquals("sssssssss", list.get(list.size() - 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDropWhileOperation() {
|
||||
List<String> list = getStreamAfterDropWhileOperation().collect(Collectors.toList());
|
||||
|
||||
assertEquals(5, list.size());
|
||||
|
||||
assertEquals("sssss", list.get(0));
|
||||
assertEquals("sssssss", list.get(2));
|
||||
assertEquals("sssssssss", list.get(list.size() - 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class IterateTest {
|
||||
|
||||
private Stream<Integer> getStream() {
|
||||
return Stream.iterate(0, i -> i < 10, i -> i + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterateOperation() {
|
||||
List<Integer> list = getStream().collect(Collectors.toList());
|
||||
|
||||
assertEquals(10, list.size());
|
||||
|
||||
assertEquals(valueOf(0), list.get(0));
|
||||
assertEquals(valueOf(5), list.get(5));
|
||||
assertEquals(valueOf(9), list.get(list.size() - 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class OfNullableTest {
|
||||
|
||||
private List<String> collection = Arrays.asList("A", "B", "C");
|
||||
private Map<String, Integer> map = new HashMap<>() {{
|
||||
put("A", 10);
|
||||
put("C", 30);
|
||||
}};
|
||||
|
||||
private Stream<Integer> getStreamWithOfNullable() {
|
||||
return collection.stream()
|
||||
.flatMap(s -> Stream.ofNullable(map.get(s)));
|
||||
}
|
||||
|
||||
private Stream<Integer> getStream() {
|
||||
return collection.stream()
|
||||
.flatMap(s -> {
|
||||
Integer temp = map.get(s);
|
||||
return temp != null ? Stream.of(temp) : Stream.empty();
|
||||
});
|
||||
}
|
||||
|
||||
private List<Integer> testOfNullableFrom(Stream<Integer> stream) {
|
||||
List<Integer> list = stream.collect(Collectors.toList());
|
||||
|
||||
assertEquals(2, list.size());
|
||||
|
||||
assertEquals(valueOf(10), list.get(0));
|
||||
assertEquals(valueOf(30), list.get(list.size() - 1));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfNullable() {
|
||||
|
||||
assertEquals(
|
||||
testOfNullableFrom(getStream()),
|
||||
testOfNullableFrom(getStreamWithOfNullable())
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public class PrimitiveClass {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private double width;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public abstract class Shape {
|
||||
public abstract double area();
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.equalshashcode.entities;
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.executable;
|
||||
package com.baeldung.executable;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.java.conversion;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import com.baeldung.datetime.UseLocalDateTime;
|
||||
|
||||
public class StringConversion {
|
||||
|
||||
public static int getInt(String str) {
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
|
||||
public static int getInteger(String str) {
|
||||
return Integer.valueOf(str);
|
||||
}
|
||||
|
||||
public static long getLongPrimitive(String str) {
|
||||
return Long.parseLong(str);
|
||||
}
|
||||
|
||||
public static Long getLong(String str) {
|
||||
return Long.valueOf(str);
|
||||
}
|
||||
|
||||
public static double getDouble(String str) {
|
||||
return Double.parseDouble(str);
|
||||
}
|
||||
|
||||
public static double getDoublePrimitive(String str) {
|
||||
return Double.valueOf(str);
|
||||
}
|
||||
|
||||
public static byte[] getByteArray(String str) {
|
||||
return str.getBytes();
|
||||
}
|
||||
|
||||
public static char[] getCharArray(String str) {
|
||||
return str.toCharArray();
|
||||
}
|
||||
|
||||
public static boolean getBooleanPrimitive(String str) {
|
||||
return Boolean.parseBoolean(str);
|
||||
}
|
||||
|
||||
public static boolean getBoolean(String str) {
|
||||
return Boolean.valueOf(str);
|
||||
}
|
||||
|
||||
public static Date getJava6Date(String str, String format) throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
return formatter.parse(str);
|
||||
}
|
||||
|
||||
public static LocalDateTime getJava8Date(String str) throws ParseException {
|
||||
return new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class EchoClient {
|
||||
public String sendEcho(String msg) {
|
||||
DatagramPacket packet = null;
|
||||
try {
|
||||
buf=msg.getBytes();
|
||||
buf = msg.getBytes();
|
||||
packet = new DatagramPacket(buf, buf.length, address, 4445);
|
||||
socket.send(packet);
|
||||
packet = new DatagramPacket(buf, buf.length);
|
||||
|
||||
@@ -17,6 +17,11 @@ public class EchoClient {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void stop() throws IOException {
|
||||
client.close();
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
private EchoClient() {
|
||||
try {
|
||||
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
|
||||
@@ -42,5 +47,4 @@ public class EchoClient {
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
package com.baeldung.java.nio.selector;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.Iterator;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
public class EchoServer {
|
||||
|
||||
public static void main(String[] args)
|
||||
|
||||
throws IOException {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Selector selector = Selector.open();
|
||||
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
||||
serverSocket.bind(new InetSocketAddress("localhost", 5454));
|
||||
@@ -49,7 +47,6 @@ public class EchoServer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Process start() throws IOException, InterruptedException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||
|
||||
@@ -8,12 +8,12 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalDateTimeUnitTest {
|
||||
|
||||
|
||||
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
|
||||
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalDateTime(){
|
||||
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
|
||||
Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
|
||||
public void givenString_whenUsingParse_thenLocalDateTime() {
|
||||
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
|
||||
Assert.assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,47 +8,47 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalDateUnitTest {
|
||||
|
||||
|
||||
UseLocalDate useLocalDate = new UseLocalDate();
|
||||
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalDate(){
|
||||
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString());
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalDate() {
|
||||
Assert.assertEquals("2016-05-10", useLocalDate.getLocalDateUsingFactoryOfMethod(2016, 5, 10).toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalDate(){
|
||||
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
|
||||
public void givenString_whenUsingParse_thenLocalDate() {
|
||||
Assert.assertEquals("2016-05-10", useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenUsingClock_thenLocalDate(){
|
||||
Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock());
|
||||
public void whenUsingClock_thenLocalDate() {
|
||||
Assert.assertEquals(LocalDate.now(), useLocalDate.getLocalDateFromClock());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingPlus_thenNextDay(){
|
||||
Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now()));
|
||||
public void givenDate_whenUsingPlus_thenNextDay() {
|
||||
Assert.assertEquals(LocalDate.now().plusDays(1), useLocalDate.getNextDay(LocalDate.now()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingMinus_thenPreviousDay(){
|
||||
Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now()));
|
||||
public void givenDate_whenUsingMinus_thenPreviousDay() {
|
||||
Assert.assertEquals(LocalDate.now().minusDays(1), useLocalDate.getPreviousDay(LocalDate.now()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){
|
||||
Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
|
||||
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek() {
|
||||
Assert.assertEquals(DayOfWeek.SUNDAY, useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){
|
||||
Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth());
|
||||
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth() {
|
||||
Assert.assertEquals(1, useLocalDate.getFirstDayOfMonth().getDayOfMonth());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){
|
||||
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
|
||||
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight() {
|
||||
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,31 +6,31 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseLocalTimeUnitTest {
|
||||
|
||||
|
||||
UseLocalTime useLocalTime = new UseLocalTime();
|
||||
|
||||
|
||||
@Test
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalTime(){
|
||||
Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString());
|
||||
public void givenValues_whenUsingFactoryOf_thenLocalTime() {
|
||||
Assert.assertEquals("07:07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7, 7).toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingParse_thenLocalTime(){
|
||||
Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
|
||||
public void givenString_whenUsingParse_thenLocalTime() {
|
||||
Assert.assertEquals("06:30", useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTime_whenAddHour_thenLocalTime(){
|
||||
Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString());
|
||||
public void givenTime_whenAddHour_thenLocalTime() {
|
||||
Assert.assertEquals("07:30", useLocalTime.addAnHour(LocalTime.of(6, 30)).toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getHourFromLocalTime(){
|
||||
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1)));
|
||||
public void getHourFromLocalTime() {
|
||||
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1, 1)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getLocalTimeWithMinuteSetToValue(){
|
||||
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20));
|
||||
public void getLocalTimeWithMinuteSetToValue() {
|
||||
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10, 10), 20));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,20 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UsePeriodUnitTest {
|
||||
UsePeriod usingPeriod=new UsePeriod();
|
||||
|
||||
UsePeriod usingPeriod = new UsePeriod();
|
||||
|
||||
@Test
|
||||
public void givenPeriodAndLocalDate_thenCalculateModifiedDate(){
|
||||
public void givenPeriodAndLocalDate_thenCalculateModifiedDate() {
|
||||
Period period = Period.ofDays(1);
|
||||
LocalDate localDate = LocalDate.parse("2007-05-10");
|
||||
Assert.assertEquals(localDate.plusDays(1),usingPeriod.modifyDates(localDate, period));
|
||||
Assert.assertEquals(localDate.plusDays(1), usingPeriod.modifyDates(localDate, period));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenDates_thenGetPeriod(){
|
||||
public void givenDates_thenGetPeriod() {
|
||||
LocalDate localDate1 = LocalDate.parse("2007-05-10");
|
||||
LocalDate localDate2 = LocalDate.parse("2007-05-15");
|
||||
|
||||
|
||||
Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseZonedDateTimeUnitTest {
|
||||
|
||||
UseZonedDateTime zonedDateTime=new UseZonedDateTime();
|
||||
|
||||
|
||||
UseZonedDateTime zonedDateTime = new UseZonedDateTime();
|
||||
|
||||
@Test
|
||||
public void givenZoneId_thenZonedDateTime(){
|
||||
ZoneId zoneId=ZoneId.of("Europe/Paris");
|
||||
ZonedDateTime zonedDatetime=zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
|
||||
Assert.assertEquals(zoneId,ZoneId.from(zonedDatetime));
|
||||
public void givenZoneId_thenZonedDateTime() {
|
||||
ZoneId zoneId = ZoneId.of("Europe/Paris");
|
||||
ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
|
||||
Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ public class EncoderDecoderUnitTest {
|
||||
return encoded;
|
||||
}
|
||||
|
||||
|
||||
private String decode(String value) {
|
||||
String decoded = null;
|
||||
try {
|
||||
@@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest {
|
||||
requestParams.put("key2", "value@!$2");
|
||||
requestParams.put("key3", "value%3");
|
||||
|
||||
String encodedURL = requestParams.keySet().stream()
|
||||
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
|
||||
.collect(joining("&", "http://www.baeldung.com?", ""));
|
||||
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
|
||||
|
||||
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
|
||||
}
|
||||
@@ -72,12 +69,9 @@ public class EncoderDecoderUnitTest {
|
||||
|
||||
String query = url.getQuery();
|
||||
|
||||
String decodedQuery = Arrays.stream(query.split("&"))
|
||||
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
|
||||
.collect(joining("&"));
|
||||
String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&"));
|
||||
|
||||
Assert.assertEquals(
|
||||
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
|
||||
Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.hexToAscii;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class HexToAscii {
|
||||
|
||||
@Test
|
||||
public static void whenHexToAscii() {
|
||||
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
|
||||
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
|
||||
|
||||
assertEquals(asciiString, hexToAscii(hexEquivalent));
|
||||
}
|
||||
|
||||
@Test
|
||||
public static void whenAsciiToHex() {
|
||||
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
|
||||
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
|
||||
|
||||
assertEquals(hexEquivalent, asciiToHex(asciiString));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private static String asciiToHex(String asciiStr) {
|
||||
char[] chars = asciiStr.toCharArray();
|
||||
StringBuilder hex = new StringBuilder();
|
||||
for (char ch : chars) {
|
||||
hex.append(Integer.toHexString((int) ch));
|
||||
}
|
||||
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
private static String hexToAscii(String hexStr) {
|
||||
StringBuilder output = new StringBuilder("");
|
||||
for (int i = 0; i < hexStr.length(); i += 2) {
|
||||
String str = hexStr.substring(i, i + 2);
|
||||
output.append((char) Integer.parseInt(str, 16));
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.java.networking.interfaces;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NetworkInterfaceManualTest {
|
||||
@Test
|
||||
public void givenName_whenReturnsNetworkInterface_thenCorrect() throws SocketException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInExistentName_whenReturnsNull_thenCorrect() throws SocketException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("inexistent_name");
|
||||
assertNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIP_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
|
||||
byte[] ip = new byte[] { 127, 0, 0, 1 };
|
||||
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByAddress(ip));
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHostName_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost"));
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalHost_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLoopBack_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress());
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIndex_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByIndex(0);
|
||||
assertNotNull(nif);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenReturnsInetAddresses_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
Enumeration<InetAddress> addressEnum = nif.getInetAddresses();
|
||||
InetAddress address = addressEnum.nextElement();
|
||||
assertEquals("127.0.0.1", address.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenReturnsInterfaceAddresses_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
|
||||
List<InterfaceAddress> addressEnum = nif.getInterfaceAddresses();
|
||||
InterfaceAddress address = addressEnum.get(0);
|
||||
InetAddress localAddress = address.getAddress();
|
||||
InetAddress broadCastAddress = address.getBroadcast();
|
||||
assertEquals("127.0.0.1", localAddress.getHostAddress());
|
||||
assertEquals("127.255.255.255", broadCastAddress.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenChecksIfLoopback_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertTrue(nif.isLoopback());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenChecksIfUp_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertTrue(nif.isUp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenChecksIfPointToPoint_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertFalse(nif.isPointToPoint());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenChecksIfVirtual_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertFalse(nif.isVirtual());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenChecksMulticastSupport_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
assertTrue(nif.supportsMulticast());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenGetsMacAddress_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("lo");
|
||||
byte[] bytes = nif.getHardwareAddress();
|
||||
assertNotNull(bytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInterface_whenGetsMTU_thenCorrect() throws SocketException, UnknownHostException {
|
||||
NetworkInterface nif = NetworkInterface.getByName("net0");
|
||||
int mtu = nif.getMTU();
|
||||
assertEquals(1500, mtu);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.java.networking.udp;
|
||||
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
package com.baeldung.java.networking.url;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UrlTest {
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com");
|
||||
assertEquals("http", url.getProtocol());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com");
|
||||
assertEquals("baeldung.com", url.getHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
|
||||
assertEquals("/articles?topic=java&version=8", url.getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com/guidelines.txt");
|
||||
assertEquals("/guidelines.txt", url.getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
|
||||
assertEquals("/articles", url.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com/articles?topic=java");
|
||||
assertEquals("topic=java", url.getQuery());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com");
|
||||
assertEquals(-1, url.getPort());
|
||||
assertEquals(80, url.getDefaultPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException {
|
||||
URL url = new URL("http://baeldung.com:8090");
|
||||
assertEquals(8090, url.getPort());
|
||||
assertEquals(80, url.getDefaultPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException {
|
||||
URL baseUrl = new URL("http://baeldung.com");
|
||||
URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets");
|
||||
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException {
|
||||
URL baseUrl = new URL("http://baeldung.com");
|
||||
URL relativeUrl = new URL(baseUrl, "http://baeldung.com/a-guide-to-java-sockets");
|
||||
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
|
||||
String protocol = "http";
|
||||
String host = "baeldung.com";
|
||||
String file = "/guidelines.txt";
|
||||
URL url = new URL(protocol, host, file);
|
||||
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException {
|
||||
String protocol = "http";
|
||||
String host = "baeldung.com";
|
||||
String file = "/articles?topic=java&version=8";
|
||||
URL url = new URL(protocol, host, file);
|
||||
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
|
||||
String protocol = "http";
|
||||
String host = "baeldung.com";
|
||||
int port = 9000;
|
||||
String file = "/guidelines.txt";
|
||||
URL url = new URL(protocol, host, port, file);
|
||||
assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,19 +4,32 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NioEchoIntegrationTest {
|
||||
|
||||
Process server;
|
||||
EchoClient client;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException, InterruptedException {
|
||||
server = EchoServer.start();
|
||||
client = EchoClient.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient_whenServerEchosMessage_thenCorrect() throws IOException, InterruptedException {
|
||||
Process process = EchoServer.start();
|
||||
EchoClient client = EchoClient.start();
|
||||
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
}
|
||||
|
||||
process.destroy();
|
||||
@After
|
||||
public void teardown() throws IOException {
|
||||
server.destroy();
|
||||
EchoClient.stop();
|
||||
}
|
||||
}
|
||||
|
||||
+58
-76
@@ -11,54 +11,54 @@ import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileUnitTest {
|
||||
public class FileTest {
|
||||
private static final String HOME = System.getProperty("user.home");
|
||||
|
||||
// checking file or dir
|
||||
@Test
|
||||
public void givenExistentPath_whenConfirmsFileExists_thenCorrect() {
|
||||
final Path p = Paths.get(HOME);
|
||||
Path p = Paths.get(HOME);
|
||||
assertTrue(Files.exists(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() {
|
||||
final Path p = Paths.get(HOME + "/inexistent_file.txt");
|
||||
Path p = Paths.get(HOME + "/inexistent_file.txt");
|
||||
assertTrue(Files.notExists(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() {
|
||||
final Path p = Paths.get(HOME);
|
||||
public void givenDirPath_whenConfirmsNotRegularFile_thenCorrect() {
|
||||
Path p = Paths.get(HOME);
|
||||
assertFalse(Files.isRegularFile(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() {
|
||||
final Path p = Paths.get(HOME);
|
||||
Path p = Paths.get(HOME);
|
||||
assertTrue(Files.isReadable(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() {
|
||||
final Path p = Paths.get(HOME);
|
||||
Path p = Paths.get(HOME);
|
||||
assertTrue(Files.isWritable(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() {
|
||||
final Path p = Paths.get(HOME);
|
||||
Path p = Paths.get(HOME);
|
||||
assertTrue(Files.isExecutable(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException {
|
||||
final Path p1 = Paths.get(HOME);
|
||||
final Path p2 = Paths.get(HOME);
|
||||
Path p1 = Paths.get(HOME);
|
||||
Path p2 = Paths.get(HOME);
|
||||
assertTrue(Files.isSameFile(p1, p2));
|
||||
}
|
||||
|
||||
@@ -66,8 +66,8 @@ public class FileUnitTest {
|
||||
// creating file
|
||||
@Test
|
||||
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
|
||||
final String fileName = "myfile_" + new Date().getTime() + ".txt";
|
||||
final Path p = Paths.get(HOME + "/" + fileName);
|
||||
String fileName = "myfile_" + UUID.randomUUID().toString() + ".txt";
|
||||
Path p = Paths.get(HOME + "/" + fileName);
|
||||
assertFalse(Files.exists(p));
|
||||
Files.createFile(p);
|
||||
assertTrue(Files.exists(p));
|
||||
@@ -76,8 +76,8 @@ public class FileUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
|
||||
final String dirName = "myDir_" + new Date().getTime();
|
||||
final Path p = Paths.get(HOME + "/" + dirName);
|
||||
String dirName = "myDir_" + UUID.randomUUID().toString();
|
||||
Path p = Paths.get(HOME + "/" + dirName);
|
||||
assertFalse(Files.exists(p));
|
||||
Files.createDirectory(p);
|
||||
assertTrue(Files.exists(p));
|
||||
@@ -86,22 +86,19 @@ public class FileUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() {
|
||||
final String dirName = "myDir_" + new Date().getTime() + "/subdir";
|
||||
final Path p = Paths.get(HOME + "/" + dirName);
|
||||
@Test(expected = NoSuchFileException.class)
|
||||
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
|
||||
String dirName = "myDir_" + UUID.randomUUID().toString() + "/subdir";
|
||||
Path p = Paths.get(HOME + "/" + dirName);
|
||||
assertFalse(Files.exists(p));
|
||||
try {
|
||||
Files.createDirectory(p);
|
||||
} catch (final IOException e) {
|
||||
assertTrue(e instanceof NoSuchFileException);
|
||||
}
|
||||
Files.createDirectory(p);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
|
||||
final Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime());
|
||||
final Path subdir = dir.resolve("subdir");
|
||||
Path dir = Paths.get(HOME + "/myDir_" + UUID.randomUUID().toString());
|
||||
Path subdir = dir.resolve("subdir");
|
||||
assertFalse(Files.exists(dir));
|
||||
assertFalse(Files.exists(subdir));
|
||||
Files.createDirectories(subdir);
|
||||
@@ -111,8 +108,8 @@ public class FileUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
|
||||
final String prefix = "log_";
|
||||
final String suffix = ".txt";
|
||||
String prefix = "log_";
|
||||
String suffix = ".txt";
|
||||
Path p = Paths.get(HOME + "/");
|
||||
p = Files.createTempFile(p, prefix, suffix);
|
||||
// like log_8821081429012075286.txt
|
||||
@@ -121,17 +118,16 @@ public class FileUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() throws IOException {
|
||||
public void givenPath_whenCreatesTempFileWithDefaults_thenCorrect() throws IOException {
|
||||
Path p = Paths.get(HOME + "/");
|
||||
p = Files.createTempFile(p, null, null);
|
||||
// like 8600179353689423985.tmp
|
||||
System.out.println(p);
|
||||
assertTrue(Files.exists(p));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException {
|
||||
final Path p = Files.createTempFile(null, null);
|
||||
Path p = Files.createTempFile(null, null);
|
||||
// like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp
|
||||
assertTrue(Files.exists(p));
|
||||
|
||||
@@ -140,7 +136,7 @@ public class FileUnitTest {
|
||||
// delete file
|
||||
@Test
|
||||
public void givenPath_whenDeletes_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get(HOME + "/fileToDelete.txt");
|
||||
Path p = Paths.get(HOME + "/fileToDelete.txt");
|
||||
assertFalse(Files.exists(p));
|
||||
Files.createFile(p);
|
||||
assertTrue(Files.exists(p));
|
||||
@@ -149,37 +145,30 @@ public class FileUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = DirectoryNotEmptyException.class)
|
||||
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
|
||||
final Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime());
|
||||
Path dir = Paths.get(HOME + "/emptyDir" + UUID.randomUUID().toString());
|
||||
Files.createDirectory(dir);
|
||||
assertTrue(Files.exists(dir));
|
||||
final Path file = dir.resolve("file.txt");
|
||||
Path file = dir.resolve("file.txt");
|
||||
Files.createFile(file);
|
||||
try {
|
||||
Files.delete(dir);
|
||||
} catch (final IOException e) {
|
||||
assertTrue(e instanceof DirectoryNotEmptyException);
|
||||
}
|
||||
Files.delete(dir);
|
||||
|
||||
assertTrue(Files.exists(dir));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = NoSuchFileException.class)
|
||||
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get(HOME + "/inexistentFile.txt");
|
||||
Path p = Paths.get(HOME + "/inexistentFile.txt");
|
||||
assertFalse(Files.exists(p));
|
||||
try {
|
||||
Files.delete(p);
|
||||
} catch (final IOException e) {
|
||||
assertTrue(e instanceof NoSuchFileException);
|
||||
}
|
||||
Files.delete(p);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get(HOME + "/inexistentFile.txt");
|
||||
Path p = Paths.get(HOME + "/inexistentFile.txt");
|
||||
assertFalse(Files.exists(p));
|
||||
Files.deleteIfExists(p);
|
||||
|
||||
@@ -188,12 +177,12 @@ public class FileUnitTest {
|
||||
// copy file
|
||||
@Test
|
||||
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
|
||||
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
|
||||
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
|
||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
||||
Files.createDirectory(dir1);
|
||||
Files.createDirectory(dir2);
|
||||
final Path file1 = dir1.resolve("filetocopy.txt");
|
||||
final Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Path file1 = dir1.resolve("filetocopy.txt");
|
||||
Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Files.createFile(file1);
|
||||
assertTrue(Files.exists(file1));
|
||||
assertFalse(Files.exists(file2));
|
||||
@@ -202,35 +191,31 @@ public class FileUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = FileAlreadyExistsException.class)
|
||||
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
|
||||
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
|
||||
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
|
||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
||||
Files.createDirectory(dir1);
|
||||
Files.createDirectory(dir2);
|
||||
final Path file1 = dir1.resolve("filetocopy.txt");
|
||||
final Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Path file1 = dir1.resolve("filetocopy.txt");
|
||||
Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Files.createFile(file1);
|
||||
Files.createFile(file2);
|
||||
assertTrue(Files.exists(file1));
|
||||
assertTrue(Files.exists(file2));
|
||||
try {
|
||||
Files.copy(file1, file2);
|
||||
} catch (final IOException e) {
|
||||
assertTrue(e instanceof FileAlreadyExistsException);
|
||||
}
|
||||
Files.copy(file1, file2);
|
||||
Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
// moving files
|
||||
@Test
|
||||
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
|
||||
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
|
||||
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
|
||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
||||
Files.createDirectory(dir1);
|
||||
Files.createDirectory(dir2);
|
||||
final Path file1 = dir1.resolve("filetocopy.txt");
|
||||
final Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Path file1 = dir1.resolve("filetocopy.txt");
|
||||
Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Files.createFile(file1);
|
||||
assertTrue(Files.exists(file1));
|
||||
assertFalse(Files.exists(file2));
|
||||
@@ -240,25 +225,22 @@ public class FileUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = FileAlreadyExistsException.class)
|
||||
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
|
||||
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
|
||||
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
|
||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
||||
Files.createDirectory(dir1);
|
||||
Files.createDirectory(dir2);
|
||||
final Path file1 = dir1.resolve("filetocopy.txt");
|
||||
final Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Path file1 = dir1.resolve("filetocopy.txt");
|
||||
Path file2 = dir2.resolve("filetocopy.txt");
|
||||
Files.createFile(file1);
|
||||
Files.createFile(file2);
|
||||
assertTrue(Files.exists(file1));
|
||||
assertTrue(Files.exists(file2));
|
||||
try {
|
||||
Files.move(file1, file2);
|
||||
} catch (final IOException e) {
|
||||
assertTrue(e instanceof FileAlreadyExistsException);
|
||||
}
|
||||
Files.move(file1, file2);
|
||||
Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING);
|
||||
assertTrue(Files.exists(file2));
|
||||
assertFalse(Files.exists(file1));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,8 +6,10 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -18,14 +20,14 @@ public class PathManualTest {
|
||||
// creating a path
|
||||
@Test
|
||||
public void givenPathString_whenCreatesPathObject_thenCorrect() {
|
||||
final Path p = Paths.get("/articles/baeldung");
|
||||
Path p = Paths.get("/articles/baeldung");
|
||||
assertEquals("\\articles\\baeldung", p.toString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathParts_whenCreatesPathObject_thenCorrect() {
|
||||
final Path p = Paths.get("/articles", "baeldung");
|
||||
Path p = Paths.get("/articles", "baeldung");
|
||||
assertEquals("\\articles\\baeldung", p.toString());
|
||||
|
||||
}
|
||||
@@ -33,13 +35,13 @@ public class PathManualTest {
|
||||
// retrieving path info
|
||||
@Test
|
||||
public void givenPath_whenRetrievesFileName_thenCorrect() {
|
||||
final Path p = Paths.get("/articles/baeldung/logs");
|
||||
Path p = Paths.get("/articles/baeldung/logs");
|
||||
assertEquals("logs", p.getFileName().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenRetrievesNameByIndex_thenCorrect() {
|
||||
final Path p = Paths.get("/articles/baeldung/logs");
|
||||
Path p = Paths.get("/articles/baeldung/logs");
|
||||
assertEquals("articles", p.getName(0).toString());
|
||||
assertEquals("baeldung", p.getName(1).toString());
|
||||
assertEquals("logs", p.getName(2).toString());
|
||||
@@ -47,13 +49,13 @@ public class PathManualTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenCountsParts_thenCorrect() {
|
||||
final Path p = Paths.get("/articles/baeldung/logs");
|
||||
Path p = Paths.get("/articles/baeldung/logs");
|
||||
assertEquals(3, p.getNameCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenCanRetrieveSubsequenceByIndex_thenCorrect() {
|
||||
final Path p = Paths.get("/articles/baeldung/logs");
|
||||
Path p = Paths.get("/articles/baeldung/logs");
|
||||
assertEquals("articles", p.subpath(0, 1).toString());
|
||||
assertEquals("articles\\baeldung", p.subpath(0, 2).toString());
|
||||
assertEquals("articles\\baeldung\\logs", p.subpath(0, 3).toString());
|
||||
@@ -64,10 +66,10 @@ public class PathManualTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenRetrievesParent_thenCorrect() {
|
||||
final Path p1 = Paths.get("/articles/baeldung/logs");
|
||||
final Path p2 = Paths.get("/articles/baeldung");
|
||||
final Path p3 = Paths.get("/articles");
|
||||
final Path p4 = Paths.get("/");
|
||||
Path p1 = Paths.get("/articles/baeldung/logs");
|
||||
Path p2 = Paths.get("/articles/baeldung");
|
||||
Path p3 = Paths.get("/articles");
|
||||
Path p4 = Paths.get("/");
|
||||
|
||||
assertEquals("\\articles\\baeldung", p1.getParent().toString());
|
||||
assertEquals("\\articles", p2.getParent().toString());
|
||||
@@ -77,8 +79,8 @@ public class PathManualTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenRetrievesRoot_thenCorrect() {
|
||||
final Path p1 = Paths.get("/articles/baeldung/logs");
|
||||
final Path p2 = Paths.get("c:/articles/baeldung/logs");
|
||||
Path p1 = Paths.get("/articles/baeldung/logs");
|
||||
Path p2 = Paths.get("c:/articles/baeldung/logs");
|
||||
|
||||
assertEquals("\\", p1.getRoot().toString());
|
||||
assertEquals("c:\\", p2.getRoot().toString());
|
||||
@@ -102,71 +104,68 @@ public class PathManualTest {
|
||||
// converting a path
|
||||
@Test
|
||||
public void givenPath_whenConvertsToBrowseablePath_thenCorrect() {
|
||||
final Path p = Paths.get("/home/baeldung/articles.html");
|
||||
final URI uri = p.toUri();
|
||||
Path p = Paths.get("/home/baeldung/articles.html");
|
||||
URI uri = p.toUri();
|
||||
assertEquals("file:///E:/home/baeldung/articles.html", uri.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenConvertsToAbsolutePath_thenCorrect() {
|
||||
final Path p = Paths.get("/home/baeldung/articles.html");
|
||||
Path p = Paths.get("/home/baeldung/articles.html");
|
||||
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePath_whenRetainsAsAbsolute_thenCorrect() {
|
||||
final Path p = Paths.get("E:\\home\\baeldung\\articles.html");
|
||||
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
|
||||
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingPath_whenGetsRealPathToFile_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get(HOME);
|
||||
Path p = Paths.get(HOME);
|
||||
assertEquals(HOME, p.toRealPath().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInExistentPath_whenFailsToConvert_thenCorrect() {
|
||||
final Path p = Paths.get("E:\\home\\baeldung\\articles.html");
|
||||
try {
|
||||
p.toRealPath();
|
||||
} catch (final IOException e) {
|
||||
assertTrue(true);
|
||||
}
|
||||
@Test(expected = NoSuchFileException.class)
|
||||
public void givenInExistentPath_whenFailsToConvert_thenCorrect() throws IOException {
|
||||
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
|
||||
|
||||
p.toRealPath();
|
||||
}
|
||||
|
||||
// joining paths
|
||||
@Test
|
||||
public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get("/baeldung/articles");
|
||||
Path p = Paths.get("/baeldung/articles");
|
||||
assertEquals("\\baeldung\\articles\\java", p.resolve("java").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePath_whenResolutionRetainsIt_thenCorrect() throws IOException {
|
||||
final Path p = Paths.get("/baeldung/articles");
|
||||
Path p = Paths.get("/baeldung/articles");
|
||||
assertEquals("C:\\baeldung\\articles\\java", p.resolve("C:\\baeldung\\articles\\java").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathWithRoot_whenResolutionRetainsIt_thenCorrect2() throws IOException {
|
||||
final Path p = Paths.get("/baeldung/articles");
|
||||
Path p = Paths.get("/baeldung/articles");
|
||||
assertEquals("\\java", p.resolve("/java").toString());
|
||||
}
|
||||
|
||||
// creating a path between 2 paths
|
||||
@Test
|
||||
public void givenSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
|
||||
final Path p1 = Paths.get("articles");
|
||||
final Path p2 = Paths.get("authors");
|
||||
Path p1 = Paths.get("articles");
|
||||
Path p2 = Paths.get("authors");
|
||||
assertEquals("..\\authors", p1.relativize(p2).toString());
|
||||
assertEquals("..\\articles", p2.relativize(p1).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
|
||||
final Path p1 = Paths.get("/baeldung");
|
||||
final Path p2 = Paths.get("/baeldung/authors/articles");
|
||||
Path p1 = Paths.get("/baeldung");
|
||||
Path p2 = Paths.get("/baeldung/authors/articles");
|
||||
assertEquals("authors\\articles", p1.relativize(p2).toString());
|
||||
assertEquals("..\\..", p2.relativize(p1).toString());
|
||||
}
|
||||
@@ -174,9 +173,9 @@ public class PathManualTest {
|
||||
// comparing 2 paths
|
||||
@Test
|
||||
public void givenTwoPaths_whenTestsEquality_thenCorrect() throws IOException {
|
||||
final Path p1 = Paths.get("/baeldung/articles");
|
||||
final Path p2 = Paths.get("/baeldung/articles");
|
||||
final Path p3 = Paths.get("/baeldung/authors");
|
||||
Path p1 = Paths.get("/baeldung/articles");
|
||||
Path p2 = Paths.get("/baeldung/articles");
|
||||
Path p3 = Paths.get("/baeldung/authors");
|
||||
|
||||
assertTrue(p1.equals(p2));
|
||||
assertFalse(p1.equals(p3));
|
||||
@@ -184,13 +183,13 @@ public class PathManualTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenInspectsStart_thenCorrect() {
|
||||
final Path p1 = Paths.get("/baeldung/articles");
|
||||
Path p1 = Paths.get("/baeldung/articles");
|
||||
assertTrue(p1.startsWith("/baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenInspectsEnd_thenCorrect() {
|
||||
final Path p1 = Paths.get("/baeldung/articles");
|
||||
Path p1 = Paths.get("/baeldung/articles");
|
||||
assertTrue(p1.endsWith("articles"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Java8ForEachTest {
|
||||
|
||||
@Test
|
||||
public void compareForEachMethods_thenPrintResults() {
|
||||
|
||||
List<String> names = new ArrayList<>();
|
||||
names.add("Larry");
|
||||
names.add("Steve");
|
||||
names.add("James");
|
||||
names.add("Conan");
|
||||
names.add("Ellen");
|
||||
|
||||
// Java 5 - for-loop
|
||||
System.out.println("--- Enhanced for-loop ---");
|
||||
for (String name : names) {
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
// Java 8 - forEach
|
||||
System.out.println("--- forEach method ---");
|
||||
names.forEach(name -> System.out.println(name));
|
||||
|
||||
// Anonymous inner class that implements Consumer interface
|
||||
System.out.println("--- Anonymous inner class ---");
|
||||
names.forEach(new Consumer<String>() {
|
||||
public void accept(String name) {
|
||||
System.out.println(name);
|
||||
}
|
||||
});
|
||||
|
||||
// Create a Consumer implementation to then use in a forEach method
|
||||
Consumer<String> consumerNames = name -> {
|
||||
System.out.println(name);
|
||||
};
|
||||
System.out.println("--- Implementation of Consumer interface ---");
|
||||
names.forEach(consumerNames);
|
||||
|
||||
// Print elements using a Method Reference
|
||||
System.out.println("--- Method Reference ---");
|
||||
names.forEach(System.out::println);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user