Package net.java.quickcheck

Source Code of net.java.quickcheck.AbstractSerializingRunnerDecorator$FileDiscovery

/*
*  Licensed to the author under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package net.java.quickcheck;

import static java.io.File.*;
import static java.lang.String.*;
import static java.lang.System.*;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

import net.java.quickcheck.util.Assert;

abstract class AbstractSerializingRunnerDecorator<T> extends RunnerDecorator<T> {

  public static final String PATH_PREFIX = getProperty("user.name") + separator + "quickcheck" + separator;

  public AbstractSerializingRunnerDecorator(Runner<T> decorated) {
    super(decorated);
  }

  public final void forAll() {
    String message = format("Started %s with decorated %s.", this.getClass().getCanonicalName(), getDecorated());
    getWriter().println(message);
    onForAll();
  }

  protected abstract void onForAll();

  <D> D discoverFiles(Characteristic<?> characteristic, FileDiscovery<D> discovery) {
    int i = 1;
    while(true) {
      String fileName = i++ + ".ser";
      File file = new File(getDirectoryForCharacteristic(characteristic), fileName);
      if(!discovery.goOn(file)) return discovery.getResult();
    }
  }

  String getDirectoryForCharacteristic(Characteristic<?> characteristic) {
    String tempDir = System.getProperty("java.io.tmpdir");
    return new File(tempDir, PATH_PREFIX + characteristicToPath(characteristic)).getAbsolutePath();
  }

  interface FileDiscovery<D> {
    boolean goOn(File file);
    D getResult();
  }

  String characteristicToPath(Characteristic<?> characteristic) {
    Assert.notNull(characteristic.name(), "characteristic.name");
    String name = characteristic.getClass().getName() + "." + characteristic.name();
    return name.replace(".", File.separator);
  }

  void closeQuietly(Closeable closable) {
    try {
      if(closable != null) closable.close();
    } catch(IOException ioe) {}
  }
}
TOP

Related Classes of net.java.quickcheck.AbstractSerializingRunnerDecorator$FileDiscovery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.