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 963cc51a7a.

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

View File

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

View File

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

View File

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

View File

@@ -1,24 +1,24 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
/**
*
* @author baeldung
*/
public class EmailCommunicationMode implements CommunicationMode {
@Override
public CommunicationModel getMode() {
return CommunicationModel.EMAIL;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String Message) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
/**
*
* @author baeldung
*/
public class EmailCommunicationMode implements CommunicationMode {
@Override
public CommunicationModel getMode() {
return CommunicationModel.EMAIL;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String Message) {
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;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;
/**
*
* @author baeldung
*/
public class IMCommunicationMode implements CommunicationMode {
@Inject
private Logger logger;
@Override
public CommunicationModel getMode() {
return CommunicationModel.IM;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String message) {
logger.info("IM Message Sent");
return true;
}
}
package com.baeldung.examples.guice;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;
/**
*
* @author baeldung
*/
public class IMCommunicationMode implements CommunicationMode {
@Inject
private Logger logger;
@Override
public CommunicationModel getMode() {
return CommunicationModel.IM;
}
@Override
@MessageSentLoggable
public boolean sendMessage(String message) {
logger.info("IM Message Sent");
return true;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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