001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.ecm.automation.test; 018 019import java.io.IOException; 020 021import org.nuxeo.ecm.automation.client.Session; 022import org.nuxeo.ecm.automation.client.jaxrs.impl.HttpAutomationClient; 023import org.nuxeo.ecm.core.test.DetectThreadDeadlocksFeature; 024import org.nuxeo.ecm.webengine.test.WebEngineFeature; 025import org.nuxeo.runtime.test.runner.Deploy; 026import org.nuxeo.runtime.test.runner.Features; 027import org.nuxeo.runtime.test.runner.FeaturesRunner; 028import org.nuxeo.runtime.test.runner.SimpleFeature; 029 030import com.google.inject.Binder; 031import com.google.inject.Provider; 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@Deploy({ "org.nuxeo.ecm.automation.core", "org.nuxeo.ecm.automation.io", "org.nuxeo.ecm.automation.server", 041 "org.nuxeo.ecm.automation.features", "org.nuxeo.ecm.platform.query.api" }) 042@Features({ DetectThreadDeadlocksFeature.class, WebEngineFeature.class }) 043@DetectThreadDeadlocksFeature.Config(dumpAtTearDown = true) 044public class EmbeddedAutomationServerFeature extends SimpleFeature { 045 046 protected static final int HTTP_CONNECTION_TIMEOUT = 60000; // 60 seconds 047 048 protected HttpAutomationClient client; 049 050 protected Session session; 051 052 @Override 053 public void afterRun(FeaturesRunner runner) throws Exception { 054 if (client != null) { 055 client.shutdown(); 056 client = null; 057 session = null; 058 } 059 super.afterRun(runner); 060 } 061 062 @Override 063 public void configure(FeaturesRunner runner, Binder binder) { 064 super.configure(runner, binder); 065 binder.bind(HttpAutomationClient.class).toProvider(new Provider<HttpAutomationClient>() { 066 @Override 067 public HttpAutomationClient get() { 068 if (client == null) { 069 client = getHttpAutomationClient(); 070 } 071 return client; 072 } 073 }).in(Scopes.SINGLETON); 074 binder.bind(Session.class).toProvider(new Provider<Session>() { 075 @Override 076 public Session get() { 077 if (client == null) { 078 client = getHttpAutomationClient(); 079 } 080 if (session == null) { 081 try { 082 session = client.getSession("Administrator", "Administrator"); 083 } catch (IOException e) { 084 throw new RuntimeException(e); 085 } 086 } 087 return session; 088 } 089 }).in(Scopes.SINGLETON); 090 } 091 092 protected HttpAutomationClient getHttpAutomationClient() { 093 HttpAutomationClient client = new HttpAutomationClient("http://localhost:18080/automation", 094 HTTP_CONNECTION_TIMEOUT); 095 // Deactivate global operation registry cache to allow tests using this 096 // feature in a test suite to deploy different set of operations 097 client.setSharedRegistryExpirationDelay(0); 098 return client; 099 } 100 101}