diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/daemon/NewThread.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/daemon/NewThread.java index d742d3a55f..4d87978070 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/daemon/NewThread.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/daemon/NewThread.java @@ -3,7 +3,6 @@ package com.baeldung.concurrent.daemon; public class NewThread extends Thread { public void run() { - long startTime = System.currentTimeMillis(); while (true) { for (int i = 0; i < 10; i++) { diff --git a/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java b/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java new file mode 100644 index 0000000000..a9911538b0 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java @@ -0,0 +1,11 @@ +package com.baeldung.nestedclass; + +public class Enclosing { + + public static class Nested { + + public void test() { + System.out.println("Calling test..."); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java b/core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java new file mode 100644 index 0000000000..c7e04e8600 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nestedclass/NewEnclosing.java @@ -0,0 +1,15 @@ +package com.baeldung.nestedclass; + +public class NewEnclosing { + + void run() { + class Local { + + void run() { + System.out.println("Welcome to Baeldung!"); + } + } + Local local = new Local(); + local.run(); + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/nestedclass/Outer.java b/core-java/src/main/java/com/baeldung/nestedclass/Outer.java new file mode 100644 index 0000000000..ebd6d27293 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nestedclass/Outer.java @@ -0,0 +1,11 @@ +package com.baeldung.nestedclass; + +public class Outer { + + public class Inner { + + public void test() { + System.out.println("Calling test..."); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java b/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java new file mode 100644 index 0000000000..586e2d12b4 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java @@ -0,0 +1,5 @@ +package com.baeldung.nestedclass; + +abstract class SimpleAbstractClass { + abstract void run(); +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/util/StreamUtils.java b/core-java/src/main/java/com/baeldung/util/StreamUtils.java new file mode 100644 index 0000000000..42f438732f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/util/StreamUtils.java @@ -0,0 +1,16 @@ +package com.baeldung.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +import org.apache.commons.io.IOUtils; + +public class StreamUtils { + + public static String getStringFromInputStream(InputStream input) throws IOException { + StringWriter writer = new StringWriter(); + IOUtils.copy(input, writer, "UTF-8"); + return writer.toString(); + } +} diff --git a/core-java/src/main/resources/countries.properties b/core-java/src/main/resources/countries.properties new file mode 100644 index 0000000000..3c1f53aded --- /dev/null +++ b/core-java/src/main/resources/countries.properties @@ -0,0 +1,3 @@ +UK +US +Germany diff --git a/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java b/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java new file mode 100644 index 0000000000..451c1b4c4d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java @@ -0,0 +1,36 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileOutputStreamTest { + + public static final String fileName = "src/main/resources/countries.properties"; + + @Test + public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { + FileOutputStream fos = new FileOutputStream(fileName, true); + fos.write("Spain\r\n".getBytes()); + fos.close(); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java b/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java new file mode 100644 index 0000000000..9ee8726575 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java @@ -0,0 +1,37 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileUtilsTest { + + public static final String fileName = "src/main/resources/countries.properties"; + + @Test + public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { + File file = new File(fileName); + FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/file/FileWriterTest.java b/core-java/src/test/java/com/baeldung/file/FileWriterTest.java new file mode 100644 index 0000000000..8d2ce4310e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/file/FileWriterTest.java @@ -0,0 +1,40 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.BufferedWriter; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FileWriterTest { + + public static final String fileName = "src/main/resources/countries.properties"; + + @Test + public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { + FileWriter fw = new FileWriter(fileName, true); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("Spain"); + bw.newLine(); + bw.close(); + + assertThat( + StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/file/FilesTest.java b/core-java/src/test/java/com/baeldung/file/FilesTest.java new file mode 100644 index 0000000000..bd39d004d3 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/file/FilesTest.java @@ -0,0 +1,36 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + +import org.junit.After; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; + +public class FilesTest { + + public static final String fileName = "src/main/resources/countries.properties"; + + @Test + public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { + Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/file/GuavaTest.java b/core-java/src/test/java/com/baeldung/file/GuavaTest.java new file mode 100644 index 0000000000..5a7ec6c4a8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/file/GuavaTest.java @@ -0,0 +1,48 @@ +package com.baeldung.file; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.util.StreamUtils; +import com.google.common.base.Charsets; +import com.google.common.io.CharSink; +import com.google.common.io.FileWriteMode; +import com.google.common.io.Files; + +public class GuavaTest { + + public static final String fileName = "src/main/resources/countries.properties"; + + @Before + public void setup() throws Exception { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } + + @Test + public void whenAppendToFileUsingGuava_thenCorrect() throws IOException { + File file = new File(fileName); + CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); + chs.write("Spain\r\n"); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @After + public void revertFile() throws IOException { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java b/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java new file mode 100644 index 0000000000..394c0bb57a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/AnonymousInnerTest.java @@ -0,0 +1,16 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class AnonymousInnerTest { + + @Test + public void whenRunAnonymousClass_thenCorrect() { + SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() { + void run() { + System.out.println("Running Anonymous Class..."); + } + }; + simpleAbstractClass.run(); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java new file mode 100644 index 0000000000..e9cb119ac2 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/InnerClassTest.java @@ -0,0 +1,13 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class InnerClassTest { + + @Test + public void givenInnerClassWhenInstantiating_thenCorrect() { + Outer outer = new Outer(); + Outer.Inner inner = outer.new Inner(); + inner.test(); + } +} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java new file mode 100644 index 0000000000..dad19161ad --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/LocalClassTest.java @@ -0,0 +1,12 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class LocalClassTest { + + @Test + public void whenTestingLocalClass_thenCorrect() { + NewEnclosing newEnclosing = new NewEnclosing(); + newEnclosing.run(); + } +} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java b/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java new file mode 100644 index 0000000000..16c883689a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/NestedClassTest.java @@ -0,0 +1,12 @@ +package com.baeldung.nestedclass; + +import org.junit.Test; + +public class NestedClassTest { + + @Test + public void whenInstantiatingStaticNestedClass_thenCorrect() { + Enclosing.Nested nested = new Enclosing.Nested(); + nested.test(); + } +} diff --git a/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java b/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java new file mode 100644 index 0000000000..e883687d33 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/nestedclass/NewOuterTest.java @@ -0,0 +1,32 @@ +package com.baeldung.nestedclass; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class NewOuterTest { + + int a = 1; + static int b = 2; + + public class InnerClass { + int a = 3; + static final int b = 4; + + @Test + public void whenShadowing_thenCorrect() { + assertEquals(3, a); + assertEquals(4, b); + assertEquals(1, NewOuterTest.this.a); + assertEquals(2, NewOuterTest.b); + assertEquals(2, NewOuterTest.this.b); + } + } + + @Test + public void shadowingTest() { + NewOuterTest outer = new NewOuterTest(); + NewOuterTest.InnerClass inner = outer.new InnerClass(); + inner.whenShadowing_thenCorrect(); + + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/string/StringTest.java b/core-java/src/test/java/com/baeldung/string/StringTest.java index fe1a69aa23..e88b2d7c2c 100644 --- a/core-java/src/test/java/com/baeldung/string/StringTest.java +++ b/core-java/src/test/java/com/baeldung/string/StringTest.java @@ -223,4 +223,4 @@ public class StringTest { assertEquals("200", String.valueOf(l)); } -} +} \ No newline at end of file