Mock Object and Interface - java

I'm a newbie in Unit Test with Mock Object. I use EasyMock. I try to understand this example:
import java.io.IOException;
public interface ExchangeRate {
double getRate(String inputCurrency, String outputCurrency) throws IOException;
}
import java.io.IOException;
public class Currency {
private String units;
private long amount;
private int cents;
public Currency(double amount, String code) {
this.units = code;
setAmount(amount);
}
private void setAmount(double amount) {
this.amount = new Double(amount).longValue();
this.cents = (int) ((amount * 100.0) % 100);
}
public Currency toEuros(ExchangeRate converter) {
if ("EUR".equals(units)) return this;
else {
double input = amount + cents/100.0;
double rate;
try {
rate = converter.getRate(units, "EUR");
double output = input * rate;
return new Currency(output, "EUR");
} catch (IOException ex) {
return null;
}
}
}
public boolean equals(Object o) {
if (o instanceof Currency) {
Currency other = (Currency) o;
return this.units.equals(other.units)
&& this.amount == other.amount
&& this.cents == other.cents;
}
return false;
}
public String toString() {
return amount + "." + Math.abs(cents) + " " + units;
}
}
import junit.framework.TestCase;
import org.easymock.EasyMock;
import java.io.IOException;
public class CurrencyTest extends TestCase {
public void testToEuros() throws IOException {
Currency testObject = new Currency(2.50, "USD");
Currency expected = new Currency(3.75, "EUR");
ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
EasyMock.replay(mock);
Currency actual = testObject.toEuros(mock);
assertEquals(expected, actual);
}
}
So, i wonder how to Currency use ExchangeRate in toEuros(..) method.
rate = converter.getRate(units, "EUR");
The behavior of getRate(..) method is not specified because ExchangeRate is an interface.
/********************************************************************************/
So I try do myself example. Following is my codes:
/**
*Interface to access data
*/
public interface Dao {
public boolean getEntityById(int id) throws SQLException;
}
/**
*Business class do something in business layer
*/
public class Bussiness {
private Dao dao;
public Dao getDao() {
return dao;
}
public void setDao(Dao dao) {
this.dao = dao;
}
public boolean doSomeThing(int id) throws SQLException {
if(dao.getEntityById(id)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) throws SQLException {
Bussiness b = new Bussiness();
b.doSomeThing(3);
}
}
package tunl;
import java.sql.SQLException;
import org.easymock.EasyMock;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/**
* This is my unit Test
*/
#Test
public class MyUnitTest {
private Bussiness bussiness;
private Dao mock;
#BeforeTest
public void setUp() {
bussiness = new Bussiness();
mock = EasyMock.createMock(Dao.class);// interface not class
bussiness.setDao(mock);
}
public void testDoSomeThing() throws SQLException {
EasyMock.expect(mock.getEntityById(3)).andReturn(true);
EasyMock.replay(mock);
Assert.assertTrue(bussiness.doSomeThing(3));
}
}
So, The unit Tess run correctly
But when i want to run main method in Business Object:
public static void main(String[] args) throws SQLException {
Bussiness b = new Bussiness();
b.doSomeThing(3);
}
I have to add the constructor for Business.
public Bussiness() {
dao = new DaoImpl();
}
So, my business class is:
package tunl;
import java.sql.SQLException;
public class Bussiness {
private Dao dao;
public Bussiness() {
dao = new DaoImpl();
}
public Dao getDao() {
return dao;
}
public void setDao(Dao dao) {
this.dao = dao;
}
public boolean doSomeThing(int id) throws SQLException {
if(dao.getEntityById(id)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) throws SQLException {
Bussiness b = new Bussiness();
b.doSomeThing(3);
}
}
Also I have to implement Dao interface:
package tunl;
import java.sql.SQLException;
public class DaoImpl implements Dao {
#Override
public boolean getEntityById(int id) throws SQLException {
if(id == 3) {
System.out.println("System input 3 ");
return true;
}
System.out.println("You have to input 3 ");
return false;
}
}
In design, you always create interface for all of the classes which will be tested (like DaoImpl) !!!
So is it correct?

EasyMock creates a mock object based on the interface. The mock object implements all the methods of the interface and for those methods you specify (e.g. with expect), it "replays" the specified behaviour when they are called.
When a mock object is created, it is in recording mode. The line
EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
specifies that when mock.getRate is called with the given parameters, it shall return 1.5 . Then the object is put into replay mode with the call
EasyMock.replay(mock);
All this is explained in more details in the documentation.
Update: to your comment - Currency is passed an instance of ExchangeRate here:
public Currency toEuros(ExchangeRate converter) { ... }
All it cares is that it gets an object implementing that interface, so that
rate = converter.getRate(units, "EUR");
can be called. The test method, then, passes the mock object it created to the currency object:
Currency actual = testObject.toEuros(mock);
Hope this helps; if not, maybe you could read some introductory text on OOP, interfaces and inheritance to get a better understanding.
In the code example in your answer, the Dao object should be passed to Bussiness rather than created internally, since the latter effectively prevents unit testing.
public static void main(String[] args) throws SQLException {
Bussiness b = new Bussiness();
b.setDao(new DaoImpl());
b.doSomeThing(3);
}
You could also add a parameterized constructor to Bussiness to make the initialization in one step instead of two.

Related

Mockito problems and says undefinied type while ı am testing my service

I am writing unit test but I am facing an error some how. I am triyng to test my ServiceImpl just showing my entire code down below My code below;
My Service Class
#Service
public class PlaneServiceImpl implements PlaneCallerService {
private final PlaneFactory planeFactory;
public PlaneServiceImpl(PlaneFactory planeFactory) {
this.planeFactory = planeFactory;
}
#Override
public String getPlaneType(String planeType) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(planeFactory.getPlane(planeType).getType());
stringBuilder.append(" Plane has produced.");
return stringBuilder.toString();
}
Plane class down below
public interface Plane {
String getType();
}
My PlaneFactory class down below;
#Component
public class PlaneFactory {
public Plane getPlane(String planeType) {
if (StringUtils.isBlank(planeType)) {
throw new PlaneTypeNotFoundException();
}
if (planeType.equalsIgnoreCase("lightJet")) {
return new LightJet();
} else if (planeType.equalsIgnoreCase("midJet")) {
return new MidJet();
My Mock Test just down below
public class PlaneCallerServiceImplTest {
private PlaneFactory planeFactory;
private PlaneCallerService planeCallerService;
private plane plane;
#Before
public void setUp() {
planeFactory = mock(PlaneFactory.class);
planeCallerService = new PlaneCallerServiceImpl(planeFactory);
plane= mock(Plane.class);
}
#Test
public void testPlaneType() {
String planeType = "";
when(planeFactory.getPlane(planeType)).thenReturn(plane);
String result = planeCallerService.getplaneType(planeType);
assertNotNull(result);
}
}
I'm getting The method getPlane(String) is undefined for the type PlaneFactory
I am quite new for unit test and also mock test any help would be appreciate
Thank you in advanced
Your issue is that from the below statement:
when(planeFactory.getPlane(planeType)).thenReturn(plane);
you are returning a mocked response of type Plane but in that mocked response when you call Plane.getType() that method is not implemented.
You can mock the response of that too, add
when(plane.getType()).thenReturn("SOME_MOCKED_STRING");
This should start to work.
Below is the complete test class:
public class PlaneServiceImplTest {
private PlaneFactory planeFactory;
private PlaneServiceImpl planeCallerService;
#Before
public void setUp() {
planeFactory = mock(PlaneFactory.class);
planeCallerService = new PlaneServiceImpl(planeFactory);
}
#Test
public void testPlaneType() {
Plane plane = mock(Plane.class);
when(planeFactory.getPlane(anyString())).thenReturn(plane);
String result = planeCallerService.getPlaneType("Test");
assertNotNull(result);
}
}

public void method throws Exception if - JUNIT test: Exception should not be thrown

I have a certain public void method which throws an Exception if a condition is fulfilled.
In my case the method looks like this:
public void toBeTestedMethod(Testobject testObject) throws CertainException {
if (testObject.getStatus().getAllowsEdit()){
throw ...}
}
getStatus() is a method which returns a certain Status and getAllowsEdit() is a method which returns a boolean value and nullable = true. For the two methods there also exist set-methods.
Edit1: The test regarding this method when it fails is already running fine:
public void testToBeTestedMethod_FailureStatus() throws Exception {
try {
TestObject testObject = _testObjectMockDAO.getNewTestObject();
_testObjectMockDAO.setTestObject(testObject);
_testObjectBusinessImpl.toBeTestedMethod(testObject);
fail("Check failed");
} catch (CertainException ex) {
assertEquals(ErrorCode.WRONG_STATUS, ex.getErrorCode());
}
}
I would now like to test the method toBeTestedMethod. The target is that the method does not throw an exception but gets executed successfully.
That means I would like to write a JUNIT-test which tests the following:
public void testToBeTestedMethod_success throws Exception{
// Enter test code here
}
Edit2 (regarding the class Status):
public class Status {
...
private String _status;
public String getStatus() {
return _status;
}
}
In my opinion, I have to modify the condition in the if-statement in order to get the expected result, correct?
Note: I did not write the method and the other code. Nevertheless, my task is to test the code via JUNIT.
I tried some code, but everytime I get the error that the Excpetion was thrown.
Even if you cannot solve this problem, I would be glad to get some hints where I should look for the problem why my test does not do what I want the test to do.
Your question is very abstract and needs more data, I am posting an answer here based on what I have understood.
Here are the classes:
public class SampleTestService {
public boolean toBeTestedMethod(TestObject testObject) throws AccessViolationException {
if (testObject.getStatus().getAllowsEdit()) {
throw new AccessViolationException("Edit is allowed for this non confirmed user");
} else {
return true;
}
}
static class TestObject {
private SomeStatus someStatus;
public SomeStatus getStatus() {
return someStatus;
}
}
static class SomeStatus {
private boolean allowsEdit;
public boolean getAllowsEdit() {
return allowsEdit;
}
}
static class AccessViolationException extends RuntimeException {
public AccessViolationException(String message) {
super(message);
}
}
}
Since the method depends on another class and that class-dependent also on another class you need to mock them in the chain. Here is how I have done it:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
#ExtendWith(SpringExtension.class)
class SampleTestServiceTest {
private final SampleTestService.TestObject mockTestObject = mock(SampleTestService.TestObject.class);
private final SampleTestService.SomeStatus mockSomeStatus = mock(SampleTestService.SomeStatus.class);
private final SampleTestService service = new SampleTestService();
#Test
void testThatMethodDoesNotThrowsException() {
when(mockTestObject.getStatus()).thenReturn(mockSomeStatus);
when(mockSomeStatus.getAllowsEdit()).thenReturn(false);
boolean result = service.toBeTestedMethod(mockTestObject);
Assertions.assertTrue(result);
}
#Test
void testThatMethodThrowsException() {
when(mockTestObject.getStatus()).thenReturn(mockSomeStatus);
when(mockSomeStatus.getAllowsEdit()).thenReturn(true);
Assertions.assertThrows(SampleTestService.AccessViolationException.class, () -> {
service.toBeTestedMethod(mockTestObject);
});
}
}

#MockClass is not working

I am new to jmockit and trying to execute the following online example.
The #MockClass is not working. My BookStore's getBookTitle() method is calling the function of orginal class instead of the mock class.
BookStore class:
public class BookStore {
public String getBookTitle(String isbn){
return BookStoreService.getBookTitle(isbn);
}
}
BookStoreService class:
public class BookStoreService {
public static String getBookTitle(String isbn){
return "Random";
}
}
Test class:
public class BookStoreTest {
private static Map<String, String> bookMap = new HashMap<String, String>(2);
#BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
}
#MockClass(realClass = BookStoreService.class)
public static class MockBookstoreService {
#Mock
public static String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
}
#Test
public void testGetBookTitle() throws Exception {
System.out.println("in testGetBookTitle()");
final String isbn = "0553293354";
final String expectedTitle = "Foundation";
BookStore store = new BookStore();
String title = store.getBookTitle(isbn);
System.out.println(title); // This prints "Random" instead of "Foundation"
Assert.assertEquals(title, expectedTitle);
}
}
PS: I am using TestNG
Using the latest stable version of jmockit you could do it like this:
#BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
new MockUp<BookStoreService>() {
#Mock
public String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
};
}
Remove the obsolete block:
public static class MockBookstoreService{...}

How to apply Simple Factory Pattern Java

I am trying ot understand how to apply a the simple factory pattern to an assigment I have but I do not understand how to do it.
This is the request: Apply the Simple Factory pattern that creates the appropriate Strategy object.
Remember, this is a Simple Factory pattern. Draw the UML diagram of the new
design.
We already implemented the strategy pattern but I do not understand how to apply the simple factory pattern to the code. I understand that the simple factory pattern is supposed to provide encapsulation for the creation of the object but I do not see how the examples I have found show how to apply to this. Any help would be appreciated.
EDIT: Updated code
EDIT: Changed code to make use of polymorphism
package Client;
import domain.Loan;
import factory.StrategyFactory;
import strategy.ComplexLoan;
import strategy.ICapitalStrategy;
public class Client {
public static void main(String[] args){
Loan complexLoan = new Loan(2.2, 2, 3.3, 4.4, 5, 6, 7, StrategyFactory.getComplexStrategy());
System.out.print("hello");
}
}
package factory;
import strategy.ComplexLoan;
import strategy.ICapitalStrategy;
import strategy.RevolvingLoan;
import strategy.TermLoan;
public class StrategyFactory {
/*
public static ICapitalStrategy getStrategy(String type) {
if (type.equals("Complex")){
return new ComplexLoan();
}
else if (type.equals("Revolving")){
return new RevolvingLoan();
}
else if (type.equals("Term")){
return new TermLoan();
}
return null;
}
*/
public static ICapitalStrategy getComplexStrategy() {
return new ComplexLoan();
}
public static ICapitalStrategy getRevolvingStrategy() {
return new RevolvingLoan();
}
public static ICapitalStrategy getTermStrategy() {
return new TermLoan();
}
}
package domain;
import strategy.ICapitalStrategy;
public class Loan {
private ICapitalStrategy strategy;
double commitment;
int duration;
double riskFactor;
double unusedPercentage;
int outstandingRiskAmount;
int unusedRiskAmount;
double unusedRiskFactor;
double capital;
public Loan(double commit, int dura, double rskFact, double unusedPer,
int outStandRskAmt, int unusedRskAmt, double unusedRskFac,
ICapitalStrategy strat) {
this.commitment = commit;
this.duration = dura;
this.riskFactor = rskFact;
this.outstandingRiskAmount = outStandRskAmt;
this.unusedRiskAmount = unusedRskAmt;
this.unusedRiskFactor = unusedRskFac;
this.strategy = strat;
}
public double CalculateCapital() {
return strategy.CapitalLoan(this);
}
public double getCommitment() {
return commitment;
}
public void setCommitment(double c) {
commitment = c;
}
public int getDuration() {
return duration;
}
public void setDuration(int dur) {
duration = dur;
}
public double getRiskFactor() {
return riskFactor;
}
public void setRiskFactor(double rskFac) {
riskFactor = rskFac;
}
public double getUnusedPercentage() {
return unusedPercentage;
}
public void setUnusedPercentage(double unusedPercent) {
unusedPercentage = unusedPercent;
}
public double getOutstandingRiskAmount() {
return outstandingRiskAmount;
}
public void setOutstandingRiskAmount(int outStandingRskAmt) {
outstandingRiskAmount = outStandingRskAmt;
}
public double getUnusedRiskAmount() {
return unusedRiskAmount;
}
public void setUnusedRiskAmount(int UnusedRskAmt) {
unusedRiskAmount = UnusedRskAmt;
}
public double getUnusedRiskFactor() {
return unusedRiskFactor;
}
public void setUnusedRiskFactor(double unusedRskFac) {
unusedRiskFactor = unusedRskFac;
}
public Loan(ICapitalStrategy strategy) {
this.strategy = strategy;
}
/*public double executeStrategy() {
return this.strategy.CapitalLoan(this);
}
*/
}
package strategy;
import domain.Loan;
public class ComplexLoan implements ICapitalStrategy {
#Override
public double CapitalLoan(Loan l) {
return ((l.getOutstandingRiskAmount() * l.getDuration() * l.getRiskFactor()) + (l.getUnusedRiskAmount()
* l.getDuration() * l.getUnusedRiskFactor() ));
}
}
package strategy;
import domain.Loan;
public interface ICapitalStrategy {
public double CapitalLoan(Loan l);
}
package strategy;
import domain.Loan;
public class RevolvingLoan implements ICapitalStrategy {
#Override
public double CapitalLoan(Loan l) {
return (l.getCommitment() * l.getUnusedPercentage() * l.getDuration() *l.getRiskFactor());
}
}
package strategy;
import domain.Loan;
public class TermLoan implements ICapitalStrategy {
public TermLoan() {
}
public double CapitalLoan(Loan l) {
return (l.getCommitment() * l.getDuration() * l.getRiskFactor());
}
}
Here's a likely helpful bit about Simple Factory Pattern[1]:
The simple factory isn't actually a pattern; it's more of a design principle. The simple factory encapsulates the object creation code, but keeps control over how the object is created. Simple factories are often designed as a class with a static method (aka static factory) that returns the object requested.
Here's an example, not suited directly to your example in order to make you think a bit hard for your homework :)
interface Foo{
double calculateStuff();
}
class MyClass implements Foo{
#Override
public double calculateStuff(){
//logic goes here
}
}
class MyFactory {
public static double getCalculatedStuff(){
return new MyClass().calculateStuff();
}
}
class RunCode {
public static void main(String[] args){
double stuff = MyFactory.getCalculatedStuff();
}
}
[1] - Learning Design Patterns - Factory Pattern
EDIT:
class LoanFactory{
public static double getComplexLoan(Loan l){
return new ComplexLoan().CapitalLoan(l);
}
}
another way (which has its uses, but in this case I prefer the method above):
class ComplexLoan implements ICapitalStrategy{
private ComplexLoan(){
}
public static double getLoan(Loan l){
return new ComplexLoan().CapitalLoan(l);
}
}
or this option that explicitly displays polymorphism:
class LoanFactory{
public static ICapitalStrategy getComplexLoan(){
return new ComplexLoan();
}
}
One other thing that is important to note: convention says method names should start with a lowercase letter, so your CapitalLoan() would be capitalLoan().
Also, there's no need to prefix your interfaces with an I, so your ICapitalStrategy would become CapitalStrategy.
In the following example. The "Document" is abstract class and "html" document. "MyDocument" and "pdf" are concrete class. As long as you provide parameter with valid string, you will get the corresponding concrete class. For example if you put "pdf" as a parameter, you will get pdf document. Document type is all you need to accept whatever type of documents you want to create.
public Document CreateDocument(String type){
if (type.isEqual("html"))
return new HtmlDocument();
if (type.isEqual("proprietary"))
return new MyDocument();
if (type.isEqual("pdf"))
return new PdfDocument ();
}
There is a better documented article thread in
Examples of GoF Design Patterns in Java's core libraries
public interface PaymentMethod {
public void makePayment();
}
class CreditCard implements PaymentMethod {
public void makePayment() {
System.out.println("Payment through credit card...");
}
}
class NetBanking implements PaymentMethod {
public void makePayment() {
System.out.println("Payment through net banking...");
}
}
public class PaymentMethodFactory {
public static PaymentMethod getPaymentMethod(String method) {
if ("creditcard".equalsIgnoreCase(method)) {
return new CreditCard();
} else if ("netbanking".equalsIgnoreCase(method)) {
return new NetBanking();
} else {
throw new IllegalArgumentException("Payment method not supported!");
}
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
PaymentMethodFactory factory = new PaymentMethodFactory();
PaymentMethod paymentMethod = factory.getPaymentMethod("creditcard");
paymentMethod.makePayment();
}
}

Java - Execute a class method with a specify annotation

I have a android application, but it is not relevant.
I have a class called "Front controller" which will receive some message
through it's constructor. The message, for brievity, could be an integer.
I want somewhere else to create a new controller which will execute
a method based on the integer defined above
public class OtherController {
#MessageId("100")
public void doSomething(){
//execute this code
}
#MessageId("101")
public void doSomethingElse(){
//code
}
}
The front controller could be something like this:
public class FrontController {
private int id;
public FrontController(int id){
this.id=id;
executeProperControllerMethodBasedOnId();
}
public void executeProperControllerMethodBasedOnId(){
//code here
}
public int getId(){
return id;
}
}
So, if the Front Controller will receive the integer 100, it
will execute the method annotated with #MessageId(100). The
front controller don't know exactly the class where this method
is.
The problem which I found is that I need to register somehow
each controller class. I Spring I had #Component or #Controller
for autoloading. After each controllers are register, I need to
call the properly annotated method.
How to achieve this task? In Spring MVC, I had this system
implemented, used to match the HTTP routes. How could I implement
this in a plain java project?
Any suggestions?
Thanks to Google Reflections (hope you can integrate this in your android project.)
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.8</version>
</dependency>
For optimisation I've added the requirement to also annotate the class with MessageType annotation and the classes should be in the same package (org.conffusion in my example):
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface MessageType {
}
The OtherController looks like:
#MessageType
public class OtherController {
#MessageId(id=101)
public void method1()
{
System.out.println("executing method1");
}
#MessageId(id=102)
public void method2()
{
System.out.println("executing method2");
}
}
The implementation will look like:
public void executeProperControllerMethodBasedOnId() {
Set<Class<?>> classes = new org.reflections.Reflections("org.conffusion")
.getTypesAnnotatedWith(MessageType.class);
System.out.println("found classes " + classes.size());
for (Class<?> c : classes) {
for (Method m : c.getMethods()) {
try {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object o = c.newInstance();
if (mid.id() == id)
m.invoke(o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Maybe you can optimise and build a static hashmap containing already scanned message ids.
You need to implement some of the work by yourself using reflection, I would recommend to prepare message handlers on initial phase in regards to performance. Also you possibly want to think about Singleton/Per Request controllers. Some of the ways to implement the solution:
interface MessageProcessor {
void execute() throws Exception;
}
/* Holds single instance and method to invoke */
class SingletonProcessor implements MessageProcessor {
private final Object instance;
private final Method method;
SingletonProcessor(Object instance, Method method) {
this.instance = instance;
this.method = method;
}
public void execute() throws Exception {
method.invoke(instance);
}
}
/* Create instance and invoke the method on execute */
class PerRequestProcessor implements MessageProcessor {
private final Class clazz;
private final Method method;
PerRequestProcessor(Class clazz, Method method) {
this.clazz = clazz;
this.method = method;
}
public void execute() throws Exception {
Object instance = clazz.newInstance();
method.invoke(instance);
}
}
/* Dummy controllers */
class PerRequestController {
#MessageId(1)
public void handleMessage1(){System.out.println(this + " - Message1");}
}
class SingletonController {
#MessageId(2)
public void handleMessage2(){System.out.println(this + " - Message2");}
}
class FrontController {
private static final Map<Integer, MessageProcessor> processors = new HashMap<Integer, MessageProcessor>();
static {
try {
// register your controllers
// also you can scan for annotated controllers as suggested by Conffusion
registerPerRequestController(PerRequestController.class);
registerSingletonController(SingletonController.class);
} catch (Exception e) {
throw new ExceptionInInitializerError();
}
}
private static void registerPerRequestController(Class aClass) {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
processors.put(mid.value(), new PerRequestProcessor(aClass, m));
}
}
}
private static void registerSingletonController(Class aClass) throws Exception {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object instance = aClass.newInstance();
processors.put(mid.value(), new SingletonProcessor(instance, m));
}
}
}
/* To process the message you just need to look up processor and execute */
public void processMessage(int id) throws Exception {
if (processors.containsKey(id)) {
processors.get(id).execute();
} else {
System.err.print("Processor not found for message " + id);
}
}
}

Categories