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