Merge remote-tracking branch 'origin/main' into ethereum
# Conflicts: # .idea/jarRepositories.xml
This commit is contained in:
commit
41a5778dec
7
.idea/compiler.xml
generated
7
.idea/compiler.xml
generated
@ -7,7 +7,6 @@
|
|||||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
<outputRelativeToContentRoot value="true" />
|
<outputRelativeToContentRoot value="true" />
|
||||||
<module name="jackson-annotations" />
|
|
||||||
<module name="assertion-libraries" />
|
<module name="assertion-libraries" />
|
||||||
<module name="core-java-serialization" />
|
<module name="core-java-serialization" />
|
||||||
<module name="core-java-annotations" />
|
<module name="core-java-annotations" />
|
||||||
@ -23,14 +22,14 @@
|
|||||||
<module name="core-java-datetime-conversion" />
|
<module name="core-java-datetime-conversion" />
|
||||||
<module name="core-java-lambdas" />
|
<module name="core-java-lambdas" />
|
||||||
<module name="junit-5-basics" />
|
<module name="junit-5-basics" />
|
||||||
<module name="activejdbc" />
|
|
||||||
<module name="spring-data-jpa-repo" />
|
<module name="spring-data-jpa-repo" />
|
||||||
<module name="apache-velocity" />
|
<module name="apache-velocity" />
|
||||||
<module name="jackson-conversions-2" />
|
<module name="jackson-conversions-2" />
|
||||||
|
<module name="activejdbc" />
|
||||||
<module name="codebank" />
|
<module name="codebank" />
|
||||||
<module name="core-java-collections-list" />
|
<module name="core-java-collections-list" />
|
||||||
<module name="image-compressing" />
|
|
||||||
<module name="spring-data-jpa-repo-2" />
|
<module name="spring-data-jpa-repo-2" />
|
||||||
|
<module name="image-compressing" />
|
||||||
<module name="core-java-streams" />
|
<module name="core-java-streams" />
|
||||||
<module name="core-java-io-2" />
|
<module name="core-java-io-2" />
|
||||||
<module name="core-java-io" />
|
<module name="core-java-io" />
|
||||||
@ -57,7 +56,9 @@
|
|||||||
<module name="core-java-11-2" target="11" />
|
<module name="core-java-11-2" target="11" />
|
||||||
<module name="core-java-8-2" target="11" />
|
<module name="core-java-8-2" target="11" />
|
||||||
<module name="core-java-collections-list-3" target="11" />
|
<module name="core-java-collections-list-3" target="11" />
|
||||||
|
<module name="jackson-annotations" target="17" />
|
||||||
<module name="spring-data-jpa-repo-2" target="17" />
|
<module name="spring-data-jpa-repo-2" target="17" />
|
||||||
|
<module name="xml" target="17" />
|
||||||
</bytecodeTargetLevel>
|
</bytecodeTargetLevel>
|
||||||
</component>
|
</component>
|
||||||
<component name="JavacSettings">
|
<component name="JavacSettings">
|
||||||
|
17
.idea/dataSources.xml
generated
Normal file
17
.idea/dataSources.xml
generated
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="postgres@nas1120" uuid="33f98331-7e30-4939-9d10-a9d8ae4f65f2">
|
||||||
|
<driver-ref>postgresql</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:postgresql://nas1120:5433/postgres</jdbc-url>
|
||||||
|
<jdbc-additional-properties>
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||||
|
</jdbc-additional-properties>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
</data-source>
|
||||||
|
</component>
|
||||||
|
</project>
|
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@ -112,5 +112,7 @@
|
|||||||
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/xml/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/xml/src/main/resources" charset="UTF-8" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2025 iSharkFly
|
Copyright (c) 2024 iSharkFly
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -16,11 +16,11 @@ Java 代码使用的是 JDK 17。
|
|||||||
请使用下面的联系方式和我们联系:
|
请使用下面的联系方式和我们联系:
|
||||||
|
|
||||||
| 联系方式名称 | 联系方式 |
|
| 联系方式名称 | 联系方式 |
|
||||||
|--------|------------------------------------------------------|
|
|--------|-----------------------------------------------|
|
||||||
| 电子邮件 | [service@ossez.com](mailto:service@ossez.com) |
|
| 电子邮件 | [service@ossez.com](mailto:service@ossez.com) |
|
||||||
| QQ 或微信 | 103899765 |
|
| QQ 或微信 | 103899765 |
|
||||||
| QQ 交流群 | 15186112 |
|
| QQ 交流群 | 15186112 |
|
||||||
| 社区论坛 | https://www.isharkfly.com/c/computer-program/java/15 |
|
| 社区论坛 | https://www.ossez.com/c/open-source/java/15 |
|
||||||
|
|
||||||
# 公众平台
|
# 公众平台
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ public class JavaPeriodUnitTest {
|
|||||||
|
|
||||||
Period period = Period.between(startDate, endDate);
|
Period period = Period.between(startDate, endDate);
|
||||||
|
|
||||||
logger.info(String.format("Years:%d months:%d days:%d", period.getYears(), period.getMonths(), period.getDays()));
|
logger.info("Years:{} months:{} days:{}", period.getYears(), period.getMonths(), period.getDays());
|
||||||
|
|
||||||
assertFalse(period.isNegative());
|
assertFalse(period.isNegative());
|
||||||
assertEquals(56, period.plusDays(50)
|
assertEquals(56, period.plusDays(50)
|
||||||
|
@ -1,23 +1,19 @@
|
|||||||
package com.ossez.jackson.optionalwithjackson;
|
package com.ossez.jackson.optionalwithjackson;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||||
|
|
||||||
import static io.restassured.path.json.JsonPath.from;
|
import static io.restassured.path.json.JsonPath.from;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class OptionalTypeUnitTest {
|
public class OptionalTypeUnitTest {
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
|
ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
|
||||||
|
|
||||||
|
@Test
|
||||||
public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException {
|
||||||
|
|
||||||
String subTitle = "The Parish Boy's Progress";
|
String subTitle = "The Parish Boy's Progress";
|
||||||
@ -30,12 +26,6 @@ public class OptionalTypeUnitTest {
|
|||||||
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
|
assertThat(from(result).getString("subTitle")).isEqualTo(subTitle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenFieldWithValue_whenDeserializing_thenThrowExceptio() throws JsonProcessingException {
|
|
||||||
String bookJson = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"foo\" }";
|
|
||||||
Book result = mapper.readValue(bookJson, Book.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
|
public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException {
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>jackson-annotations</module>
|
<!-- <module>jackson-annotations</module>-->
|
||||||
<module>jackson-conversions</module>
|
<module>jackson-conversions</module>
|
||||||
<module>jackson-conversions-2</module>
|
<module>jackson-conversions-2</module>
|
||||||
<module>jackson-custom-conversions</module>
|
<module>jackson-custom-conversions</module>
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
public class Dog {
|
|
||||||
private String name;
|
|
||||||
private Float weight;
|
|
||||||
|
|
||||||
public Dog(String name, Float weight) {
|
|
||||||
this.name = name;
|
|
||||||
this.weight = weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Float getWeight() {
|
|
||||||
return weight;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
public class Member {
|
|
||||||
private String name;
|
|
||||||
private int age;
|
|
||||||
|
|
||||||
public Member(String name, int age) {
|
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
public class Person {
|
|
||||||
private String name;
|
|
||||||
private Integer age;
|
|
||||||
|
|
||||||
public Person(String name, Integer age) {
|
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package com.baeldung.assertj.custom;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Person {
|
|
||||||
private String fullName;
|
|
||||||
private int age;
|
|
||||||
private List<String> nicknames;
|
|
||||||
|
|
||||||
public Person(String fullName, int age) {
|
|
||||||
this.fullName = fullName;
|
|
||||||
this.age = age;
|
|
||||||
this.nicknames = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addNickname(String nickname) {
|
|
||||||
nicknames.add(nickname);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFullName() {
|
|
||||||
return fullName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getNicknames() {
|
|
||||||
return nicknames;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package com.baeldung.assertj.exceptions;
|
|
||||||
|
|
||||||
public class CityNotFoundException extends RuntimeException {
|
|
||||||
|
|
||||||
private String city;
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
CityNotFoundException(String city, String message) {
|
|
||||||
this.city = city;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCity() {
|
|
||||||
return city;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.assertj.exceptions;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public final class CityUtils {
|
|
||||||
|
|
||||||
private static final List<String> CITIES = Arrays.asList("Tamassint", "London", "Madrid", "New york");
|
|
||||||
|
|
||||||
public static String search(String searchedCity) {
|
|
||||||
return CITIES.stream()
|
|
||||||
.filter(searchedCity::equals)
|
|
||||||
.findFirst()
|
|
||||||
.orElseThrow(() -> new CityNotFoundException(searchedCity, "The specified city is not found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.baeldung.assertj.extracting;
|
|
||||||
|
|
||||||
class Address {
|
|
||||||
private String street;
|
|
||||||
private String city;
|
|
||||||
private ZipCode zipCode;
|
|
||||||
|
|
||||||
Address(String street, String city, ZipCode zipCode) {
|
|
||||||
this.street = street;
|
|
||||||
this.city = city;
|
|
||||||
this.zipCode = zipCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStreet() {
|
|
||||||
return street;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCity() {
|
|
||||||
return city;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ZipCode getZipCode() {
|
|
||||||
return zipCode;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package com.baeldung.assertj.extracting;
|
|
||||||
|
|
||||||
class Person {
|
|
||||||
private String firstName;
|
|
||||||
private String lastName;
|
|
||||||
private Address address;
|
|
||||||
|
|
||||||
Person(String firstName, String lastName, Address address) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
this.lastName = lastName;
|
|
||||||
this.address = address;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Address getAddress() {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.baeldung.assertj.extracting;
|
|
||||||
|
|
||||||
class ZipCode {
|
|
||||||
private long zipcode;
|
|
||||||
|
|
||||||
ZipCode(long zipcode) {
|
|
||||||
this.zipcode = zipcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getZipcode() {
|
|
||||||
return zipcode;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
public abstract class Animal {
|
|
||||||
|
|
||||||
protected String name;
|
|
||||||
|
|
||||||
public Animal(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
Animal other = (Animal) obj;
|
|
||||||
if (name == null) {
|
|
||||||
if (other.name != null)
|
|
||||||
return false;
|
|
||||||
} else if (!name.equals(other.name))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class Cage {
|
|
||||||
|
|
||||||
private Set<Animal> animals = new HashSet<>();
|
|
||||||
|
|
||||||
public void put(Animal animal) {
|
|
||||||
animals.add(animal);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void put(Animal... animals) {
|
|
||||||
this.animals.addAll(Arrays.asList(animals));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Animal release(Animal animal) {
|
|
||||||
return animals.remove(animal) ? animal : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void open() {
|
|
||||||
animals.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasAnimals() {
|
|
||||||
return animals.size() > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return animals.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<Animal> getAnimals() {
|
|
||||||
return this.animals;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return animals.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Cage [animals=" + animals + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
public class Cat extends Animal {
|
|
||||||
|
|
||||||
public Cat(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Cat [name=" + name + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
public class Dog extends Animal {
|
|
||||||
|
|
||||||
public Dog(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Dog [name=" + name + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package com.baeldung.truth;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class User implements Comparable<User> {
|
|
||||||
private String name = "John Doe";
|
|
||||||
private List<String> emails = Arrays.asList("contact@baeldung.com", "staff@baeldung.com");
|
|
||||||
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getEmails() {
|
|
||||||
return emails;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmails(List<String> emails) {
|
|
||||||
this.emails = emails;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int hash = 5;
|
|
||||||
hash = 37 * hash + Objects.hashCode(this.name);
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj == null || getClass() != obj.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final User other = (User) obj;
|
|
||||||
return Objects.equals(this.name, other.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(User o) {
|
|
||||||
return this.getName()
|
|
||||||
.compareToIgnoreCase(o.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.baeldung.truth;
|
|
||||||
|
|
||||||
import com.google.common.truth.ComparableSubject;
|
|
||||||
import com.google.common.truth.FailureStrategy;
|
|
||||||
import com.google.common.truth.IterableSubject;
|
|
||||||
import com.google.common.truth.SubjectFactory;
|
|
||||||
import com.google.common.truth.Truth;
|
|
||||||
|
|
||||||
public class UserSubject extends ComparableSubject<UserSubject, User> {
|
|
||||||
|
|
||||||
private UserSubject(FailureStrategy failureStrategy, User target) {
|
|
||||||
super(failureStrategy, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final SubjectFactory<UserSubject, User> USER_SUBJECT_FACTORY = new SubjectFactory<UserSubject, User>() {
|
|
||||||
@Override
|
|
||||||
public UserSubject getSubject(FailureStrategy failureStrategy, User target) {
|
|
||||||
return new UserSubject(failureStrategy, target);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static UserSubject assertThat(User user) {
|
|
||||||
return Truth.assertAbout(USER_SUBJECT_FACTORY)
|
|
||||||
.that(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Our API begins here
|
|
||||||
public void hasName(String name) {
|
|
||||||
if (!actual().getName()
|
|
||||||
.equals(name)) {
|
|
||||||
fail("has name", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void hasNameIgnoringCase(String name) {
|
|
||||||
if (!actual().getName()
|
|
||||||
.equalsIgnoreCase(name)) {
|
|
||||||
fail("has name ignoring case", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IterableSubject emails() {
|
|
||||||
return Truth.assertThat(actual().getEmails());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.allOf;
|
|
||||||
import static org.assertj.core.api.Assertions.anyOf;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.assertj.core.api.Assertions.not;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.assertj.core.api.Condition;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class AssertJConditionUnitTest {
|
|
||||||
private Condition<Member> senior = new Condition<>(m -> m.getAge() >= 60, "senior");
|
|
||||||
private Condition<Member> nameJohn = new Condition<>(m -> m.getName().equalsIgnoreCase("John"), "name John");
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingMemberAgeCondition_thenCorrect() {
|
|
||||||
Member member = new Member("John", 65);
|
|
||||||
assertThat(member).is(senior);
|
|
||||||
|
|
||||||
try {
|
|
||||||
assertThat(member).isNot(senior);
|
|
||||||
fail();
|
|
||||||
} catch (AssertionError e) {
|
|
||||||
assertThat(e).hasMessageContaining("not to be senior");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingMemberNameCondition_thenCorrect() {
|
|
||||||
Member member = new Member("Jane", 60);
|
|
||||||
assertThat(member).doesNotHave(nameJohn);
|
|
||||||
|
|
||||||
try {
|
|
||||||
assertThat(member).has(nameJohn);
|
|
||||||
fail();
|
|
||||||
} catch (AssertionError e) {
|
|
||||||
assertThat(e).hasMessageContaining("name John");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCollectionConditionsAreSatisfied_thenCorrect() {
|
|
||||||
List<Member> members = new ArrayList<>();
|
|
||||||
members.add(new Member("Alice", 50));
|
|
||||||
members.add(new Member("Bob", 60));
|
|
||||||
|
|
||||||
assertThat(members).haveExactly(1, senior);
|
|
||||||
assertThat(members).doNotHave(nameJohn);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCombiningAllOfConditions_thenCorrect() {
|
|
||||||
Member john = new Member("John", 60);
|
|
||||||
Member jane = new Member("Jane", 50);
|
|
||||||
|
|
||||||
assertThat(john).is(allOf(senior, nameJohn));
|
|
||||||
assertThat(jane).is(allOf(not(nameJohn), not(senior)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCombiningAnyOfConditions_thenCorrect() {
|
|
||||||
Member john = new Member("John", 50);
|
|
||||||
Member jane = new Member("Jane", 60);
|
|
||||||
|
|
||||||
assertThat(john).is(anyOf(senior, nameJohn));
|
|
||||||
assertThat(jane).is(anyOf(nameJohn, senior));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
import org.assertj.core.util.Maps;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
|
|
||||||
public class AssertJCoreUnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingReferences_thenNotEqual() throws Exception {
|
|
||||||
Dog fido = new Dog("Fido", 5.15f);
|
|
||||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
|
||||||
|
|
||||||
assertThat(fido).isNotEqualTo(fidosClone);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingFields_thenEqual() throws Exception {
|
|
||||||
Dog fido = new Dog("Fido", 5.15f);
|
|
||||||
Dog fidosClone = new Dog("Fido", 5.15f);
|
|
||||||
|
|
||||||
assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingForElement_thenContains() throws Exception {
|
|
||||||
List<String> list = Arrays.asList("1", "2", "3");
|
|
||||||
|
|
||||||
assertThat(list).contains("1");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingForElement_thenMultipleAssertions() throws Exception {
|
|
||||||
List<String> list = Arrays.asList("1", "2", "3");
|
|
||||||
|
|
||||||
assertThat(list).isNotEmpty();
|
|
||||||
assertThat(list).startsWith("1");
|
|
||||||
assertThat(list).doesNotContainNull();
|
|
||||||
|
|
||||||
assertThat(list).isNotEmpty().contains("1").startsWith("1").doesNotContainNull().containsSequence("2", "3");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingRunnable_thenIsInterface() throws Exception {
|
|
||||||
assertThat(Runnable.class).isInterface();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingCharacter_thenIsUnicode() throws Exception {
|
|
||||||
char someCharacter = 'c';
|
|
||||||
|
|
||||||
assertThat(someCharacter).isNotEqualTo('a').inUnicode().isGreaterThanOrEqualTo('b').isLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenAssigningNSEExToException_thenIsAssignable() throws Exception {
|
|
||||||
assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingWithOffset_thenEquals() throws Exception {
|
|
||||||
assertThat(5.1).isEqualTo(5, withPrecision(1d));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_then() throws Exception {
|
|
||||||
assertThat("".isEmpty()).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingFile_then() throws Exception {
|
|
||||||
final File someFile = File.createTempFile("aaa", "bbb");
|
|
||||||
someFile.deleteOnExit();
|
|
||||||
|
|
||||||
assertThat(someFile).exists().isFile().canRead().canWrite();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIS_then() throws Exception {
|
|
||||||
InputStream given = new ByteArrayInputStream("foo".getBytes());
|
|
||||||
InputStream expected = new ByteArrayInputStream("foo".getBytes());
|
|
||||||
|
|
||||||
assertThat(given).hasSameContentAs(expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenGivenMap_then() throws Exception {
|
|
||||||
Map<Integer, String> map = Maps.newHashMap(2, "a");
|
|
||||||
|
|
||||||
assertThat(map).isNotEmpty().containsKey(2).doesNotContainKeys(10).contains(entry(2, "a"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenGivenException_then() throws Exception {
|
|
||||||
Exception ex = new Exception("abc");
|
|
||||||
|
|
||||||
assertThat(ex).hasNoCause().hasMessageEndingWith("c");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore // IN ORDER TO TEST, REMOVE THIS LINE
|
|
||||||
@Test
|
|
||||||
public void whenRunningAssertion_thenDescribed() throws Exception {
|
|
||||||
Person person = new Person("Alex", 34);
|
|
||||||
|
|
||||||
assertThat(person.getAge()).as("%s's age should be equal to 100").isEqualTo(100);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,96 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
import com.google.common.base.Optional;
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
|
||||||
import com.google.common.collect.HashBasedTable;
|
|
||||||
import com.google.common.collect.Multimap;
|
|
||||||
import com.google.common.collect.Multimaps;
|
|
||||||
import com.google.common.collect.Range;
|
|
||||||
import com.google.common.collect.Table;
|
|
||||||
import com.google.common.collect.TreeRangeMap;
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
import org.assertj.guava.data.MapEntry;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
import static org.assertj.guava.api.Assertions.assertThat;
|
|
||||||
import static org.assertj.guava.api.Assertions.entry;
|
|
||||||
|
|
||||||
public class AssertJGuavaUnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception {
|
|
||||||
final File temp1 = File.createTempFile("bael", "dung1");
|
|
||||||
final File temp2 = File.createTempFile("bael", "dung2");
|
|
||||||
|
|
||||||
assertThat(Files.asByteSource(temp1)).hasSize(0).hasSameContentAs(Files.asByteSource(temp2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenMultimap_whenVerifying_thenCorrect() throws Exception {
|
|
||||||
final Multimap<Integer, String> mmap = ArrayListMultimap.create();
|
|
||||||
mmap.put(1, "one");
|
|
||||||
mmap.put(1, "1");
|
|
||||||
|
|
||||||
assertThat(mmap).hasSize(2).containsKeys(1).contains(entry(1, "one")).contains(entry(1, "1"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenMultimaps_whenVerifyingContent_thenCorrect() throws Exception {
|
|
||||||
final Multimap<Integer, String> mmap1 = ArrayListMultimap.create();
|
|
||||||
mmap1.put(1, "one");
|
|
||||||
mmap1.put(1, "1");
|
|
||||||
mmap1.put(2, "two");
|
|
||||||
mmap1.put(2, "2");
|
|
||||||
|
|
||||||
final Multimap<Integer, String> mmap1_clone = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
|
||||||
mmap1_clone.put(1, "one");
|
|
||||||
mmap1_clone.put(1, "1");
|
|
||||||
mmap1_clone.put(2, "two");
|
|
||||||
mmap1_clone.put(2, "2");
|
|
||||||
|
|
||||||
final Multimap<Integer, String> mmap2 = Multimaps.newSetMultimap(new HashMap<>(), HashSet::new);
|
|
||||||
mmap2.put(1, "one");
|
|
||||||
mmap2.put(1, "1");
|
|
||||||
|
|
||||||
assertThat(mmap1).containsAllEntriesOf(mmap2).containsAllEntriesOf(mmap1_clone).hasSameEntriesAs(mmap1_clone);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception {
|
|
||||||
final Optional<String> something = Optional.of("something");
|
|
||||||
|
|
||||||
assertThat(something).isPresent().extractingValue().isEqualTo("something");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception {
|
|
||||||
final Range<String> range = Range.openClosed("a", "g");
|
|
||||||
|
|
||||||
assertThat(range).hasOpenedLowerBound().isNotEmpty().hasClosedUpperBound().contains("b");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception {
|
|
||||||
final TreeRangeMap<Integer, String> map = TreeRangeMap.create();
|
|
||||||
|
|
||||||
map.put(Range.closed(0, 60), "F");
|
|
||||||
map.put(Range.closed(61, 70), "D");
|
|
||||||
|
|
||||||
assertThat(map).isNotEmpty().containsKeys(0).contains(MapEntry.entry(34, "F"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception {
|
|
||||||
final Table<Integer, String, String> table = HashBasedTable.create(2, 2);
|
|
||||||
|
|
||||||
table.put(1, "A", "PRESENT");
|
|
||||||
table.put(1, "B", "ABSENT");
|
|
||||||
|
|
||||||
assertThat(table).hasRowCount(1).containsValues("ABSENT").containsCell(1, "B", "ABSENT");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,108 +0,0 @@
|
|||||||
package com.baeldung.assertj;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import static java.time.LocalDate.ofYearDay;
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
public class AssertJJava8UnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenOptional_shouldAssert() throws Exception {
|
|
||||||
final Optional<String> givenOptional = Optional.of("something");
|
|
||||||
|
|
||||||
assertThat(givenOptional).isPresent().hasValue("something");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenPredicate_shouldAssert() throws Exception {
|
|
||||||
final Predicate<String> predicate = s -> s.length() > 4;
|
|
||||||
|
|
||||||
assertThat(predicate).accepts("aaaaa", "bbbbb").rejects("a", "b").acceptsAll(asList("aaaaa", "bbbbb")).rejectsAll(asList("a", "b"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLocalDate_shouldAssert() throws Exception {
|
|
||||||
final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
|
|
||||||
final LocalDate todayDate = LocalDate.now();
|
|
||||||
|
|
||||||
assertThat(givenLocalDate).isBefore(LocalDate.of(2020, 7, 8)).isAfterOrEqualTo(LocalDate.of(1989, 7, 8));
|
|
||||||
|
|
||||||
assertThat(todayDate).isAfter(LocalDate.of(1989, 7, 8)).isToday();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLocalDateTime_shouldAssert() throws Exception {
|
|
||||||
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
|
|
||||||
|
|
||||||
assertThat(givenLocalDate).isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenLocalTime_shouldAssert() throws Exception {
|
|
||||||
final LocalTime givenLocalTime = LocalTime.of(12, 15);
|
|
||||||
|
|
||||||
assertThat(givenLocalTime).isAfter(LocalTime.of(1, 0)).hasSameHourAs(LocalTime.of(12, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenList_shouldAssertFlatExtracting() throws Exception {
|
|
||||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
|
||||||
|
|
||||||
assertThat(givenList).flatExtracting(LocalDate::getYear).contains(2015);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception {
|
|
||||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
|
||||||
|
|
||||||
assertThat(givenList).flatExtracting(LocalDate::isLeapYear).contains(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenList_shouldAssertFlatExtractingClass() throws Exception {
|
|
||||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
|
||||||
|
|
||||||
assertThat(givenList).flatExtracting(Object::getClass).contains(LocalDate.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
|
|
||||||
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
|
|
||||||
|
|
||||||
assertThat(givenList).flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth).contains(2015, 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_shouldSatisfy() throws Exception {
|
|
||||||
final String givenString = "someString";
|
|
||||||
|
|
||||||
assertThat(givenString).satisfies(s -> {
|
|
||||||
assertThat(s).isNotEmpty();
|
|
||||||
assertThat(s).hasSize(10);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenString_shouldMatch() throws Exception {
|
|
||||||
final String emptyString = "";
|
|
||||||
|
|
||||||
assertThat(emptyString).matches(String::isEmpty);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
|
|
||||||
final List<String> givenList = Arrays.asList("");
|
|
||||||
|
|
||||||
assertThat(givenList).hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
package com.baeldung.assertj.custom;
|
|
||||||
|
|
||||||
import static com.baeldung.assertj.custom.Assertions.assertThat;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.ExpectedException;
|
|
||||||
|
|
||||||
public class AssertJCustomAssertionsUnitTest {
|
|
||||||
@Rule
|
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPersonNameMatches_thenCorrect() {
|
|
||||||
Person person = new Person("John Doe", 20);
|
|
||||||
assertThat(person).hasFullName("John Doe");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPersonAgeLessThanEighteen_thenNotAdult() {
|
|
||||||
Person person = new Person("Jane Roe", 16);
|
|
||||||
|
|
||||||
try {
|
|
||||||
assertThat(person).isAdult();
|
|
||||||
fail();
|
|
||||||
} catch (AssertionError e) {
|
|
||||||
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to be adult");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() {
|
|
||||||
Person person = new Person("John Doe", 20);
|
|
||||||
person.addNickname("Nick");
|
|
||||||
|
|
||||||
try {
|
|
||||||
assertThat(person).hasNickname("John");
|
|
||||||
fail();
|
|
||||||
} catch (AssertionError e) {
|
|
||||||
org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected person to have nickname John");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package com.baeldung.assertj.custom;
|
|
||||||
|
|
||||||
public class Assertions {
|
|
||||||
public static PersonAssert assertThat(Person actual) {
|
|
||||||
return new PersonAssert(actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static factory methods of other assertion classes
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.assertj.custom;
|
|
||||||
|
|
||||||
import org.assertj.core.api.AbstractAssert;
|
|
||||||
|
|
||||||
public class PersonAssert extends AbstractAssert<PersonAssert, Person> {
|
|
||||||
|
|
||||||
public PersonAssert(Person actual) {
|
|
||||||
super(actual, PersonAssert.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static PersonAssert assertThat(Person actual) {
|
|
||||||
return new PersonAssert(actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PersonAssert hasFullName(String fullName) {
|
|
||||||
isNotNull();
|
|
||||||
if (!actual.getFullName().equals(fullName)) {
|
|
||||||
failWithMessage("Expected person to have full name %s but was %s", fullName, actual.getFullName());
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PersonAssert isAdult() {
|
|
||||||
isNotNull();
|
|
||||||
if (actual.getAge() < 18) {
|
|
||||||
failWithMessage("Expected person to be adult");
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PersonAssert hasNickname(String nickName) {
|
|
||||||
isNotNull();
|
|
||||||
if (!actual.getNicknames().contains(nickName)) {
|
|
||||||
failWithMessage("Expected person to have nickname %s", nickName);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.assertj.exceptions;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.assertj.core.api.Assertions.fail;
|
|
||||||
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class Java7StyleAssertions {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenDividingByZero_thenArithmeticException() {
|
|
||||||
try {
|
|
||||||
int numerator = 10;
|
|
||||||
int denominator = 0;
|
|
||||||
int quotient = numerator / denominator;
|
|
||||||
fail("ArithmeticException expected because dividing by zero yields an ArithmeticException.");
|
|
||||||
failBecauseExceptionWasNotThrown(ArithmeticException.class);
|
|
||||||
} catch (Exception e) {
|
|
||||||
assertThat(e).hasMessage("/ by zero");
|
|
||||||
assertThat(e).isInstanceOf(ArithmeticException.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
package com.baeldung.assertj.exceptions;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
||||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
|
||||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class Java8StyleAssertions {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() {
|
|
||||||
assertThatThrownBy(() -> {
|
|
||||||
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
|
||||||
myStringList.get(2);
|
|
||||||
}).isInstanceOf(IndexOutOfBoundsException.class)
|
|
||||||
.hasMessageStartingWith("Index: 2")
|
|
||||||
.hasMessageContaining("2")
|
|
||||||
.hasMessageEndingWith("Size: 2")
|
|
||||||
.hasMessageContaining("Index: 2, Size: 2")
|
|
||||||
.hasMessage("Index: %s, Size: %s", 2, 2)
|
|
||||||
.hasMessageMatching("Index: \\d+, Size: \\d+")
|
|
||||||
.hasNoCause();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() {
|
|
||||||
assertThatThrownBy(() -> {
|
|
||||||
try {
|
|
||||||
throw new IOException();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}).isInstanceOf(RuntimeException.class)
|
|
||||||
.hasCauseInstanceOf(IOException.class)
|
|
||||||
.hasStackTraceContaining("IOException");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenDividingByZero_thenArithmeticException() {
|
|
||||||
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
|
||||||
int numerator = 10;
|
|
||||||
int denominator = 0;
|
|
||||||
int quotient = numerator / denominator;
|
|
||||||
})
|
|
||||||
.withMessageContaining("/ by zero");
|
|
||||||
|
|
||||||
// Alternatively:
|
|
||||||
|
|
||||||
// when
|
|
||||||
Throwable thrown = catchThrowable(() -> {
|
|
||||||
int numerator = 10;
|
|
||||||
int denominator = 0;
|
|
||||||
int quotient = numerator / denominator;
|
|
||||||
});
|
|
||||||
|
|
||||||
// then
|
|
||||||
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
|
||||||
.hasMessageContaining("/ by zero");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingCatchThrowableOfType_thenAssertField() {
|
|
||||||
String givenCity = "Paris";
|
|
||||||
CityNotFoundException exception = catchThrowableOfType(() -> CityUtils.search(givenCity), CityNotFoundException.class);
|
|
||||||
|
|
||||||
assertThat(exception.getCity()).isEqualTo(givenCity);
|
|
||||||
assertThat(exception.getMessage()).isEqualTo("The specified city is not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUsingAssertThatThrownBy_thenAssertField() {
|
|
||||||
String givenCity = "Geneva";
|
|
||||||
assertThatThrownBy(() -> CityUtils.search(givenCity)).isInstanceOf(CityNotFoundException.class)
|
|
||||||
.extracting("city")
|
|
||||||
.isEqualTo(givenCity);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.baeldung.assertj.extracting;
|
|
||||||
|
|
||||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.as;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
class AssertJExtractingUnitTest {
|
|
||||||
static final List<Address> RESTRICTED_ADDRESSES = new ArrayList<>();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenUsingRegularAssertionFlow_thenCorrect() {
|
|
||||||
|
|
||||||
// Given
|
|
||||||
Person person = new Person("aName", "aLastName", new Address("aStreet", "aCity", new ZipCode(90210)));
|
|
||||||
|
|
||||||
// Then
|
|
||||||
Address address = person.getAddress();
|
|
||||||
assertThat(address).isNotNull()
|
|
||||||
.isNotIn(RESTRICTED_ADDRESSES);
|
|
||||||
ZipCode zipCode = address.getZipCode();
|
|
||||||
assertThat(zipCode).isNotNull();
|
|
||||||
assertThat(zipCode.getZipcode()).isBetween(1000L, 100_000L);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void whenUsingExtractingAssertionFlow_thenCorrect() {
|
|
||||||
|
|
||||||
// Given
|
|
||||||
Person person = new Person("aName", "aLastName", new Address("aStreet", "aCity", new ZipCode(90210)));
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertThat(person)
|
|
||||||
.extracting(Person::getAddress)
|
|
||||||
.isNotNull()
|
|
||||||
.isNotIn(RESTRICTED_ADDRESSES)
|
|
||||||
.extracting(Address::getZipCode)
|
|
||||||
.isNotNull()
|
|
||||||
.extracting(ZipCode::getZipcode, as(InstanceOfAssertFactories.LONG))
|
|
||||||
.isBetween(1_000L, 100_000L);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
package com.baeldung.testing.jgotesting;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import org.jgotesting.rule.JGoTestRule;
|
|
||||||
import static org.jgotesting.Assert.*; // same methods as org.junit.Assert.*
|
|
||||||
import static org.jgotesting.Check.*; // ditto, with different names
|
|
||||||
import static org.jgotesting.Testing.*;
|
|
||||||
import org.jgotesting.Checker;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class JGoTestingUnitTest {
|
|
||||||
@Rule
|
|
||||||
public final JGoTestRule test = new JGoTestRule();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingIntegers_thenEqual() {
|
|
||||||
int anInt = 10;
|
|
||||||
|
|
||||||
assertEquals(anInt, 10);
|
|
||||||
checkEquals(anInt, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenComparingNumbers_thenLoggedMessage() {
|
|
||||||
log("There was something wrong when comparing numbers");
|
|
||||||
|
|
||||||
int anInt = 10;
|
|
||||||
int anotherInt = 10;
|
|
||||||
|
|
||||||
checkEquals(anInt, 10);
|
|
||||||
checkTrue("First number should be bigger", 10 > anotherInt);
|
|
||||||
checkSame(anInt, anotherInt);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenComparingNumbers_thenFormattedMessage() {
|
|
||||||
int anInt = 10;
|
|
||||||
int anotherInt = 10;
|
|
||||||
|
|
||||||
logf("There was something wrong when comparing numbers %d and %d", anInt, anotherInt);
|
|
||||||
|
|
||||||
checkEquals(anInt, 10);
|
|
||||||
checkTrue("First number should be bigger", 10 > anotherInt);
|
|
||||||
checkSame(anInt, anotherInt);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingStrings_thenMultipleAssertions() {
|
|
||||||
String aString = "This is a string";
|
|
||||||
String anotherString = "This Is A String";
|
|
||||||
|
|
||||||
test.check(aString, equalToIgnoringCase(anotherString))
|
|
||||||
.check(aString.length() == 16)
|
|
||||||
.check(aString.startsWith("This"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenComparingStrings_thenMultipleFailingAssertions() {
|
|
||||||
String aString = "the test string";
|
|
||||||
String anotherString = "The Test String";
|
|
||||||
|
|
||||||
checkEquals("Strings are not equal!", aString, anotherString);
|
|
||||||
checkTrue("String is longer than one character", aString.length() == 1);
|
|
||||||
checkSame("Strings are not the same", aString, anotherString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void givenFile_whenDoesnotExists_thenTerminated() throws Exception {
|
|
||||||
File aFile = new File("a_dummy_file.txt");
|
|
||||||
terminateIf(aFile.exists(), is(false));
|
|
||||||
|
|
||||||
// This doesn't get executed
|
|
||||||
checkEquals(aFile.getName(), "a_dummy_file.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenChecker_whenComparingStrings_thenEqual() throws Exception {
|
|
||||||
Checker<String> aChecker = s -> s.matches("\\d+");
|
|
||||||
|
|
||||||
String aString = "1235";
|
|
||||||
test.check(aString, aChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,126 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
import static org.javalite.test.jspec.JSpec.$;
|
|
||||||
import static org.javalite.test.jspec.JSpec.expect;
|
|
||||||
import static org.javalite.test.jspec.JSpec.the;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.javalite.test.jspec.DifferenceExpectation;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class CageUnitTest {
|
|
||||||
|
|
||||||
Cat tomCat = new Cat("Tom");
|
|
||||||
Cat felixCat = new Cat("Felix");
|
|
||||||
Dog boltDog = new Dog("Bolt");
|
|
||||||
Cage cage = new Cage();
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void puttingAnimals_shouldIncreaseCageSize() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, boltDog);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(cage.size()).shouldEqual(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void releasingAnimals_shouldDecreaseCageSize() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, boltDog);
|
|
||||||
cage.release(tomCat);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(cage.size()).shouldEqual(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void puttingAnimals_shouldLeaveThemInsideTheCage() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, boltDog);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(cage).shouldHave("animals");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void openingTheCage_shouldReleaseAllAnimals() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, boltDog);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(cage).shouldNotBe("empty");
|
|
||||||
|
|
||||||
// When
|
|
||||||
cage.open();
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(cage).shouldBe("empty");
|
|
||||||
the(cage.isEmpty()).shouldBeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void comparingTwoDogs() {
|
|
||||||
// When
|
|
||||||
Dog firstDog = new Dog("Rex");
|
|
||||||
Dog secondDog = new Dog("Rex");
|
|
||||||
|
|
||||||
// Then
|
|
||||||
$(firstDog).shouldEqual(secondDog);
|
|
||||||
$(firstDog).shouldNotBeTheSameAs(secondDog);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void puttingCatsOnly_shouldLetCageAnimalsToContainCats() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, felixCat);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
Set<Animal> animals = cage.getAnimals();
|
|
||||||
the(animals).shouldContain(tomCat);
|
|
||||||
the(animals).shouldContain(felixCat);
|
|
||||||
the(animals).shouldNotContain(boltDog);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void puttingCatsOnly_shouldLetCageToContainCats() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, felixCat);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
// Check with toString of the tested objects
|
|
||||||
the(cage).shouldContain(tomCat);
|
|
||||||
the(cage).shouldContain(felixCat);
|
|
||||||
the(cage).shouldNotContain(boltDog);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void puttingMoreAnimals_shouldChangeSize() {
|
|
||||||
// When
|
|
||||||
cage.put(tomCat, boltDog);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
expect( new DifferenceExpectation<Integer>(cage.size()) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer exec() {
|
|
||||||
cage.release(tomCat);
|
|
||||||
return cage.size();
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void releasingTheDog_shouldReleaseAnAnimalOfDogType() {
|
|
||||||
// When
|
|
||||||
cage.put(boltDog);
|
|
||||||
Animal releasedAnimal = cage.release(boltDog);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
the(releasedAnimal).shouldNotBeNull();
|
|
||||||
the(releasedAnimal).shouldBeA(Dog.class);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package com.baeldung.jspec;
|
|
||||||
|
|
||||||
import static org.javalite.test.jspec.JSpec.$;
|
|
||||||
import static org.javalite.test.jspec.JSpec.a;
|
|
||||||
import static org.javalite.test.jspec.JSpec.expect;
|
|
||||||
import static org.javalite.test.jspec.JSpec.it;
|
|
||||||
import static org.javalite.test.jspec.JSpec.the;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.javalite.test.jspec.ExceptionExpectation;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class JSpecUnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onePlusTwo_shouldEqualThree() {
|
|
||||||
$(1 + 2).shouldEqual(3);
|
|
||||||
a(1 + 2).shouldEqual(3);
|
|
||||||
the(1 + 2).shouldEqual(3);
|
|
||||||
it(1 + 2).shouldEqual(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void messageShouldContainJSpec() {
|
|
||||||
String message = "Welcome to JSpec demo";
|
|
||||||
// The message should not be empty
|
|
||||||
the(message).shouldNotBe("empty");
|
|
||||||
// The message should contain JSpec
|
|
||||||
the(message).shouldContain("JSpec");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void colorsListShouldContainRed() {
|
|
||||||
List<String> colorsList = Arrays.asList("red", "green", "blue");
|
|
||||||
$(colorsList).shouldContain("red");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void guessedNumberShouldEqualHiddenNumber() {
|
|
||||||
Integer guessedNumber = 11;
|
|
||||||
Integer hiddenNumber = 11;
|
|
||||||
|
|
||||||
$(guessedNumber).shouldEqual(hiddenNumber);
|
|
||||||
$(guessedNumber).shouldNotBeTheSameAs(hiddenNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void dividingByThero_shouldThrowArithmeticException() {
|
|
||||||
expect(new ExceptionExpectation<ArithmeticException>(ArithmeticException.class) {
|
|
||||||
@Override
|
|
||||||
public void exec() throws ArithmeticException {
|
|
||||||
System.out.println(1 / 0);
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,561 +0,0 @@
|
|||||||
package com.baeldung.truth;
|
|
||||||
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
|
||||||
import com.google.common.collect.Multimap;
|
|
||||||
import com.google.common.collect.Range;
|
|
||||||
import com.google.common.collect.Table;
|
|
||||||
import com.google.common.collect.TreeBasedTable;
|
|
||||||
import com.google.common.collect.TreeMultiset;
|
|
||||||
import static com.baeldung.truth.UserSubject.*;
|
|
||||||
import static com.google.common.truth.Truth.*;
|
|
||||||
import static com.google.common.truth.Truth8.*;
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.stream.IntStream;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class GoogleTruthUnitTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingInteger_thenEqual() {
|
|
||||||
int anInt = 10;
|
|
||||||
|
|
||||||
assertThat(anInt).isEqualTo(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingFloat_thenIsBigger() {
|
|
||||||
float aFloat = 10.0f;
|
|
||||||
|
|
||||||
assertThat(aFloat).isGreaterThan(1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingDouble_thenIsSmaller() {
|
|
||||||
double aDouble = 10.0f;
|
|
||||||
|
|
||||||
assertThat(aDouble).isLessThan(20.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingFloat_thenWithinPrecision() {
|
|
||||||
float aFloat = 23.04f;
|
|
||||||
|
|
||||||
assertThat(aFloat).isWithin(1.3f)
|
|
||||||
.of(23.3f);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingFloat_thenNotWithinPrecision() {
|
|
||||||
float aFloat = 23.04f;
|
|
||||||
|
|
||||||
assertThat(aFloat).isNotWithin(1.3f)
|
|
||||||
.of(100f);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingDouble_thenWithinPrecision() {
|
|
||||||
double aDouble = 22.18;
|
|
||||||
|
|
||||||
assertThat(aDouble).isWithin(2)
|
|
||||||
.of(23d);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingDouble_thenNotWithinPrecision() {
|
|
||||||
double aDouble = 22.08;
|
|
||||||
|
|
||||||
assertThat(aDouble).isNotWithin(2)
|
|
||||||
.of(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingBigDecimal_thenEqualIgnoringScale() {
|
|
||||||
BigDecimal aBigDecimal = BigDecimal.valueOf(1000, 3);
|
|
||||||
|
|
||||||
assertThat(aBigDecimal).isEqualToIgnoringScale(new BigDecimal(1.0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingBoolean_thenTrue() {
|
|
||||||
boolean aBoolean = true;
|
|
||||||
|
|
||||||
assertThat(aBoolean).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingBoolean_thenFalse() {
|
|
||||||
boolean aBoolean = false;
|
|
||||||
|
|
||||||
assertThat(aBoolean).isFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingArrays_thenEqual() {
|
|
||||||
String[] firstArrayOfStrings = { "one", "two", "three" };
|
|
||||||
String[] secondArrayOfStrings = { "one", "two", "three" };
|
|
||||||
|
|
||||||
assertThat(firstArrayOfStrings).isEqualTo(secondArrayOfStrings);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingArrays_thenNotEqual() {
|
|
||||||
String[] firstArrayOfStrings = { "one", "two", "three" };
|
|
||||||
String[] secondArrayOfStrings = { "three", "two", "one" };
|
|
||||||
|
|
||||||
assertThat(firstArrayOfStrings).isNotEqualTo(secondArrayOfStrings);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingArray_thenEmpty() {
|
|
||||||
Object[] anArray = {};
|
|
||||||
|
|
||||||
assertThat(anArray).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingArray_thenNotEmpty() {
|
|
||||||
String[] arrayOfStrings = { "One String " };
|
|
||||||
|
|
||||||
assertThat(arrayOfStrings).isNotEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingArrayOfDoubles_thenWithinPrecision() {
|
|
||||||
double[] arrayOfDoubles = { 1, 2, 3, 4, 5 };
|
|
||||||
|
|
||||||
assertThat(arrayOfDoubles).hasValuesWithin(5)
|
|
||||||
.of(6, 7, 8, 9, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUsers_thenEqual() {
|
|
||||||
User aUser = new User("John Doe");
|
|
||||||
User anotherUser = new User("John Doe");
|
|
||||||
|
|
||||||
assertThat(aUser).isEqualTo(anotherUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUser_thenIsNull() {
|
|
||||||
User aUser = null;
|
|
||||||
|
|
||||||
assertThat(aUser).isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUser_thenNotNull() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUser_thenInstanceOf() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).isInstanceOf(User.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUser_thenInList() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).isIn(Arrays.asList(1, 3, aUser, null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUser_thenNotInList() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).isNotIn(Arrays.asList(1, 3, "Three"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingNullUser_thenInList() {
|
|
||||||
User aUser = null;
|
|
||||||
User anotherUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).isIn(Arrays.asList(1, 3, anotherUser, null));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenStartsWithString() {
|
|
||||||
String aString = "This is a string";
|
|
||||||
|
|
||||||
assertThat(aString).startsWith("This");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenContainsString() {
|
|
||||||
String aString = "This is a string";
|
|
||||||
|
|
||||||
assertThat(aString).contains("is a");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenEndsWithString() {
|
|
||||||
String aString = "This is a string";
|
|
||||||
|
|
||||||
assertThat(aString).endsWith("string");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenExpectedLength() {
|
|
||||||
String aString = "This is a string";
|
|
||||||
|
|
||||||
assertThat(aString).hasLength(16);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenEmpty() {
|
|
||||||
String aString = "";
|
|
||||||
|
|
||||||
assertThat(aString).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingString_thenMatches() {
|
|
||||||
String aString = "The string to match";
|
|
||||||
|
|
||||||
assertThat(aString).matches(Pattern.compile("[a-zA-Z\\s]+"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingComparable_thenAtLeast() {
|
|
||||||
Comparable<Integer> aComparable = 5;
|
|
||||||
|
|
||||||
assertThat(aComparable).isAtLeast(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingComparable_thenAtMost() {
|
|
||||||
Comparable<Integer> aComparable = 5;
|
|
||||||
|
|
||||||
assertThat(aComparable).isAtMost(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingComparable_thenInList() {
|
|
||||||
Comparable<Integer> aComparable = 5;
|
|
||||||
|
|
||||||
assertThat(aComparable).isIn(Arrays.asList(4, 5, 6));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingComparable_thenInRange() {
|
|
||||||
Comparable<Integer> aComparable = 5;
|
|
||||||
|
|
||||||
assertThat(aComparable).isIn(Range.closed(1, 10));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingComparable_thenNotInRange() {
|
|
||||||
Comparable<Integer> aComparable = 5;
|
|
||||||
|
|
||||||
assertThat(aComparable).isNotIn(Range.closed(10, 15));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenComparingUsers_thenEquivalent() {
|
|
||||||
User aUser = new User();
|
|
||||||
aUser.setName("John Doe");
|
|
||||||
|
|
||||||
User anotherUser = new User();
|
|
||||||
anotherUser.setName("john doe");
|
|
||||||
|
|
||||||
assertThat(aUser).isEquivalentAccordingToCompareTo(anotherUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContains() {
|
|
||||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
|
||||||
|
|
||||||
assertThat(aList).contains(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenDoesNotContains() {
|
|
||||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
|
||||||
|
|
||||||
assertThat(aList).doesNotContain(9);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContainsAny() {
|
|
||||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
|
||||||
|
|
||||||
assertThat(aList).containsAnyOf(0, 5, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContainsAnyInList() {
|
|
||||||
List<Integer> aList = Arrays.asList(1, 2, 3);
|
|
||||||
|
|
||||||
assertThat(aList).containsAnyIn(Arrays.asList(1, 5, 10));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenNoDuplicates() {
|
|
||||||
List<Integer> aList = Arrays.asList(-2, -1, 0, 1, 2);
|
|
||||||
|
|
||||||
assertThat(aList).containsNoDuplicates();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContainsNoneOf() {
|
|
||||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
|
||||||
|
|
||||||
assertThat(aList).containsNoneOf(9, 8, 7);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContainsNoneIn() {
|
|
||||||
List<Integer> aList = Arrays.asList(4, 5, 6);
|
|
||||||
|
|
||||||
assertThat(aList).containsNoneIn(Arrays.asList(9, 10, 11));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenContainsExactElements() {
|
|
||||||
List<String> aList = Arrays.asList("10", "20", "30");
|
|
||||||
List<String> anotherList = Arrays.asList("10", "20", "30");
|
|
||||||
|
|
||||||
assertThat(aList).containsExactlyElementsIn(anotherList)
|
|
||||||
.inOrder();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingIterable_thenOrdered() {
|
|
||||||
Set<String> aSet = new LinkedHashSet<>(Arrays.asList("one", "three", "two"));
|
|
||||||
|
|
||||||
assertThat(aSet).isOrdered();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenComparator_whenCheckingIterable_thenOrdered() {
|
|
||||||
Comparator<String> aComparator = (a, b) -> new Float(a).compareTo(new Float(b));
|
|
||||||
|
|
||||||
List<String> aList = Arrays.asList("1", "012", "0020", "100");
|
|
||||||
|
|
||||||
assertThat(aList).isOrdered(aComparator);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingMap_thenContainsEntry() {
|
|
||||||
Map<String, Object> aMap = new HashMap<>();
|
|
||||||
aMap.put("one", 1L);
|
|
||||||
|
|
||||||
assertThat(aMap).containsEntry("one", 1L);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingMap_thenContainsKey() {
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
map.put("one", 1L);
|
|
||||||
|
|
||||||
assertThat(map).containsKey("one");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingMap_thenContainsEntries() {
|
|
||||||
Map<String, Object> aMap = new HashMap<>();
|
|
||||||
aMap.put("first", 1L);
|
|
||||||
aMap.put("second", 2.0);
|
|
||||||
aMap.put("third", 3f);
|
|
||||||
|
|
||||||
Map<String, Object> anotherMap = new HashMap<>(aMap);
|
|
||||||
|
|
||||||
assertThat(aMap).containsExactlyEntriesIn(anotherMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingException_thenInstanceOf() {
|
|
||||||
Exception anException = new IllegalArgumentException(new NumberFormatException());
|
|
||||||
|
|
||||||
assertThat(anException).hasCauseThat()
|
|
||||||
.isInstanceOf(NumberFormatException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingException_thenCauseMessageIsKnown() {
|
|
||||||
Exception anException = new IllegalArgumentException("Bad value");
|
|
||||||
|
|
||||||
assertThat(anException).hasMessageThat()
|
|
||||||
.startsWith("Bad");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingClass_thenIsAssignable() {
|
|
||||||
Class<Double> aClass = Double.class;
|
|
||||||
|
|
||||||
assertThat(aClass).isAssignableTo(Number.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Java 8 Tests
|
|
||||||
@Test
|
|
||||||
public void whenCheckingJavaOptional_thenHasValue() {
|
|
||||||
Optional<Integer> anOptional = Optional.of(1);
|
|
||||||
|
|
||||||
assertThat(anOptional).hasValue(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingJavaOptional_thenPresent() {
|
|
||||||
Optional<String> anOptional = Optional.of("Baeldung");
|
|
||||||
|
|
||||||
assertThat(anOptional).isPresent();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingJavaOptional_thenEmpty() {
|
|
||||||
Optional anOptional = Optional.empty();
|
|
||||||
|
|
||||||
assertThat(anOptional).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingStream_thenContainsInOrder() {
|
|
||||||
Stream<Integer> anStream = Stream.of(1, 2, 3);
|
|
||||||
|
|
||||||
assertThat(anStream).containsAllOf(1, 2, 3)
|
|
||||||
.inOrder();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingStream_thenDoesNotContain() {
|
|
||||||
Stream<Integer> anStream = IntStream.range(1, 100)
|
|
||||||
.boxed();
|
|
||||||
|
|
||||||
assertThat(anStream).doesNotContain(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Guava Tests
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaOptional_thenIsAbsent() {
|
|
||||||
com.google.common.base.Optional anOptional = com.google.common.base.Optional.absent();
|
|
||||||
|
|
||||||
assertThat(anOptional).isAbsent();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaMultimap_thenExpectedSize() {
|
|
||||||
Multimap<String, Object> aMultimap = ArrayListMultimap.create();
|
|
||||||
aMultimap.put("one", 1L);
|
|
||||||
aMultimap.put("one", 2.0);
|
|
||||||
|
|
||||||
assertThat(aMultimap).valuesForKey("one")
|
|
||||||
.hasSize(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaMultiset_thenExpectedCount() {
|
|
||||||
TreeMultiset<String> aMultiset = TreeMultiset.create();
|
|
||||||
aMultiset.add("baeldung", 10);
|
|
||||||
|
|
||||||
assertThat(aMultiset).hasCount("baeldung", 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaTable_thenContains() {
|
|
||||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
|
||||||
|
|
||||||
assertThat(aTable).contains("firstRow", "firstColumn");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaTable_thenContainsCell() {
|
|
||||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
|
||||||
|
|
||||||
assertThat(aTable).containsCell("firstRow", "firstColumn", "baeldung");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaTable_thenContainsRow() {
|
|
||||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
|
||||||
|
|
||||||
assertThat(aTable).containsRow("firstRow");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaTable_thenContainsColumn() {
|
|
||||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
|
||||||
|
|
||||||
assertThat(aTable).containsColumn("firstColumn");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingGuavaTable_thenContainsValue() {
|
|
||||||
Table<String, String, String> aTable = getDummyGuavaTable();
|
|
||||||
|
|
||||||
assertThat(aTable).containsValue("baeldung");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenFailingAssertion_thenMessagePrefix() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).named("User [%s]", aUser.getName())
|
|
||||||
.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenFailingAssertion_thenCustomMessage() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertWithMessage("TEST-985: Secret user subject was NOT null!").that(aUser)
|
|
||||||
.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void whenFailingAssertion_thenCustomMessageAndPrefix() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertWithMessage("TEST-985: Secret user subject was NOT null!").that(aUser)
|
|
||||||
.named("User [%s]", aUser.getName())
|
|
||||||
.isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Table<String, String, String> getDummyGuavaTable() {
|
|
||||||
Table<String, String, String> aTable = TreeBasedTable.create();
|
|
||||||
aTable.put("firstRow", "firstColumn", "baeldung");
|
|
||||||
return aTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom User type
|
|
||||||
@Test
|
|
||||||
public void whenCheckingUser_thenHasName() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).hasName("John Doe");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenCheckingUser_thenHasNameIgnoringCase() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).hasNameIgnoringCase("john doe");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenUser_whenCheckingEmails_thenExpectedSize() {
|
|
||||||
User aUser = new User();
|
|
||||||
|
|
||||||
assertThat(aUser).emails()
|
|
||||||
.hasSize(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -4,11 +4,14 @@
|
|||||||
|
|
||||||
### 相关文章:
|
### 相关文章:
|
||||||
- [Intro to XPath with Java](https://www.baeldung.com/java-xpath)
|
- [Intro to XPath with Java](https://www.baeldung.com/java-xpath)
|
||||||
- [Introduction to JiBX](https://www.baeldung.com/jibx)
|
|
||||||
- [XML Libraries Support in Java](https://www.baeldung.com/java-xml-libraries)
|
- [XML Libraries Support in Java](https://www.baeldung.com/java-xml-libraries)
|
||||||
- [Working with XML Files in Java Using DOM Parsing](https://www.baeldung.com/java-xerces-dom-parsing)
|
- [Working with XML Files in Java Using DOM Parsing](https://www.baeldung.com/java-xerces-dom-parsing)
|
||||||
- [Write an org.w3.dom.Document to a File](https://www.baeldung.com/java-write-xml-document-file)
|
|
||||||
- [Modifying an XML Attribute in Java](https://www.baeldung.com/java-modify-xml-attribute)
|
|
||||||
- [Convert XML to HTML in Java](https://www.baeldung.com/java-convert-xml-to-html)
|
|
||||||
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
- [Parsing an XML File Using StAX](https://www.baeldung.com/java-stax)
|
||||||
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
- [Parsing an XML File Using SAX Parser](https://www.baeldung.com/java-sax-parser)
|
||||||
|
- [Remove HTML Tags Using Java](https://www.baeldung.com/java-remove-html-tags)
|
||||||
|
- [Pretty-Print XML in Java](https://www.baeldung.com/java-pretty-print-xml)
|
||||||
|
- [Validate an XML File Against an XSD File](https://www.baeldung.com/java-validate-xml-xsd)
|
||||||
|
- [Converting JSON to XML in Java](https://www.baeldung.com/java-convert-json-to-xml)
|
||||||
|
- [Convert an XML Object to a String in Java](https://www.baeldung.com/java-convert-xml-object-string)
|
||||||
|
|
||||||
|
- More articles: [[next -->]](../xml-2)
|
||||||
|
341
xml/pom.xml
341
xml/pom.xml
@ -25,41 +25,31 @@
|
|||||||
<artifactId>jaxen</artifactId>
|
<artifactId>jaxen</artifactId>
|
||||||
<version>${jaxen.version}</version>
|
<version>${jaxen.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.jooq</groupId>
|
|
||||||
<artifactId>joox-java-6</artifactId>
|
|
||||||
<version>${joox.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jdom</groupId>
|
<groupId>org.jdom</groupId>
|
||||||
<artifactId>jdom2</artifactId>
|
<artifactId>jdom2</artifactId>
|
||||||
<version>${jdom2.version}</version>
|
<version>${jdom2.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.xml.bind</groupId>
|
<groupId>jakarta.xml.bind</groupId>
|
||||||
<artifactId>jaxb-api</artifactId>
|
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||||
<version>${jaxb-api.version}</version>
|
<version>${jakarta.xml.bind-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sun.xml.bind</groupId>
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
<artifactId>jaxb-impl</artifactId>
|
<artifactId>jaxb-impl</artifactId>
|
||||||
<version>${jaxb-impl.version}</version>
|
<version>${jaxb-impl.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jaxb</groupId>
|
||||||
|
<artifactId>jaxb-runtime</artifactId>
|
||||||
|
<version>${jaxb-runtime.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sun.xml.bind</groupId>
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
<artifactId>jaxb-core</artifactId>
|
<artifactId>jaxb-core</artifactId>
|
||||||
<version>${jaxb-core.version}</version>
|
<version>${jaxb-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>javax.xml</groupId>
|
|
||||||
<artifactId>jaxp-api</artifactId>
|
|
||||||
<version>${jaxp-api.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.xml.stream</groupId>
|
|
||||||
<artifactId>stax-api</artifactId>
|
|
||||||
<version>${stax-api.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.freemarker</groupId>
|
<groupId>org.freemarker</groupId>
|
||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
@ -70,17 +60,6 @@
|
|||||||
<artifactId>compiler</artifactId>
|
<artifactId>compiler</artifactId>
|
||||||
<version>${mustache.version}</version>
|
<version>${mustache.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- JMH Libraries -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
|
||||||
<artifactId>jmh-core</artifactId>
|
|
||||||
<version>${jmh-core.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
|
||||||
<artifactId>jmh-generator-annprocess</artifactId>
|
|
||||||
<version>${jmh-generator.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- utils -->
|
<!-- utils -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
@ -97,47 +76,70 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>${json.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.javadev</groupId>
|
||||||
|
<artifactId>underscore</artifactId>
|
||||||
|
<version>${underscore.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.xmlbeans</groupId>
|
||||||
|
<artifactId>xmlbeans</artifactId>
|
||||||
|
<version>${xmlbeans.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>1.37</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jooq</groupId>
|
||||||
|
<artifactId>joox</artifactId>
|
||||||
|
<version>2.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.htmlparser.jericho</groupId>
|
||||||
|
<artifactId>jericho-html</artifactId>
|
||||||
|
<version>3.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.htmlcleaner</groupId>
|
||||||
|
<artifactId>htmlcleaner</artifactId>
|
||||||
|
<version>2.1</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jibx</groupId>
|
<groupId>org.jibx</groupId>
|
||||||
<artifactId>jibx-run</artifactId>
|
<artifactId>jibx-run</artifactId>
|
||||||
<version>${jibx-version}</version>
|
<version>1.4.2</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>commons-lang</groupId>
|
|
||||||
<artifactId>commons-lang</artifactId>
|
|
||||||
<version>${commons-lang.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter</artifactId>
|
|
||||||
<version>${junit-jupiter.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.vintage</groupId>
|
|
||||||
<artifactId>junit-vintage-engine</artifactId>
|
|
||||||
<version>${junit-jupiter.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.assertj</groupId>
|
|
||||||
<artifactId>assertj-core</artifactId>
|
|
||||||
<version>${assertj-core.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.xmlunit</groupId>
|
<groupId>org.xmlunit</groupId>
|
||||||
<artifactId>xmlunit-assertj</artifactId>
|
<artifactId>xmlunit-assertj</artifactId>
|
||||||
<version>${xmlunit-assertj.version}</version>
|
<version>2.10.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>ch.qos.logback</groupId>
|
|
||||||
<artifactId>logback-classic</artifactId>
|
|
||||||
<version>1.2.5</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>xml</finalName>
|
<finalName>xml</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
@ -146,240 +148,33 @@
|
|||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
<pluginManagement>
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
|
||||||
<groupId>org.jibx</groupId>
|
|
||||||
<artifactId>maven-jibx-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</pluginManagement>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.jibx</groupId>
|
|
||||||
<artifactId>maven-jibx-plugin</artifactId>
|
|
||||||
<version>${maven-jibx-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<includes>
|
|
||||||
<includes>*-binding.xml</includes>
|
|
||||||
</includes>
|
|
||||||
<excludes>
|
|
||||||
<exclude>template-binding.xml</exclude>
|
|
||||||
</excludes>
|
|
||||||
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
|
|
||||||
<includeSchemaBindings>
|
|
||||||
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
|
|
||||||
</includeSchemaBindings>
|
|
||||||
<verbose>true</verbose>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>process-classes</id>
|
|
||||||
<phase>process-classes</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>bind</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>process-test-classes</id>
|
|
||||||
<phase>process-test-classes</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test-bind</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<profiles>
|
|
||||||
<profile>
|
|
||||||
<id>schemaGen</id>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.jibx</groupId>
|
|
||||||
<artifactId>maven-jibx-plugin</artifactId>
|
|
||||||
<version>${maven-jibx-plugin.version}</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>generate-java-code-from-schema</id>
|
|
||||||
<goals>
|
|
||||||
<goal>schema-codegen</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<includes>
|
|
||||||
<include>Order.xsd</include>
|
|
||||||
</includes>
|
|
||||||
<verbose>true</verbose>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>compile-binding</id>
|
|
||||||
<goals>
|
|
||||||
<goal>bind</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<directory>target/generated-sources</directory>
|
|
||||||
<load>true</load>
|
|
||||||
<validate>true</validate>
|
|
||||||
<!--<verbose>true</verbose> -->
|
|
||||||
<verify>true</verify>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>generate-test-code-from-schema</id>
|
|
||||||
<phase>generate-test-sources</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test-schema-codegen</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>compile-test-binding</id>
|
|
||||||
<phase>process-test-classes</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test-bind</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<directory>target/generated-test-sources</directory>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<exclude>CustomerTest.java</exclude>
|
|
||||||
</excludes>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</profile>
|
|
||||||
<profile>
|
|
||||||
<id>bindGen</id>
|
|
||||||
<build>
|
|
||||||
<pluginManagement>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.jibx</groupId>
|
|
||||||
<artifactId>maven-jibx-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</pluginManagement>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.jibx</groupId>
|
|
||||||
<artifactId>maven-jibx-plugin</artifactId>
|
|
||||||
<version>${maven-jibx-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<includes>
|
|
||||||
<includes>*-binding.xml</includes>
|
|
||||||
</includes>
|
|
||||||
<excludes>
|
|
||||||
<exclude>template-binding.xml</exclude>
|
|
||||||
</excludes>
|
|
||||||
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
|
|
||||||
<includeSchemaBindings>
|
|
||||||
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
|
|
||||||
</includeSchemaBindings>
|
|
||||||
<verbose>true</verbose>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>process-classes</id>
|
|
||||||
<phase>process-classes</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>bind</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
<execution>
|
|
||||||
<id>process-test-classes</id>
|
|
||||||
<phase>process-test-classes</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>test-bind</goal>
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<source>${java.version}</source>
|
|
||||||
<target>${java.version}</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<!-- NOTE: We don't need a groupId specification because the group is org.apache.maven.plugins
|
|
||||||
...which is assumed by default. -->
|
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
|
||||||
<descriptorRefs>
|
|
||||||
<descriptorRef>
|
|
||||||
jar-with-dependencies
|
|
||||||
</descriptorRef>
|
|
||||||
</descriptorRefs>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>
|
|
||||||
com.baeldung.xml.jibx.JiBXDemoApplication
|
|
||||||
</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>make-assembly</id><!-- this is used for inheritance merges -->
|
|
||||||
<phase>package</phase><!-- append to the packaging phase. -->
|
|
||||||
<goals>
|
|
||||||
<goal>attached</goal><!-- goals == mojos -->
|
|
||||||
</goals>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</profile>
|
|
||||||
</profiles>
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<dom4j.version>2.1.3</dom4j.version>
|
<dom4j.version>2.1.1</dom4j.version>
|
||||||
<jaxen.version>1.2.0</jaxen.version>
|
<jaxen.version>1.2.0</jaxen.version>
|
||||||
<jdom2.version>2.0.6</jdom2.version>
|
<jdom2.version>2.0.6</jdom2.version>
|
||||||
<joox.version>1.6.2</joox.version>
|
|
||||||
<commons-collections4.version>4.1</commons-collections4.version>
|
|
||||||
<jibx-version>1.2.4.5</jibx-version>
|
|
||||||
<jaxb-api.version>2.3.1</jaxb-api.version>
|
|
||||||
<jaxp-api.version>1.4.2</jaxp-api.version>
|
|
||||||
<jaxb-core.version>2.3.0.1</jaxb-core.version>
|
<jaxb-core.version>2.3.0.1</jaxb-core.version>
|
||||||
<jaxb-impl.version>2.3.2</jaxb-impl.version>
|
<jaxb-impl.version>4.0.4</jaxb-impl.version>
|
||||||
<stax-api.version>1.0-2</stax-api.version>
|
|
||||||
<assertj-core.version>3.12.2</assertj-core.version>
|
|
||||||
<xmlunit-assertj.version>2.6.3</xmlunit-assertj.version>
|
|
||||||
<junit-jupiter.version>5.5.0</junit-jupiter.version>
|
|
||||||
<freemarker.version>2.3.29</freemarker.version>
|
<freemarker.version>2.3.29</freemarker.version>
|
||||||
<mustache.version>0.9.6</mustache.version>
|
<mustache.version>0.9.6</mustache.version>
|
||||||
<!-- util -->
|
|
||||||
<commons-lang.version>2.4</commons-lang.version>
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-jibx-plugin.version>1.3.1</maven-jibx-plugin.version>
|
<jakarta.xml.bind-api.version>4.0.2</jakarta.xml.bind-api.version>
|
||||||
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
|
<jaxb-runtime.version>4.0.3</jaxb-runtime.version>
|
||||||
|
<json.version>20240303</json.version>
|
||||||
|
<underscore.version>1.89</underscore.version>
|
||||||
|
<xmlbeans.version>5.0.2</xmlbeans.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,120 +0,0 @@
|
|||||||
package com.baeldung.sax;
|
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.parsers.SAXParser;
|
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SaxParserMain {
|
|
||||||
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
|
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
||||||
SAXParser saxParser = factory.newSAXParser();
|
|
||||||
|
|
||||||
BaeldungHandler baeldungHandler = new BaeldungHandler();
|
|
||||||
saxParser.parse("xml/src/main/resources/sax/baeldung.xml", baeldungHandler);
|
|
||||||
System.out.println(baeldungHandler.getWebsite());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class BaeldungHandler extends DefaultHandler {
|
|
||||||
private static final String ARTICLES = "articles";
|
|
||||||
private static final String ARTICLE = "article";
|
|
||||||
private static final String TITLE = "title";
|
|
||||||
private static final String CONTENT = "content";
|
|
||||||
|
|
||||||
private Baeldung website;
|
|
||||||
private StringBuilder elementValue;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
|
||||||
if (elementValue == null) {
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
} else {
|
|
||||||
elementValue.append(ch, start, length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startDocument() throws SAXException {
|
|
||||||
website = new Baeldung();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
||||||
switch (qName) {
|
|
||||||
case ARTICLES:
|
|
||||||
website.setArticleList(new ArrayList<>());
|
|
||||||
break;
|
|
||||||
case ARTICLE:
|
|
||||||
website.getArticleList().add(new BaeldungArticle());
|
|
||||||
break;
|
|
||||||
case TITLE:
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
break;
|
|
||||||
case CONTENT:
|
|
||||||
elementValue = new StringBuilder();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
|
||||||
switch (qName) {
|
|
||||||
case TITLE:
|
|
||||||
latestArticle().setTitle(elementValue.toString());
|
|
||||||
break;
|
|
||||||
case CONTENT:
|
|
||||||
latestArticle().setContent(elementValue.toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private BaeldungArticle latestArticle() {
|
|
||||||
List<BaeldungArticle> articleList = website.getArticleList();
|
|
||||||
int latestArticleIndex = articleList.size() - 1;
|
|
||||||
return articleList.get(latestArticleIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Baeldung getWebsite() {
|
|
||||||
return website;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Baeldung {
|
|
||||||
private List<BaeldungArticle> articleList;
|
|
||||||
|
|
||||||
public void setArticleList(List<BaeldungArticle> articleList) {
|
|
||||||
this.articleList = articleList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<BaeldungArticle> getArticleList() {
|
|
||||||
return this.articleList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class BaeldungArticle {
|
|
||||||
private String title;
|
|
||||||
private String content;
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return this.title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContent(String content) {
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContent() {
|
|
||||||
return this.content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.jdom2.Document;
|
|
||||||
import org.jdom2.Element;
|
|
||||||
import org.jdom2.JDOMException;
|
|
||||||
import org.jdom2.filter.Filters;
|
|
||||||
import org.jdom2.input.SAXBuilder;
|
|
||||||
import org.jdom2.xpath.XPathExpression;
|
|
||||||
import org.jdom2.xpath.XPathFactory;
|
|
||||||
|
|
||||||
public class JDomParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public JDomParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Element> getAllTitles() {
|
|
||||||
try {
|
|
||||||
SAXBuilder builder = new SAXBuilder();
|
|
||||||
Document doc = builder.build(this.getFile());
|
|
||||||
Element tutorials = doc.getRootElement();
|
|
||||||
List<Element> titles = tutorials.getChildren("tutorial");
|
|
||||||
return titles;
|
|
||||||
} catch (JDOMException | IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Element getNodeById(String id) {
|
|
||||||
try {
|
|
||||||
SAXBuilder builder = new SAXBuilder();
|
|
||||||
Document document = (Document) builder.build(file);
|
|
||||||
String filter = "//*[@tutId='" + id + "']";
|
|
||||||
XPathFactory xFactory = XPathFactory.instance();
|
|
||||||
XPathExpression<Element> expr = xFactory.compile(filter, Filters.element());
|
|
||||||
List<Element> node = expr.evaluate(document);
|
|
||||||
|
|
||||||
return node.get(0);
|
|
||||||
} catch (JDOMException | IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Marshaller;
|
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
|
|
||||||
import com.baeldung.xml.binding.Tutorial;
|
|
||||||
import com.baeldung.xml.binding.Tutorials;
|
|
||||||
|
|
||||||
public class JaxbParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public JaxbParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tutorials getFullDocument() {
|
|
||||||
try {
|
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
|
|
||||||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
|
||||||
Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
|
|
||||||
return tutorials;
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createNewDocument() {
|
|
||||||
Tutorials tutorials = new Tutorials();
|
|
||||||
tutorials.setTutorial(new ArrayList<Tutorial>());
|
|
||||||
Tutorial tut = new Tutorial();
|
|
||||||
tut.setTutId("01");
|
|
||||||
tut.setType("XML");
|
|
||||||
tut.setTitle("XML with Jaxb");
|
|
||||||
tut.setDescription("XML Binding with Jaxb");
|
|
||||||
tut.setDate("04/02/2015");
|
|
||||||
tut.setAuthor("Jaxb author");
|
|
||||||
tutorials.getTutorial().add(tut);
|
|
||||||
|
|
||||||
try {
|
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
|
|
||||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
|
||||||
|
|
||||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
||||||
|
|
||||||
jaxbMarshaller.marshal(tutorials, file);
|
|
||||||
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
|
|
||||||
import org.jaxen.JaxenException;
|
|
||||||
import org.jaxen.XPath;
|
|
||||||
import org.jaxen.dom.DOMXPath;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
public class JaxenDemo {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public JaxenDemo(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List getAllTutorial() {
|
|
||||||
try {
|
|
||||||
FileInputStream fileIS = new FileInputStream(this.getFile());
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(fileIS);
|
|
||||||
|
|
||||||
String expression = "/tutorials/tutorial";
|
|
||||||
|
|
||||||
XPath path = new DOMXPath(expression);
|
|
||||||
List result = path.selectNodes(xmlDocument);
|
|
||||||
return result;
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package com.baeldung.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
|
|
||||||
public class XMLDocumentWriter {
|
|
||||||
|
|
||||||
public void write(Document document, String fileName, boolean excludeDeclaration, boolean prettyPrint) {
|
|
||||||
try(FileWriter writer = new FileWriter(new File(fileName))) {
|
|
||||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
||||||
Transformer transformer = transformerFactory.newTransformer();
|
|
||||||
if(excludeDeclaration) {
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
||||||
}
|
|
||||||
if(prettyPrint) {
|
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
||||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
|
||||||
}
|
|
||||||
DOMSource source = new DOMSource(document);
|
|
||||||
StreamResult result = new StreamResult(writer);
|
|
||||||
transformer.transform(source, result);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
} catch (TransformerConfigurationException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
} catch (TransformerException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package com.baeldung.xml.attribute;
|
|
||||||
|
|
||||||
import org.dom4j.*;
|
|
||||||
import org.dom4j.io.DocumentSource;
|
|
||||||
import org.dom4j.io.SAXReader;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Dom4jTransformer {
|
|
||||||
private final Document input;
|
|
||||||
|
|
||||||
public Dom4jTransformer(String resourcePath) throws DocumentException, SAXException {
|
|
||||||
// 1- Build the doc from the XML file
|
|
||||||
SAXReader xmlReader = new SAXReader();
|
|
||||||
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
||||||
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
||||||
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
||||||
this.input = xmlReader.read(resourcePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerException {
|
|
||||||
// 2- Locate the node(s) with xpath, we can use index and iterator too.
|
|
||||||
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
|
|
||||||
XPath xpath = DocumentHelper.createXPath(expr);
|
|
||||||
List<Node> nodes = xpath.selectNodes(input);
|
|
||||||
// 3- Make the change on the selected nodes
|
|
||||||
for (int i = 0; i < nodes.size(); i++) {
|
|
||||||
Element element = (Element) nodes.get(i);
|
|
||||||
element.addAttribute(attribute, newValue);
|
|
||||||
}
|
|
||||||
// 4- Save the result to a new XML doc
|
|
||||||
TransformerFactory factory = TransformerFactory.newInstance();
|
|
||||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
||||||
Transformer xformer = factory.newTransformer();
|
|
||||||
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
||||||
Writer output = new StringWriter();
|
|
||||||
xformer.transform(new DocumentSource(input), new StreamResult(output));
|
|
||||||
return output.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
package com.baeldung.xml.stax;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.EndElement;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.namespace.QName;
|
|
||||||
|
|
||||||
public class StaxParser {
|
|
||||||
|
|
||||||
public static List<WebSite> parse(String path) {
|
|
||||||
List<WebSite> websites = new ArrayList<WebSite>();
|
|
||||||
WebSite website = null;
|
|
||||||
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
|
|
||||||
try {
|
|
||||||
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(path));
|
|
||||||
while (reader.hasNext()) {
|
|
||||||
XMLEvent nextEvent = reader.nextEvent();
|
|
||||||
if (nextEvent.isStartElement()) {
|
|
||||||
StartElement startElement = nextEvent.asStartElement();
|
|
||||||
switch (startElement.getName()
|
|
||||||
.getLocalPart()) {
|
|
||||||
case "website":
|
|
||||||
website = new WebSite();
|
|
||||||
Attribute url = startElement.getAttributeByName(new QName("url"));
|
|
||||||
if (url != null) {
|
|
||||||
website.setUrl(url.getValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "name":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setName(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
case "category":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setCategory(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
case "status":
|
|
||||||
nextEvent = reader.nextEvent();
|
|
||||||
website.setStatus(nextEvent.asCharacters()
|
|
||||||
.getData());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nextEvent.isEndElement()) {
|
|
||||||
EndElement endElement = nextEvent.asEndElement();
|
|
||||||
if (endElement.getName()
|
|
||||||
.getLocalPart()
|
|
||||||
.equals("website")) {
|
|
||||||
websites.add(website);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (XMLStreamException xse) {
|
|
||||||
System.out.println("XMLStreamException");
|
|
||||||
xse.printStackTrace();
|
|
||||||
} catch (FileNotFoundException fnfe) {
|
|
||||||
System.out.println("FileNotFoundException");
|
|
||||||
fnfe.printStackTrace();
|
|
||||||
}
|
|
||||||
return websites;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.baeldung.xml.stax;
|
|
||||||
public class WebSite {
|
|
||||||
|
|
||||||
private String url;
|
|
||||||
private String name;
|
|
||||||
private String category;
|
|
||||||
private String status;
|
|
||||||
|
|
||||||
public String getUrl() {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrl(String url) {
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCategory() {
|
|
||||||
return category;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCategory(String category) {
|
|
||||||
this.category = category;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatus(String status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml;
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.helpers.XMLRunner;
|
|
||||||
|
|
||||||
public class Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
XMLRunner.doWork();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.helpers;
|
|
||||||
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.helpers.jaxb.JAXBHelper;
|
|
||||||
import com.baeldung.xmlhtml.stax.StaxTransformer;
|
|
||||||
|
|
||||||
public class XMLRunner {
|
|
||||||
|
|
||||||
public static void doWork() {
|
|
||||||
JAXBHelper.example();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html;
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@XmlType(propOrder = {"head", "body"})
|
|
||||||
@XmlRootElement(name = "html")
|
|
||||||
public class ExampleHTML {
|
|
||||||
|
|
||||||
private List<Meta> head = new ArrayList<>();
|
|
||||||
|
|
||||||
private Body body;
|
|
||||||
|
|
||||||
public ExampleHTML() { }
|
|
||||||
|
|
||||||
public List<Meta> getHead() {
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElementWrapper(name = "head")
|
|
||||||
@XmlElement(name = "meta")
|
|
||||||
public void setHead(List<Meta> head) {
|
|
||||||
this.head = head;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Body getBody() {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "body")
|
|
||||||
public void setBody(Body body) {
|
|
||||||
this.body = body;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class NestedElement {
|
|
||||||
|
|
||||||
private CustomElement customElement;
|
|
||||||
|
|
||||||
public CustomElement getCustomElement() {
|
|
||||||
return customElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "p")
|
|
||||||
public void setCustomElement(CustomElement customElement) {
|
|
||||||
this.customElement = customElement;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml;
|
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.xml.elements.Ancestor;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "xmlexample")
|
|
||||||
public class XMLExample {
|
|
||||||
|
|
||||||
private Ancestor ancestor;
|
|
||||||
|
|
||||||
@XmlElement(name = "ancestor")
|
|
||||||
public void setAncestor(Ancestor ancestor) {
|
|
||||||
this.ancestor = ancestor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Ancestor getAncestor() {
|
|
||||||
return ancestor;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class Ancestor {
|
|
||||||
|
|
||||||
private DescendantOne descendantOne;
|
|
||||||
private DescendantTwo descendantTwo;
|
|
||||||
|
|
||||||
public DescendantOne getDescendantOne() {
|
|
||||||
return descendantOne;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "descendantOne")
|
|
||||||
public void setDescendantOne(DescendantOne descendantOne) {
|
|
||||||
this.descendantOne = descendantOne;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DescendantTwo getDescendantTwo() {
|
|
||||||
return descendantTwo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "descendantTwo")
|
|
||||||
public void setDescendantTwo(DescendantTwo descendantTwo) {
|
|
||||||
this.descendantTwo = descendantTwo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class DescendantOne {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
|
||||||
|
|
||||||
public class DescendantThree {
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
@XmlValue
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.sax;
|
package com.isharkfly.sax;
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
2
xml/src/main/java/com/baeldung/xml/Dom4JParser.java → xml/src/main/java/com/isharkfly/xml/Dom4JParser.java
Executable file → Normal file
2
xml/src/main/java/com/baeldung/xml/Dom4JParser.java → xml/src/main/java/com/isharkfly/xml/Dom4JParser.java
Executable file → Normal file
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -8,8 +8,8 @@ import javax.xml.bind.JAXBException;
|
|||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
import com.ossez.xml.binding.Tutorial;
|
import com.isharkfly.xml.binding.Tutorial;
|
||||||
import com.ossez.xml.binding.Tutorials;
|
import com.isharkfly.xml.binding.Tutorials;
|
||||||
|
|
||||||
public class JaxbParser {
|
public class JaxbParser {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -17,7 +17,7 @@ import javax.xml.stream.events.EndElement;
|
|||||||
import javax.xml.stream.events.StartElement;
|
import javax.xml.stream.events.StartElement;
|
||||||
import javax.xml.stream.events.XMLEvent;
|
import javax.xml.stream.events.XMLEvent;
|
||||||
|
|
||||||
import com.baeldung.xml.binding.Tutorial;
|
import com.isharkfly.xml.binding.Tutorial;
|
||||||
|
|
||||||
public class StaxParser {
|
public class StaxParser {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml;
|
package com.isharkfly.xml;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml.attribute;
|
package com.isharkfly.xml.attribute;
|
||||||
|
|
||||||
import org.dom4j.*;
|
import org.dom4j.*;
|
||||||
import org.dom4j.io.DocumentSource;
|
import org.dom4j.io.DocumentSource;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.attribute;
|
package com.isharkfly.xml.attribute;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.attribute;
|
package com.isharkfly.xml.attribute;
|
||||||
|
|
||||||
import org.joox.JOOX;
|
import org.joox.JOOX;
|
||||||
import org.joox.Match;
|
import org.joox.Match;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.attribute.jmh;
|
package com.isharkfly.xml.attribute.jmh;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -20,9 +20,9 @@ import org.openjdk.jmh.runner.options.Options;
|
|||||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
import com.baeldung.xml.attribute.Dom4jTransformer;
|
import com.isharkfly.xml.attribute.Dom4jTransformer;
|
||||||
import com.baeldung.xml.attribute.JaxpTransformer;
|
import com.isharkfly.xml.attribute.JaxpTransformer;
|
||||||
import com.baeldung.xml.attribute.JooxTransformer;
|
import com.isharkfly.xml.attribute.JooxTransformer;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.binding;
|
package com.isharkfly.xml.binding;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.binding;
|
package com.isharkfly.xml.binding;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -4,7 +4,7 @@
|
|||||||
* Copyright 2008 Stefan Jäger
|
* Copyright 2008 Stefan Jäger
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.xml.jibx;
|
package com.isharkfly.xml.jibx;
|
||||||
|
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.jibx;
|
package com.isharkfly.xml.jibx;
|
||||||
|
|
||||||
public class Identity {
|
public class Identity {
|
||||||
|
|
@ -4,7 +4,7 @@
|
|||||||
* Copyright 2008 Stefan Jäger
|
* Copyright 2008 Stefan Jäger
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.xml.jibx;
|
package com.isharkfly.xml.jibx;
|
||||||
|
|
||||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xml.jibx;
|
package com.isharkfly.xml.jibx;
|
||||||
|
|
||||||
public class Phone {
|
public class Phone {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml.stax;
|
package com.isharkfly.xml.stax;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xml.stax;
|
package com.isharkfly.xml.stax;
|
||||||
public class WebSite {
|
public class WebSite {
|
||||||
|
|
||||||
private String url;
|
private String url;
|
@ -1,6 +1,6 @@
|
|||||||
package com.ossez.xmlhtml;
|
package com.isharkfly.xmlhtml;
|
||||||
|
|
||||||
import com.ossez.xmlhtml.helpers.XMLRunner;
|
import com.isharkfly.xmlhtml.helpers.XMLRunner;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml;
|
package com.isharkfly.xmlhtml;
|
||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.xmlhtml.freemarker;
|
package com.isharkfly.xmlhtml.freemarker;
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.stax.StaxTransformer;
|
import com.isharkfly.xmlhtml.stax.StaxTransformer;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.isharkfly.xmlhtml.helpers;
|
||||||
|
|
||||||
|
|
||||||
|
import com.isharkfly.xmlhtml.helpers.jaxb.JAXBHelper;
|
||||||
|
import com.isharkfly.xmlhtml.stax.StaxTransformer;
|
||||||
|
|
||||||
|
public class XMLRunner {
|
||||||
|
|
||||||
|
public static void doWork() {
|
||||||
|
JAXBHelper.example();
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.xmlhtml.helpers.jaxb;
|
package com.isharkfly.xmlhtml.helpers.jaxb;
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.ExampleHTML;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.ExampleHTML;
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Body;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.Body;
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.CustomElement;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.CustomElement;
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.Meta;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.Meta;
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.html.elements.NestedElement;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.NestedElement;
|
||||||
import com.baeldung.xmlhtml.pojo.jaxb.xml.XMLExample;
|
import com.isharkfly.xmlhtml.pojo.jaxb.xml.XMLExample;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
@ -13,7 +13,7 @@ import javax.xml.bind.Unmarshaller;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static com.baeldung.xmlhtml.Constants.*;
|
import static com.isharkfly.xmlhtml.Constants.*;
|
||||||
|
|
||||||
public class JAXBHelper {
|
public class JAXBHelper {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.jaxp;
|
package com.isharkfly.xmlhtml.jaxp;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.xmlhtml.mustache;
|
package com.isharkfly.xmlhtml.mustache;
|
||||||
|
|
||||||
import com.baeldung.xmlhtml.stax.StaxTransformer;
|
import com.isharkfly.xmlhtml.stax.StaxTransformer;
|
||||||
import com.github.mustachejava.DefaultMustacheFactory;
|
import com.github.mustachejava.DefaultMustacheFactory;
|
||||||
import com.github.mustachejava.Mustache;
|
import com.github.mustachejava.Mustache;
|
||||||
import com.github.mustachejava.MustacheFactory;
|
import com.github.mustachejava.MustacheFactory;
|
@ -1,7 +1,7 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.html;
|
package com.isharkfly.xmlhtml.pojo.jaxb.html;
|
||||||
|
|
||||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.Body;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.Body;
|
||||||
import com.ossez.xmlhtml.pojo.jaxb.html.elements.Meta;
|
import com.isharkfly.xmlhtml.pojo.jaxb.html.elements.Meta;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.*;
|
import javax.xml.bind.annotation.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.html.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.html.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
import javax.xml.bind.annotation.XmlValue;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.html.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.html.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
import javax.xml.bind.annotation.XmlValue;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.html.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.html.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.xml;
|
package com.isharkfly.xmlhtml.pojo.jaxb.xml;
|
||||||
|
|
||||||
import com.ossez.xmlhtml.pojo.jaxb.xml.elements.Ancestor;
|
import com.isharkfly.xmlhtml.pojo.jaxb.xml.elements.Ancestor;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.xml.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.xml.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.xml.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.xml.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
import javax.xml.bind.annotation.XmlValue;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.ossez.xmlhtml.pojo.jaxb.xml.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.xml.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlValue;
|
import javax.xml.bind.annotation.XmlValue;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.pojo.jaxb.xml.elements;
|
package com.isharkfly.xmlhtml.pojo.jaxb.xml.elements;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.xmlhtml.stax;
|
package com.isharkfly.xmlhtml.stax;
|
||||||
|
|
||||||
import javax.xml.stream.*;
|
import javax.xml.stream.*;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
@ -1,193 +0,0 @@
|
|||||||
package com.ossez.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import javax.xml.namespace.NamespaceContext;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.xpath.XPath;
|
|
||||||
import javax.xml.xpath.XPathConstants;
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
|
||||||
import javax.xml.xpath.XPathFactory;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Node;
|
|
||||||
import org.w3c.dom.NodeList;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
public class DefaultParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public DefaultParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeList getFirstLevelNodeList() {
|
|
||||||
NodeList nodeList = null;
|
|
||||||
try {
|
|
||||||
FileInputStream fileIS = new FileInputStream(this.getFile());
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(fileIS);
|
|
||||||
|
|
||||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
||||||
|
|
||||||
String expression = "/tutorials/tutorial";
|
|
||||||
|
|
||||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return nodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node getNodeById(String id) {
|
|
||||||
Node node = null;
|
|
||||||
try {
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(this.getFile());
|
|
||||||
|
|
||||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
||||||
|
|
||||||
String expression = "/tutorials/tutorial[@tutId=" + "'" + id + "'" + "]";
|
|
||||||
|
|
||||||
node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeList getNodeListByTitle(String name) {
|
|
||||||
NodeList nodeList = null;
|
|
||||||
try {
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(this.getFile());
|
|
||||||
|
|
||||||
this.clean(xmlDocument);
|
|
||||||
|
|
||||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
||||||
|
|
||||||
String expression = "//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]";
|
|
||||||
|
|
||||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return nodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeList getElementsByDate(String date) {
|
|
||||||
NodeList nodeList = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(this.getFile());
|
|
||||||
|
|
||||||
this.clean(xmlDocument);
|
|
||||||
|
|
||||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
||||||
|
|
||||||
String expression = "//tutorial[number(translate(date, '/', '')) > " + date + "]";
|
|
||||||
|
|
||||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return nodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeList getAllTutorials() {
|
|
||||||
NodeList nodeList = null;
|
|
||||||
try {
|
|
||||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
|
||||||
builderFactory.setNamespaceAware(true);
|
|
||||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
|
||||||
|
|
||||||
Document xmlDocument = builder.parse(this.getFile());
|
|
||||||
|
|
||||||
this.clean(xmlDocument);
|
|
||||||
|
|
||||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
|
||||||
|
|
||||||
xPath.setNamespaceContext(new NamespaceContext() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator getPrefixes(String arg0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getPrefix(String arg0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getNamespaceURI(String arg0) {
|
|
||||||
if ("bdn".equals(arg0)) {
|
|
||||||
return "http://www.baeldung.com/full_archive";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
String expression = "/bdn:tutorials/bdn:tutorial";
|
|
||||||
|
|
||||||
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
|
|
||||||
|
|
||||||
} catch (SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return nodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clean(Node node) {
|
|
||||||
|
|
||||||
NodeList childs = node.getChildNodes();
|
|
||||||
|
|
||||||
for (int n = childs.getLength() - 1; n >= 0; n--) {
|
|
||||||
Node child = childs.item(n);
|
|
||||||
short nodeType = child.getNodeType();
|
|
||||||
|
|
||||||
if (nodeType == Node.ELEMENT_NODE)
|
|
||||||
clean(child);
|
|
||||||
else if (nodeType == Node.TEXT_NODE) {
|
|
||||||
String trimmedNodeVal = child.getNodeValue().trim();
|
|
||||||
if (trimmedNodeVal.length() == 0)
|
|
||||||
node.removeChild(child);
|
|
||||||
else
|
|
||||||
child.setNodeValue(trimmedNodeVal);
|
|
||||||
} else if (nodeType == Node.COMMENT_NODE)
|
|
||||||
node.removeChild(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,130 +0,0 @@
|
|||||||
package com.ossez.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.dom4j.Document;
|
|
||||||
import org.dom4j.DocumentException;
|
|
||||||
import org.dom4j.DocumentHelper;
|
|
||||||
import org.dom4j.Element;
|
|
||||||
import org.dom4j.Node;
|
|
||||||
import org.dom4j.io.OutputFormat;
|
|
||||||
import org.dom4j.io.SAXReader;
|
|
||||||
import org.dom4j.io.XMLWriter;
|
|
||||||
|
|
||||||
public class Dom4JParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public Dom4JParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Element getRootElement() {
|
|
||||||
try {
|
|
||||||
SAXReader reader = new SAXReader();
|
|
||||||
Document document = reader.read(file);
|
|
||||||
return document.getRootElement();
|
|
||||||
} catch (DocumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Element> getFirstElementList() {
|
|
||||||
try {
|
|
||||||
SAXReader reader = new SAXReader();
|
|
||||||
Document document = reader.read(file);
|
|
||||||
return document.getRootElement().elements();
|
|
||||||
} catch (DocumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node getNodeById(String id) {
|
|
||||||
try {
|
|
||||||
SAXReader reader = new SAXReader();
|
|
||||||
Document document = reader.read(file);
|
|
||||||
List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
|
|
||||||
return elements.get(0);
|
|
||||||
} catch (DocumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node getElementsListByTitle(String name) {
|
|
||||||
try {
|
|
||||||
SAXReader reader = new SAXReader();
|
|
||||||
Document document = reader.read(file);
|
|
||||||
List<Node> elements = document.selectNodes("//tutorial[descendant::title[text()=" + "'" + name + "'" + "]]");
|
|
||||||
return elements.get(0);
|
|
||||||
} catch (DocumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateModifiedDocument() {
|
|
||||||
try {
|
|
||||||
SAXReader reader = new SAXReader();
|
|
||||||
Document document = reader.read(file);
|
|
||||||
List<Node> nodes = document.selectNodes("/tutorials/tutorial");
|
|
||||||
for (Node node : nodes) {
|
|
||||||
Element element = (Element) node;
|
|
||||||
Iterator<Element> iterator = element.elementIterator("title");
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
Element title = (Element) iterator.next();
|
|
||||||
title.setText(title.getText() + " updated");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_updated.xml")));
|
|
||||||
writer.write(document);
|
|
||||||
writer.close();
|
|
||||||
} catch (DocumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateNewDocument() {
|
|
||||||
try {
|
|
||||||
Document document = DocumentHelper.createDocument();
|
|
||||||
Element root = document.addElement("XMLTutorials");
|
|
||||||
Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
|
|
||||||
tutorialElement.addAttribute("type", "xml");
|
|
||||||
|
|
||||||
tutorialElement.addElement("title").addText("XML with Dom4J");
|
|
||||||
|
|
||||||
tutorialElement.addElement("description").addText("XML handling with Dom4J");
|
|
||||||
|
|
||||||
tutorialElement.addElement("date").addText("14/06/2016");
|
|
||||||
|
|
||||||
tutorialElement.addElement("author").addText("Dom4J tech writer");
|
|
||||||
|
|
||||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
||||||
XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_new.xml")), format);
|
|
||||||
writer.write(document);
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,120 +0,0 @@
|
|||||||
package com.ossez.xml;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamConstants;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.events.Attribute;
|
|
||||||
import javax.xml.stream.events.Characters;
|
|
||||||
import javax.xml.stream.events.EndElement;
|
|
||||||
import javax.xml.stream.events.StartElement;
|
|
||||||
import javax.xml.stream.events.XMLEvent;
|
|
||||||
|
|
||||||
import com.ossez.xml.binding.Tutorial;
|
|
||||||
|
|
||||||
public class StaxParser {
|
|
||||||
|
|
||||||
private File file;
|
|
||||||
|
|
||||||
public StaxParser(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Tutorial> getAllTutorial() {
|
|
||||||
boolean bTitle = false;
|
|
||||||
boolean bDescription = false;
|
|
||||||
boolean bDate = false;
|
|
||||||
boolean bAuthor = false;
|
|
||||||
List<Tutorial> tutorials = new ArrayList<Tutorial>();
|
|
||||||
try {
|
|
||||||
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
||||||
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
|
|
||||||
Tutorial current = null;
|
|
||||||
while (eventReader.hasNext()) {
|
|
||||||
XMLEvent event = eventReader.nextEvent();
|
|
||||||
switch (event.getEventType()) {
|
|
||||||
case XMLStreamConstants.START_ELEMENT:
|
|
||||||
StartElement startElement = event.asStartElement();
|
|
||||||
String qName = startElement.getName().getLocalPart();
|
|
||||||
if (qName.equalsIgnoreCase("tutorial")) {
|
|
||||||
current = new Tutorial();
|
|
||||||
Iterator<Attribute> attributes = startElement.getAttributes();
|
|
||||||
while (attributes.hasNext()) {
|
|
||||||
Attribute currentAt = attributes.next();
|
|
||||||
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
|
|
||||||
current.setTutId(currentAt.getValue());
|
|
||||||
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
|
|
||||||
current.setType(currentAt.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (qName.equalsIgnoreCase("title")) {
|
|
||||||
bTitle = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("description")) {
|
|
||||||
bDescription = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("date")) {
|
|
||||||
bDate = true;
|
|
||||||
} else if (qName.equalsIgnoreCase("author")) {
|
|
||||||
bAuthor = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case XMLStreamConstants.CHARACTERS:
|
|
||||||
Characters characters = event.asCharacters();
|
|
||||||
if (bTitle) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setTitle(characters.getData());
|
|
||||||
}
|
|
||||||
bTitle = false;
|
|
||||||
}
|
|
||||||
if (bDescription) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setDescription(characters.getData());
|
|
||||||
}
|
|
||||||
bDescription = false;
|
|
||||||
}
|
|
||||||
if (bDate) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setDate(characters.getData());
|
|
||||||
}
|
|
||||||
bDate = false;
|
|
||||||
}
|
|
||||||
if (bAuthor) {
|
|
||||||
if (current != null) {
|
|
||||||
current.setAuthor(characters.getData());
|
|
||||||
}
|
|
||||||
bAuthor = false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case XMLStreamConstants.END_ELEMENT:
|
|
||||||
EndElement endElement = event.asEndElement();
|
|
||||||
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
|
|
||||||
if (current != null) {
|
|
||||||
tutorials.add(current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (FileNotFoundException | XMLStreamException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return tutorials;
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFile(File file) {
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package com.ossez.xml.attribute;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
|
|
||||||
import javax.xml.XMLConstants;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import javax.xml.xpath.XPath;
|
|
||||||
import javax.xml.xpath.XPathConstants;
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
|
||||||
import javax.xml.xpath.XPathFactory;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.NodeList;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
public class JaxpTransformer {
|
|
||||||
|
|
||||||
private Document input;
|
|
||||||
|
|
||||||
public JaxpTransformer(String resourcePath) throws SAXException, IOException, ParserConfigurationException {
|
|
||||||
// 1- Build the doc from the XML file
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
||||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
||||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
||||||
this.input = factory.newDocumentBuilder()
|
|
||||||
.parse(resourcePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
|
|
||||||
// 2- Locate the node(s) with xpath
|
|
||||||
XPath xpath = XPathFactory.newInstance()
|
|
||||||
.newXPath();
|
|
||||||
NodeList nodes = (NodeList) xpath.evaluate(String.format("//*[contains(@%s, '%s')]", attribute, oldValue), this.input, XPathConstants.NODESET);
|
|
||||||
// 3- Make the change on the selected nodes
|
|
||||||
for (int i = 0; i < nodes.getLength(); i++) {
|
|
||||||
Element value = (Element) nodes.item(i);
|
|
||||||
value.setAttribute(attribute, newValue);
|
|
||||||
}
|
|
||||||
// Stream api syntax
|
|
||||||
// IntStream
|
|
||||||
// .range(0, nodes.getLength())
|
|
||||||
// .mapToObj(i -> (Element) nodes.item(i))
|
|
||||||
// .forEach(value -> value.setAttribute(attribute, newValue));
|
|
||||||
// 4- Save the result to a new XML doc
|
|
||||||
TransformerFactory factory = TransformerFactory.newInstance();
|
|
||||||
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
||||||
Transformer xformer = factory.newTransformer();
|
|
||||||
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
||||||
Writer output = new StringWriter();
|
|
||||||
xformer.transform(new DOMSource(this.input), new StreamResult(output));
|
|
||||||
return output.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package com.ossez.xml.attribute;
|
|
||||||
|
|
||||||
import org.joox.JOOX;
|
|
||||||
import org.joox.Match;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.joox.JOOX.$;
|
|
||||||
|
|
||||||
public class JooxTransformer {
|
|
||||||
|
|
||||||
private final Document input;
|
|
||||||
|
|
||||||
public JooxTransformer(String resourcePath) throws SAXException, IOException {
|
|
||||||
// 1- Build the doc from the XML file
|
|
||||||
DocumentBuilder builder = JOOX.builder();
|
|
||||||
input = builder.parse(resourcePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerFactoryConfigurationError {
|
|
||||||
// 2- Select the document
|
|
||||||
Match $ = $(input);
|
|
||||||
// 3 - Find node to modify
|
|
||||||
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
|
|
||||||
$
|
|
||||||
// .find("to") or with xpath
|
|
||||||
.xpath(expr)
|
|
||||||
.get()
|
|
||||||
.stream()
|
|
||||||
.forEach(e -> e.setAttribute(attribute, newValue));
|
|
||||||
// 4- Return result as String
|
|
||||||
return $.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
package com.ossez.xml.attribute.jmh;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
|
||||||
|
|
||||||
import com.ossez.xml.attribute.JaxpTransformer;
|
|
||||||
import org.dom4j.DocumentException;
|
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
|
||||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
|
||||||
import org.openjdk.jmh.annotations.Mode;
|
|
||||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
|
||||||
import org.openjdk.jmh.annotations.Scope;
|
|
||||||
import org.openjdk.jmh.annotations.State;
|
|
||||||
import org.openjdk.jmh.runner.Runner;
|
|
||||||
import org.openjdk.jmh.runner.RunnerException;
|
|
||||||
import org.openjdk.jmh.runner.options.Options;
|
|
||||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import com.ossez.xml.attribute.Dom4jTransformer;
|
|
||||||
import com.ossez.xml.attribute.JooxTransformer;
|
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
|
||||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
|
||||||
@State(Scope.Benchmark)
|
|
||||||
public class AttributeBenchMark {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws RunnerException {
|
|
||||||
Options opt = new OptionsBuilder().include(AttributeBenchMark.class.getSimpleName())
|
|
||||||
.forks(1)
|
|
||||||
.build();
|
|
||||||
new Runner(opt).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
public String dom4jBenchmark() throws DocumentException, TransformerException, SAXException {
|
|
||||||
String path = this.getClass()
|
|
||||||
.getResource("/xml/attribute.xml")
|
|
||||||
.toString();
|
|
||||||
Dom4jTransformer transformer = new Dom4jTransformer(path);
|
|
||||||
String attribute = "customer";
|
|
||||||
String oldValue = "true";
|
|
||||||
String newValue = "false";
|
|
||||||
|
|
||||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
public String jooxBenchmark() throws IOException, SAXException {
|
|
||||||
String path = this.getClass()
|
|
||||||
.getResource("/xml/attribute.xml")
|
|
||||||
.toString();
|
|
||||||
JooxTransformer transformer = new JooxTransformer(path);
|
|
||||||
String attribute = "customer";
|
|
||||||
String oldValue = "true";
|
|
||||||
String newValue = "false";
|
|
||||||
|
|
||||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
public String jaxpBenchmark() throws TransformerException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
|
|
||||||
String path = this.getClass()
|
|
||||||
.getResource("/xml/attribute.xml")
|
|
||||||
.toString();
|
|
||||||
JaxpTransformer transformer = new JaxpTransformer(path);
|
|
||||||
String attribute = "customer";
|
|
||||||
String oldValue = "true";
|
|
||||||
String newValue = "false";
|
|
||||||
|
|
||||||
return transformer.modifyAttribute(attribute, oldValue, newValue);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
package com.ossez.xml.binding;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
|
|
||||||
public class Tutorial {
|
|
||||||
|
|
||||||
private String tutId;
|
|
||||||
private String type;
|
|
||||||
private String title;
|
|
||||||
private String description;
|
|
||||||
private String date;
|
|
||||||
private String author;
|
|
||||||
|
|
||||||
public String getTutId() {
|
|
||||||
return tutId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute
|
|
||||||
public void setTutId(String tutId) {
|
|
||||||
this.tutId = tutId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute
|
|
||||||
public void setType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setDescription(String description) {
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDate() {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setDate(String date) {
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAuthor() {
|
|
||||||
return author;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement
|
|
||||||
public void setAuthor(String author) {
|
|
||||||
this.author = author;
|
|
||||||
}
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user