org.cakeframework.test.container.tck.RunState.java Source code

Java tutorial

Introduction

Here is the source code for org.cakeframework.test.container.tck.RunState.java

Source

/*
 * Copyright (c) 2008 Kasper Nielsen.
 *
 * Licensed 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 org.cakeframework.test.container.tck;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.cakeframework.container.Container;
import org.cakeframework.container.Container.State;

import com.google.common.collect.Collections2;

/**
 * 
 * @author Kasper Nielsen
 */
public enum RunState {

    /**
     * Represents the point after all services has been properly injected and the state of the container has been
     * initialized to {@link State#INITIALIZED}.
     * <p>
     * Since handlers using this invocation point is processed from within the containers constructor. They should not
     * attempt to start new threads or perform other exotic things.
     * <p>
     * A container will never try to handle any exception a processing handler might throw. Instead the exception will
     * propagated out from the containers constructor.
     */
    INJECT,

    /**
     * Represents the point after all services has been properly injected and the state of the container has been
     * initialized to {@link State#INITIALIZED}.
     * <p>
     * Since handlers using this invocation point is processed from within the containers constructor. They should not
     * attempt to start new threads or perform other exotic things.
     * <p>
     * A container will never try to handle any exception a processing handler might throw. Instead the exception will
     * propagated out from the containers constructor.
     */
    INITIALIZED,

    /**  */
    STARTING, // Start,

    /**  */
    RUNNING, // RUN,

    /**
     * This state indicates the point after {@link Container#shutdown()} has been invoked and the state of the container
     * has transitioned to {@link Container.State#SHUTDOWN}.
     */
    SHUTDOWN; // STOP;

    public static Set<RunState> setOf(RunState... states) {
        if (states.length == 0) {
            states = RunState.values();
        }
        return new HashSet<>(Arrays.asList(states));
    }

    public boolean isAnyOf(RunState... states) {
        for (RunState s : states) {
            if (s == this) {
                return true;
            }
        }
        return false;
    }

    public RunState next() {
        if (this == SHUTDOWN) {
            return null;
        }
        return RunState.values()[ordinal() + 1];
    }

    /**
     * Returns a parameterized combination of all run states.
     * 
     * @return
     */
    public static Collection<Object[]> getAllStateCombinations() {
        // Test all combinations
        Collection<List<RunState>> subSets = Collections2.orderedPermutations(
                Arrays.asList(RunState.INITIALIZED, RunState.STARTING, RunState.RUNNING, RunState.SHUTDOWN));

        ArrayList<Object[]> l = new ArrayList<>();
        HashSet<Collection<RunState>> lhs = new HashSet<>();
        for (int i = 1; i <= 4; i++) {
            for (List<RunState> li : subSets) {
                Collection<RunState> c = new TreeSet<>(li.subList(0, i));
                if (lhs.add(c)) {
                    l.add(new Object[] { c });
                }
            }
        }
        assertEquals(15, l.size());
        return l;
    }
}