001/*
002 * Licensed to the author under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package de.cuioss.test.generator.internal.net.java.quickcheck.characteristic;
018
019import de.cuioss.test.generator.internal.net.java.quickcheck.Characteristic;
020
021/**
022 * {@link AbstractCharacteristic} is an implementation of {@link Characteristic}
023 * with {@link Classification} handling. The methods
024 * {@link AbstractCharacteristic#classify(Object)} and
025 * {@link AbstractCharacteristic#classify(boolean, Object)} can be used to
026 * classify test data.
027 *
028 * @param <T> Type of generated random test instances.
029 */
030@SuppressWarnings("ProhibitedExceptionDeclared")
031public abstract class AbstractCharacteristic<T> implements Characteristic<T> {
032
033    private final Classification classification = new Classification();
034    private final String name;
035
036    public AbstractCharacteristic() {
037        this(null);
038    }
039
040    public AbstractCharacteristic(String name) {
041        this.name = name;
042    }
043
044    /**
045     * {@inheritDoc}
046     *
047     * This method will call {@link AbstractCharacteristic#doSpecify(Object)}.
048     */
049    @Override
050    public void specify(T any) throws Throwable {
051        doSpecify(any);
052        this.classification.call();
053    }
054
055    /**
056     * Add a classification with the given key if the predicate is true.
057     *
058     * @param predicate      Predicate for the classification.
059     * @param classification classification key.
060     */
061    protected void classify(boolean predicate, Object classification) {
062        this.classification.doClassify(predicate, classification);
063    }
064
065    /**
066     * Add a classification with the given key.
067     *
068     * @param classification classification key.
069     */
070    protected void classify(Object classification) {
071        classify(true, classification);
072    }
073
074    /**
075     * Implement this method to specify the characteristic
076     * ({@link Characteristic#specify(Object)}).
077     */
078    protected abstract void doSpecify(T any) throws Throwable;
079
080    /**
081     * {@link Classification} data about the test cases executed.
082     */
083    public Classification getClassification() {
084        return classification;
085    }
086
087    @Override
088    public void setUp() throws Exception {
089    }
090
091    @Override
092    public void tearDown() throws Exception {
093    }
094
095    @Override
096    public String name() {
097        return name;
098    }
099}