001/*
002 * (C) Copyright 2006-2017 Nuxeo (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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.runtime.test;
022
023import java.io.IOException;
024import java.net.URL;
025
026import org.apache.commons.io.FileUtils;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.AbstractRuntimeService;
030import org.nuxeo.runtime.Version;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.model.impl.DefaultRuntimeContext;
033
034/**
035 * A runtime service used for JUnit tests.
036 * <p>
037 * The Test Runtime has only one virtual bundle
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class TestRuntime extends AbstractRuntimeService {
042    private static final Log log = LogFactory.getLog(TestRuntime.class);
043
044    public static final String NAME = "Test Runtime";
045
046    public static final Version VERSION = Version.parseString("1.0.0");
047
048    private static int counter = 0;
049
050    public TestRuntime() {
051        super(new DefaultRuntimeContext());
052        try {
053            workingDir = Framework.createTempFile("NXTestFramework", generateId());
054            workingDir.delete();
055        } catch (IOException e) {
056            log.error(e, e);
057        }
058    }
059
060    @Override
061    public String getName() {
062        return NAME;
063    }
064
065    @Override
066    public Version getVersion() {
067        return VERSION;
068    }
069
070    private static synchronized String generateId() {
071        long stamp = System.currentTimeMillis();
072        counter++;
073        return Long.toHexString(stamp) + '-' + System.identityHashCode(System.class) + '.' + counter;
074    }
075
076    public void deploy(URL url) throws Exception {
077        context.deploy(url);
078    }
079
080    public void undeploy(URL url) throws Exception {
081        context.undeploy(url);
082    }
083
084    @Override
085    public void stop() {
086        try {
087            super.stop();
088        } finally {
089            if (workingDir != null) {
090                FileUtils.deleteQuietly(workingDir);
091            }
092        }
093    }
094
095    @Override
096    public void reloadProperties() {
097        throw new UnsupportedOperationException("Not yet implemented");
098    }
099}