BAEL-722 Intro to JSONassert (#1437)

* yasin.bhojawala@gmail.com

Evaluation article on Different Types of Bean Injection in Spring

* Revert "yasin.bhojawala@gmail.com"

This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032.

* Fixing compilation error and removing unused import

* Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com

Code examples for the article "Introduction to Java9 StackWalking API"

* BAEL-608 Introduction to Java9 StackWalking API

* BAEL-608 Introduction to Java9 StackWalking API

changing the test names to BDD style

* BAEL-608 Introduction to Java9 StackWalking API

correcting the typo

* BAEL-608 Introduction to Java9 StackWalking API

improving method names

* BAEL-608 Introduction to Java9 StackWalking API

test method names improvements

* BAEL-718 Quick intro to javatuples

* merging pom from master

* BAEL-722 Intro to JSONassert
This commit is contained in:
Yasin 2017-03-19 14:21:25 +05:30 committed by maibin
parent 22b5c4924b
commit 9d0cb1e2aa
27 changed files with 753 additions and 632 deletions

View File

@ -1,57 +1,57 @@
package com.baeldung.algorithms.ga.dijkstra; package com.baeldung.algorithms.ga.dijkstra;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
public class Dijkstra { public class Dijkstra {
public static Graph calculateShortestPathFromSource(Graph graph, Node source) { public static Graph calculateShortestPathFromSource(Graph graph, Node source) {
source.setDistance(0); source.setDistance(0);
Set<Node> settledNodes = new HashSet<>(); Set<Node> settledNodes = new HashSet<>();
Set<Node> unsettledNodes = new HashSet<>(); Set<Node> unsettledNodes = new HashSet<>();
unsettledNodes.add(source); unsettledNodes.add(source);
while (unsettledNodes.size() != 0) { while (unsettledNodes.size() != 0) {
Node currentNode = getLowestDistanceNode(unsettledNodes); Node currentNode = getLowestDistanceNode(unsettledNodes);
unsettledNodes.remove(currentNode); unsettledNodes.remove(currentNode);
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) { for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) {
Node adjacentNode = adjacencyPair.getKey(); Node adjacentNode = adjacencyPair.getKey();
Integer edgeWeigh = adjacencyPair.getValue(); Integer edgeWeigh = adjacencyPair.getValue();
if (!settledNodes.contains(adjacentNode)) { if (!settledNodes.contains(adjacentNode)) {
CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode); CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode);
unsettledNodes.add(adjacentNode); unsettledNodes.add(adjacentNode);
} }
} }
settledNodes.add(currentNode); settledNodes.add(currentNode);
} }
return graph; return graph;
} }
private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) { private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) {
Integer sourceDistance = sourceNode.getDistance(); Integer sourceDistance = sourceNode.getDistance();
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) { if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
evaluationNode.setDistance(sourceDistance + edgeWeigh); evaluationNode.setDistance(sourceDistance + edgeWeigh);
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath()); LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
shortestPath.add(sourceNode); shortestPath.add(sourceNode);
evaluationNode.setShortestPath(shortestPath); evaluationNode.setShortestPath(shortestPath);
} }
} }
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) { private static Node getLowestDistanceNode(Set<Node> unsettledNodes) {
Node lowestDistanceNode = null; Node lowestDistanceNode = null;
int lowestDistance = Integer.MAX_VALUE; int lowestDistance = Integer.MAX_VALUE;
for (Node node : unsettledNodes) { for (Node node : unsettledNodes) {
int nodeDistance = node.getDistance(); int nodeDistance = node.getDistance();
if (nodeDistance < lowestDistance) { if (nodeDistance < lowestDistance) {
lowestDistance = nodeDistance; lowestDistance = nodeDistance;
lowestDistanceNode = node; lowestDistanceNode = node;
} }
} }
return lowestDistanceNode; return lowestDistanceNode;
} }
} }

View File

@ -1,21 +1,21 @@
package com.baeldung.algorithms.ga.dijkstra; package com.baeldung.algorithms.ga.dijkstra;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class Graph { public class Graph {
private Set<Node> nodes = new HashSet<>(); private Set<Node> nodes = new HashSet<>();
public void addNode(Node nodeA) { public void addNode(Node nodeA) {
nodes.add(nodeA); nodes.add(nodeA);
} }
public Set<Node> getNodes() { public Set<Node> getNodes() {
return nodes; return nodes;
} }
public void setNodes(Set<Node> nodes) { public void setNodes(Set<Node> nodes) {
this.nodes = nodes; this.nodes = nodes;
} }
} }

View File

@ -1,58 +1,58 @@
package com.baeldung.algorithms.ga.dijkstra; package com.baeldung.algorithms.ga.dijkstra;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class Node { public class Node {
private String name; private String name;
private LinkedList<Node> shortestPath = new LinkedList<>(); private LinkedList<Node> shortestPath = new LinkedList<>();
private Integer distance = Integer.MAX_VALUE; private Integer distance = Integer.MAX_VALUE;
private Map<Node, Integer> adjacentNodes = new HashMap<>(); private Map<Node, Integer> adjacentNodes = new HashMap<>();
public Node(String name) { public Node(String name) {
this.name = name; this.name = name;
} }
public void addDestination(Node destination, int distance) { public void addDestination(Node destination, int distance) {
adjacentNodes.put(destination, distance); adjacentNodes.put(destination, distance);
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Map<Node, Integer> getAdjacentNodes() { public Map<Node, Integer> getAdjacentNodes() {
return adjacentNodes; return adjacentNodes;
} }
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) { public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
this.adjacentNodes = adjacentNodes; this.adjacentNodes = adjacentNodes;
} }
public Integer getDistance() { public Integer getDistance() {
return distance; return distance;
} }
public void setDistance(Integer distance) { public void setDistance(Integer distance) {
this.distance = distance; this.distance = distance;
} }
public List<Node> getShortestPath() { public List<Node> getShortestPath() {
return shortestPath; return shortestPath;
} }
public void setShortestPath(LinkedList<Node> shortestPath) { public void setShortestPath(LinkedList<Node> shortestPath) {
this.shortestPath = shortestPath; this.shortestPath = shortestPath;
} }
} }

View File

@ -1,32 +1,32 @@
package com.baeldung.examples; package com.baeldung.examples;
import com.baeldung.examples.guice.Communication; import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.binding.AOPModule; import com.baeldung.examples.guice.binding.AOPModule;
import com.baeldung.examples.guice.modules.BasicModule; import com.baeldung.examples.guice.modules.BasicModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import java.util.Scanner; import java.util.Scanner;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class RunGuice { public class RunGuice {
public static void main(String[] args) { public static void main(String[] args) {
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule()); Injector injector = Guice.createInjector(new BasicModule(), new AOPModule());
Communication comms = injector.getInstance(Communication.class); Communication comms = injector.getInstance(Communication.class);
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
while (true) { while (true) {
String input = scanner.nextLine(); String input = scanner.nextLine();
if (input.equalsIgnoreCase("q")) { if (input.equalsIgnoreCase("q")) {
System.exit(0); System.exit(0);
} else { } else {
comms.sendMessage(input); comms.sendMessage(input);
} }
} }
} }
} }

View File

@ -1,40 +1,40 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import java.util.Date; import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class Communication { public class Communication {
final Date start = new Date(); final Date start = new Date();
@Inject @Inject
private Logger logger; private Logger logger;
@Inject @Inject
private DefaultCommunicator communicator; private DefaultCommunicator communicator;
public Communication(Boolean keepRecords) { public Communication(Boolean keepRecords) {
if (keepRecords) { if (keepRecords) {
System.out.println("keeping records"); System.out.println("keeping records");
} }
} }
public boolean sendMessage(String message) { public boolean sendMessage(String message) {
return communicator.sendMessage(message); return communicator.sendMessage(message);
} }
public DefaultCommunicator getCommunicator() { public DefaultCommunicator getCommunicator() {
return this.communicator; return this.communicator;
} }
} }

View File

@ -1,12 +1,12 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.guice.constant.CommunicationModel; import com.baeldung.examples.guice.constant.CommunicationModel;
public interface CommunicationMode { public interface CommunicationMode {
public CommunicationModel getMode(); public CommunicationModel getMode();
public boolean sendMessage(String message); public boolean sendMessage(String message);
} }

View File

@ -1,51 +1,51 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.guice.marker.Communicator; import com.baeldung.examples.guice.marker.Communicator;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named; import com.google.inject.name.Named;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class DefaultCommunicator implements Communicator { public class DefaultCommunicator implements Communicator {
private CommunicationMode defaultCommsMode; private CommunicationMode defaultCommsMode;
@Inject @Inject
@Named("SMSComms") @Named("SMSComms")
CommunicationMode smsCommsMode; CommunicationMode smsCommsMode;
@Inject @Inject
@Named("EmailComms") @Named("EmailComms")
CommunicationMode emailCommsMode; CommunicationMode emailCommsMode;
@Inject @Inject
@Named("IMComms") @Named("IMComms")
CommunicationMode imCommsMode; CommunicationMode imCommsMode;
protected DefaultCommunicator(CommunicationMode defaultComms) { protected DefaultCommunicator(CommunicationMode defaultComms) {
this.defaultCommsMode = defaultComms; this.defaultCommsMode = defaultComms;
} }
public DefaultCommunicator() { public DefaultCommunicator() {
} }
public boolean sendMessage(String message) { public boolean sendMessage(String message) {
boolean sent = false; boolean sent = false;
if (defaultCommsMode != null) { if (defaultCommsMode != null) {
sent = sendMessageByDefault(message); sent = sendMessageByDefault(message);
} else { } else {
sent = smsCommsMode.sendMessage(message); sent = smsCommsMode.sendMessage(message);
} }
return sent; return sent;
} }
private boolean sendMessageByDefault(String message) { private boolean sendMessageByDefault(String message) {
boolean sent = false; boolean sent = false;
if (message != null && !message.trim().equals("")) { if (message != null && !message.trim().equals("")) {
return defaultCommsMode.sendMessage(message); return defaultCommsMode.sendMessage(message);
} }
return sent; return sent;
} }
} }

View File

@ -1,24 +1,24 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable; import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel; import com.baeldung.examples.guice.constant.CommunicationModel;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class EmailCommunicationMode implements CommunicationMode { public class EmailCommunicationMode implements CommunicationMode {
@Override @Override
public CommunicationModel getMode() { public CommunicationModel getMode() {
return CommunicationModel.EMAIL; return CommunicationModel.EMAIL;
} }
@Override @Override
@MessageSentLoggable @MessageSentLoggable
public boolean sendMessage(String Message) { public boolean sendMessage(String Message) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
} }

View File

@ -1,30 +1,30 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable; import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel; import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class IMCommunicationMode implements CommunicationMode { public class IMCommunicationMode implements CommunicationMode {
@Inject @Inject
private Logger logger; private Logger logger;
@Override @Override
public CommunicationModel getMode() { public CommunicationModel getMode() {
return CommunicationModel.IM; return CommunicationModel.IM;
} }
@Override @Override
@MessageSentLoggable @MessageSentLoggable
public boolean sendMessage(String message) { public boolean sendMessage(String message) {
logger.info("IM Message Sent"); logger.info("IM Message Sent");
return true; return true;
} }
} }

View File

@ -1,30 +1,30 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable; import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel; import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class SMSCommunicationMode implements CommunicationMode { public class SMSCommunicationMode implements CommunicationMode {
@Inject @Inject
private Logger logger; private Logger logger;
@Override @Override
public CommunicationModel getMode() { public CommunicationModel getMode() {
return CommunicationModel.SMS; return CommunicationModel.SMS;
} }
@Override @Override
@MessageSentLoggable @MessageSentLoggable
public boolean sendMessage(String message) { public boolean sendMessage(String message) {
logger.info("SMS message sent"); logger.info("SMS message sent");
return true; return true;
} }
} }

View File

@ -1,24 +1,24 @@
package com.baeldung.examples.guice.aop; package com.baeldung.examples.guice.aop;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class MessageLogger implements MethodInterceptor { public class MessageLogger implements MethodInterceptor {
@Override @Override
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] objectArray = invocation.getArguments(); Object[] objectArray = invocation.getArguments();
int i = 0; int i = 0;
for (Object object : objectArray) { for (Object object : objectArray) {
Logger.getAnonymousLogger().info("Sending message: " + object.toString()); Logger.getAnonymousLogger().info("Sending message: " + object.toString());
} }
return invocation.proceed(); return invocation.proceed();
} }
} }

View File

@ -1,17 +1,17 @@
package com.baeldung.examples.guice.aop; package com.baeldung.examples.guice.aop;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface MessageSentLoggable { public @interface MessageSentLoggable {
} }

View File

@ -1,23 +1,23 @@
package com.baeldung.examples.guice.binding; package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.aop.MessageLogger; import com.baeldung.examples.guice.aop.MessageLogger;
import com.baeldung.examples.guice.aop.MessageSentLoggable; import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.matcher.Matchers; import com.google.inject.matcher.Matchers;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class AOPModule extends AbstractModule { public class AOPModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
bindInterceptor(Matchers.any(), bindInterceptor(Matchers.any(),
Matchers.annotatedWith(MessageSentLoggable.class), Matchers.annotatedWith(MessageSentLoggable.class),
new MessageLogger() new MessageLogger()
); );
} }
} }

View File

@ -1,37 +1,37 @@
package com.baeldung.examples.guice.binding; package com.baeldung.examples.guice.binding;
import com.baeldung.examples.guice.Communication; import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode; import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator; import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode; import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode; import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode; import com.baeldung.examples.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.name.Names; import com.google.inject.name.Names;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class BasicModule extends AbstractModule { public class BasicModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
try { try {
bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE)); bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE));
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) { } catch (SecurityException ex) {
Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} }
bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton();
bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class);
} }
} }

View File

@ -1,17 +1,17 @@
package com.baeldung.examples.guice.constant; package com.baeldung.examples.guice.constant;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public enum CommunicationModel { public enum CommunicationModel {
EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone"); EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone");
final String name; final String name;
CommunicationModel(String name) { CommunicationModel(String name) {
this.name = name; this.name = name;
} }
} }

View File

@ -1,14 +1,14 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
* and open the template in the editor. * and open the template in the editor.
*/ */
package com.baeldung.examples.guice.marker; package com.baeldung.examples.guice.marker;
/** /**
* *
* @author Tayo * @author Tayo
*/ */
public interface Communicator { public interface Communicator {
} }

View File

@ -1,38 +1,38 @@
package com.baeldung.examples.guice.modules; package com.baeldung.examples.guice.modules;
import com.baeldung.examples.guice.Communication; import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode; import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator; import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode; import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode; import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode; import com.baeldung.examples.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.name.Names; import com.google.inject.name.Names;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author baeldung * @author baeldung
*/ */
public class BasicModule extends AbstractModule { public class BasicModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
try { try {
bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class)); bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class));
bind(Boolean.class).toInstance(true); bind(Boolean.class).toInstance(true);
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) { } catch (SecurityException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} }
bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton();
bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class);
bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class);
} }
} }

View File

@ -1,36 +1,36 @@
package com.baeldung.javaeeannotations; package com.baeldung.javaeeannotations;
import java.io.IOException; import java.io.IOException;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig; import javax.servlet.FilterConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@WebFilter( @WebFilter(
urlPatterns = "/bankAccount/*", urlPatterns = "/bankAccount/*",
filterName = "LogInFilter", filterName = "LogInFilter",
description = "Filter all account transaction URLs" description = "Filter all account transaction URLs"
) )
public class LogInFilter implements javax.servlet.Filter { public class LogInFilter implements javax.servlet.Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
} }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request; HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/login.jsp"); res.sendRedirect(req.getContextPath() + "/login.jsp");
chain.doFilter(request, response); chain.doFilter(request, response);
} }
@Override @Override
public void destroy() { public void destroy() {
} }
} }

View File

@ -1,77 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>libraries</artifactId> <artifactId>libraries</artifactId>
<name>libraries</name> <name>libraries</name>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib --> <!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency> <dependency>
<groupId>cglib</groupId> <groupId>cglib</groupId>
<artifactId>cglib</artifactId> <artifactId>cglib</artifactId>
<version>${cglib.version}</version> <version>${cglib.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version> <version>${commons-lang.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jasypt</groupId> <groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId> <artifactId>jasypt</artifactId>
<version>${jasypt.version}</version> <version>${jasypt.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.javatuples</groupId> <groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId> <artifactId>javatuples</artifactId>
<version>${javatuples.version}</version> <version>${javatuples.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.javassist</groupId> <groupId>org.javassist</groupId>
<artifactId>javassist</artifactId> <artifactId>javassist</artifactId>
<version>${javaassist.version}</version> <version>${javaassist.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core --> <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<version>${assertj.version}</version> <version>${assertj.version}</version>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${jsonassert.version}</version>
</dependency>
</dependencies>
<properties> <properties>
<cglib.version>3.2.4</cglib.version> <cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version> <commons-lang.version>3.5</commons-lang.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<jasypt.version>1.9.2</jasypt.version> <jasypt.version>1.9.2</jasypt.version>
<javatuples.version>1.2</javatuples.version> <javatuples.version>1.2</javatuples.version>
<javaassist.version>3.21.0-GA</javaassist.version> <javaassist.version>3.21.0-GA</javaassist.version>
<assertj.version>3.6.2</assertj.version> <assertj.version>3.6.2</assertj.version>
</properties> <jsonassert.version>1.4.0</jsonassert.version>
</properties>
</project> </project>

View File

@ -0,0 +1,115 @@
package com.baeldung.jsonassert;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.RegularExpressionValueMatcher;
import org.skyscreamer.jsonassert.comparator.ArraySizeComparator;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
public class JsonAssertTest {
@Test
public void givenLenientode_whenAssertEqualsSameJsonString_thenPass() throws JSONException {
String actual = "{id:123,name:\"John\"}";
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
actual = "{id:123,name:\"John\",zip:\"33025\"}";
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
}
@Test
public void givenStrictMode_whenAssertNotEqualsExtendedJsonString_thenPass() throws JSONException {
String actual = "{id:123,name:\"John\"}";
JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT);
}
@Test
public void whenUsingCompareModeOrBoolean_thenBothAreSame() throws JSONException {
String actual = "{id:123,name:\"John\",zip:\"33025\"}";
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, JSONCompareMode.LENIENT);
JSONAssert.assertEquals("{id:123,name:\"John\"}", actual, false);
actual = "{id:123,name:\"John\"}";
JSONAssert.assertNotEquals("{name:\"John\"}", actual, JSONCompareMode.STRICT);
JSONAssert.assertNotEquals("{name:\"John\"}", actual, true);
}
@Test
public void givenDifferentOrderForJsonObject_whenAssertEquals_thenPass() throws JSONException {
String result = "{id:1,name:\"John\"}";
JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.STRICT);
JSONAssert.assertEquals("{name:\"John\",id:1}", result, JSONCompareMode.LENIENT);
}
@Test
public void givenDifferentTypes_whenAssertEqualsSameValue_thenPass() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12345));
JSONAssert.assertEquals(expected, actual, false);
JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
}
@Test
public void givenNestedObjects_whenAssertEquals_thenPass() throws JSONException {
String result = "{id:1,name:\"Juergen\", address:{city:\"Hollywood\", "
+ "state:\"LA\", zip:91601}}";
JSONAssert.assertEquals("{id:1,name:\"Juergen\", address:{city:\"Hollywood\", "
+ "state:\"LA\", zip:91601}}", result, false);
}
@Test
public void givenArray_whenComparing_thenOrderMustMatchForStrict() throws JSONException {
String result = "[Alex, Barbera, Charlie, Xavier]";
JSONAssert.assertEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.LENIENT);
JSONAssert.assertEquals("[Alex, Barbera, Charlie, Xavier]", result, JSONCompareMode.STRICT);
JSONAssert.assertNotEquals("[Charlie, Alex, Xavier, Barbera]", result, JSONCompareMode.STRICT);
}
@Test
public void givenArray_whenComparingExtended_thenNotEqual() throws JSONException {
String result = "[1,2,3,4,5]";
JSONAssert.assertEquals("[1,2,3,4,5]", result, JSONCompareMode.LENIENT);
JSONAssert.assertNotEquals("[1,2,3]", result, JSONCompareMode.LENIENT);
JSONAssert.assertNotEquals("[1,2,3,4,5,6]", result, JSONCompareMode.LENIENT);
}
@Test
public void whenComparingSizeOfArray_thenPass() throws JSONException {
String names = "{names:[Alex, Barbera, Charlie, Xavier]}";
JSONAssert.assertEquals(
"{names:[4]}",
names,
new ArraySizeComparator(JSONCompareMode.LENIENT));
}
@Test
public void whenComparingContentsOfArray_thenPass() throws JSONException {
String ratings = "{ratings:[3.2,3.5,4.1,5,1]}";
JSONAssert.assertEquals(
"{ratings:[1,5]}",
ratings,
new ArraySizeComparator(JSONCompareMode.LENIENT));
}
@Test
public void givenValueMatcher_whenComparingUsingRegex_thenPass() throws IllegalArgumentException, JSONException {
JSONAssert.assertEquals("{entry:{id:x}}", "{entry:{id:1, id:2}}",
new CustomComparator(
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))));
JSONAssert.assertNotEquals("{entry:{id:x}}", "{entry:{id:1, id:as}}",
new CustomComparator(JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))));
}
}

0
xml/src/main/resources/customer-binding.xml Executable file → Normal file
View File