/** * Copyright (c) 2011 Cloudsmith Inc. and other contributors, as listed below. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Cloudsmith * */ package org.cloudsmith.geppetto.forge.tests; import static com.google.inject.name.Names.named; import static org.cloudsmith.geppetto.injectable.CommonModuleProvider.getCommonModule; import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; import org.cloudsmith.geppetto.common.util.BundleAccess; import org.cloudsmith.geppetto.forge.Cache; import org.cloudsmith.geppetto.forge.Forge; import org.cloudsmith.geppetto.forge.ForgeService; import org.cloudsmith.geppetto.forge.client.ForgeHttpModule; import org.cloudsmith.geppetto.forge.client.GsonModule; import org.cloudsmith.geppetto.forge.impl.ForgeModule; import org.cloudsmith.geppetto.forge.impl.ForgeServiceModule; import com.google.gson.Gson; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; public class AbstractForgeTest { private static String TEST_FORGE_URI = "http://forge-staging-api.puppetlabs.com/"; // private static String TEST_FORGE_URI = "http://localhost:4567/"; private static Injector injector; private static Injector commonInjector = Guice.createInjector(getCommonModule()); private static File basedir; public static void delete(File fileOrDir) throws IOException { File[] children = fileOrDir.listFiles(); if(children != null) for(File child : children) delete(child); if(!fileOrDir.delete() && fileOrDir.exists()) throw new IOException("Unable to delete " + fileOrDir); } private static File getBasedir() { if(basedir == null) { String basedirProp = System.getProperty("basedir"); if(basedirProp == null) { try { File testData = getBundleAccess().getFileFromClassBundle(AbstractForgeTest.class, "testData"); if(testData == null || !testData.isDirectory()) fail("Unable to determine basedir"); basedir = testData.getParentFile(); } catch(IOException e) { fail(e.getMessage()); } } else basedir = new File(basedirProp); } return basedir; } public static BundleAccess getBundleAccess() { return commonInjector.getInstance(BundleAccess.class); } public static Cache getCache() { return getInjector().getInstance(Cache.class); } public static ForgeService getForge() { return getInjector().getInstance(ForgeService.class); } public static Forge getForgeUtil() { return getInjector().getInstance(Forge.class); } public static Gson getGson() { return getInjector().getInstance(Gson.class); } private synchronized static Injector getInjector() { if(injector == null) { Module testBindings = new AbstractModule() { @Override protected void configure() { try { bind(File.class).annotatedWith(named(Forge.CACHE_LOCATION)).toInstance( getTestOutputFolder("cachefolder", true)); } catch(IOException e) { fail(e.getMessage()); } } }; try { injector = commonInjector.createChildInjector(GsonModule.INSTANCE, new ForgeHttpModule() { @Override protected String getBaseURL() { return TEST_FORGE_URI; } }, new ForgeServiceModule(), new ForgeModule(), testBindings); } catch(Exception e) { e.printStackTrace(); fail(e.getMessage()); } } return injector; } public static File getTestData(String path) throws IOException { return new File(new File(getBasedir(), "testData"), path); } public static File getTestOutputFolder(String name, boolean purge) throws IOException { File testFolder = new File(new File(new File(getBasedir(), "target"), "testOutput"), name); testFolder.mkdirs(); if(purge) { // Ensure that the folder is empty for(File file : testFolder.listFiles()) delete(file); } return testFolder; } }