001/* 002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.test; 020 021import java.io.IOException; 022 023import org.nuxeo.ecm.automation.client.Session; 024import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient; 025import org.nuxeo.ecm.core.test.DetectThreadDeadlocksFeature; 026import org.nuxeo.ecm.webengine.test.WebEngineFeature; 027import org.nuxeo.runtime.test.runner.Features; 028import org.nuxeo.runtime.test.runner.FeaturesRunner; 029import org.nuxeo.runtime.test.runner.SimpleFeature; 030 031import com.google.inject.Binder; 032import com.google.inject.Scopes; 033 034/** 035 * Shortcut to deploy bundles required by automation in your test 036 * 037 * @since 5.7 038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 039 */ 040@Features({ DetectThreadDeadlocksFeature.class, WebEngineFeature.class, AutomationServerFeature.class }) 041@DetectThreadDeadlocksFeature.Config(dumpAtTearDown = true) 042public class EmbeddedAutomationServerFeature extends SimpleFeature { 043 044 protected static final int HTTP_CONNECTION_TIMEOUT = 60000; // 60 seconds 045 046 protected HttpAutomationClient client; 047 048 protected Session session; 049 050 @Override 051 public void afterRun(FeaturesRunner runner) throws Exception { 052 if (client != null) { 053 client.shutdown(); 054 client = null; 055 session = null; 056 } 057 super.afterRun(runner); 058 } 059 060 @Override 061 public void configure(FeaturesRunner runner, Binder binder) { 062 super.configure(runner, binder); 063 binder.bind(HttpAutomationClient.class).toProvider(() -> { 064 if (client == null) { 065 client = getHttpAutomationClient(); 066 } 067 return client; 068 }).in(Scopes.SINGLETON); 069 binder.bind(Session.class).toProvider(() -> { 070 if (client == null) { 071 client = getHttpAutomationClient(); 072 } 073 if (session == null) { 074 try { 075 session = client.getSession("Administrator", "Administrator"); 076 } catch (IOException e) { 077 throw new RuntimeException(e); 078 } 079 } 080 return session; 081 }).in(Scopes.SINGLETON); 082 } 083 084 protected HttpAutomationClient getHttpAutomationClient() { 085 HttpAutomationClient client = new HttpAutomationClient("http://localhost:18080/automation", 086 HTTP_CONNECTION_TIMEOUT); 087 // Deactivate global operation registry cache to allow tests using this 088 // feature in a test suite to deploy different set of operations 089 client.setSharedRegistryExpirationDelay(0); 090 return client; 091 } 092 093}