| /* |
| * 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.sling.uca.impl; |
| |
| import java.io.IOException; |
| import java.lang.annotation.ElementType; |
| import java.lang.annotation.Retention; |
| import java.lang.annotation.RetentionPolicy; |
| import java.lang.annotation.Target; |
| import java.util.concurrent.TimeUnit; |
| |
| import javax.servlet.ServletException; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| |
| import org.eclipse.jetty.server.Connector; |
| import org.eclipse.jetty.server.Request; |
| import org.eclipse.jetty.server.Server; |
| import org.eclipse.jetty.server.ServerConnector; |
| import org.eclipse.jetty.server.handler.AbstractHandler; |
| import org.junit.jupiter.api.extension.AfterAllCallback; |
| import org.junit.jupiter.api.extension.BeforeAllCallback; |
| import org.junit.jupiter.api.extension.ExtensionContext; |
| import org.junit.jupiter.api.extension.ParameterContext; |
| import org.junit.jupiter.api.extension.ParameterResolutionException; |
| import org.junit.jupiter.api.extension.ParameterResolver; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolver, ServerControl { |
| |
| @Retention(RetentionPolicy.RUNTIME) |
| @Target(ElementType.PARAMETER) |
| public @interface MisbehavingServer { } |
| |
| private final Logger logger = LoggerFactory.getLogger(getClass()); |
| |
| public int getLocalPort() { |
| return ((ServerConnector) server.getConnectors()[0]).getLocalPort(); |
| } |
| |
| private Server server; |
| |
| @Override |
| public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| throws ParameterResolutionException { |
| return parameterContext.isAnnotated(MisbehavingServer.class); |
| } |
| |
| @Override |
| public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| throws ParameterResolutionException { |
| if ( parameterContext.getParameter().getType() == ServerControl.class ) |
| return this; |
| |
| throw new ParameterResolutionException("Unable to get a Server instance for " + parameterContext); |
| } |
| |
| @Override |
| public void beforeAll(ExtensionContext context) throws Exception { |
| |
| server = new Server(); |
| ServerConnector connector = new ServerConnector(server) { |
| @Override |
| public void accept(int acceptorID) throws IOException { |
| LOG.info("Waiting before accepting"); |
| try { |
| Thread.sleep(TimeUnit.SECONDS.toMillis(10)); |
| } catch (InterruptedException e) { |
| Thread.currentThread().interrupt(); |
| return; |
| } |
| super.accept(acceptorID); |
| LOG.info("Accepted"); |
| } |
| }; |
| server.setConnectors(new Connector[] { connector }); |
| server.setHandler(new AbstractHandler() { |
| |
| @Override |
| public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) |
| throws IOException, ServletException { |
| response.setStatus(HttpServletResponse.SC_NO_CONTENT); |
| baseRequest.setHandled(true); |
| } |
| }); |
| |
| server.start(); |
| } |
| |
| @Override |
| public void afterAll(ExtensionContext context) throws Exception { |
| if ( server == null ) |
| return; |
| try { |
| server.stop(); |
| } catch (Exception e) { |
| logger.info("Failed shutting down server", e); |
| } |
| } |
| } |