|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.scene; |
| 33 | + |
| 34 | +import com.jme3.scene.control.UpdateControl; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests selected methods of the Spatial class. |
| 40 | + * |
| 41 | + * @author Stephen Gold |
| 42 | + */ |
| 43 | +public class SpatialTest { |
| 44 | + |
| 45 | + /** |
| 46 | + * Tests addControlAt() with a duplicate Control. |
| 47 | + */ |
| 48 | + @Test(expected = IllegalStateException.class) |
| 49 | + public void addControlAtDuplicate() { |
| 50 | + Spatial testSpatial = new Node("testSpatial"); |
| 51 | + UpdateControl control1 = new UpdateControl(); |
| 52 | + testSpatial.addControlAt(0, control1); |
| 53 | + testSpatial.addControlAt(1, control1); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Tests addControlAt() with a negative index. |
| 58 | + */ |
| 59 | + @Test(expected = IndexOutOfBoundsException.class) |
| 60 | + public void addControlAtNegativeIndex() { |
| 61 | + Spatial testSpatial = new Node("testSpatial"); |
| 62 | + UpdateControl control1 = new UpdateControl(); |
| 63 | + testSpatial.addControlAt(-1, control1); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests addControlAt() with a null argument. |
| 68 | + */ |
| 69 | + @Test(expected = IllegalArgumentException.class) |
| 70 | + public void addControlAtNullControl() { |
| 71 | + Spatial testSpatial = new Node("testSpatial"); |
| 72 | + testSpatial.addControlAt(0, null); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Tests addControlAt() with an out-of-range positive index. |
| 77 | + */ |
| 78 | + @Test(expected = IndexOutOfBoundsException.class) |
| 79 | + public void addControlAtOutOfRange() { |
| 80 | + Spatial testSpatial = new Node("testSpatial"); |
| 81 | + UpdateControl control1 = new UpdateControl(); |
| 82 | + testSpatial.addControlAt(1, control1); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests typical uses of addControlAt(). |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void testAddControlAt() { |
| 90 | + Spatial testSpatial = new Node("testSpatial"); |
| 91 | + |
| 92 | + // Add to an empty list. |
| 93 | + UpdateControl control1 = new UpdateControl(); |
| 94 | + testSpatial.addControlAt(0, control1); |
| 95 | + |
| 96 | + Assert.assertEquals(1, testSpatial.getNumControls()); |
| 97 | + Assert.assertEquals(control1, testSpatial.getControl(0)); |
| 98 | + Assert.assertEquals(testSpatial, control1.getSpatial()); |
| 99 | + |
| 100 | + // Add at the end of a non-empty list. |
| 101 | + UpdateControl control2 = new UpdateControl(); |
| 102 | + testSpatial.addControlAt(1, control2); |
| 103 | + |
| 104 | + Assert.assertEquals(2, testSpatial.getNumControls()); |
| 105 | + Assert.assertEquals(control1, testSpatial.getControl(0)); |
| 106 | + Assert.assertEquals(control2, testSpatial.getControl(1)); |
| 107 | + Assert.assertEquals(testSpatial, control1.getSpatial()); |
| 108 | + Assert.assertEquals(testSpatial, control2.getSpatial()); |
| 109 | + |
| 110 | + // Add at the beginning of a non-empty list. |
| 111 | + UpdateControl control0 = new UpdateControl(); |
| 112 | + testSpatial.addControlAt(0, control0); |
| 113 | + |
| 114 | + Assert.assertEquals(3, testSpatial.getNumControls()); |
| 115 | + Assert.assertEquals(control0, testSpatial.getControl(0)); |
| 116 | + Assert.assertEquals(control1, testSpatial.getControl(1)); |
| 117 | + Assert.assertEquals(control2, testSpatial.getControl(2)); |
| 118 | + Assert.assertEquals(testSpatial, control0.getSpatial()); |
| 119 | + Assert.assertEquals(testSpatial, control1.getSpatial()); |
| 120 | + Assert.assertEquals(testSpatial, control2.getSpatial()); |
| 121 | + } |
| 122 | +} |
0 commit comments