view com.redhat.thermostat.tools.eclipse.plugin.tests/src/com/redhat/thermostat/tools/eclipse/plugin/tests/model/CommandTest.java @ 106:4356c844cff9

Implement `option`, `options` and `group`
author Omair Majid <omajid@redhat.com>
date Wed, 19 Feb 2014 18:32:13 -0500
parents 874bfd075462
children
line wrap: on
line source

package com.redhat.thermostat.tools.eclipse.plugin.tests.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.Collections;

import org.junit.Before;
import org.junit.Test;

import com.redhat.thermostat.tools.eclipse.plugin.model.Bundle;
import com.redhat.thermostat.tools.eclipse.plugin.model.Command;
import com.redhat.thermostat.tools.eclipse.plugin.model.Environments;
import com.redhat.thermostat.tools.eclipse.plugin.model.Option;
import com.redhat.thermostat.tools.eclipse.plugin.model.Options;

public class CommandTest {

    private CustomListener listener;
    private Command command;

    @Before
    public void setUp() {
        command = new Command();

        listener = new CustomListener();
    }

    @Test
    public void testConstruction() {
        Command command = new Command("name", "description", "usage",
                Collections.<String> emptyList(),
                new Options(),
                new Environments(),
                Collections.<Bundle> emptyList());

        assertNotNull(command);

        assertEquals("name", command.getName());
        assertEquals("description", command.getDescription());
        assertEquals("usage", command.getUsage());
    }

    @Test
    public void testNameChange() {
        command.setName("old");
        command.addPropertyChangeListener("name", listener);

        command.setName("new");

        assertPropertyChanged(listener, "name", "old", "new");
    }

    @Test
    public void testDescriptionChange() {
        command.setDescription("old");
        command.addPropertyChangeListener("description", listener);

        command.setDescription("new");

        assertPropertyChanged(listener, "description", "old", "new");
    }

    @Test
    public void testUsageChange() {
        command.setUsage("old");
        command.addPropertyChangeListener("usage", listener);

        command.setUsage("new");

        assertPropertyChanged(listener, "usage", "old", "new");
    }

    @Test
    public void testArgumentsChange() {
        command.setArguments(Arrays.asList(new String[] { "old" }));
        command.addPropertyChangeListener("arguments", listener);

        command.setArguments(Arrays.asList(new String[] { "new" }));

        assertPropertyChanged(listener, "arguments",
                Arrays.asList(new String[] { "old" }), Arrays.asList(new String[] { "new" }));
    }

    private static void assertPropertyChanged(CustomListener listener,
            String propertyName, Object oldValue, Object newValue) {

        assertTrue(listener.getInvoked());
        assertEquals(propertyName, listener.getPropertyName());
        assertEquals(oldValue, listener.getOldValue());
        assertEquals(newValue, listener.getNewValue());
    }

    static class CustomListener implements PropertyChangeListener {

        private boolean invoked = false;
        private String propertyName;
        private Object oldValue;
        private Object newValue;

        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            this.invoked = true;
            this.propertyName = evt.getPropertyName();
            this.oldValue = evt.getOldValue();
            this.newValue = evt.getNewValue();
        };

        public boolean getInvoked() {
            return invoked;
        }

        public String getPropertyName() {
            return propertyName;
        }

        public Object getOldValue() {
            return oldValue;
        }

        public Object getNewValue() {
            return newValue;
        }

    }
}