001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007-2008 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.antidote;
032    
033    import java.util.List;
034    
035    import static org.junit.Assert.assertEquals;
036    import static org.junit.Assert.fail;
037    
038    /**
039     * A static helper utility for dealing with {@link RequiresValidation} objects in unit tests.
040     */
041    public class ValidationTestHelper
042    {
043        /**
044         * Assert that the given {@link RequiresValidation} object has no violations.
045         */
046        public static void assertNoViolations (RequiresValidation validation)
047        {
048            final List<Violation> violations = validation.validate();
049            assertEquals("Unexpected number of violations,", 0, violations.size());
050        }
051    
052        /**
053         * Assert that the given {@link RequiresValidation} object has one violation and is an instance
054         * of the supplied {@link Violation} class.
055         */
056        public static void assertOneViolation (RequiresValidation validation, Class<? extends Violation> clazz)
057        {
058            final List<Violation> violations = validation.validate();
059            assertViolationCount(violations, 1);
060    
061            assertViolationClass(violations.get(0), clazz);
062        }
063    
064        /**
065         * Assert that the given {@link RequiresValidation} object has two violations and are instances
066         * of the supplied {@link Violation} class.
067         */
068        public static void assertTwoViolations (RequiresValidation validation, Class<? extends Violation> clazz)
069        {
070            assertTwoViolations(validation, clazz, clazz);
071        }
072    
073        /**
074         * Assert that the given {@link RequiresValidation} object has two violations and are instances
075         * of the supplied {@link Violation} classes.
076         */
077        public static void assertTwoViolations (RequiresValidation validation,
078            Class<? extends Violation> one, Class<? extends Violation> two)
079        {
080            final List<Violation> violations = validation.validate();
081            assertViolationCount(violations, 2);
082    
083            assertViolationClass(violations.get(0), one);
084            assertViolationClass(violations.get(1), two);
085        }
086    
087        /**
088         * Validate the supplied violation is a subclass of the supplied class.
089         */
090        private static void assertViolationCount (List<Violation> violations, int count)
091        {
092            assertEquals("Unexpected number of Violations,", count, violations.size());
093        }
094    
095        /**
096         * Validate the supplied violation is a subclass of the supplied class.
097         */
098        private static void assertViolationClass (Violation violation, Class<? extends Violation> clazz)
099        {
100            if (!(clazz.isAssignableFrom(violation.getClass()))) {
101                fail("Incorrect Violation class. expected: " + clazz.getName() + " found: " + violation.getClass().getName());
102            }
103        }
104    }