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.Deploy; 028import org.nuxeo.runtime.test.runner.Features; 029import org.nuxeo.runtime.test.runner.FeaturesRunner; 030import org.nuxeo.runtime.test.runner.SimpleFeature; 031 032import com.google.inject.Binder; 033import com.google.inject.Provider; 034import com.google.inject.Scopes; 035 036/** 037 * Shortcut to deploy bundles required by automation in your test 038 * 039 * @since 5.7 040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 041 */ 042@Deploy({ "org.nuxeo.ecm.automation.core", "org.nuxeo.ecm.automation.io", "org.nuxeo.ecm.automation.server", 043 "org.nuxeo.ecm.automation.features", "org.nuxeo.ecm.platform.query.api" }) 044@Features({ DetectThreadDeadlocksFeature.class, WebEngineFeature.class }) 045@DetectThreadDeadlocksFeature.Config(dumpAtTearDown = true) 046public class EmbeddedAutomationServerFeature extends SimpleFeature { 047 048 protected static final int HTTP_CONNECTION_TIMEOUT = 60000; // 60 seconds 049 050 protected HttpAutomationClient client; 051 052 protected Session session; 053 054 @Override 055 public void afterRun(FeaturesRunner runner) throws Exception { 056 if (client != null) { 057 client.shutdown(); 058 client = null; 059 session = null; 060 } 061 super.afterRun(runner); 062 } 063 064 @Override 065 public void configure(FeaturesRunner runner, Binder binder) { 066 super.configure(runner, binder); 067 binder.bind(HttpAutomationClient.class).toProvider(new Provider<HttpAutomationClient>() { 068 @Override 069 public HttpAutomationClient get() { 070 if (client == null) { 071 client = getHttpAutomationClient(); 072 } 073 return client; 074 } 075 }).in(Scopes.SINGLETON); 076 binder.bind(Session.class).toProvider(new Provider<Session>() { 077 @Override 078 public Session get() { 079 if (client == null) { 080 client = getHttpAutomationClient(); 081 } 082 if (session == null) { 083 try { 084 session = client.getSession("Administrator", "Administrator"); 085 } catch (IOException e) { 086 throw new RuntimeException(e); 087 } 088 } 089 return session; 090 } 091 }).in(Scopes.SINGLETON); 092 } 093 094 protected HttpAutomationClient getHttpAutomationClient() { 095 HttpAutomationClient client = new HttpAutomationClient("http://localhost:18080/automation", 096 HTTP_CONNECTION_TIMEOUT); 097 // Deactivate global operation registry cache to allow tests using this 098 // feature in a test suite to deploy different set of operations 099 client.setSharedRegistryExpirationDelay(0); 100 return client; 101 } 102 103}