Merge branch 'master' of https://github.com/eugenp/tutorials into story/BAEL-7636
This commit is contained in:
commit
5a9297aa0d
@ -0,0 +1,18 @@
|
||||
package com.baeldung.keywords.finalize;
|
||||
|
||||
public class FinalizeObject {
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
System.out.println("Execute finalize method");
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
FinalizeObject object = new FinalizeObject();
|
||||
object = null;
|
||||
System.gc();
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.keywords.finalkeyword;
|
||||
|
||||
public final class Child extends Parent {
|
||||
|
||||
@Override
|
||||
void method1(int arg1, final int arg2) {
|
||||
// OK
|
||||
}
|
||||
|
||||
/* @Override
|
||||
void method2() {
|
||||
// Compilation error
|
||||
}*/
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.keywords.finalkeyword;
|
||||
|
||||
/*public class GrandChild extends Child {
|
||||
// Compilation error
|
||||
}*/
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.keywords.finalkeyword;
|
||||
|
||||
public class Parent {
|
||||
|
||||
int field1 = 1;
|
||||
final int field2 = 2;
|
||||
|
||||
Parent() {
|
||||
field1 = 2; // OK
|
||||
// field2 = 3; // Compilation error
|
||||
}
|
||||
|
||||
void method1(int arg1, final int arg2) {
|
||||
arg1 = 2; // OK
|
||||
// arg2 = 3; // Compilation error
|
||||
}
|
||||
|
||||
final void method2() {
|
||||
final int localVar = 2; // OK
|
||||
// localVar = 3; // Compilation error
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.keywords.finallykeyword;
|
||||
|
||||
public class FinallyExample {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
try {
|
||||
System.out.println("Execute try block");
|
||||
throw new Exception();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Execute catch block");
|
||||
} finally {
|
||||
System.out.println("Execute finally block");
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Execute try block");
|
||||
} finally {
|
||||
System.out.println("Execute finally block");
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Execute try block");
|
||||
throw new Exception();
|
||||
} finally {
|
||||
System.out.println("Execute finally block");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.baeldung.regexp.datepattern.optmization;
|
||||
|
||||
public class OptimizedMatcher {
|
||||
|
||||
|
||||
}
|
@ -9,28 +9,15 @@ import java.util.stream.Stream;
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
@Log
|
||||
public class ListInitializationUnitTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void givenAnonymousInnerClass_thenInitialiseList() {
|
||||
List<String> cities = new ArrayList() {
|
||||
// Inside declaration of the subclass
|
||||
|
||||
// You can have multiple initializer block
|
||||
{
|
||||
log.info("Inside the first initializer block.");
|
||||
}
|
||||
|
||||
{
|
||||
log.info("Inside the second initializer block.");
|
||||
add("New York");
|
||||
add("Rio");
|
||||
add("Tokyo");
|
||||
@ -47,11 +34,10 @@ public class ListInitializationUnitTest {
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenArraysAsList_whenAdd_thenUnsupportedException() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
exception.expect(UnsupportedOperationException.class);
|
||||
list.add("baz");
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,109 @@
|
||||
package com.baeldung.regexp.optmization;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OptimizedMatcherUnitTest {
|
||||
|
||||
private long time;
|
||||
private long mstimePreCompiled;
|
||||
private long mstimeNotPreCompiled;
|
||||
|
||||
private String action;
|
||||
|
||||
private List<String> items;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Random random = new Random();
|
||||
items = new ArrayList<String>();
|
||||
long average = 0;
|
||||
|
||||
for (int i = 0; i < 100000; ++i) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
int characters = random.nextInt(7) + 1;
|
||||
for (int k = 0; k < characters; ++ k) {
|
||||
char c = (char)(random.nextInt('Z' - 'A') + 'A');
|
||||
int rep = random.nextInt(95) + 5;
|
||||
for (int j = 0; j < rep; ++ j)
|
||||
s.append(c);
|
||||
average += rep;
|
||||
}
|
||||
items.add(s.toString());
|
||||
}
|
||||
|
||||
average /= 100000;
|
||||
System.out.println("generated data, average length: " + average);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
|
||||
testPatterns("A*");
|
||||
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
|
||||
testPatterns("A*B*C*");
|
||||
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() {
|
||||
|
||||
testPatterns("E*C*W*F*");
|
||||
assertTrue(mstimePreCompiled < mstimeNotPreCompiled);
|
||||
}
|
||||
|
||||
private void testPatterns(String regex) {
|
||||
time = System.nanoTime();
|
||||
int matched = 0;
|
||||
int unmatched = 0;
|
||||
|
||||
for (String item : this.items) {
|
||||
if (item.matches(regex)) {
|
||||
++matched;
|
||||
}
|
||||
else {
|
||||
++unmatched;
|
||||
}
|
||||
}
|
||||
|
||||
this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
|
||||
|
||||
this.mstimeNotPreCompiled = (System.nanoTime() - time) / 1000000;
|
||||
System.out.println(this.action + ": " + mstimeNotPreCompiled + "ms");
|
||||
|
||||
time = System.nanoTime();
|
||||
|
||||
Matcher matcher = Pattern.compile(regex).matcher("");
|
||||
matched = 0;
|
||||
unmatched = 0;
|
||||
|
||||
for (String item : this.items) {
|
||||
if (matcher.reset(item).matches()) {
|
||||
++matched;
|
||||
}
|
||||
else {
|
||||
++unmatched;
|
||||
}
|
||||
}
|
||||
|
||||
this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched;
|
||||
|
||||
this.mstimePreCompiled = (System.nanoTime() - time) / 1000000;
|
||||
System.out.println(this.action + ": " + mstimePreCompiled + "ms");
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-faker</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>0.15</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -1,115 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.github.javafaker.Faker;
|
||||
import com.github.javafaker.service.FakeValuesService;
|
||||
import com.github.javafaker.service.FakerIDN;
|
||||
import com.github.javafaker.service.LocaleDoesNotExistException;
|
||||
import com.github.javafaker.service.RandomService;
|
||||
import javafx.scene.Parent;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class JavaFakerTest {
|
||||
|
||||
private Faker faker;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
faker = new Faker();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception {
|
||||
|
||||
Faker faker = new Faker();
|
||||
|
||||
String streetName = faker.address().streetName();
|
||||
String number = faker.address().buildingNumber();
|
||||
String city = faker.address().city();
|
||||
String country = faker.address().country();
|
||||
|
||||
System.out.println(String.format("%s\n%s\n%s\n%s",
|
||||
number,
|
||||
streetName,
|
||||
city,
|
||||
country));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception {
|
||||
|
||||
Faker faker1 = new Faker(new Random(24));
|
||||
Faker faker2 = new Faker(new Random(24));
|
||||
|
||||
assertEquals(faker1.name().firstName(), faker2.name().firstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception {
|
||||
|
||||
Faker ukFaker = new Faker(new Locale("en-GB"));
|
||||
Faker usFaker = new Faker(new Locale("en-US"));
|
||||
|
||||
System.out.println(String.format("American zipcode: %s", usFaker.address().zipCode()));
|
||||
System.out.println(String.format("British postcode: %s", ukFaker.address().zipCode()));
|
||||
|
||||
Pattern ukPattern = Pattern.compile("([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\\s?[0-9][A-Za-z]{2})");
|
||||
Matcher ukMatcher = ukPattern.matcher(ukFaker.address().zipCode());
|
||||
|
||||
assertTrue(ukMatcher.find());
|
||||
|
||||
Matcher usMatcher = Pattern.compile("^\\d{5}(?:[-\\s]\\d{4})?$").matcher(usFaker.address().zipCode());
|
||||
|
||||
assertTrue(usMatcher.find());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakerService_testFakersCreated() throws Exception {
|
||||
|
||||
RandomService randomService = new RandomService();
|
||||
|
||||
System.out.println(randomService.nextBoolean());
|
||||
System.out.println(randomService.nextDouble());
|
||||
|
||||
Faker faker = new Faker(new Random(randomService.nextLong()));
|
||||
|
||||
System.out.println(faker.address().city());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFakeValuesService() throws Exception {
|
||||
|
||||
FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new RandomService());
|
||||
|
||||
String email = fakeValuesService.bothify("????##@gmail.com");
|
||||
Matcher emailMatcher = Pattern.compile("\\w{4}\\d{2}@gmail.com").matcher(email);
|
||||
assertTrue(emailMatcher.find());
|
||||
|
||||
String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
|
||||
Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}").matcher(alphaNumericString);
|
||||
assertTrue(alphaNumericMatcher.find());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = LocaleDoesNotExistException.class)
|
||||
public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception {
|
||||
|
||||
Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
|
||||
|
||||
}
|
||||
}
|
@ -88,6 +88,11 @@
|
||||
<artifactId>javalite-common</artifactId>
|
||||
<version>${javalite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>0.15</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,121 @@
|
||||
package com.baeldung.javafaker;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.javafaker.Faker;
|
||||
import com.github.javafaker.service.FakeValuesService;
|
||||
import com.github.javafaker.service.LocaleDoesNotExistException;
|
||||
import com.github.javafaker.service.RandomService;
|
||||
|
||||
public class JavaFakerUnitTest {
|
||||
|
||||
private Faker faker;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
faker = new Faker();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFaker_whenAddressObjectCalled_checkValidAddressInfoGiven() throws Exception {
|
||||
|
||||
Faker faker = new Faker();
|
||||
|
||||
String streetName = faker.address()
|
||||
.streetName();
|
||||
String number = faker.address()
|
||||
.buildingNumber();
|
||||
String city = faker.address()
|
||||
.city();
|
||||
String country = faker.address()
|
||||
.country();
|
||||
|
||||
System.out.println(String.format("%s\n%s\n%s\n%s", number, streetName, city, country));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakersWithSameSeed_whenNameCalled_CheckSameName() throws Exception {
|
||||
|
||||
Faker faker1 = new Faker(new Random(24));
|
||||
Faker faker2 = new Faker(new Random(24));
|
||||
|
||||
assertEquals(faker1.name()
|
||||
.firstName(),
|
||||
faker2.name()
|
||||
.firstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakersWithDifferentLocals_checkZipCodesMatchRegex() throws Exception {
|
||||
|
||||
Faker ukFaker = new Faker(new Locale("en-GB"));
|
||||
Faker usFaker = new Faker(new Locale("en-US"));
|
||||
|
||||
System.out.println(String.format("American zipcode: %s", usFaker.address()
|
||||
.zipCode()));
|
||||
System.out.println(String.format("British postcode: %s", ukFaker.address()
|
||||
.zipCode()));
|
||||
|
||||
Pattern ukPattern = Pattern.compile("([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\\s?[0-9][A-Za-z]{2})");
|
||||
Matcher ukMatcher = ukPattern.matcher(ukFaker.address()
|
||||
.zipCode());
|
||||
|
||||
assertTrue(ukMatcher.find());
|
||||
|
||||
Matcher usMatcher = Pattern.compile("^\\d{5}(?:[-\\s]\\d{4})?$")
|
||||
.matcher(usFaker.address()
|
||||
.zipCode());
|
||||
|
||||
assertTrue(usMatcher.find());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaFakerService_testFakersCreated() throws Exception {
|
||||
|
||||
RandomService randomService = new RandomService();
|
||||
|
||||
System.out.println(randomService.nextBoolean());
|
||||
System.out.println(randomService.nextDouble());
|
||||
|
||||
Faker faker = new Faker(new Random(randomService.nextLong()));
|
||||
|
||||
System.out.println(faker.address()
|
||||
.city());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFakeValuesService() throws Exception {
|
||||
|
||||
FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new RandomService());
|
||||
|
||||
String email = fakeValuesService.bothify("????##@gmail.com");
|
||||
Matcher emailMatcher = Pattern.compile("\\w{4}\\d{2}@gmail.com")
|
||||
.matcher(email);
|
||||
assertTrue(emailMatcher.find());
|
||||
|
||||
String alphaNumericString = fakeValuesService.regexify("[a-z1-9]{10}");
|
||||
Matcher alphaNumericMatcher = Pattern.compile("[a-z1-9]{10}")
|
||||
.matcher(alphaNumericString);
|
||||
assertTrue(alphaNumericMatcher.find());
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = LocaleDoesNotExistException.class)
|
||||
public void givenWrongLocale_whenFakerIsInitialised_testLocaleDoesNotExistExceptionIsThrown() throws Exception {
|
||||
|
||||
Faker wrongLocaleFaker = new Faker(new Locale("en-seaWorld"));
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user