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.hamcrest.MatcherAssert;
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 implements RunnerFeature {
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<>();
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            MatcherAssert.assertThat(tracked, CoreMatchers.is(created)); // replace
072                                                                  // with
073                                                                  // contains
074            for (File each : created) {
075                MatcherAssert.assertThat("File should have been deleted: " + each,
076                        each.exists(), CoreMatchers.is(false));
077            }
078        } finally {
079            tracked.clear();
080            created.clear();
081        }
082    }
083
084    public ThreadEvent onThreadEnter(boolean isLongRunning) {
085        return ThreadEvent.onEnter(this, isLongRunning);
086    }
087
088    public FileEvent onFile(File aFile, Object aMarker) {
089        return FileEvent.onFile(this, resolveAndCreate(aFile), aMarker);
090    }
091
092    public File resolveAndCreate(File aFile) {
093        File temp = Environment.getDefault().getTemp();
094        File actual = temp.toPath().resolve(aFile.toPath()).toFile();
095        try {
096            actual.createNewFile();
097        } catch (IOException e) {
098            throw new RuntimeException("Cannot create temp file " + actual);
099        }
100        created.add(actual);
101        return actual;
102    }
103}