/*
 * Copyright 2016 MovingBlocks
 *
 * 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.terasology.config.flexible;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.terasology.assets.ResourceUrn;
import org.terasology.config.flexible.validators.RangedNumberValueValidator;

import static org.junit.Assert.*;

@RunWith(Enclosed.class)
public class SettingTest {
    public static class SetValue {
        private Setting<Integer> setting;

        private int eventResult;

        @Before
        public void setUp() {
            setting = new Setting<>(new ResourceUrn("engine-tests", "TestSetting"),
                    50, new RangedNumberValueValidator<>(0, 100));

            eventResult = -1;

            setting.subscribe(propertyChangeEvent -> eventResult = (int) propertyChangeEvent.getNewValue());
        }

        @Test
        public void testSetsValue() {
            assertTrue(setting.setValue(25));

            assertEquals(25, eventResult);
        }

        @Test
        public void testDoesNotSetValue() {
            assertFalse(setting.setValue(101));

            assertEquals(-1, eventResult);
        }
    }
}
@@ -878,16 +878,157 @@
 lidator;
+%0Aimport org.terasology.utilities.random.FastRandom;%0Aimport org.terasology.utilities.random.Random;%0A%0Aimport java.beans.PropertyChangeListener;
 %0A%0Aimport
@@ -1887,10 +1887,2261 @@
  %7D%0A    %7D
+%0A%0A    public static class Subscribers %7B%0A        private Setting%3CInteger%3E setting;%0A%0A        private PropertyChangeListener listener;%0A%0A        private int eventCallCount;%0A%0A        @Before%0A        public void setUp() %7B%0A            setting = new Setting%3C%3E(new ResourceUrn(%22engine-tests%22, %22TestSetting%22),%0A                    50, new RangedNumberValueValidator%3C%3E(0, 100));%0A%0A            eventCallCount = 0;%0A%0A            listener = propertyChangeEvent -%3E eventCallCount++;%0A        %7D%0A%0A        @Test%0A        public void testSetEventCall() %7B%0A            setting.subscribe(listener);%0A%0A            Random random = new FastRandom();%0A%0A            final int n = 50;%0A            int expectedEventCallCount = 0;%0A%0A            for (int i = 0; i %3C n; i++) %7B%0A                int r = random.nextInt(-50, 150);%0A                expectedEventCallCount += setting.setValue(r) ? 1 : 0;%0A            %7D%0A%0A            assertEquals(expectedEventCallCount, eventCallCount);%0A        %7D%0A%0A        @Test%0A        public void testSubscribe() %7B%0A            final int n = 10;%0A%0A            for (int i = 0; i %3C n; i++) %7B%0A                setting.subscribe(listener);%0A            %7D%0A%0A            setting.setValue(30);%0A%0A            assertEquals(n, eventCallCount);%0A        %7D%0A%0A        @Test%0A        public void testUnsubscribe() %7B%0A            int n = 10;%0A%0A            for (int i = 0; i %3C n; i++) %7B%0A                setting.subscribe(listener);%0A            %7D%0A%0A            int halfN = n / 2;%0A%0A            for(int i = 0; i %3C new FastRandom().nextInt(halfN); i++)%7B%0A                setting.unsubscribe(listener);%0A                n--;%0A            %7D%0A%0A            setting.setValue(30);%0A%0A            assertEquals(n, eventCallCount);%0A        %7D%0A    %7D%0A%0A    public static class SetValueValidator %7B%0A        @Test%0A        public void testSetValueValidator() %7B%0A            Setting%3CInteger%3E setting = new Setting%3C%3E(new ResourceUrn(%22engine-tests%22, %22TestSetting%22),%0A                    50, new RangedNumberValueValidator%3C%3E(0, 100));%0A%0A            assertTrue(setting.setValue(5));%0A            assertTrue(setting.setValue(95));%0A%0A            setting.setValueValidator(new RangedNumberValueValidator%3CInteger%3E(10, 50));%0A%0A            assertFalse(setting.setValue(5));%0A            assertFalse(setting.setValue(95));%0A        %7D%0A    %7D
 %0A%7D

Add tests for Setting