Java tutorial
package com.conwet.silbops; /* * #%L * SilboPS Service Test * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import static org.assertj.core.api.Assertions.fail; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; import java.util.List; import org.apache.catalina.LifecycleException; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; /** * * @author sergio */ public class TestEnvironment { private static final Logger appLogger = (Logger) LoggerFactory.getLogger("com.conwet"); private static final Logger rootLogger = (Logger) LoggerFactory.getLogger("root"); @Rule public TemporaryFolder tempDir = new TemporaryFolder(); private TomcatEmbedded tomcat; private ListAppender<ILoggingEvent> appAppender; private ListAppender<ILoggingEvent> rootAppender; protected int serverPort() { return 0; } @Before public void setUpEnvironment() { // intercept application and root loggers appLogger.setAdditive(false); appLogger.detachAndStopAllAppenders(); rootLogger.detachAndStopAllAppenders(); // SilboPS appender appAppender = new ListAppender<>(); appAppender.setContext(appLogger.getLoggerContext()); appAppender.start(); // root appender rootAppender = new ListAppender<>(); rootAppender.setContext(rootLogger.getLoggerContext()); rootAppender.start(); appLogger.addAppender(appAppender); rootLogger.addAppender(rootAppender); // Tomcat setup tomcat = new TomcatEmbedded(tempDir.getRoot().getAbsolutePath(), serverPort(), false); try { tomcat.boot(); tomcat.deploy("/silbops", findWar("silbops-service-?.?.?*.war")); } catch (LifecycleException ex) { throw new IllegalStateException(ex); } catch (FileNotFoundException e) { fail("Unable to deploy silbps.war", e); } } @After public void tearDownEnvironment() { try { tomcat.shutdown(); } catch (LifecycleException ex) { throw new IllegalStateException(ex); } // remove SilboPS appender. appLogger.detachAppender(appAppender); appLogger.setAdditive(true); appAppender.stop(); // remove root appender rootLogger.detachAppender(rootAppender); rootAppender.stop(); } public File findWar(String filename) throws FileNotFoundException { FileFilter warFile = new WildcardFileFilter(filename); File dir = new File("target/lib/"); File[] files = dir.listFiles(warFile); if (files.length == 0) { throw new FileNotFoundException("Unable to find file " + filename + " in " + dir.getAbsolutePath()); } return files[0]; } public List<ILoggingEvent> getAppAppender() { return appAppender.list; } public List<ILoggingEvent> getRootAppender() { return rootAppender.list; } public int getServerPort() { return tomcat.getTomcatPort(); } public TomcatEmbedded getTomcat() { return tomcat; } }