You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Zsolt Grigore <gr...@gmail.com> on 2015/05/11 17:29:19 UTC

Sling Mocks Register Anonymus Adapter Factory

Dear All,

Sorry if this has been already asked, I haven't found any clue how to solve
that.
Considering the following JUnit test class:

public class TestComponentTest {
private ProvidedClass mockProvided = mock(ProvidedClass.class);

@Rule
public SlingContext context = new SlingContext();
 @Before
public void setUp() {
context.addModelsForPackage("com.asd.models");
 context.registerService(new AdapterFactory() {
@Override
public <AdapterType> AdapterType getAdapter(Object adapter,
Class<AdapterType> type) {
if (adapter instanceof ResourceResolver &&
type.equals(ProvidedClass.class)) {
return (AdapterType)mockProvided;
}
return null;
}
});
}
 @Test
public void testAMethod() throws Exception {
when(mockProvided.someMethod("foo")).thenReturn("bar");
/*
...
*/
assertEquals(instance.aMethod(), "bar");
}
}

Having the adapterfactory implemented as anonymus, is there any possibility
to register and use that inside SlingContext? Eg by getting a reference to
the Adapter Manager somehow to call it's registerFactory?

I've tried multiple ways[1][2]  but inside the "instance" the current
resource never adapts to the desired ProvidedClass possibly because of the
lack of SCR metadata. The original AdapterFactory is provided by the
framework I use so I can't register it the way the docs suggests.

Thanks
Zsolt

[1]
http://sling.apache.org/documentation/development/sling-mock.html#adapter-factories
[2] http://sling.apache.org/documentation/development/osgi-mock.html#usage

RE: Sling Mocks Register Anonymus Adapter Factory

Posted by Stefan Seifert <ss...@pro-vision.de>.
hello zsolt.

it should be possible with a code snippet like this:

context.registerService(AdapterFactory.class, anonymousFactoryInstance, ImmutableMap.<String, Object>builder()
    .put(AdapterFactory.ADAPTABLE_CLASSES, new String[] {
      SlingHttpServletRequest.class.getName()
    })
    .put(AdapterFactory.ADAPTER_CLASSES, new String[] {
      XSSAPI.class.getName()
    })
   .build());

ImmutableMap is a shortcut from guava, of course you can use a simple hash map as well.
with this you pass in the osgi metadata that is normally read from the SCR annotations.

stefan

>-----Original Message-----
>From: Zsolt Grigore [mailto:grigo.billboard@gmail.com]
>Sent: Monday, May 11, 2015 5:29 PM
>To: users@sling.apache.org
>Subject: Sling Mocks Register Anonymus Adapter Factory
>
>Dear All,
>
>Sorry if this has been already asked, I haven't found any clue how to solve
>that.
>Considering the following JUnit test class:
>
>public class TestComponentTest {
>private ProvidedClass mockProvided = mock(ProvidedClass.class);
>
>@Rule
>public SlingContext context = new SlingContext();
> @Before
>public void setUp() {
>context.addModelsForPackage("com.asd.models");
> context.registerService(new AdapterFactory() {
>@Override
>public <AdapterType> AdapterType getAdapter(Object adapter,
>Class<AdapterType> type) {
>if (adapter instanceof ResourceResolver &&
>type.equals(ProvidedClass.class)) {
>return (AdapterType)mockProvided;
>}
>return null;
>}
>});
>}
> @Test
>public void testAMethod() throws Exception {
>when(mockProvided.someMethod("foo")).thenReturn("bar");
>/*
>...
>*/
>assertEquals(instance.aMethod(), "bar");
>}
>}
>
>Having the adapterfactory implemented as anonymus, is there any possibility
>to register and use that inside SlingContext? Eg by getting a reference to
>the Adapter Manager somehow to call it's registerFactory?
>
>I've tried multiple ways[1][2]  but inside the "instance" the current
>resource never adapts to the desired ProvidedClass possibly because of the
>lack of SCR metadata. The original AdapterFactory is provided by the
>framework I use so I can't register it the way the docs suggests.
>
>Thanks
>Zsolt
>
>[1]
>http://sling.apache.org/documentation/development/sling-mock.html#adapter-
>factories
>[2] http://sling.apache.org/documentation/development/osgi-mock.html#usage