Merge branch 'master' of https://github.com/ahmedtawila/tutorials
This commit is contained in:
commit
956ad7cccb
@ -1,11 +0,0 @@
|
|||||||
package com.baeldung.nestedclass;
|
|
||||||
|
|
||||||
public class Enclosing {
|
|
||||||
|
|
||||||
public static class Nested {
|
|
||||||
|
|
||||||
public void test() {
|
|
||||||
System.out.println("Calling test...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.baeldung.nestedclass;
|
|
||||||
|
|
||||||
public class Outer {
|
|
||||||
|
|
||||||
public class Inner {
|
|
||||||
|
|
||||||
public void test() {
|
|
||||||
System.out.println("Calling test...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
package com.baeldung.nestedclass;
|
|
||||||
|
|
||||||
abstract class SimpleAbstractClass {
|
|
||||||
abstract void run();
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.nestedclass;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class LocalClassTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenTestingLocalClass_thenCorrect() {
|
|
||||||
NewEnclosing newEnclosing = new NewEnclosing();
|
|
||||||
newEnclosing.run();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.nestedclass;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class NestedClassTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenInstantiatingStaticNestedClass_thenCorrect() {
|
|
||||||
Enclosing.Nested nested = new Enclosing.Nested();
|
|
||||||
nested.test();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
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();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user