// make sure all the lists were read and processed by tracking the number of hits to the ctors
Provider1 p1 = new Provider1();
assertEquals(5, p1.getNumCtorHits());
Provider2 p2 = new Provider2();
assertEquals(3, p2.getNumCtorHits());
Provider3 p3 = new Provider3();
assertEquals(2, p3.getNumCtorHits());
Provider4 p4 = new Provider4();
assertEquals(2, p4.getNumCtorHits());
// to actually inspect the list in the 'data' object in the MessageBodyReaders in the ProvidersRegistry would take a lot of
// reflection and hacking. We'll at least confirm that only 5 (due to AssetProvider) are in the ProvidersRegistry.
RestServlet servlet = (RestServlet)this.getServlet();
ServletContext context = servlet.getServletContext();
RequestProcessor processor = (RequestProcessor) context.getAttribute(RequestProcessor.class.getName());
DeploymentConfiguration config = processor.getConfiguration();
ProvidersRegistry providersRegistry = config.getProvidersRegistry();
// to confirm that the ignores are indeed happening, I need to get the private field
// "messageBodyReaders" object, then it's superclass "data" object and inspect it:
Field field = providersRegistry.getClass().getDeclaredField("messageBodyReaders");
field.setAccessible(true);
Object messageBodyReaders = field.get(providersRegistry);
Field field2 = messageBodyReaders.getClass().getSuperclass().getDeclaredField("data");
field2.setAccessible(true);
HashMap data = (HashMap)field2.get(messageBodyReaders);
HashSet readers = (HashSet)data.get(MediaType.WILDCARD_TYPE);
assertEquals(6, readers.size());
// under the covers, the "list" is a treeset, so the iterator does not really tell us any info about the sort
Set<String> expectedSet = new HashSet<String>(6);
expectedSet.add("Priority: 0.500000, ObjectFactory: SingletonOF: " + org.apache.wink.server.internal.registry.providers.Provider1.class.getName());
expectedSet.add("Priority: 0.100000, ObjectFactory: ClassMetadataPrototypeOF Class: " + org.apache.wink.common.internal.providers.entity.AssetProvider.class.getName());
expectedSet.add("Priority: 0.100000, ObjectFactory: SingletonOF: " + org.apache.wink.server.internal.registry.providers.Provider3.class.getName());
expectedSet.add("Priority: 0.500000, ObjectFactory: SingletonOF: " + org.apache.wink.server.internal.registry.providers.Provider2.class.getName());
expectedSet.add("Priority: 0.100000, ObjectFactory: SingletonOF: " + org.apache.wink.server.internal.registry.providers.Provider4.class.getName());
expectedSet.add("Priority: 0.100000, ObjectFactory: SingletonOF: " + StringProvider.class.getName());
// this is obviously not the best way to check this. If toString() output is changed, or the TreeSet implementation is modified,
// this test code will also have to be modified. Also, can't check order of the readers HashSet. But we're compatible with other
// locales now.
int count = 0;
for (Iterator it = readers.iterator(); it.hasNext();) {
Object obj = it.next();
assertTrue(obj.toString(), expectedSet.contains(obj.toString()));
count++;
}
// do a real transaction too to confirm that the listing in getClasses took the top spot
MockHttpServletRequest request =
MockRequestConstructor.constructMockRequest("POST",
"/test/priority",
MediaType.TEXT_PLAIN,
MediaType.TEXT_PLAIN,
"".getBytes());
MockHttpServletResponse response = invoke(request);
assertEquals(200, response.getStatus());
// make sure the first instance processed has top priority, and that it is indeed the first instance of Provider2 (because it was given
// higher priority due to getClasses being processed after getSingletons)
assertEquals(Provider2.class.getName() + "_" + p2.getFirstInstanceHashCode(), response.getContentAsString());
}