Java Doc for SurrogateManager.java in  » Testing » Surrogate » net » sf » surrogate » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Surrogate » net.sf.surrogate.core 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   net.sf.surrogate.core.SurrogateManager

All known Subclasses:   net.sf.surrogate.core.MockMethod,
SurrogateManager
public class SurrogateManager (Code)
The link between Mock/ Unit Test objects and AspectJ code.

The SurrogateManager is the common controller for Junit tests manipulating MockObjects and mock methods.

Test Case View

The SurrogateManager provides utility methods for test cases to control which Mock objects should be active for a particular test case run. These methods are normally:
SurrogateManager.getInstance()SurrogateManager.reset()SurrogateManager.addMock(Object)SurrogateManager.addMockMethod(MockMethod)SurrogateManager.removeMock(Object)SurrogateManager.removeMockMethod(MockMethod) 

Aspect View

Every time an aspect detects a "mock poincut", it will ask the SurrogateManager if a mock object has been registered for the corresponding joinpoint. If so, the mock executes instead of the "real" method. Otherwise, the real object is allowed to execute. The methods used by the aspects are normally:
SurrogateManager.getInstance()SurrogateManager.getMockExecutor(JoinPoint) 

Test Case responsibility

A test case should normally always call the SurrogateManager.reset method before starting to use the SurrogateManager to avoid independence from other testcases which might have been run earlier from within the same VM.

Debugging

If the System property "surrogate.debug" is set to "true", e.g. with the java -Dsurrogate.debug=true option, the SurrogateManager will report every time it is asked to resolve a JoinPoint into a mock object or method. The information is written to System.out. Each debug line is on the following format:
 surrogate:<joinpoint id>|added|removed=<mock id>
 Example:
 surrogate:added=public static native long java.lang.System.currentTimeMillis()
 surrogate:added=net.sf.surrogate.example.MockFileWriter@38e059
 surrogate:call(long java.lang.System.currentTimeMillis())=public static native long java.lang.System.currentTimeMillis()
 surrogate:call(java.io.FileWriter(String))=net.sf.surrogate.example.MockFileWriter@38e059
 surrogate:call(java.io.BufferedWriter(Writer))=null
 
I.e. the unit test has added mocks for currentTimeMillis and the FileWriter. As expected, mocks were returned for the currentTimeMillis and FileWriter mockJoinPoint's but no mock was found for the BufferedWriter joinpoint (null was returned).
See Also:   SurrogateCalls
See Also:   MockMethod
author:
   Per S Hustad

Inner Class :public static interface MockExecutor



Method Summary
public  ObjectaddMock(Object o)
     Adds a Mock object to the manager for later lookup by Aspect code.
public  MockMethodaddMockMethod(MockMethod m)
     Adds a mock method to the manager for later lookup by Aspect code.

Note that for objects to be substituted with their mock implementation, there must have been defined a pointcut intercepting the method call or method execution.

protected static  SurrogateManagercreateInstance()
     Created an instance of the manager.
public static  SurrogateManagergetInstance()
    
 MockMethodgetMockConstructor(Constructor c)
    
public  MockExecutorgetMockExecutor(JoinPoint p)
     Gets the mock object or mock method for a joinpoint.
 MockMethodgetMockMethod(Method m)
     Gets a registered Mock method reference for a method.
protected  MockMethodgetMockMethod(JoinPoint p)
     Checks if an AspectJ "mock" joinpoint has an assoicated mock method registered by the manager.
 ObjectgetMockObject(Class myClassOrInterface)
     Locates and gets a Mock object for an interface or a class.
protected  ObjectgetReturnedClassMock(JoinPoint p)
     Checks if an AspectJ "mock" joinpoint has an assoicated mock object registered by the manager.
public  ObjectremoveMock(Object o)
     Removes a mock from the manager.
public  MockMethodremoveMockMethod(MockMethod m)
     Removes a mock method from the manager.
public  voidreset()
     Removes all Mock objects and methods from the list of active objects.



Method Detail
addMock
public Object addMock(Object o)(Code)
Adds a Mock object to the manager for later lookup by Aspect code.

Note that for objects to be substituted with their mock implementation, there must have been defined a pointcut intercepting the method call or method execution. Otherwise, the mock object will never be substituted, even if it has been registered. See SurrogateCalls for pointcut definition details.

Surrogate uses the java.lang.Class.isAssignableFrom to see whether the registered mock can subsitute a "real" object. It does this by looking up the declared return type signature of the method or the class signature of the constructor call.

Example usage:

 SurrogateManager mm = SurrogateManager.getInstance();
 mm.reset();
 MockCustomerService mock = new MockCustomerService();
 mm.addMock(mock);
 mock.setGetCustomerReturnValue("MockCustomer");
 ...
 

Parameters:
  o - the Mock object to add. Must be non-null. the Mock object given as argument
See Also:   SurrogateManager.addMockMethod(MockMethod)
See Also:   SurrogateManager.removeMock(Object)
See Also:   SurrogateCalls



addMockMethod
public MockMethod addMockMethod(MockMethod m)(Code)
Adds a mock method to the manager for later lookup by Aspect code.

Note that for objects to be substituted with their mock implementation, there must have been defined a pointcut intercepting the method call or method execution. Otherwise, the mock object will never be substituted, even if it has been registered. See SurrogateCalls for pointcut definition details.

Example usage:

 SurrogateManager mm = SurrogateManager.getInstance();
 mm.reset();
 MockMethod mockTime = 
 mm.addMockMethod(new MockMethod(System.class,"currentTimeMillis"));
 mockTime.addReturnValue(1000L);
 mockTime.addReturnValue(2000L);
 mockTime.setExpectedCalls(2);
 ... Call object using System.currentTimeMillis
 mockTime.verify(); 
 ...
 

Parameters:
  m - the Mock object to add. Must be non-null. the MockMethod as given on input
See Also:   SurrogateManager.addMock(Object)
See Also:   SurrogateManager.removeMockMethod(MockMethod)



createInstance
protected static SurrogateManager createInstance()(Code)
Created an instance of the manager. Override this method if you want to create a subclass of the manager a new instance of the manager



getInstance
public static SurrogateManager getInstance()(Code)
Gets the manager singleton the manager instance.
See Also:   SurrogateManager.reset



getMockConstructor
MockMethod getMockConstructor(Constructor c)(Code)
Gets a registered Mock method reference for a constructor
Parameters:
  c - the constructor to check the registered Mock method reference or null if nosuch reference exists for the method
See Also:   SurrogateManager.addMockMethod(MockMethod)



getMockExecutor
public MockExecutor getMockExecutor(JoinPoint p) throws IllegalArgumentException, NoSuchMethodException(Code)
Gets the mock object or mock method for a joinpoint. This method should normally only be called from the corresponding SurrogateCalls advice but can also be called from other advices which might want to check if a mock matches the joinpoint.

The rules are as follows

  • If a MockMethod matches the joinpoint, this method is returned.
  • Otherwise, if a Mock object matches the joinpoint, this object is returned, wrapped behind a MockExecutor
  • Otherwise, null is returned.
The advices should, if receiving a non-null MockExecutor return value, call the MockExecutor.execute with the same JoinPoint as used as arguments to getMockExecutor. If the returned MockExecutor is null, the advice should return a sensible value, e.g. with return proceed();
Parameters:
  p - the thisJoinPoint on the advice executing the corresponding mock executor or null if nomatch has been found with a registered mock object or mock method
throws:
  IllegalArgumentException - if the joinpoint signature is not a "constructor" or "method"signature.
throws:
  NoSuchMethodException - if the joinpoint is inconsistent. This should normally beconsidered as a bug.
See Also:   SurrogateCalls
See Also:   SurrogateManager.addMock(Object)
See Also:   SurrogateManager.addMockMethod(MockMethod)



getMockMethod
MockMethod getMockMethod(Method m)(Code)
Gets a registered Mock method reference for a method. This method should normally only be called from the aspect code.
Parameters:
  m - the Method to check the registered Mock method reference or null if nosuch reference exists for the method
See Also:   SurrogateManager.addMockMethod(MockMethod)



getMockMethod
protected MockMethod getMockMethod(JoinPoint p) throws IllegalArgumentException, NoSuchMethodException(Code)
Checks if an AspectJ "mock" joinpoint has an assoicated mock method registered by the manager.
Parameters:
  p - the join point the corresponding mock method or null if no matchis found
throws:
  IllegalArgumentException - if the joinpoint signature is not a "method" or "constructor"signature



getMockObject
Object getMockObject(Class myClassOrInterface)(Code)
Locates and gets a Mock object for an interface or a class. This method looks for a Mock object implementing the interface or the class assignable from myClassOrInterface by searching in the list of Mock objects for the first object which myClassOrInterface is assignable from
Parameters:
  myClassOrInterface - the interface/class to find a mock object for the Mock object for the interface/class or null ifno such mock object has been registered via theaddMock method.
See Also:   SurrogateManager.addMock(Object)
See Also:   Class.isAssignableFrom(java.lang.Class)



getReturnedClassMock
protected Object getReturnedClassMock(JoinPoint p) throws IllegalArgumentException(Code)
Checks if an AspectJ "mock" joinpoint has an assoicated mock object registered by the manager.
Parameters:
  p - the join point the corresponding mock object or null if no matchis found



removeMock
public Object removeMock(Object o)(Code)
Removes a mock from the manager. The object will hence no longer be returned instead of a "real" object when the corresponding aspect advice executes
Parameters:
  o - the object to remove. the removed object or null if the object was notfound
See Also:   SurrogateManager.addMock(Object)



removeMockMethod
public MockMethod removeMockMethod(MockMethod m)(Code)
Removes a mock method from the manager. The method will hence no longer execute instead of the "real" method when the corresponding aspect advice executes.
Parameters:
  o - the object to remove. the removed method or null if the mock method wasnot found.
See Also:   SurrogateManager.addMockMethod(MockMethod)



reset
public void reset()(Code)
Removes all Mock objects and methods from the list of active objects. This method should be called by every TestCase method to ensure that "old" Mocks created by other testcases are not hanging around in the system ...



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.