CAPÍTULO 1. ANTECEDENTES
1.5 La educación ambiental para la sustentabilidad
1 . Let’s now write a service that grants the group, named deny-access, read access to /content/
geometrixx/fr .
Your pom .xml has already the following dependency
<dependency>
<groupId>org .apache .jackrabbit</groupId>
<artifactId>jackrabbit-api</artifactId>
<version>2 .2 .0</version>
<scope>provided</scope>
</dependency>
You cannot easily find this bundle in the AEM Web Console . This bundle is exported by the system bundle (bundle 0) .
The service you implement will add the required permissions to the specified location upon activation of the OSGi component . Create the ModifyPermissions component, with the appropriate OSGi annotations .
ADOBE COPYRIGHT PROTECTED
package com.adobe.training.core;
}
Privilege[] privileges =
{accessControlManager.privilegeFromName(Privilege.JCR_READ)};
acl.addAccessControlEntry(denyAccess.getPrincipal(), privileges);
accessControlManager.setPolicy(CONTENT_GEOMETRIXX_FR, acl);
adminSession.save();
}catch (RepositoryException e){
LOGGER.error(“**************************Repo Exception”, e);
}finally{
if (adminSession != null) adminSession.logout();
} }
}
2 . Deploy the bundle by executing:
mvn clean install –P bundle
3 . Reopen the ACL Editor and check the permissions for the page:
/content/geometrixx/fr
ADOBE COPYRIGHT PROTECTED
4 . And of course, when you sign in with user John Doe again, you are able to open the French page .
ADOBE COPYRIGHT PROTECTED
10 Testing (Sling and Maven)
In this chapter we are going to describe how unit tests can be conducted inside AEM .
First we will see how to use the JUnit framework, and then we will use Easymock and Powermock to perform various tests . Finally we will proceed to do some Slingbased tests using JUnit .
Junit
JUnit is a simple framework to write repeatable tests . It is an instance of the xUnit architecture for unit testing frameworks .
How do you write testing code?
The simplest way is as a test expression in a debugger . You can change debug expressions without recompiling, and you can wait to decide which expressions to define until you have seen the running objects . You can also write test expressions as statements that print to the standard output stream . Both styles of tests are limited because they require human judgment to analyze their results . Also, they don’t compose nicely- you can only execute one debug expression at a time and a program with too many print statements causes the dreaded “Scroll Blindness” .
JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time . When you need to test something, here is what you do:
1 . Annotate a method with @org .junit .Test
2 . When you want to check a value, import org .junit .Assert .* statically, call assertTrue() and pass a boolean that is true if the test succeeds (eventually you can use all the other methods provided by the Assert class like assertEquals, assertArrayEquals, assertFalse, assertNonNull, assertSame, etc . .)
ADOBE COPYRIGHT PROTECTED
3 . Then because we are using Maven we just need to test it by using the following script “mvn test”
or “mvn clean test” to force Maven to recompile your project . (Eventually if you want to run only some tests amongst all the ones in your code you can do so by providing on the command line
“mvn –Dtest=MyTestsCase, MyOtherTestCase test” to run only the mentioned tests .
EasyMock
EasyMock provides MockObjects for interfaces (and objects through the class extension) by generating them on the fly using Java’s Dynamic Proxy mechanism . EasyMock is a library that provides an easy way to use Mock Objects for given interfaces or classes .
Mock Objects simulate parts of the behavior of domain code, and are able to check whether they are used as defined . Domain classes can be tested in isolation by simulating their collaborators with Mock Objects .
Writing and maintaining Mock Objects often is a tedious task that may introduce errors . EasyMock generates Mock Objects dynamically - no need to write them, and no generated code!
EasyMock Benefits
• Hand-writing classes for Mock Objects is not needed .
• Supports refactoring-safe Mock Objects: test code will not break at runtime when renaming methods or reordering method parameters
• Supports return values and exceptions .
• Supports checking the order of method calls, for one or more Mock Objects
Usually when writing a test using EasyMock the following steps are taken in order to Mock our Objects:
• Create a Mock Object for the interface we would like to simulate,
• Record the expected behavior, and
• Switch the Mock Object to replay state . Up to this state in our tests, all missed expectations are shown, as well as all fulfilled expectations for the unexpected call . If the method call is executed too often, the Mock Object complains, too
• Verify that the specified behavior has been used (so if none of the methods or
expectations before are called we need also to be notified about it) . To do this, we have to call verify(mock):
Here is a small example: most parts of a software system do not work in isolation, but collaborate with other parts to get their job done . In a lot of cases, we do not care about using collaborators
ADOBE COPYRIGHT PROTECTED
The following simple example use the interface Collaborator and a Class that we want to test: We want to test its behavior in the following class:
public class ClassUnderTest {
In order to Mock and verify it’s behaviour we could write the following Example Test class:
import static org.easymock.EasyMock.*;
//switch the mock Object to a replay state replay(mock);
classUnderTest.addDocument(“New Document”, new byte[0]);
//verify that the behavior of the Mock was as expected verify(mock);
This is a very simple example of how a test could be achieved using EasyMock, of course, there are many other methods and different annotations . If you never used EasyMock, and you are interested on it, you can find all the necessary information on their webpage: http://www .easymock .org/
PowerMock
PoweMock extends EasyMock with static mocking and setting expectations on constructors . PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more . By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption . Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors . PowerMock aims to extend the existing API’s with a small number of methods and annotations to enable the extra features .
The scope of this exercises is not to show how to do Unit tests using any of the frameworks described before, but how to use them inside AEM using maven and Sling, If you are interested on Unit testing you can take a look to the different homepages of the frameworks mentioned:
http://www .junit .org/
http://www .easymock .org/
http://code .google .com/p/powermock/