Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
7b618afcd5
@ -0,0 +1,256 @@
|
||||
package com.baeldung.apachecayenne;
|
||||
|
||||
import com.baeldung.apachecayenne.persistent.Author;
|
||||
import org.apache.cayenne.ObjectContext;
|
||||
import org.apache.cayenne.QueryResponse;
|
||||
import org.apache.cayenne.configuration.server.ServerRuntime;
|
||||
import org.apache.cayenne.exp.Expression;
|
||||
import org.apache.cayenne.exp.ExpressionFactory;
|
||||
import org.apache.cayenne.query.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class CayenneAdvancedOperationTests {
|
||||
private static ObjectContext context = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTheCayenneContext() {
|
||||
ServerRuntime cayenneRuntime = ServerRuntime.builder()
|
||||
.addConfig("cayenne-project.xml")
|
||||
.build();
|
||||
context = cayenneRuntime.newContext();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void saveThreeAuthors() {
|
||||
Author authorOne = context.newObject(Author.class);
|
||||
authorOne.setName("Paul Xavier");
|
||||
Author authorTwo = context.newObject(Author.class);
|
||||
authorTwo.setName("pAuL Smith");
|
||||
Author authorThree = context.newObject(Author.class);
|
||||
authorThree.setName("Vicky Sarra");
|
||||
context.commitChanges();
|
||||
}
|
||||
|
||||
@After
|
||||
public void deleteAllAuthors() {
|
||||
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
|
||||
context.performGenericQuery(deleteAuthors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindAllSQLTmplt_thenWeGetThreeAuthors() {
|
||||
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author");
|
||||
List<Author> authors = context.performQuery(select);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindByNameSQLTmplt_thenWeGetOneAuthor() {
|
||||
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author where name = 'Vicky Sarra'");
|
||||
List<Author> authors = context.performQuery(select);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenLikeSltQry_thenWeGetOneAuthor() {
|
||||
Expression qualifier = ExpressionFactory.likeExp(Author.NAME.getName(), "Paul%");
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authorsTwo = context.performQuery(query);
|
||||
|
||||
assertEquals(authorsTwo.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenCtnsIgnorCaseSltQry_thenWeGetTwoAuthors() {
|
||||
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul");
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
assertEquals(authors.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenCtnsIgnorCaseEndsWSltQry_thenWeGetTwoAuthors() {
|
||||
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul")
|
||||
.andExp(ExpressionFactory.endsWithExp(Author.NAME.getName(), "h"));
|
||||
SelectQuery query = new SelectQuery(Author.class, qualifier);
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "pAuL Smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenAscOrderingSltQry_thenWeGetOrderedAuthors() {
|
||||
SelectQuery query = new SelectQuery(Author.class);
|
||||
query.addOrdering(Author.NAME.asc());
|
||||
|
||||
List<Author> authors = query.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
assertEquals(firstAuthor.getName(), "Paul Xavier");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenDescOrderingSltQry_thenWeGetOrderedAuthors() {
|
||||
SelectQuery query = new SelectQuery(Author.class);
|
||||
query.addOrdering(Author.NAME.desc());
|
||||
|
||||
List<Author> authors = query.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
assertEquals(firstAuthor.getName(), "pAuL Smith");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_onContainsObjS_thenWeGetOneRecord() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.contains("Paul"))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenLikeObjS_thenWeGetTwoAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.likeIgnoreCase("Paul%"))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenEndsWithObjS_thenWeGetOrderedAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.endsWith("Sarra"))
|
||||
.select(context);
|
||||
Author firstAuthor = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(firstAuthor.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenInObjS_thenWeGetAuthors() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.in(Arrays.asList(args)))
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenNinObjS_thenWeGetAuthors() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith"};
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.nin(Arrays.asList(args)))
|
||||
.select(context);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoAuthor_whenIsNotNullObjS_thenWeGetAuthors() {
|
||||
List<Author> authors = ObjectSelect.query(Author.class)
|
||||
.where(Author.NAME.isNotNull())
|
||||
.select(context);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindAllEJBQL_thenWeGetThreeAuthors() {
|
||||
EJBQLQuery query = new EJBQLQuery("select a FROM Author a");
|
||||
List<Author> authors = context.performQuery(query);
|
||||
|
||||
assertEquals(authors.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenFindByNameEJBQL_thenWeGetOneAuthor() {
|
||||
EJBQLQuery query = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Sarra'");
|
||||
List<Author> authors = context.performQuery(query);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertEquals(authors.size(), 1);
|
||||
assertEquals(author.getName(), "Vicky Sarra");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenUpdadingByNameEJBQL_thenWeGetTheUpdatedAuthor() {
|
||||
EJBQLQuery query = new EJBQLQuery("UPDATE Author AS a SET a.name = 'Vicky Edison' WHERE a.name = 'Vicky Sarra'");
|
||||
QueryResponse queryResponse = context.performGenericQuery(query);
|
||||
|
||||
EJBQLQuery queryUpdatedAuthor = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Edison'");
|
||||
List<Author> authors = context.performQuery(queryUpdatedAuthor);
|
||||
Author author = authors.get(0);
|
||||
|
||||
assertNotNull(author);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenSeletingNamesEJBQL_thenWeGetListWithSizeThree() {
|
||||
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
|
||||
List<String> names = Arrays.asList(args);
|
||||
EJBQLQuery query = new EJBQLQuery("select a.name FROM Author a");
|
||||
List<String> nameList = context.performQuery(query);
|
||||
|
||||
Collections.sort(names);
|
||||
Collections.sort(nameList);
|
||||
|
||||
assertEquals(names.size(), 3);
|
||||
assertEquals(nameList.size(), 3);
|
||||
assertEquals(names, nameList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenDeletingAllWithEJB_thenWeGetNoAuthor() {
|
||||
EJBQLQuery deleteQuery = new EJBQLQuery("delete FROM Author");
|
||||
EJBQLQuery findAllQuery = new EJBQLQuery("select a FROM Author a");
|
||||
|
||||
context.performQuery(deleteQuery);
|
||||
List<Author> objects = context.performQuery(findAllQuery);
|
||||
|
||||
assertEquals(objects.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenInsertingSQLExec_thenWeGetNewAuthor() {
|
||||
int inserted = SQLExec
|
||||
.query("INSERT INTO Author (name) VALUES ('Baeldung')")
|
||||
.update(context);
|
||||
|
||||
assertEquals(inserted, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthors_whenUpdatingSQLExec_thenItsUpdated() {
|
||||
int updated = SQLExec
|
||||
.query("UPDATE Author SET name = 'Baeldung' WHERE name = 'Vicky Sarra'")
|
||||
.update(context);
|
||||
|
||||
assertEquals(updated, 1);
|
||||
}
|
||||
}
|
66
javaxval/src/main/java/org/baeldung/Customer.java
Normal file
66
javaxval/src/main/java/org/baeldung/Customer.java
Normal file
@ -0,0 +1,66 @@
|
||||
package org.baeldung;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.PositiveOrZero;
|
||||
|
||||
public class Customer {
|
||||
|
||||
@NotBlank(message="Name cannot be empty")
|
||||
private String name;
|
||||
|
||||
private List<@NotBlank(message="Address must not be blank") String> addresses;
|
||||
|
||||
private Integer age;
|
||||
|
||||
@PositiveOrZero
|
||||
private OptionalInt numberOfOrders;
|
||||
|
||||
//@NotBlank
|
||||
private Profile profile;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public void setAddresses(List<String> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public Optional<@Min(18) Integer> getAge() {
|
||||
return Optional.ofNullable(age);
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public OptionalInt getNumberOfOrders() {
|
||||
return numberOfOrders;
|
||||
}
|
||||
|
||||
public void setNumberOfOrders(OptionalInt numberOfOrders) {
|
||||
this.numberOfOrders = numberOfOrders;
|
||||
}
|
||||
|
||||
public Profile getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public void setProfile(Profile profile) {
|
||||
this.profile = profile;
|
||||
}
|
||||
|
||||
}
|
19
javaxval/src/main/java/org/baeldung/CustomerMap.java
Normal file
19
javaxval/src/main/java/org/baeldung/CustomerMap.java
Normal file
@ -0,0 +1,19 @@
|
||||
package org.baeldung;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class CustomerMap {
|
||||
|
||||
private Map<@Email(message="Must be a valid email") String, @NotNull Customer> customers;
|
||||
|
||||
public Map<String, Customer> getCustomers() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
public void setCustomers(Map<String, Customer> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
}
|
13
javaxval/src/main/java/org/baeldung/Profile.java
Normal file
13
javaxval/src/main/java/org/baeldung/Profile.java
Normal file
@ -0,0 +1,13 @@
|
||||
package org.baeldung;
|
||||
|
||||
public class Profile {
|
||||
private String companyName;
|
||||
|
||||
public String getCompanyName() {
|
||||
return companyName;
|
||||
}
|
||||
|
||||
public void setCompanyName(String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package org.baeldung.valueextractors;
|
||||
|
||||
import javax.validation.valueextraction.ExtractedValue;
|
||||
import javax.validation.valueextraction.UnwrapByDefault;
|
||||
import javax.validation.valueextraction.ValueExtractor;
|
||||
|
||||
import org.baeldung.Profile;
|
||||
|
||||
@UnwrapByDefault
|
||||
public class ProfileValueExtractor implements ValueExtractor<@ExtractedValue(type = String.class) Profile> {
|
||||
|
||||
@Override
|
||||
public void extractValues(Profile originalValue, ValueExtractor.ValueReceiver receiver) {
|
||||
receiver.value(null, originalValue.getCompanyName());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.baeldung.valueextractors.ProfileValueExtractor
|
@ -0,0 +1,88 @@
|
||||
package org.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import org.baeldung.valueextractors.ProfileValueExtractor;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ContainerValidationIntegrationTest {
|
||||
private Validator validator;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ValidatorFactory factory = Validation.byDefaultProvider().configure()
|
||||
.addValueExtractor(new ProfileValueExtractor()).buildValidatorFactory();
|
||||
validator = factory.getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmptyAddress_thenValidationFails() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
||||
customer.setAddresses(Collections.singletonList(" "));
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(1, violations.size());
|
||||
assertEquals("Address must not be blank", violations.iterator()
|
||||
.next()
|
||||
.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidEmail_thenValidationFails() {
|
||||
CustomerMap map = new CustomerMap();
|
||||
map.setCustomers(Collections.singletonMap("john", new Customer()));
|
||||
Set<ConstraintViolation<CustomerMap>> violations = validator.validate(map);
|
||||
assertEquals(1, violations.size());
|
||||
assertEquals("Must be a valid email", violations.iterator()
|
||||
.next()
|
||||
.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAgeTooLow_thenValidationFails() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
||||
customer.setAge(15);
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(1, violations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAgeNull_thenValidationSucceeds() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(0, violations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNumberOrdersValid_thenValidationSucceeds() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
||||
customer.setNumberOfOrders(OptionalInt.of(1));
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(0, violations.size());
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void whenProfileCompanyNameBlank_thenValidationFails() {
|
||||
Customer customer = new Customer();
|
||||
customer.setName("John");
|
||||
Profile profile = new Profile();
|
||||
profile.setCompanyName(" ");
|
||||
customer.setProfile(profile);
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(1, violations.size());
|
||||
}
|
||||
|
||||
}
|
@ -3,6 +3,8 @@ package com.baeldung.commons.lang3;
|
||||
import org.apache.commons.lang3.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.concurrent.ConcurrentException;
|
||||
import org.apache.commons.lang3.concurrent.BackgroundInitializer;
|
||||
|
||||
public class BuilderMethods {
|
||||
|
||||
@ -56,5 +58,36 @@ public class BuilderMethods {
|
||||
System.out.println(simple1.getName());
|
||||
System.out.println(simple1.hashCode());
|
||||
System.out.println(simple1.toString());
|
||||
|
||||
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
|
||||
|
||||
try {
|
||||
sampleLazyInitializer.get();
|
||||
} catch (ConcurrentException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer();
|
||||
sampleBackgroundInitializer.start();
|
||||
|
||||
// Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish.
|
||||
|
||||
try {
|
||||
Object result = sampleBackgroundInitializer.get();
|
||||
} catch (ConcurrentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SampleBackgroundInitializer extends BackgroundInitializer<String>{
|
||||
|
||||
@Override
|
||||
protected String initialize() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Any complex task that takes some time
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ package com.baeldung.commons.lang3;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
@ -20,6 +22,7 @@ import org.apache.commons.lang3.ArchUtils;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.apache.commons.lang3.arch.Processor;
|
||||
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
||||
import org.apache.commons.lang3.concurrent.ConcurrentException;
|
||||
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
|
||||
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
|
||||
@ -133,4 +136,14 @@ public class Lang3UtilsTest {
|
||||
assertEquals(sampleObjectOne, sampleObjectTwo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildDefaults() {
|
||||
BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder();
|
||||
BasicThreadFactory factory = builder.build();
|
||||
assertNull("No naming pattern set Yet", factory.getNamingPattern());
|
||||
BasicThreadFactory factory2 = builder.namingPattern("sampleNamingPattern").daemon(true).priority(Thread.MIN_PRIORITY).build();
|
||||
assertNotNull("Got a naming pattern", factory2.getNamingPattern());
|
||||
assertEquals("sampleNamingPattern", factory2.getNamingPattern());
|
||||
|
||||
}
|
||||
}
|
||||
|
11
testing/src/main/java/com/baeldung/junit/Calculator.java
Normal file
11
testing/src/main/java/com/baeldung/junit/Calculator.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
public class Calculator {
|
||||
public int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public int sub(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
}
|
14
testing/src/test/java/com/baeldung/junit/AdditionTest.java
Normal file
14
testing/src/test/java/com/baeldung/junit/AdditionTest.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AdditionTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public class BlockingTestRunner extends BlockJUnit4ClassRunner {
|
||||
public BlockingTestRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Statement methodInvoker(FrameworkMethod method, Object test) {
|
||||
System.out.println("invoking: " + method.getName());
|
||||
return super.methodInvoker(method, test);
|
||||
}
|
||||
}
|
17
testing/src/test/java/com/baeldung/junit/CalculatorTest.java
Normal file
17
testing/src/test/java/com/baeldung/junit/CalculatorTest.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class CalculatorTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SubstractionTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void substraction() {
|
||||
assertEquals("substraction", 2, calculator.sub(5, 3));
|
||||
}
|
||||
}
|
12
testing/src/test/java/com/baeldung/junit/SuiteTest.java
Normal file
12
testing/src/test/java/com/baeldung/junit/SuiteTest.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
AdditionTest.class,
|
||||
SubstractionTest.class})
|
||||
public class SuiteTest {
|
||||
}
|
41
testing/src/test/java/com/baeldung/junit/TestRunner.java
Normal file
41
testing/src/test/java/com/baeldung/junit/TestRunner.java
Normal file
@ -0,0 +1,41 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.Runner;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestRunner extends Runner {
|
||||
|
||||
private Class testClass;
|
||||
public TestRunner(Class testClass) {
|
||||
super();
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Description getDescription() {
|
||||
return Description.createTestDescription(testClass, "My runner description");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(RunNotifier notifier) {
|
||||
System.out.println("running the tests from MyRunner: " + testClass);
|
||||
try {
|
||||
Object testObject = testClass.newInstance();
|
||||
for (Method method : testClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(Test.class)) {
|
||||
notifier.fireTestStarted(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
method.invoke(testObject);
|
||||
notifier.fireTestFinished(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user