merge with base repo
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.arraydeque;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ArrayDequeTest {
|
||||
|
||||
@Test
|
||||
public void whenOffer_addsAtLast() {
|
||||
final Deque<String> deque = new ArrayDeque<>();
|
||||
|
||||
deque.offer("first");
|
||||
deque.offer("second");
|
||||
|
||||
assertEquals("second", deque.getLast());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPoll_removesFirst() {
|
||||
final Deque<String> deque = new ArrayDeque<>();
|
||||
|
||||
deque.offer("first");
|
||||
deque.offer("second");
|
||||
|
||||
assertEquals("first", deque.poll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPush_addsAtFirst() {
|
||||
final Deque<String> deque = new ArrayDeque<>();
|
||||
|
||||
deque.push("first");
|
||||
deque.push("second");
|
||||
|
||||
assertEquals("second", deque.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPop_removesLast() {
|
||||
final Deque<String> deque = new ArrayDeque<>();
|
||||
|
||||
deque.push("first");
|
||||
deque.push("second");
|
||||
|
||||
assertEquals("second", deque.pop());
|
||||
}
|
||||
}
|
||||
@@ -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,14 +2,24 @@ package com.baeldung.file;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharSink;
|
||||
import com.google.common.io.FileWriteMode;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.util.StreamUtils;
|
||||
@@ -18,6 +28,26 @@ public class FilesTest {
|
||||
|
||||
public static final String fileName = "src/main/resources/countries.properties";
|
||||
|
||||
@Before
|
||||
@After
|
||||
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 = com.google.common.io.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");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenAppendToFileUsingFiles_thenCorrect() throws IOException {
|
||||
Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND);
|
||||
@@ -27,10 +57,38 @@ public class FilesTest {
|
||||
.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();
|
||||
@Test
|
||||
public void whenAppendToFileUsingFileUtils_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");
|
||||
}
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SampleJavacPluginIntegrationTest {
|
||||
|
||||
private static final String CLASS_TEMPLATE =
|
||||
"package com.baeldung.javac;\n" +
|
||||
"\n" +
|
||||
"public class Test {\n" +
|
||||
" public static %1$s service(@Positive %1$s i) {\n" +
|
||||
" return i;\n" +
|
||||
" }\n" +
|
||||
"}\n" +
|
||||
"";
|
||||
|
||||
private TestCompiler compiler = new TestCompiler();
|
||||
private TestRunner runner = new TestRunner();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenInt_whenNegative_thenThrowsException() throws Throwable {
|
||||
compileAndRun(double.class,-1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenInt_whenZero_thenThrowsException() throws Throwable {
|
||||
compileAndRun(int.class,0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInt_whenPositive_thenSuccess() throws Throwable {
|
||||
assertEquals(1, compileAndRun(int.class, 1));
|
||||
}
|
||||
|
||||
private Object compileAndRun(Class<?> argumentType, Object argument) throws Throwable {
|
||||
String qualifiedClassName = "com.baeldung.javac.Test";
|
||||
byte[] byteCode = compiler.compile(qualifiedClassName, String.format(CLASS_TEMPLATE, argumentType.getName()));
|
||||
return runner.run(byteCode, qualifiedClassName, "service", new Class[] {argumentType}, argument);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
|
||||
/** Holds compiled byte code in a byte array */
|
||||
public class SimpleClassFile extends SimpleJavaFileObject {
|
||||
|
||||
private ByteArrayOutputStream out;
|
||||
|
||||
public SimpleClassFile(URI uri) {
|
||||
super(uri, Kind.CLASS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream openOutputStream() throws IOException {
|
||||
return out = new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
public byte[] getCompiledBinaries() {
|
||||
return out.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Adapts {@link SimpleClassFile} to the {@link JavaCompiler} */
|
||||
public class SimpleFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
|
||||
|
||||
private final List<SimpleClassFile> compiled = new ArrayList<>();
|
||||
|
||||
public SimpleFileManager(StandardJavaFileManager delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaFileObject getJavaFileForOutput(Location location,
|
||||
String className,
|
||||
JavaFileObject.Kind kind,
|
||||
FileObject sibling)
|
||||
{
|
||||
SimpleClassFile result = new SimpleClassFile(URI.create("string://" + className));
|
||||
compiled.add(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return compiled binaries processed by the current class
|
||||
*/
|
||||
public List<SimpleClassFile> getCompiled() {
|
||||
return compiled;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import java.net.URI;
|
||||
|
||||
/** Exposes given test source to the compiler. */
|
||||
public class SimpleSourceFile extends SimpleJavaFileObject {
|
||||
|
||||
private final String content;
|
||||
|
||||
public SimpleSourceFile(String qualifiedClassName, String testSource) {
|
||||
super(URI.create(String.format("file://%s%s",
|
||||
qualifiedClassName.replaceAll("\\.", "/"),
|
||||
Kind.SOURCE.extension)),
|
||||
Kind.SOURCE);
|
||||
content = testSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
public class TestCompiler {
|
||||
public byte[] compile(String qualifiedClassName, String testSource) {
|
||||
StringWriter output = new StringWriter();
|
||||
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
SimpleFileManager fileManager = new SimpleFileManager(compiler.getStandardFileManager(
|
||||
null,
|
||||
null,
|
||||
null
|
||||
));
|
||||
List<SimpleSourceFile> compilationUnits = singletonList(new SimpleSourceFile(qualifiedClassName, testSource));
|
||||
List<String> arguments = new ArrayList<>();
|
||||
arguments.addAll(asList("-classpath", System.getProperty("java.class.path"),
|
||||
"-Xplugin:" + SampleJavacPlugin.NAME));
|
||||
JavaCompiler.CompilationTask task = compiler.getTask(output,
|
||||
fileManager,
|
||||
null,
|
||||
arguments,
|
||||
null,
|
||||
compilationUnits);
|
||||
task.call();
|
||||
return fileManager.getCompiled().iterator().next().getCompiledBinaries();
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.javac;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestRunner {
|
||||
|
||||
public Object run(byte[] byteCode,
|
||||
String qualifiedClassName,
|
||||
String methodName,
|
||||
Class<?>[] argumentTypes,
|
||||
Object... args)
|
||||
throws Throwable
|
||||
{
|
||||
ClassLoader classLoader = new ClassLoader() {
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
return defineClass(name, byteCode, 0, byteCode.length);
|
||||
}
|
||||
};
|
||||
Class<?> clazz;
|
||||
try {
|
||||
clazz = classLoader.loadClass(qualifiedClassName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("Can't load compiled test class", e);
|
||||
}
|
||||
|
||||
Method method;
|
||||
try {
|
||||
method = clazz.getMethod(methodName, argumentTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("Can't find the 'main()' method in the compiled test class", e);
|
||||
}
|
||||
|
||||
try {
|
||||
return method.invoke(null, args);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw e.getCause();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.loops;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class WhenUsingLoops {
|
||||
|
||||
private LoopsInJava loops = new LoopsInJava();
|
||||
|
||||
@Test
|
||||
public void shouldRunForLoop() {
|
||||
int[] expected = { 0, 1, 2, 3, 4 };
|
||||
int[] actual = loops.simple_for_loop();
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunEnhancedForeachLoop() {
|
||||
int[] expected = { 0, 1, 2, 3, 4 };
|
||||
int[] actual = loops.enhanced_for_each_loop();
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunWhileLoop() {
|
||||
int[] expected = { 0, 1, 2, 3, 4 };
|
||||
int[] actual = loops.while_loop();
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunDoWhileLoop() {
|
||||
int[] expected = { 0, 1, 2, 3, 4 };
|
||||
int[] actual = loops.do_while_loop();
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
abstract class SimpleAbstractClass {
|
||||
abstract void run();
|
||||
}
|
||||
|
||||
public class AnonymousInner {
|
||||
|
||||
@Test
|
||||
public void run() {
|
||||
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
|
||||
void run() {
|
||||
System.out.println("Running Anonymous Class...");
|
||||
}
|
||||
};
|
||||
simpleAbstractClass.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Enclosing {
|
||||
|
||||
private static int x = 1;
|
||||
|
||||
public static class StaticNested {
|
||||
|
||||
private void run() {
|
||||
System.out.println("x = " + x);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Enclosing.StaticNested nested = new Enclosing.StaticNested();
|
||||
nested.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NewEnclosing {
|
||||
|
||||
private void run() {
|
||||
class Local {
|
||||
void run() {
|
||||
System.out.println("Welcome to Baeldung!");
|
||||
}
|
||||
}
|
||||
Local local = new Local();
|
||||
local.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
NewEnclosing newEnclosing = new NewEnclosing();
|
||||
newEnclosing.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NewOuter {
|
||||
|
||||
int a = 1;
|
||||
static int b = 2;
|
||||
|
||||
public class InnerClass {
|
||||
int a = 3;
|
||||
static final int b = 4;
|
||||
|
||||
public void run() {
|
||||
System.out.println("a = " + a);
|
||||
System.out.println("b = " + b);
|
||||
System.out.println("NewOuterTest.this.a = " + NewOuter.this.a);
|
||||
System.out.println("NewOuterTest.b = " + NewOuter.b);
|
||||
System.out.println("NewOuterTest.this.b = " + NewOuter.this.b);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
NewOuter outer = new NewOuter();
|
||||
NewOuter.InnerClass inner = outer.new InnerClass();
|
||||
inner.run();
|
||||
|
||||
}
|
||||
}
|
||||
20
core-java/src/test/java/com/baeldung/nestedclass/Outer.java
Normal file
20
core-java/src/test/java/com/baeldung/nestedclass/Outer.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Outer {
|
||||
|
||||
public class Inner {
|
||||
|
||||
public void run() {
|
||||
System.out.println("Calling test...");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Outer outer = new Outer();
|
||||
Outer.Inner inner = outer.new Inner();
|
||||
inner.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PolymorphismUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImageFile_whenFileCreated_shouldSucceed() {
|
||||
ImageFile imageFile = FileManager.createImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
|
||||
.getBytes(), "v1.0.0");
|
||||
assertEquals(200, imageFile.getHeight());
|
||||
}
|
||||
|
||||
// Downcasting then Upcasting
|
||||
@Test
|
||||
public void givenTextFile_whenTextFileCreatedAndAssignedToGenericFileAndCastBackToTextFileOnGetWordCount_shouldSucceed() {
|
||||
GenericFile textFile = FileManager.createTextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
|
||||
TextFile textFile2 = (TextFile) textFile;
|
||||
assertEquals(6, textFile2.getWordCount());
|
||||
}
|
||||
|
||||
// Downcasting
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenGenericFile_whenCastToTextFileAndInvokeGetWordCount_shouldFail() {
|
||||
GenericFile genericFile = new GenericFile();
|
||||
TextFile textFile = (TextFile) genericFile;
|
||||
System.out.println(textFile.getWordCount());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.varargs;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class FormatterTest {
|
||||
|
||||
private final static String FORMAT = "%s %s %s";
|
||||
|
||||
@Test
|
||||
public void givenNoArgument_thenEmptyAndTwoSpacesAreReturned() {
|
||||
String actualResult = format();
|
||||
|
||||
assertThat(actualResult, is("empty "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneArgument_thenResultHasTwoTrailingSpace() {
|
||||
String actualResult = format("baeldung");
|
||||
|
||||
assertThat(actualResult, is("baeldung "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArguments_thenOneTrailingSpaceExists() {
|
||||
String actualResult = format("baeldung", "rocks");
|
||||
|
||||
assertThat(actualResult, is("baeldung rocks "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMoreThanThreeArguments_thenTheFirstThreeAreUsed() {
|
||||
String actualResult = formatWithVarArgs("baeldung", "rocks", "java", "and", "spring");
|
||||
|
||||
assertThat(actualResult, is("baeldung rocks java"));
|
||||
}
|
||||
|
||||
public String format() {
|
||||
return format("empty", "");
|
||||
}
|
||||
|
||||
public String format(String value) {
|
||||
return format(value, "");
|
||||
}
|
||||
|
||||
public String format(String val1, String val2) {
|
||||
return String.format(FORMAT, val1, val2, "");
|
||||
}
|
||||
|
||||
public String formatWithVarArgs(String... values) {
|
||||
if (values.length == 0) {
|
||||
return "no arguments given";
|
||||
}
|
||||
|
||||
return String.format(FORMAT, values);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user