001/*
002 * (C) Copyright 2014-2015 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 *     Stephane Lacoin
018 */
019package org.nuxeo.runtime.test.runner;
020
021import java.io.File;
022import java.io.IOException;
023import java.nio.file.Path;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.hamcrest.CoreMatchers;
028import org.junit.Assert;
029import org.junit.runners.model.FrameworkMethod;
030import org.nuxeo.common.Environment;
031import org.nuxeo.runtime.trackers.concurrent.ThreadEvent;
032import org.nuxeo.runtime.trackers.files.FileEvent;
033import org.nuxeo.runtime.trackers.files.FileEventHandler;
034import org.nuxeo.runtime.trackers.files.FileEventListener;
035
036@Features(RuntimeFeature.class)
037public class FileEventsTrackingFeature extends SimpleFeature {
038
039    protected class Tracker implements FileEventHandler {
040
041        @Override
042        public void onFile(File file, Object marker) {
043            tracked.add(file);
044        }
045
046    }
047
048    protected final Set<File> tracked = new HashSet<>();
049
050    protected Tracker tracker = new Tracker();
051
052    protected FileEventListener listener = new FileEventListener(tracker);
053
054    protected Path tempPath;
055
056    protected Set<File> created = new HashSet<File>();
057
058    @Override
059    public void beforeMethodRun(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
060        File temp = Environment.getDefault().getTemp();
061        tempPath = temp.toPath();
062        tracked.clear();
063        created.clear();
064        listener.install();
065    }
066
067    @Override
068    public void afterMethodRun(FeaturesRunner runner, FrameworkMethod method, Object test) throws Exception {
069        listener.uninstall();
070        try {
071            Assert.assertThat(tracked, CoreMatchers.is(created)); // replace
072                                                                  // with
073                                                                  // contains
074            for (File each : created) {
075                Assert.assertThat(each.exists(), CoreMatchers.is(false));
076            }
077        } finally {
078            tracked.clear();
079            created.clear();
080        }
081    }
082
083    public ThreadEvent onThreadEnter(boolean isLongRunning) {
084        return ThreadEvent.onEnter(this, isLongRunning);
085    }
086
087    public FileEvent onFile(File aFile, Object aMarker) {
088        return FileEvent.onFile(this, resolveAndCreate(aFile), aMarker);
089    }
090
091    public File resolveAndCreate(File aFile) {
092        File temp = Environment.getDefault().getTemp();
093        File actual = temp.toPath().resolve(aFile.toPath()).toFile();
094        try {
095            actual.createNewFile();
096        } catch (IOException e) {
097            throw new RuntimeException("Cannot create temp file " + actual);
098        }
099        created.add(actual);
100        return actual;
101    }
102}