/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.wink.server.serviceability; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.Formatter; import java.util.List; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import org.apache.wink.logging.InMemoryHandler; import org.apache.wink.server.internal.servlet.MockServletInvocationTest; public class DebugOneProviderOnlyAppStartupTest extends MockServletInvocationTest { @Override protected Class<?>[] getClasses() { return new Class<?>[] {Provider1.class}; } @Provider public static class Provider1 implements MessageBodyWriter<Object> { public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; } public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return false; } public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { } } private InMemoryHandler handler; private Logger winkLogger = Logger.getLogger("org.apache.wink"); private Handler consoleHandler; @Override protected void setUp() throws Exception { Handler[] defaultHandlers = Logger.getLogger("").getHandlers(); if (defaultHandlers.length == 1) { consoleHandler = defaultHandlers[0]; consoleHandler.setLevel(Level.FINE); } handler = new InMemoryHandler(); handler.setLevel(Level.FINE); winkLogger.setLevel(Level.FINE); winkLogger.addHandler(handler); super.setUp(); } @Override protected void tearDown() throws Exception { if (consoleHandler != null) { consoleHandler.setLevel(Level.INFO); } winkLogger.removeHandler(handler); winkLogger.setLevel(Level.INFO); super.tearDown(); } /** * Tests that a Provider is logged at the debug level. */ public void testLogOneProvider() throws Exception { List<LogRecord> records = handler.getRecords(); assertEquals(Level.INFO, records.get(11).getLevel()); assertEquals("The class org.apache.wink.server.serviceability.DebugOneProviderOnlyAppStartupTest$Provider1 was registered as a JAX-RS MessageBodyWriter provider for all Java types and */* media types.", records.get(11).getMessage()); assertEquals(Level.FINE, records.get(12).getLevel()); StringBuffer sb = new StringBuffer(); Formatter f = new Formatter(sb); f.format("The following JAX-RS MessageBodyWriter providers are registered:%n"); f .format("Produces Media Type Generic Type Custom? Provider Class%n"); f .format("*/* Object true org.apache.wink.server.serviceability.DebugOneProviderOnlyAppStartupTest$Provider1"); assertEquals(sb.toString(), records.get(12).getMessage()); assertEquals(13, records.size()); } }