added reflection project

This commit is contained in:
egimaben
2016-09-03 20:37:55 +03:00
121 changed files with 4258 additions and 183 deletions
+33 -32
View File
@@ -1,36 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>autovalue-tutorial</artifactId>
<version>1.0</version>
<name>AutoValue</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.2</version>
</dependency>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>autovalue-tutorial</artifactId>
<version>1.0</version>
<name>AutoValue</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>
</project>
@@ -0,0 +1,148 @@
package org.baeldung.java.collections;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.stream.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.*;
public class ArrayListTest {
List<String> stringsToSearch;
@Before
public void setUp() {
List<String> xs = LongStream.range(0, 16)
.boxed()
.map(Long::toHexString)
.collect(Collectors.toList());
stringsToSearch = new ArrayList<>(xs);
stringsToSearch.addAll(xs);
}
@Test
public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() {
List<String> xs = new ArrayList<>();
assertTrue(xs.isEmpty());
}
@Test
public void givenCollection_whenProvideItToArrayListCtor_thenArrayListIsPopulatedWithItsElements() {
Collection<Integer> numbers =
IntStream.range(0, 10).boxed().collect(Collectors.toSet());
List<Integer> xs = new ArrayList<>(numbers);
assertEquals(10, xs.size());
assertTrue(numbers.containsAll(xs));
}
@Test
public void givenElement_whenAddToArrayList_thenIsAdded() {
List<Long> xs = new ArrayList<>();
xs.add(1L);
xs.add(2L);
xs.add(1, 3L);
assertThat(Arrays.asList(1L, 3L, 2L), equalTo(xs));
}
@Test
public void givenCollection_whenAddToArrayList_thenIsAdded() {
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
Collection<Long> ys = LongStream.range(4, 10).boxed().collect(Collectors.toList());
xs.addAll(0, ys);
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
}
@Test
public void givenExistingElement_whenCallIndexOf_thenReturnCorrectIndex() {
assertEquals(10, stringsToSearch.indexOf("a"));
assertEquals(26, stringsToSearch.lastIndexOf("a"));
}
@Test
public void givenCondition_whenIterateArrayList_thenFindAllElementsSatisfyingCondition() {
Iterator<String> it = stringsToSearch.iterator();
Set<String> matchingStrings = new HashSet<>(Arrays.asList("a", "c", "9"));
List<String> result = new ArrayList<>();
while (it.hasNext()) {
String s = it.next();
if (matchingStrings.contains(s)) {
result.add(s);
}
}
assertEquals(6, result.size());
}
@Test
public void givenPredicate_whenIterateArrayList_thenFindAllElementsSatisfyingPredicate() {
Set<String> matchingStrings = new HashSet<>(Arrays.asList("a", "c", "9"));
List<String> result = stringsToSearch
.stream()
.filter(matchingStrings::contains)
.collect(Collectors.toList());
assertEquals(6, result.size());
}
@Test
public void givenSortedArray_whenUseBinarySearch_thenFindElement() {
List<String> copy = new ArrayList<>(stringsToSearch);
Collections.sort(copy);
int index = Collections.binarySearch(copy, "f");
assertThat(index, not(equalTo(-1)));
}
@Test
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
List<Integer> xs = new ArrayList<>(
IntStream.range(0, 10).boxed().collect(Collectors.toList())
);
Collections.reverse(xs);
xs.remove(0);
assertThat(xs.get(0), equalTo(8));
xs.remove(Integer.valueOf(0));
assertFalse(xs.contains(0));
}
@Test
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
List<Integer> xs = new ArrayList<>(
IntStream.range(0, 10).boxed().collect(Collectors.toList())
);
ListIterator<Integer> it = xs.listIterator(xs.size());
List<Integer> result = new ArrayList<>(xs.size());
while (it.hasPrevious()) {
result.add(it.previous());
}
Collections.reverse(xs);
assertThat(result, equalTo(xs));
}
@Test
public void givenCondition_whenIterateArrayList_thenRemoveAllElementsSatisfyingCondition() {
Set<String> matchingStrings
= new HashSet<>(Arrays.asList("a", "b", "c", "d", "e", "f"));
Iterator<String> it = stringsToSearch.iterator();
while (it.hasNext()) {
if (matchingStrings.contains(it.next())) {
it.remove();
}
}
assertEquals(20, stringsToSearch.size());
}
}
@@ -0,0 +1,67 @@
package org.baeldung.equalshashcode.entities;
import java.util.ArrayList;
import java.util.HashSet;
public class ComplexClass {
private ArrayList<?> genericArrayList;
private HashSet<Integer> integerHashSet;
public ComplexClass(ArrayList<?> genericArrayList,
HashSet<Integer> integerHashSet) {
super();
this.genericArrayList = genericArrayList;
this.integerHashSet = integerHashSet;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
result = prime * result
+ ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ComplexClass))
return false;
ComplexClass other = (ComplexClass) obj;
if (genericArrayList == null) {
if (other.genericArrayList != null)
return false;
} else if (!genericArrayList.equals(other.genericArrayList))
return false;
if (integerHashSet == null) {
if (other.integerHashSet != null)
return false;
} else if (!integerHashSet.equals(other.integerHashSet))
return false;
return true;
}
protected ArrayList<?> getGenericArrayList() {
return genericArrayList;
}
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
this.genericArrayList = genericArrayList;
}
protected HashSet<Integer> getIntegerHashSet() {
return integerHashSet;
}
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
this.integerHashSet = integerHashSet;
}
}
@@ -0,0 +1,54 @@
package org.baeldung.equalshashcode.entities;
public class PrimitiveClass {
private boolean primitiveBoolean;
private int primitiveInt;
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
super();
this.primitiveBoolean = primitiveBoolean;
this.primitiveInt = primitiveInt;
}
protected boolean isPrimitiveBoolean() {
return primitiveBoolean;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (primitiveBoolean ? 1231 : 1237);
result = prime * result + primitiveInt;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PrimitiveClass other = (PrimitiveClass) obj;
if (primitiveBoolean != other.primitiveBoolean)
return false;
if (primitiveInt != other.primitiveInt)
return false;
return true;
}
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
this.primitiveBoolean = primitiveBoolean;
}
protected int getPrimitiveInt() {
return primitiveInt;
}
protected void setPrimitiveInt(int primitiveInt) {
this.primitiveInt = primitiveInt;
}
}
@@ -0,0 +1,62 @@
package org.baeldung.equalshashcode.entities;
public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
@Override
public double area() {
// A = w * l
return width * length;
}
@Override
public double perimeter() {
// P = 2(w + l)
return 2 * (width + length);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(length);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
if (Double.doubleToLongBits(length) != Double
.doubleToLongBits(other.length))
return false;
if (Double.doubleToLongBits(width) != Double
.doubleToLongBits(other.width))
return false;
return true;
}
protected double getWidth() {
return width;
}
protected double getLength() {
return length;
}
}
@@ -0,0 +1,7 @@
package org.baeldung.equalshashcode.entities;
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
@@ -0,0 +1,47 @@
package org.baeldung.equalshashcode.entities;
import java.awt.Color;
public class Square extends Rectangle {
Color color;
public Square(double width, Color color) {
super(width, width);
this.color = color;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Square other = (Square) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
protected Color getColor() {
return color;
}
protected void setColor(Color color) {
this.color = color;
}
}
@@ -0,0 +1,39 @@
package org.baeldung.equalshashcode.entities;
import java.util.ArrayList;
import java.util.HashSet;
import org.junit.Assert;
import org.junit.Test;
public class ComplexClassTest {
@Test
public void testEqualsAndHashcodes() {
ArrayList<String> strArrayList = new ArrayList<String>();
strArrayList.add("abc");
strArrayList.add("def");
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
ComplexClass cObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
ArrayList<String> strArrayListD = new ArrayList<String>();
strArrayListD.add("lmn");
strArrayListD.add("pqr");
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45,67));
// equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode()
Assert.assertFalse(aObject.equals(dObject));
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
}
}
@@ -0,0 +1,29 @@
package org.baeldung.equalshashcode.entities;
import org.junit.Assert;
import org.junit.Test;
public class PrimitiveClassTest {
@Test
public void testTwoEqualsObjects() {
PrimitiveClass aObject = new PrimitiveClass(false, 2);
PrimitiveClass bObject = new PrimitiveClass(false, 2);
PrimitiveClass cObject = new PrimitiveClass(false, 2);
PrimitiveClass dObject = new PrimitiveClass(true, 2);
// equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode()
Assert.assertFalse(aObject.equals(dObject));
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
}
}
@@ -0,0 +1,32 @@
package org.baeldung.equalshashcode.entities;
import java.awt.Color;
import org.junit.Assert;
import org.junit.Test;
public class SquareClassTest {
@Test
public void testEqualsAndHashcodes() {
Square aObject = new Square(10, Color.BLUE);
Square bObject = new Square(10, Color.BLUE);
Square cObject = new Square(10, Color.BLUE);
Square dObject = new Square(20, Color.BLUE);
// equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode()
Assert.assertFalse(aObject.equals(dObject));
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
}
}
-49
View File
@@ -1,49 +0,0 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>mapstruct</artifactId>
<name>mapstruct</name>
<groupId>com.baeldung</groupId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mapstruct</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -1,4 +1,4 @@
package org.baeldung.dto;
package com.baeldung.dto;
public class DivisionDTO {
@@ -1,4 +1,4 @@
package org.baeldung.dto;
package com.baeldung.dto;
public class EmployeeDTO {
@@ -1,4 +1,4 @@
package org.baeldung.dto;
package com.baeldung.dto;
public class SimpleSource {
@@ -1,4 +1,4 @@
package org.baeldung.entity;
package com.baeldung.entity;
public class Division {
@@ -1,4 +1,4 @@
package org.baeldung.entity;
package com.baeldung.entity;
import java.util.Date;
@@ -1,4 +1,4 @@
package org.baeldung.entity;
package com.baeldung.entity;
public class SimpleDestination {
@@ -1,15 +1,15 @@
package org.baeldung.mapper;
package com.baeldung.mapper;
import java.util.List;
import org.baeldung.dto.DivisionDTO;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
import org.baeldung.entity.Employee;
import com.baeldung.dto.DivisionDTO;
import com.baeldung.dto.EmployeeDTO;
import com.baeldung.entity.Division;
import com.baeldung.entity.Employee;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import java.util.List;
@Mapper
public interface EmployeeMapper {
@@ -1,7 +1,7 @@
package org.baeldung.mapper;
package com.baeldung.mapper;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import com.baeldung.dto.SimpleSource;
import com.baeldung.entity.SimpleDestination;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
@@ -10,6 +10,6 @@
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="org.baeldung" />
<context:component-scan base-package="com.baeldung" />
</beans>
@@ -1,6 +1,11 @@
package org.baeldung.mapper;
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import com.baeldung.dto.DivisionDTO;
import com.baeldung.dto.EmployeeDTO;
import com.baeldung.entity.Division;
import com.baeldung.entity.Employee;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -8,12 +13,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.baeldung.dto.DivisionDTO;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
import org.baeldung.entity.Employee;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
import static org.junit.Assert.assertEquals;
public class EmployeeMapperTest {
@@ -1,9 +1,9 @@
package org.baeldung.mapper;
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import com.baeldung.dto.SimpleSource;
import com.baeldung.entity.SimpleDestination;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
+12 -10
View File
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
@@ -8,9 +9,9 @@
<packaging>pom</packaging>
<properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</properties>
<modules>
<module>assertj</module>
@@ -52,7 +53,7 @@
<module>rest-assured</module>
<module>rest-testing</module>
<module>resteasy</module>
<module>log4j</module>
<module>log4j</module>
<module>spring-all</module>
<module>spring-akka</module>
@@ -74,7 +75,7 @@
<module>spring-hibernate3</module>
<module>spring-hibernate4</module>
<module>spring-jpa</module>
<module>spring-jpa-jndi</module>
<module>spring-jpa-jndi</module>
<module>spring-katharsis</module>
<module>spring-mockito</module>
<module>spring-mvc-java</module>
@@ -87,8 +88,7 @@
<module>spring-rest</module>
<module>spring-rest-angular</module>
<module>spring-rest-docs</module>
<module>spring-cloud-config</module>
<module>spring-cloud-hystrix</module>
<module>spring-cloud</module>
<module>spring-security-basic-auth</module>
<module>spring-security-custom-permission</module>
@@ -110,14 +110,16 @@
<module>xml</module>
<module>lombok</module>
<module>redis</module>
<module>webjars</module>
<module>mutation-testing</module>
<module>spring-mvc-velocity</module>
<module>mutation-testing</module>
<module>spring-mvc-velocity</module>
<module>xstream</module>
<module>dozer</module>
<module>orika</module>
<module>reflection</module>
<module>dozer</module>
<module>orika</module>
</modules>
</project>
+16 -1
View File
@@ -24,7 +24,11 @@
<spring.version>4.3.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@@ -80,6 +84,17 @@
<version>3.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
@@ -0,0 +1 @@
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip

Some files were not shown because too many files have changed in this diff Show More