Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -115,4 +115,8 @@
|
||||
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
|
||||
- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static)
|
||||
- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array)
|
||||
- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool)
|
||||
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
|
||||
|
||||
|
||||
@@ -460,7 +460,7 @@
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>22.0</guava.version>
|
||||
<guava.version>23.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public interface AbstractFactory {
|
||||
Animal getAnimal(String toyType) ;
|
||||
Color getColor(String colorType);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class AbstractPatternDriver {
|
||||
public static void main(String[] args) {
|
||||
AbstractFactory abstractFactory;
|
||||
|
||||
//creating a brown toy dog
|
||||
abstractFactory = FactoryProvider.getFactory("Toy");
|
||||
Animal toy = abstractFactory.getAnimal("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color = abstractFactory.getColor("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public interface Animal {
|
||||
String getType();
|
||||
String makeSound();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class AnimalFactory implements AbstractFactory {
|
||||
|
||||
@Override
|
||||
public Animal getAnimal(String animalType) {
|
||||
if ("Dog".equalsIgnoreCase(animalType)) {
|
||||
return new Dog();
|
||||
} else if ("Duck".equalsIgnoreCase(animalType)) {
|
||||
return new Duck();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor(String color) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class Brown implements Color {
|
||||
|
||||
@Override
|
||||
public String getColor() {
|
||||
return "brown";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public interface Color {
|
||||
String getColor();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class ColorFactory implements AbstractFactory {
|
||||
|
||||
@Override
|
||||
public Color getColor(String colorType) {
|
||||
if ("Brown".equalsIgnoreCase(colorType)) {
|
||||
return new Brown();
|
||||
} else if ("White".equalsIgnoreCase(colorType)) {
|
||||
return new White();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animal getAnimal(String toyType) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class Dog implements Animal {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Dog";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Barks";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class Duck implements Animal {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Duck";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String makeSound() {
|
||||
return "Squeks";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class FactoryProvider {
|
||||
public static AbstractFactory getFactory(String choice){
|
||||
|
||||
if("Toy".equalsIgnoreCase(choice)){
|
||||
return new AnimalFactory();
|
||||
}
|
||||
else if("Color".equalsIgnoreCase(choice)){
|
||||
return new ColorFactory();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
public class White implements Color {
|
||||
|
||||
@Override
|
||||
public String getColor() {
|
||||
return "White";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.designpatterns.creational.builder;
|
||||
|
||||
public class BankAccount {
|
||||
private String name;
|
||||
private String accountNumber;
|
||||
private String email;
|
||||
private boolean newsletter;
|
||||
|
||||
//The constructor that takes a builder from which it will create object
|
||||
//the access to this is only provided to builder
|
||||
private BankAccount(BankAccountBuilder builder) {
|
||||
this.name = builder.name;
|
||||
this.accountNumber = builder.accountNumber;
|
||||
this.email = builder.email;
|
||||
this.newsletter = builder.newsletter;
|
||||
}
|
||||
|
||||
public static class BankAccountBuilder {
|
||||
private String name;
|
||||
private String accountNumber;
|
||||
private String email;
|
||||
private boolean newsletter;
|
||||
|
||||
//All Mandatory parameters goes with this constructor
|
||||
public BankAccountBuilder(String name, String accountNumber) {
|
||||
this.name = name;
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
//setters for optional parameters which returns this same builder
|
||||
//to support fluent design
|
||||
public BankAccountBuilder withEmail(String email) {
|
||||
this.email = email;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BankAccountBuilder wantNewsletter(boolean newsletter) {
|
||||
this.newsletter = newsletter;
|
||||
return this;
|
||||
}
|
||||
|
||||
//the actual build method that prepares and returns a BankAccount object
|
||||
public BankAccount build() {
|
||||
return new BankAccount(this);
|
||||
}
|
||||
}
|
||||
|
||||
//getters
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public boolean isNewsletter() {
|
||||
return newsletter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.designpatterns.creational.builder;
|
||||
|
||||
public class BuilderPatternDriver {
|
||||
public static void main(String[] args) {
|
||||
BankAccount newAccount = new BankAccount
|
||||
.BankAccountBuilder("Jon", "22738022275")
|
||||
.withEmail("jon@example.com")
|
||||
.wantNewsletter(true)
|
||||
.build();
|
||||
|
||||
System.out.println("Name: " + newAccount.getName());
|
||||
System.out.println("AccountNumber:" + newAccount.getAccountNumber());
|
||||
System.out.println("Email: " + newAccount.getEmail());
|
||||
System.out.println("Want News letter?: " + newAccount.isNewsletter());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class FactoryDriver {
|
||||
public static void main(String[] args) {
|
||||
Polygon p;
|
||||
PolygonFactory factory = new PolygonFactory();
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(4);
|
||||
System.out.println("The shape with 4 sides is a " + p.getType());
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(8);
|
||||
System.out.println("The shape with 8 sides is a " + p.getType());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class Heptagon implements Polygon {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Heptagon";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class Octagon implements Polygon {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Octagon";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class Pentagon implements Polygon {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Pentagon";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public interface Polygon {
|
||||
String getType();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class PolygonFactory {
|
||||
public Polygon getPolygon(int numberOfSides) {
|
||||
if(numberOfSides == 3) {
|
||||
return new Triangle();
|
||||
}
|
||||
if(numberOfSides == 4) {
|
||||
return new Square();
|
||||
}
|
||||
if(numberOfSides == 5) {
|
||||
return new Pentagon();
|
||||
}
|
||||
if(numberOfSides == 4) {
|
||||
return new Heptagon();
|
||||
}
|
||||
else if(numberOfSides == 8) {
|
||||
return new Octagon();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class Square implements Polygon {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Square";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
public class Triangle implements Polygon {
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "Triangle";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.designpatterns.creational.singleton;
|
||||
|
||||
public class Singleton {
|
||||
private Singleton() {}
|
||||
|
||||
private static class SingletonHolder {
|
||||
public static final Singleton instance = new Singleton();
|
||||
}
|
||||
|
||||
public static Singleton getInstance() {
|
||||
return SingletonHolder.instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.creational.singleton;
|
||||
|
||||
public class SingletonDriver {
|
||||
public static void main(String[] args) {
|
||||
Singleton instance = Singleton.getInstance();
|
||||
System.out.println(instance.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimezoneDisplayJava7 {
|
||||
|
||||
public enum OffsetBase {
|
||||
GMT, UTC
|
||||
}
|
||||
|
||||
public List<String> getTimeZoneList(TimezoneDisplayJava7.OffsetBase base) {
|
||||
String[] availableZoneIds = TimeZone.getAvailableIDs();
|
||||
List<String> result = new ArrayList<>(availableZoneIds.length);
|
||||
|
||||
for (String zoneId : availableZoneIds) {
|
||||
TimeZone curTimeZone = TimeZone.getTimeZone(zoneId);
|
||||
|
||||
String offset = calculateOffset(curTimeZone.getRawOffset());
|
||||
|
||||
result.add(String.format("(%s%s) %s", base, offset, zoneId));
|
||||
}
|
||||
|
||||
Collections.sort(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String calculateOffset(int rawOffset) {
|
||||
if (rawOffset == 0) {
|
||||
return "+00:00";
|
||||
}
|
||||
long hours = TimeUnit.MILLISECONDS.toHours(rawOffset);
|
||||
long minutes = TimeUnit.MILLISECONDS.toMinutes(rawOffset);
|
||||
minutes = Math.abs(minutes - TimeUnit.HOURS.toMinutes(hours));
|
||||
|
||||
return String.format("%+03d:%02d", hours, Math.abs(minutes));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TimezoneDisplayJava7App {
|
||||
|
||||
public static void main(String... args) {
|
||||
TimezoneDisplayJava7 display = new TimezoneDisplayJava7();
|
||||
|
||||
System.out.println("Time zones in UTC:");
|
||||
List<String> utc = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.UTC);
|
||||
for (String timeZone : utc) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
|
||||
System.out.println("Time zones in GMT:");
|
||||
List<String> gmt = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.GMT);
|
||||
for (String timeZone : gmt) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.copyfiles;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class FileCopierTest {
|
||||
File original = new File("src/test/resources/original.txt");
|
||||
|
||||
@Before
|
||||
public void init() throws IOException {
|
||||
if (!original.exists())
|
||||
Files.createFile(original.toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithIo.txt");
|
||||
try (InputStream in = new BufferedInputStream(new FileInputStream(original));
|
||||
OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int lengthRead;
|
||||
while ((lengthRead = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, lengthRead);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
|
||||
FileUtils.copyFile(original, copied);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
|
||||
Path originalPath = original.toPath();
|
||||
Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
|
||||
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
|
||||
com.google.common.io.Files.copy(original, copied);
|
||||
assertThat(copied).exists();
|
||||
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.designpatterns.creational.abstractfactory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AbstractPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenAbstractFactory_whenGettingObjects_thenSuccessful() {
|
||||
AbstractFactory abstractFactory;
|
||||
|
||||
//creating a brown toy dog
|
||||
abstractFactory = FactoryProvider.getFactory("Toy");
|
||||
Animal toy = abstractFactory.getAnimal("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color = abstractFactory.getColor("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
assertEquals("A Dog with brown color Barks", result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.designpatterns.creational.builder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BuilderPatternIntegrationTest {
|
||||
@Test
|
||||
public void whenCreatingObjectThroughBuilder_thenObjectValid() {
|
||||
BankAccount newAccount = new BankAccount
|
||||
.BankAccountBuilder("Jon", "22738022275")
|
||||
.withEmail("jon@example.com")
|
||||
.wantNewsletter(true)
|
||||
.build();
|
||||
|
||||
assertEquals(newAccount.getName(), "Jon");
|
||||
assertEquals(newAccount.getAccountNumber(), "22738022275");
|
||||
assertEquals(newAccount.getEmail(), "jon@example.com");
|
||||
assertEquals(newAccount.isNewsletter(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkippingOptionalParameters_thenObjectValid() {
|
||||
BankAccount newAccount = new BankAccount
|
||||
.BankAccountBuilder("Jon", "22738022275")
|
||||
.build();
|
||||
|
||||
assertEquals(newAccount.getName(), "Jon");
|
||||
assertEquals(newAccount.getAccountNumber(), "22738022275");
|
||||
assertEquals(newAccount.getEmail(), null);
|
||||
assertEquals(newAccount.isNewsletter(), false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.designpatterns.creational.factory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FactoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingFactoryForSquare_thenCorrectObjectReturned() {
|
||||
Polygon p;
|
||||
PolygonFactory factory = new PolygonFactory();
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(4);
|
||||
String result = "The shape with 4 sides is a " + p.getType();
|
||||
|
||||
assertEquals("The shape with 4 sides is a Square", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFactoryForOctagon_thenCorrectObjectReturned() {
|
||||
Polygon p;
|
||||
PolygonFactory factory = new PolygonFactory();
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(8);
|
||||
String result = "The shape with 8 sides is a " + p.getType();
|
||||
|
||||
assertEquals("The shape with 8 sides is a Octagon", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.designpatterns.creational.singleton;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SingletonIntegrationTest {
|
||||
|
||||
@Test
|
||||
/**
|
||||
* Although there is absolutely no way to determine whether
|
||||
* a class is Singleton, in this test case, we will just
|
||||
* check for two objects if they point to same instance or
|
||||
* not. We will also check for their hashcode.
|
||||
*/
|
||||
public void whenGettingMultipleObjects_thenAllPointToSame() {
|
||||
//first object
|
||||
Singleton obj1 = Singleton.getInstance();
|
||||
|
||||
//Second object
|
||||
Singleton obj2 = Singleton.getInstance();
|
||||
|
||||
assertTrue(obj1 == obj2);
|
||||
assertEquals(obj1.hashCode(), obj2.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,8 @@ public class StringFormatterExampleTests {
|
||||
public void givenString_whenFormatSpecifierForCalendar_thenGotExpected() {
|
||||
//Syntax of Format Specifiers for Date/Time Representation
|
||||
Calendar c = new GregorianCalendar(2017, 11, 10);
|
||||
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
|
||||
String s = String.format("The date is: %tm %1$te,%1$tY", c);
|
||||
|
||||
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
}
|
||||
@@ -84,7 +85,7 @@ public class StringFormatterExampleTests {
|
||||
public void givenString_whenLineSeparatorConversion_thenConvertedString() {
|
||||
//Line Separator Conversion
|
||||
String s = String.format("First Line %nSecond Line");
|
||||
assertEquals("First Line \n"
|
||||
assertEquals("First Line " + System.getProperty("line.separator")
|
||||
+ "Second Line", s);
|
||||
}
|
||||
|
||||
@@ -114,10 +115,10 @@ public class StringFormatterExampleTests {
|
||||
public void givenString_whenSpecifyArgumentIndex_thenGotExpected() {
|
||||
Calendar c = new GregorianCalendar(2017, 11, 10);
|
||||
//Argument_Index
|
||||
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
|
||||
String s = String.format("The date is: %tm %1$te,%1$tY", c);
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
|
||||
s = String.format("The date is: %1$tm %<te,%<tY", c);
|
||||
|
||||
s = String.format("The date is: %tm %<te,%<tY", c);
|
||||
assertEquals("The date is: 12 10,2017", s);
|
||||
}
|
||||
|
||||
@@ -126,8 +127,7 @@ public class StringFormatterExampleTests {
|
||||
//Using String Formatter with Appendable
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Formatter formatter = new Formatter(sb);
|
||||
formatter.format("I am writting to a %1$s Instance.", sb.getClass());
|
||||
|
||||
formatter.format("I am writting to a %s Instance.", sb.getClass());
|
||||
assertEquals("I am writting to a class java.lang.StringBuilder Instance.", sb.toString());
|
||||
}
|
||||
|
||||
|
||||
2
core-java/src/test/resources/original.txt
Normal file
2
core-java/src/test/resources/original.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
#Copy a File with Java (www.Baeldung.com)
|
||||
Copying Files with Java is Fun!
|
||||
Reference in New Issue
Block a user