Merge master into BAEL-2797

This commit is contained in:
anilkivilcim.eray 2019-04-04 16:48:34 +03:00
commit bc38dde66c
34 changed files with 510 additions and 169 deletions

View File

@ -1,42 +1,42 @@
package com.baeldung.algorithms.reversingtree;
public class TreeNode {
private int value;
private TreeNode rightChild;
private TreeNode leftChild;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public TreeNode getRightChild() {
return rightChild;
}
public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
}
public TreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
}
public TreeNode(int value, TreeNode rightChild, TreeNode leftChild) {
this.value = value;
this.rightChild = rightChild;
this.leftChild = leftChild;
}
public TreeNode(int value) {
this.value = value;
}
}
package com.baeldung.algorithms.reversingtree;
public class TreeNode {
private int value;
private TreeNode rightChild;
private TreeNode leftChild;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public TreeNode getRightChild() {
return rightChild;
}
public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
}
public TreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
}
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
this.value = value;
this.rightChild = rightChild;
this.leftChild = leftChild;
}
public TreeNode(int value) {
this.value = value;
}
}

View File

@ -1,68 +1,53 @@
package com.baeldung.algorithms.reversingtree;
import java.util.LinkedList;
public class TreeReverser {
public TreeNode createBinaryTree() {
TreeNode leaf1 = new TreeNode(3);
TreeNode leaf2 = new TreeNode(1);
TreeNode leaf3 = new TreeNode(9);
TreeNode leaf4 = new TreeNode(6);
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
TreeNode root = new TreeNode(4, nodeRight, nodeLeft);
return root;
}
public void reverseRecursive(TreeNode treeNode) {
if (treeNode == null) {
return;
}
TreeNode temp = treeNode.getLeftChild();
treeNode.setLeftChild(treeNode.getRightChild());
treeNode.setRightChild(temp);
reverseRecursive(treeNode.getLeftChild());
reverseRecursive(treeNode.getRightChild());
}
public void reverseIterative(TreeNode treeNode) {
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
if (treeNode != null) {
queue.add(treeNode);
}
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.getLeftChild() != null)
queue.add(node.getLeftChild());
if (node.getRightChild() != null)
queue.add(node.getRightChild());
TreeNode temp = node.getLeftChild();
node.setLeftChild(node.getRightChild());
node.setRightChild(temp);
}
}
public String toString(TreeNode root) {
if (root == null) {
return "";
}
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
buffer.append(toString(root.getLeftChild()));
buffer.append(toString(root.getRightChild()));
return buffer.toString();
}
}
package com.baeldung.algorithms.reversingtree;
import java.util.LinkedList;
public class TreeReverser {
public void reverseRecursive(TreeNode treeNode) {
if (treeNode == null) {
return;
}
TreeNode temp = treeNode.getLeftChild();
treeNode.setLeftChild(treeNode.getRightChild());
treeNode.setRightChild(temp);
reverseRecursive(treeNode.getLeftChild());
reverseRecursive(treeNode.getRightChild());
}
public void reverseIterative(TreeNode treeNode) {
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
if (treeNode != null) {
queue.add(treeNode);
}
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.getLeftChild() != null)
queue.add(node.getLeftChild());
if (node.getRightChild() != null)
queue.add(node.getRightChild());
TreeNode temp = node.getLeftChild();
node.setLeftChild(node.getRightChild());
node.setRightChild(temp);
}
}
public String toString(TreeNode root) {
if (root == null) {
return "";
}
StringBuffer buffer = new StringBuffer(String.valueOf(root.getValue())).append(" ");
buffer.append(toString(root.getLeftChild()));
buffer.append(toString(root.getRightChild()));
return buffer.toString();
}
}

View File

@ -1,33 +1,47 @@
package com.baeldung.algorithms.reversingtree;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TreeReverserUnitTest {
@Test
public void givenTreeWhenReversingRecursivelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = reverser.createBinaryTree();
reverser.reverseRecursive(treeNode);
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
.trim());
}
@Test
public void givenTreeWhenReversingIterativelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = reverser.createBinaryTree();
reverser.reverseIterative(treeNode);
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
.trim());
}
}
package com.baeldung.algorithms.reversingtree;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TreeReverserUnitTest {
@Test
public void givenTreeWhenReversingRecursivelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = createBinaryTree();
reverser.reverseRecursive(treeNode);
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
.trim());
}
@Test
public void givenTreeWhenReversingIterativelyThenReversed() {
TreeReverser reverser = new TreeReverser();
TreeNode treeNode = createBinaryTree();
reverser.reverseIterative(treeNode);
assertEquals("4 7 9 6 2 3 1", reverser.toString(treeNode)
.trim());
}
private TreeNode createBinaryTree() {
TreeNode leaf1 = new TreeNode(1);
TreeNode leaf2 = new TreeNode(3);
TreeNode leaf3 = new TreeNode(6);
TreeNode leaf4 = new TreeNode(9);
TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);
TreeNode root = new TreeNode(4, nodeLeft, nodeRight);
return root;
}
}

View File

@ -34,6 +34,7 @@
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>

View File

@ -0,0 +1,37 @@
package com.baeldung.switchExpression;
import org.junit.Assert;
import org.junit.Test;
public class SwitchUnitTest {
@Test
public void switchJava12(){
var month = Month.AUG;
var value = switch(month){
case JAN,JUN, JUL -> 3;
case FEB,SEP, OCT, NOV, DEC -> 1;
case MAR,MAY, APR, AUG -> 2;
};
Assert.assertEquals(value, 2);
}
@Test
public void switchLocalVariable(){
var month = Month.AUG;
int i = switch (month){
case JAN,JUN, JUL -> 3;
case FEB,SEP, OCT, NOV, DEC -> 1;
case MAR,MAY, APR, AUG -> {
int j = month.toString().length() * 4;
break j;
}
};
Assert.assertEquals(12, i);
}
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.customer;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue
private long id;
private String name;
private String email;
public Customer(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
List<Customer> findByName(String name);
List<Customer> findByNameAndEmail(String name, String email);
@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
}

View File

@ -0,0 +1,66 @@
package com.baeldung.customer;
import com.baeldung.config.PersistenceConfiguration;
import com.baeldung.config.PersistenceProductConfiguration;
import com.baeldung.config.PersistenceUserConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import static org.junit.Assert.assertEquals;
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
@RunWith(SpringRunner.class)
public class CustomerRepositoryIntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private CustomerRepository repository;
@Before
public void before() {
entityManager.persist(new Customer("A", "A@example.com"));
entityManager.persist(new Customer("D", null));
entityManager.persist(new Customer("D", "D@example.com"));
}
@Test
public void givenQueryMethod_whenEmailIsNull_thenFoundByNullEmail() {
List<Customer> customers = repository.findByNameAndEmail("D", null);
assertEquals(1, customers.size());
Customer actual = customers.get(0);
assertEquals(null, actual.getEmail());
assertEquals("D", actual.getName());
}
@Test
public void givenQueryMethod_whenEmailIsAbsent_thenIgnoreEmail() {
List<Customer> customers = repository.findByName("D");
assertEquals(2, customers.size());
}
@Test
public void givenQueryAnnotation_whenEmailIsNull_thenIgnoreEmail() {
List<Customer> customers = repository.findCustomerByNameAndEmail("D", null);
assertEquals(2, customers.size());
}
@After
public void cleanUp() {
repository.deleteAll();
}
}

View File

@ -1,8 +1,12 @@
package com.baeldung.spring.cloud.archaius.additionalsources.config;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.configuration.AbstractConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.FixedDelayPollingScheduler;
@ -13,8 +17,9 @@ import com.netflix.config.sources.URLConfigurationSource;
public class ApplicationPropertiesConfigurations {
@Bean
public AbstractConfiguration addApplicationPropertiesSource() {
PolledConfigurationSource source = new URLConfigurationSource("classpath:other-config.properties");
public AbstractConfiguration addApplicationPropertiesSource() throws IOException {
URL configPropertyURL = (new ClassPathResource("other-config.properties")).getURL();
PolledConfigurationSource source = new URLConfigurationSource(configPropertyURL);
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
}

View File

@ -1,12 +1,10 @@
package org.baeldung;
package com.baeldung.spring.cloud.archaius.additionalsources;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.cloud.archaius.additionalsources.AdditionalSourcesSimpleApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdditionalSourcesSimpleApplication.class)
public class SpringContextIntegrationTest {

View File

@ -0,0 +1,15 @@
package com.baeldung.spring.cloud.aws;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringCloudAwsApplication.class)
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -1,18 +1,22 @@
package com.baeldung.spring.cloud.config.server;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
*
* The context will load successfully with some properties provided by docker
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ConfigServer.class)
@WebAppConfiguration
@Ignore
public class ConfigServerListIntegrationTest {
public class SpringContextLiveTest {
@Test
public void contextLoads() {
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -21,6 +21,15 @@
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>

View File

@ -26,12 +26,6 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.eureka.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -1,17 +1,16 @@
package org.baeldung;
package com.baeldung.spring.cloud.feign.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.spring.cloud.aws.InstanceProfileAwsApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InstanceProfileAwsApplication.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.eureka.server;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.spring.cloud.hystrix.rest.consumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RestConsumerFeignApplication.class)
@WebAppConfiguration
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -40,6 +40,13 @@
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot-starter-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring-boot-starter-web.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
@ -53,5 +60,10 @@
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<!-- we need the Mockito version provided by Spring Boot -->
<mockito.version>1.10.19</mockito.version>
</properties>
</project>

View File

@ -1,18 +1,18 @@
package org.baeldung;
package com.baeldung.spring.cloud.hystrix.rest.consumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.baeldung.spring.cloud.config.server.ConfigServer;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ConfigServer.class)
@ContextConfiguration(classes = RestConsumerApplication.class)
@WebAppConfiguration
public class SpringContextIntegrationTest {
@Test
public void contextLoads() {
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -20,6 +20,18 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-web.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<!-- we need the Mockito version provided by Spring Boot -->
<mockito.version>1.10.19</mockito.version>
</properties>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.hystrix.rest.producer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -25,6 +25,13 @@
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-web.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.eureka.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -25,7 +25,13 @@
<artifactId>commons-configuration</artifactId>
<version>${commons-config.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-web.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.eureka.server;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -33,6 +33,13 @@
<artifactId>rxjava</artifactId>
<version>${rxjava.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-web.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.zuul.config;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -1,2 +1,3 @@
###Relevant Articles:
- [A Guide to REST-assured](http://www.baeldung.com/rest-assured-tutorial)
- [REST-assured Support for Spring MockMvc](https://www.baeldung.com/spring-mock-mvc-rest-assured)