text:
package org.datalorax.populace.core.util;

import org.datalorax.populace.core.util.testtypes.TypeWithGetterSetter;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.testng.Assert.fail;

@SuppressWarnings("UnusedDeclaration")
public class GetterSetterTypeUtilsTest {
    @Test
    public void shouldNotThrowForValidGetterAndSetter() throws Exception {
        // When:
        GetterSetterTypeUtils.validateGetterSetterPair(
            getMethod(TypeWithGetterSetter.class, "getThing"),
            getMethod(TypeWithGetterSetter.class, "setThing"));

        // Then:
        // It didn't throw.
    }

    @Test
    public void shouldNotThrowForValidGetterAndSetterOnInnerClass() throws Exception {
        // Given:
        class SomeType {
            int getIt() {
                return 0;
            }

            void setIt(int value) {
            }
        }

        // When:
        GetterSetterTypeUtils.validateGetterSetterPair(
            getMethod(SomeType.class, "getIt"),
            getMethod(SomeType.class, "setIt"));

        // Then:
        // It didn't throw.
    }

    @Test
    public void shouldThrowIfGetterAndSetterAreFromUnrelatedClasses() throws Exception {
        // Given:
        class TypeOne {
            int getIt() {
                return 0;
            }
        }
        class TypeTwo {
            void setIt(int value) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(getMethod(TypeOne.class, "getIt"), getMethod(TypeTwo.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("must be on related classes"));
        }
    }

    @Test
    public void shouldIncludeGetterAndSetterDetailsInException() throws Exception {
        // Given:
        class TypeOne {
            int getIt() {
                return 0;
            }
        }
        class TypeTwo {
            void setIt(int value) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(getMethod(TypeOne.class, "getIt"), getMethod(TypeTwo.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString(TypeOne.class.getName()));
            assertThat(e.getMessage(), containsString(TypeTwo.class.getName()));
            assertThat(e.getMessage(), containsString("getIt"));
            assertThat(e.getMessage(), containsString("setIt"));
        }
    }

    @Test
    public void shouldThrowIfGetterIsStatic() throws Exception {
        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(getClass(), "getStaticGetter"),
                getMethod(getClass(), "setStaticGetter"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Getter can not be static"));
        }
    }

    @Test
    public void shouldThrowIfGetterReturnsVoid() throws Exception {
        // Given:
        class SomeType {
            void getIt() {
            }

            void setIt(int v) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Getter must return a value"));
        }
    }

    @Test
    public void shouldThrowIfGetterTakesParameters() throws Exception {
        // Given:
        class SomeType {
            int getIt(int v) {
                return 0;
            }

            void setIt(int v) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Getter must not take parameters"));
        }
    }

    @Test
    public void shouldThrowIfSetterIsStatic() throws Exception {
        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(getClass(), "getStaticSetter"),
                getMethod(getClass(), "setStaticSetter"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Setter can not be static"));
        }
    }

    @Test
    public void shouldThrowIfSetterTakesNoParameters() throws Exception {
        // Given:
        class SomeType {
            int getIt() {
                return 0;
            }

            void setIt() {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Setter must take only a single parameter"));
        }
    }

    @Test
    public void shouldThrowIfSetterTakesMoreThanOneParameter() throws Exception {
        // Given:
        class SomeType {
            int getIt() {
                return 0;
            }

            void setIt(int v, int v2) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("Setter must take only a single parameter"));
        }
    }

    @Test
    public void shouldThrowIfGetterReturnValueDoesNotMatchSetterParam() throws Exception {
        // Given:
        class SomeType {
            int getIt() {
                return 0;
            }

            void setIt(Integer v) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("getter return type must match setter param type"));
        }
    }

    @Test
    public void shouldThrowIfGetterGenericReturnTypeDoesNotMatchSetterGenericParam() throws Exception {
        // Given:
        class SomeType {
            List<Integer> getIt() {
                return null;
            }

            void setIt(List<Number> v) {
            }
        }

        try {
            // When:
            GetterSetterTypeUtils.validateGetterSetterPair(
                getMethod(SomeType.class, "getIt"),
                getMethod(SomeType.class, "setIt"));

            fail("exception expected");
        } catch (IllegalArgumentException e) {
            // Then:
            assertThat(e.getMessage(), containsString("getter return type must match setter param type"));
        }
    }

    @Test
    public void shouldDetectValidAndInvalidGetter() throws Exception {
        // Given:
        class SomeType {
            int getValid() {
                return 0;
            }

            void getInvalid() {
            }
        }

        // Then:
        assertThat(GetterSetterTypeUtils.isValidGetter(getMethod(SomeType.class, "getValid")), is(true));
        assertThat(GetterSetterTypeUtils.isValidGetter(getMethod(SomeType.class, "getInvalid")), is(false));
    }

    @Test
    public void shouldDetectValidAndInvalidSetter() throws Exception {
        // Given:
        class SomeType {
            void setInvalid() {
            }

            void setValid(int v) {
            }
        }

        // Then:
        assertThat(GetterSetterTypeUtils.isValidSetter(getMethod(SomeType.class, "setValid")), is(true));
        assertThat(GetterSetterTypeUtils.isValidSetter(getMethod(SomeType.class, "setInvalid")), is(false));
    }

    @Test
    public void shouldNotThrowOnValidGetter() throws Exception {
        // Given:
        class SomeType {
            int getIt() {
                return 0;
            }
        }

        // When:
        GetterSetterTypeUtils.validateGetter(getMethod(SomeType.class, "getIt"));

        // Then:
        // It didn't throw
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void shouldThrowOnInvalidGetter() throws Exception {
        // Given:
        class SomeType {
            void getIt() {
            }
        }

        // When:
        GetterSetterTypeUtils.validateGetter(getMethod(SomeType.class, "getIt"));
    }

    @Test
    public void shouldNotThrowOnValidSetter() throws Exception {
        // Given:
        class SomeType {
            void setIt(int v) {
            }
        }

        // When:
        GetterSetterTypeUtils.validateSetter(getMethod(SomeType.class, "setIt"));

        // Then:
        // It didn't throw
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void shouldThrowOnInvalidSetter() throws Exception {
        // Given:
        class SomeType {
            void setIt() {
            }
        }

        // When:
        GetterSetterTypeUtils.validateSetter(getMethod(SomeType.class, "setIt"));
    }

    // Getter & Setters used in tests:
    public void setStaticGetter() {
    }

    public long getStaticSetter() {
        return 0;
    }

    private Method getMethod(final Class<?> type, final String methodName) {
        return Arrays.stream(type.getDeclaredMethods())
            .filter(m -> m.getName().equals(methodName))
            .findAny().get();
    }

    private static long getStaticGetter() {
        return 0;
    }

    private static void setStaticSetter() {
    }
}


text:
"""
Tests for the IBM Storwize V7000 and SVC volume driver.
"""

import random
import socket

from nova import exception
from nova.openstack.common import excutils
from nova.openstack.common import log as logging
from nova import test
from nova import utils
from nova.volume import storwize_svc

LOG = logging.getLogger(__name__)

class StorwizeSVCManagementSimulator:
    def __init__(self, pool_name):
        self._flags = {"storwize_svc_volpool_name": pool_name}
        self._volumes_list = {}
        self._hosts_list = {}
        self._mappings_list = {}
        self._fcmappings_list = {}
        self._next_cmd_error = {
            "lsportip": "",
            "lsnodecanister": "",
            "lsvdisk": "",
            "lsfcmap": "",
            "prestartfcmap": "",
            "startfcmap": "",
            "rmfcmap": "",
        }
        self._errors = {
            "CMMVC5701E": ("", "CMMVC5701E No object ID was specified."),
            "CMMVC6035E": ("", "CMMVC6035E The action failed as the " +
                               "object already exists."),
            "CMMVC5753E": ("", "CMMVC5753E The specified object does not " +
                               "exist or is not a suitable candidate."),
            "CMMVC5707E": ("", "CMMVC5707E Required parameters are missing."),
            "CMMVC6581E": ("", "CMMVC6581E The command has failed because " +
                               "the maximum number of allowed iSCSI " +
                               "qualified names (IQNs) has been reached, " +
                               "or the IQN is already assigned or is not " +
                               "valid."),
            "CMMVC5754E": ("", "CMMVC5754E The specified object does not " +
                               "exist, or the name supplied does not meet " +
                               "the naming rules."),
            "CMMVC6071E": ("", "CMMVC6071E The VDisk-to-host mapping was " +
                               "not created because the VDisk is already " +
                               "mapped to a host."),
            "CMMVC5879E": ("", "CMMVC5879E The VDisk-to-host mapping was " +
                               "not created because a VDisk is already " +
                               "mapped to this host with this SCSI LUN."),
            "CMMVC5840E": ("", "CMMVC5840E The virtual disk (VDisk) was " +
                               "not deleted because it is mapped to a " +
                               "host or because it is part of a FlashCopy " +
                               "or Remote Copy mapping, or is involved in " +
                               "an image mode migrate."),
            "CMMVC6527E": ("", "CMMVC6527E The name that you have entered " +
                               "is not valid. The name can contain letters, " +
                               "numbers, spaces, periods, dashes, and " +
                               "underscores. The name must begin with a " +
                               "letter or an underscore. The name must not " +
                               "begin or end with a space."),
            "CMMVC5871E": ("", "CMMVC5871E The action failed because one or " +
                               "more of the configured port names is in a " +
                               "mapping."),
            "CMMVC5924E": ("", "CMMVC5924E The FlashCopy mapping was not " +
                               "created because the source and target " +
                               "virtual disks (VDisks) are different sizes."),
            "CMMVC6303E": ("", "CMMVC6303E The create failed because the " +
                               "source and target VDisks are the same."),
        }

    # Find an unused ID
    def _find_unused_id(self, d):
        ids = []
        for k, v in d.iteritems():
            ids.append(int(v["id"]))
        ids.sort()
        for index, n in enumerate(ids):
            if n > index:
                return str(index)
        return str(len(ids))

    # Check if name is valid
    def _is_invalid_name(self, name):
        if (name[0] == " ") or (name[-1] == " "):
            return True
        for c in name:
            if ((not c.isalnum()) and (c != " ") and (c != ".")
                    and (c != "-") and (c != "_")):
                return True
        return False

    # Convert argument string to dictionary
    def _cmd_to_dict(self, cmd):
        arg_list = cmd.split()
        no_param_args = [
            "autodelete",
            "autoexpand",
            "bytes",
            "compressed",
            "force",
            "nohdr",
        ]
        one_param_args = [
            "cleanrate",
            "delim",
            "filtervalue",
            "grainsize",
            "host",
            "iogrp",
            "iscsiname",
            "mdiskgrp",
            "name",
            "rsize",
            "scsi",
            "size",
            "source",
            "target",
            "unit",
            "vtype",
        ]

        # Handle the special case of lsnode which is a two-word command
        # Use the one word version of the command internally
        if arg_list[0] == "svcinfo" and arg_list[1] == "lsnode":
            ret = {"cmd": "lsnodecanister"}
            arg_list.pop(0)
        else:
            ret = {"cmd": arg_list[0]}

        skip = False
        for i in range(1, len(arg_list)):
            if skip:
                skip = False
                continue
            if arg_list[i][0] == "-":
                if arg_list[i][1:] in no_param_args:
                    ret[arg_list[i][1:]] = True
                elif arg_list[i][1:] in one_param_args:
                    ret[arg_list[i][1:]] = arg_list[i + 1]
                    skip = True
                else:
                    raise exception.InvalidInput(
                        reason=_('unrecognized argument %s') % arg_list[i])
            else:
                ret["obj"] = arg_list[i]
        return ret

    # Generic function for printing information
    def _print_info_cmd(self, rows, delim=" ", nohdr=False, **kwargs):
        if nohdr:
                del rows[0]

        for index in range(len(rows)):
            rows[index] = delim.join(rows[index])
        return ("%s" % "\n".join(rows), "")

    # Print mostly made-up stuff in the correct syntax
    def _cmd_lsmdiskgrp(self, **kwargs):
        rows = [None] * 2
        rows[0] = ["id", "name", "status", "mdisk_count",
                   "vdisk_count capacity", "extent_size", "free_capacity",
                   "virtual_capacity", "used_capacity", "real_capacity",
                   "overallocation", "warning", "easy_tier",
                   "easy_tier_status"]
        rows[1] = ["0", self._flags["storwize_svc_volpool_name"], "online",
                   "1", str(len(self._volumes_list)), "3.25TB", "256",
                   "3.21TB", "1.54TB", "264.97MB", "35.58GB", "47", "80",
                   "auto", "inactive"]
        return self._print_info_cmd(rows=rows, **kwargs)

    # Print mostly made-up stuff in the correct syntax
    def _cmd_lsnodecanister(self, **kwargs):
        rows = [None] * 3
        rows[0] = ["id", "name", "UPS_serial_number", "WWNN", "status",
                   "IO_group_id", "IO_group_name", "config_node",
                   "UPS_unique_id", "hardware", "iscsi_name", "iscsi_alias",
                   "panel_name", "enclosure_id", "canister_id",
                   "enclosure_serial_number"]
        rows[1] = ["5", "node1", "", "123456789ABCDEF0", "online", "0",
                   "io_grp0",
                   "yes", "123456789ABCDEF0", "100",
                   "iqn.1982-01.com.ibm:1234.sim.node1", "", "01-1", "1", "1",
                   "0123ABC"]
        rows[2] = ["6", "node2", "", "123456789ABCDEF1", "online", "0",
                   "io_grp0",
                   "no", "123456789ABCDEF1", "100",
                   "iqn.1982-01.com.ibm:1234.sim.node2", "", "01-2", "1", "2",
                   "0123ABC"]

        if self._next_cmd_error["lsnodecanister"] == "header_mismatch":
            rows[0].pop(2)
            self._next_cmd_error["lsnodecanister"] = ""
        if self._next_cmd_error["lsnodecanister"] == "remove_field":
            for row in rows:
                row.pop(0)
            self._next_cmd_error["lsnodecanister"] = ""

        return self._print_info_cmd(rows=rows, **kwargs)

    # Print mostly made-up stuff in the correct syntax
    def _cmd_lsportip(self, **kwargs):
        if self._next_cmd_error["lsportip"] == "ip_no_config":
            self._next_cmd_error["lsportip"] = ""
            ip_addr1 = ""
            ip_addr2 = ""
            gw = ""
        else:
            ip_addr1 = "1.234.56.78"
            ip_addr2 = "1.234.56.79"
            gw = "1.234.56.1"

        rows = [None] * 17
        rows[0] = ["id", "node_id", "node_name", "IP_address", "mask",
                   "gateway", "IP_address_6", "prefix_6", "gateway_6", "MAC",
                   "duplex", "state", "speed", "failover"]
        rows[1] = ["1", "5", "node1", ip_addr1, "255.255.255.0",
                   gw, "", "", "", "01:23:45:67:89:00", "Full",
                   "online", "1Gb/s", "no"]
        rows[2] = ["1", "5", "node1", "", "", "", "", "", "",
                   "01:23:45:67:89:00", "Full", "online", "1Gb/s", "yes"]
        rows[3] = ["2", "5", "node1", "", "", "", "", "", "",
                   "01:23:45:67:89:01", "Full", "unconfigured", "1Gb/s", "no"]
        rows[4] = ["2", "5", "node1", "", "", "", "", "", "",
                   "01:23:45:67:89:01", "Full", "unconfigured", "1Gb/s", "yes"]
        rows[5] = ["3", "5", "node1", "", "", "", "", "", "", "", "",
                   "unconfigured", "", "no"]
        rows[6] = ["3", "5", "node1", "", "", "", "", "", "", "", "",
                   "unconfigured", "", "yes"]
        rows[7] = ["4", "5", "node1", "", "", "", "", "", "", "", "",
                   "unconfigured", "", "no"]
        rows[8] = ["4", "5", "node1", "", "", "", "", "", "", "", "",
                   "unconfigured", "", "yes"]
        rows[9] = ["1", "6", "node2", ip_addr2, "255.255.255.0",
                   gw, "", "", "", "01:23:45:67:89:02", "Full",
                   "online", "1Gb/s", "no"]
        rows[10] = ["1", "6", "node2", "", "", "", "", "", "",
                    "01:23:45:67:89:02", "Full", "online", "1Gb/s", "yes"]
        rows[11] = ["2", "6", "node2", "", "", "", "", "", "",
                    "01:23:45:67:89:03", "Full", "unconfigured", "1Gb/s", "no"]
        rows[12] = ["2", "6", "node2", "", "", "", "", "", "",
                    "01:23:45:67:89:03", "Full", "unconfigured", "1Gb/s",
                    "yes"]
        rows[13] = ["3", "6", "node2", "", "", "", "", "", "", "", "",
                    "unconfigured", "", "no"]
        rows[14] = ["3", "6", "node2", "", "", "", "", "", "", "", "",
                    "unconfigured", "", "yes"]
        rows[15] = ["4", "6", "node2", "", "", "", "", "", "", "", "",
                    "unconfigured", "", "no"]
        rows[16] = ["4", "6", "node2", "", "", "", "", "", "", "", "",
                    "unconfigured", "", "yes"]

        if self._next_cmd_error["lsportip"] == "header_mismatch":
            rows[0].pop(2)
            self._next_cmd_error["lsportip"] = ""
        if self._next_cmd_error["lsportip"] == "remove_field":
            for row in rows:
                row.pop(1)
            self._next_cmd_error["lsportip"] = ""

        return self._print_info_cmd(rows=rows, **kwargs)

    # Create a vdisk
    def _cmd_mkvdisk(self, **kwargs):
        # We only save the id/uid, name, and size - all else will be made up
        volume_info = {}
        volume_info["id"] = self._find_unused_id(self._volumes_list)
        volume_info["uid"] = ("ABCDEF" * 3) + ("0" * 14) + volume_info["id"]

        if "name" in kwargs:
            volume_info["name"] = kwargs["name"].strip('\'\"')
        else:
            volume_info["name"] = "vdisk" + volume_info["id"]

        # Assume size and unit are given, store it in bytes
        capacity = int(kwargs["size"])
        unit = kwargs["unit"]

        if unit == "b":
            volume_info["capacity"] = capacity
        elif unit == "kb":
            volume_info["capacity"] = capacity * pow(1024, 1)
        elif unit == "mb":
            volume_info["capacity"] = capacity * pow(1024, 2)
        elif unit == "gb":
            volume_info["capacity"] = capacity * pow(1024, 3)
        elif unit == "tb":
            volume_info["capacity"] = capacity * pow(1024, 4)
        elif unit == "pb":
            volume_info["capacity"] = capacity * pow(1024, 5)

        if volume_info["name"] in self._volumes_list:
            return self._errors["CMMVC6035E"]
        else:
            self._volumes_list[volume_info["name"]] = volume_info
            return ("Virtual Disk, id [%s], successfully created" %
                    (volume_info["id"]), "")

    # Delete a vdisk
    def _cmd_rmvdisk(self, **kwargs):
        force = 0
        if "force" in kwargs:
            force = 1

        if "obj" not in kwargs:
            return self._errors["CMMVC5701E"]
        vol_name = kwargs["obj"].strip('\'\"')

        if not vol_name in self._volumes_list:
            return self._errors["CMMVC5753E"]

        if force == 0:
            for k, mapping in self._mappings_list.iteritems():
                if mapping["vol"] == vol_name:
                    return self._errors["CMMVC5840E"]
            for k, fcmap in self._fcmappings_list.iteritems():
                if ((fcmap["source"] == vol_name) or
                        (fcmap["target"] == vol_name)):
                    return self._errors["CMMVC5840E"]

        del self._volumes_list[vol_name]
        return ("", "")

    def _get_fcmap_info(self, vol_name):
        ret_vals = {
            "fc_id": "",
            "fc_name": "",
            "fc_map_count": "0",
        }
        for k, fcmap in self._fcmappings_list.iteritems():
            if ((fcmap["source"] == vol_name) or
                    (fcmap["target"] == vol_name)):
                ret_vals["fc_id"] = fcmap["id"]
                ret_vals["fc_name"] = fcmap["name"]
                ret_vals["fc_map_count"] = "1"
        return ret_vals

    # List information about vdisks
    def _cmd_lsvdisk(self, **kwargs):
        if "obj" not in kwargs:
            rows = []
            rows.append(["id", "name", "IO_group_id", "IO_group_name",
                         "status", "mdisk_grp_id", "mdisk_grp_name",
                         "capacity", "type", "FC_id", "FC_name", "RC_id",
                         "RC_name", "vdisk_UID", "fc_map_count", "copy_count",
                         "fast_write_state", "se_copy_count", "RC_change"])

            for k, vol in self._volumes_list.iteritems():
                if (("filtervalue" not in kwargs) or
                        (kwargs["filtervalue"] == "name=" + vol["name"])):
                    fcmap_info = self._get_fcmap_info(vol["name"])

                    rows.append([str(vol["id"]), vol["name"], "0", "io_grp0",
                                "online", "0",
                                self._flags["storwize_svc_volpool_name"],
                                str(vol["capacity"]), "striped",
                                fcmap_info["fc_id"], fcmap_info["fc_name"],
                                "", "", vol["uid"],
                                fcmap_info["fc_map_count"], "1", "empty",
                                "1", "no"])

            return self._print_info_cmd(rows=rows, **kwargs)

        else:
            if kwargs["obj"] not in self._volumes_list:
                return self._errors["CMMVC5754E"]
            vol = self._volumes_list[kwargs["obj"]]
            fcmap_info = self._get_fcmap_info(vol["name"])
            rows = []
            rows.append(["id", str(vol["id"])])
            rows.append(["name", vol["name"]])
            rows.append(["IO_group_id", "0"])
            rows.append(["IO_group_name", "io_grp0"])
            rows.append(["status", "online"])
            rows.append(["mdisk_grp_id", "0"])
            rows.append(["mdisk_grp_name",
                    self._flags["storwize_svc_volpool_name"]])
            rows.append(["capacity", str(vol["capacity"])])
            rows.append(["type", "striped"])
            rows.append(["formatted", "no"])
            rows.append(["mdisk_id", ""])
            rows.append(["mdisk_name", ""])
            rows.append(["FC_id", fcmap_info["fc_id"]])
            rows.append(["FC_name", fcmap_info["fc_name"]])
            rows.append(["RC_id", ""])
            rows.append(["RC_name", ""])
            rows.append(["vdisk_UID", vol["uid"]])
            rows.append(["throttling", "0"])

            if self._next_cmd_error["lsvdisk"] == "blank_pref_node":
                rows.append(["preferred_node_id", ""])
                self._next_cmd_error["lsvdisk"] = ""
            elif self._next_cmd_error["lsvdisk"] == "no_pref_node":
                self._next_cmd_error["lsvdisk"] = ""
            else:
                rows.append(["preferred_node_id", "6"])
            rows.append(["fast_write_state", "empty"])
            rows.append(["cache", "readwrite"])
            rows.append(["udid", ""])
            rows.append(["fc_map_count", fcmap_info["fc_map_count"]])
            rows.append(["sync_rate", "50"])
            rows.append(["copy_count", "1"])
            rows.append(["se_copy_count", "0"])
            rows.append(["mirror_write_priority", "latency"])
            rows.append(["RC_change", "no"])

            if "nohdr" in kwargs:
                for index in range(len(rows)):
                    rows[index] = " ".join(rows[index][1:])

            if "delim" in kwargs:
                for index in range(len(rows)):
                    rows[index] = kwargs["delim"].join(rows[index])

            return ("%s" % "\n".join(rows), "")

    # Make a host
    def _cmd_mkhost(self, **kwargs):
        host_info = {}
        host_info["id"] = self._find_unused_id(self._hosts_list)

        if "name" in kwargs:
            host_name = kwargs["name"].strip('\'\"')
        else:
            host_name = "host" + str(host_info["id"])
        host_info["host_name"] = host_name

        if "iscsiname" not in kwargs:
            return self._errors["CMMVC5707E"]
        host_info["iscsi_name"] = kwargs["iscsiname"].strip('\'\"')

        if self._is_invalid_name(host_name):
            return self._errors["CMMVC6527E"]

        if host_name in self._hosts_list:
            return self._errors["CMMVC6035E"]

        for k, v in self._hosts_list.iteritems():
            if v["iscsi_name"] == host_info["iscsi_name"]:
                return self._errors["CMMVC6581E"]

        self._hosts_list[host_name] = host_info
        return ("Host, id [%s], successfully created" %
                (host_info["id"]), "")

    # Remove a host
    def _cmd_rmhost(self, **kwargs):
        if "obj" not in kwargs:
            return self._errors["CMMVC5701E"]

        host_name = kwargs["obj"].strip('\'\"')
        if host_name not in self._hosts_list:
            return self._errors["CMMVC5753E"]

        for k, v in self._mappings_list.iteritems():
            if (v["host"] == host_name):
                return self._errors["CMMVC5871E"]

        del self._hosts_list[host_name]
        return ("", "")

    # List information about hosts
    def _cmd_lshost(self, **kwargs):
        if "obj" not in kwargs:
            rows = []
            rows.append(["id", "name", "port_count", "iogrp_count", "status"])

            found = False
            for k, host in self._hosts_list.iteritems():
                filterstr = "name=" + host["host_name"]
                if (("filtervalue" not in kwargs) or
                        (kwargs["filtervalue"] == filterstr)):
                    rows.append([host["id"], host["host_name"], "1", "4",
                                "offline"])
                    found = True
            if found:
                return self._print_info_cmd(rows=rows, **kwargs)
            else:
                return ("", "")
        else:
            if kwargs["obj"] not in self._hosts_list:
                return self._errors["CMMVC5754E"]
            host = self._hosts_list[kwargs["obj"]]
            rows = []
            rows.append(["id", host["id"]])
            rows.append(["name", host["host_name"]])
            rows.append(["port_count", "1"])
            rows.append(["type", "generic"])
            rows.append(["mask", "1111"])
            rows.append(["iogrp_count", "4"])
            rows.append(["status", "offline"])
            rows.append(["iscsi_name", host["iscsi_name"]])
            rows.append(["node_logged_in_count", "0"])
            rows.append(["state", "offline"])

            if "nohdr" in kwargs:
                for index in range(len(rows)):
                    rows[index] = " ".join(rows[index][1:])

            if "delim" in kwargs:
                for index in range(len(rows)):
                    rows[index] = kwargs["delim"].join(rows[index])

            return ("%s" % "\n".join(rows), "")

    # Create a vdisk-host mapping
    def _cmd_mkvdiskhostmap(self, **kwargs):
        mapping_info = {}
        mapping_info["id"] = self._find_unused_id(self._mappings_list)

        if "host" not in kwargs:
            return self._errors["CMMVC5707E"]
        mapping_info["host"] = kwargs["host"].strip('\'\"')

        if "scsi" not in kwargs:
            return self._errors["CMMVC5707E"]
        mapping_info["lun"] = kwargs["scsi"].strip('\'\"')

        if "obj" not in kwargs:
            return self._errors["CMMVC5707E"]
        mapping_info["vol"] = kwargs["obj"].strip('\'\"')

        if not mapping_info["vol"] in self._volumes_list:
            return self._errors["CMMVC5753E"]

        if not mapping_info["host"] in self._hosts_list:
            return self._errors["CMMVC5754E"]

        if mapping_info["vol"] in self._mappings_list:
            return self._errors["CMMVC6071E"]

        for k, v in self._mappings_list.iteritems():
            if ((v["host"] == mapping_info["host"]) and
                    (v["lun"] == mapping_info["lun"])):
                return self._errors["CMMVC5879E"]

        self._mappings_list[mapping_info["vol"]] = mapping_info
        return ("Virtual Disk to Host map, id [%s], successfully created"
                % (mapping_info["id"]), "")

    # Delete a vdisk-host mapping
    def _cmd_rmvdiskhostmap(self, **kwargs):
        if "host" not in kwargs:
            return self._errors["CMMVC5707E"]
        host = kwargs["host"].strip('\'\"')

        if "obj" not in kwargs:
            return self._errors["CMMVC5701E"]
        vol = kwargs["obj"].strip('\'\"')

        if not vol in self._mappings_list:
            return self._errors["CMMVC5753E"]

        if self._mappings_list[vol]["host"] != host:
            return self._errors["CMMVC5753E"]

        del self._mappings_list[vol]
        return ("", "")

    # List information about vdisk-host mappings
    def _cmd_lshostvdiskmap(self, **kwargs):
        index = 1
        no_hdr = 0
        delimeter = ""
        host_name = kwargs["obj"]

        if host_name not in self._hosts_list:
            return self._errors["CMMVC5754E"]

        rows = []
        rows.append(["id", "name", "SCSI_id", "vdisk_id", "vdisk_name",
                     "vdisk_UID"])

        for k, mapping in self._mappings_list.iteritems():
            if (host_name == "") or (mapping["host"] == host_name):
                volume = self._volumes_list[mapping["vol"]]
                rows.append([mapping["id"], mapping["host"],
                            mapping["lun"], volume["id"],
                            volume["name"], volume["uid"]])

        return self._print_info_cmd(rows=rows, **kwargs)

    # Create a FlashCopy mapping
    def _cmd_mkfcmap(self, **kwargs):
        source = ""
        target = ""

        if "source" not in kwargs:
            return self._errors["CMMVC5707E"]
        source = kwargs["source"].strip('\'\"')
        if not source in self._volumes_list:
            return self._errors["CMMVC5754E"]

        if "target" not in kwargs:
            return self._errors["CMMVC5707E"]
        target = kwargs["target"].strip('\'\"')
        if not target in self._volumes_list:
            return self._errors["CMMVC5754E"]

        if source == target:
            return self._errors["CMMVC6303E"]

        if (self._volumes_list[source]["capacity"] !=
                self._volumes_list[target]["capacity"]):
            return self._errors["CMMVC5924E"]

        fcmap_info = {}
        fcmap_info["source"] = source
        fcmap_info["target"] = target
        fcmap_info["id"] = self._find_unused_id(self._fcmappings_list)
        fcmap_info["name"] = "fcmap" + fcmap_info["id"]
        fcmap_info["status"] = "idle_or_copied"
        fcmap_info["progress"] = "0"
        self._fcmappings_list[target] = fcmap_info

        return("FlashCopy Mapping, id [" + fcmap_info["id"] +
               "], successfully created", "")

    # Same function used for both prestartfcmap and startfcmap
    def _cmd_gen_startfcmap(self, mode, **kwargs):
        if "obj" not in kwargs:
            return self._errors["CMMVC5701E"]
        id_num = kwargs["obj"]

        if mode == "pre":
            if self._next_cmd_error["prestartfcmap"] == "bad_id":
                id_num = -1
                self._next_cmd_error["prestartfcmap"] = ""
        else:
            if self._next_cmd_error["startfcmap"] == "bad_id":
                id_num = -1
                self._next_cmd_error["startfcmap"] = ""

        for k, fcmap in self._fcmappings_list.iteritems():
            if fcmap["id"] == id_num:
                if mode == "pre":
                    fcmap["status"] = "preparing"
                else:
                    fcmap["status"] = "copying"
                fcmap["progress"] = "0"
                return ("", "")
        return self._errors["CMMVC5753E"]

    # Same function used for both stopfcmap and rmfcmap
    # Assumes it is called with "-force <fc_map_id>"
    def _cmd_stoprmfcmap(self, mode, **kwargs):
        if "obj" not in kwargs:
            return self._errors["CMMVC5701E"]
        id_num = kwargs["obj"]

        if self._next_cmd_error["rmfcmap"] == "bad_id":
            id_num = -1
            self._next_cmd_error["rmfcmap"] = ""

        to_delete = None
        found = False
        for k, fcmap in self._fcmappings_list.iteritems():
            if fcmap["id"] == id_num:
                found = True
                if mode == "rm":
                    to_delete = k

        if to_delete:
            del self._fcmappings_list[to_delete]

        if found:
            return ("", "")
        else:
            return self._errors["CMMVC5753E"]

    def _cmd_lsfcmap(self, **kwargs):
        rows = []
        rows.append(["id", "name", "source_vdisk_id", "source_vdisk_name",
                     "target_vdisk_id", "target_vdisk_name", "group_id",
                     "group_name", "status", "progress", "copy_rate",
                     "clean_progress", "incremental", "partner_FC_id",
                     "partner_FC_name", "restoring", "start_time",
                     "rc_controlled"])

        # Assume we always get a filtervalue argument
        filter_key = kwargs["filtervalue"].split("=")[0]
        filter_value = kwargs["filtervalue"].split("=")[1]
        to_delete = []
        for k, v in self._fcmappings_list.iteritems():
            if str(v[filter_key]) == filter_value:
                source = self._volumes_list[v["source"]]
                target = self._volumes_list[v["target"]]
                old_status = v["status"]
                if old_status == "preparing":
                    new_status = "prepared"
                    if self._next_cmd_error["lsfcmap"] == "bogus_prepare":
                        new_status = "bogus"
                elif (old_status == "copying") and (v["progress"] == "0"):
                    new_status = "copying"
                    v["progress"] = "50"
                elif (old_status == "copying") and (v["progress"] == "50"):
                    new_status = "idle_or_copied"
                    to_delete.append(k)
                else:
                    new_status = old_status
                v["status"] = new_status

                if ((self._next_cmd_error["lsfcmap"] == "speed_up") or
                        (self._next_cmd_error["lsfcmap"] == "bogus_prepare")):
                    print_status = new_status
                    self._next_cmd_error["lsfcmap"] = ""
                else:
                    print_status = old_status

                rows.append([v["id"], v["name"], source["id"],
                            source["name"], target["id"], target["name"], "",
                            "", print_status, v["progress"], "50", "100",
                            "off", "", "", "no", "", "no"])

        for d in to_delete:
            del self._fcmappings_list[k]

        return self._print_info_cmd(rows=rows, **kwargs)

    # The main function to run commands on the management simulator
    def execute_command(self, cmd, check_exit_code=True):
        try:
            kwargs = self._cmd_to_dict(cmd)
        except IndexError:
            return self._errors["CMMVC5707E"]

        command = kwargs["cmd"]
        del kwargs["cmd"]
        arg_list = cmd.split()

        if command == "lsmdiskgrp":
            out, err = self._cmd_lsmdiskgrp(**kwargs)
        elif command == "lsnodecanister":
            out, err = self._cmd_lsnodecanister(**kwargs)
        elif command == "lsportip":
            out, err = self._cmd_lsportip(**kwargs)
        elif command == "mkvdisk":
            out, err = self._cmd_mkvdisk(**kwargs)
        elif command == "rmvdisk":
            out, err = self._cmd_rmvdisk(**kwargs)
        elif command == "lsvdisk":
            out, err = self._cmd_lsvdisk(**kwargs)
        elif command == "mkhost":
            out, err = self._cmd_mkhost(**kwargs)
        elif command == "rmhost":
            out, err = self._cmd_rmhost(**kwargs)
        elif command == "lshost":
            out, err = self._cmd_lshost(**kwargs)
        elif command == "mkvdiskhostmap":
            out, err = self._cmd_mkvdiskhostmap(**kwargs)
        elif command == "rmvdiskhostmap":
            out, err = self._cmd_rmvdiskhostmap(**kwargs)
        elif command == "lshostvdiskmap":
            out, err = self._cmd_lshostvdiskmap(**kwargs)
        elif command == "mkfcmap":
            out, err = self._cmd_mkfcmap(**kwargs)
        elif command == "prestartfcmap":
            out, err = self._cmd_gen_startfcmap(mode="pre", **kwargs)
        elif command == "startfcmap":
            out, err = self._cmd_gen_startfcmap(mode="start", **kwargs)
        elif command == "stopfcmap":
            out, err = self._cmd_stoprmfcmap(mode="stop", **kwargs)
        elif command == "rmfcmap":
            out, err = self._cmd_stoprmfcmap(mode="rm", **kwargs)
        elif command == "lsfcmap":
            out, err = self._cmd_lsfcmap(**kwargs)
        else:
            out, err = ("", "ERROR: Unsupported command")

        if (check_exit_code) and (len(err) != 0):
            raise exception.ProcessExecutionError(exit_code=1,
                                                  stdout=out,
                                                  stderr=err,
                                                  cmd=' '.join(cmd))

        return (out, err)

    # After calling this function, the next call to the specified command will
    # result in in the error specified
    def error_injection(self, cmd, error):
        self._next_cmd_error[cmd] = error

class StorwizeSVCFakeDriver(storwize_svc.StorwizeSVCDriver):
    def __init__(self, *args, **kwargs):
        super(StorwizeSVCFakeDriver, self).__init__(*args, **kwargs)

    def set_fake_storage(self, fake):
        self.fake_storage = fake

    def _run_ssh(self, cmd, check_exit_code=True):
        try:
            LOG.debug(_('Run CLI command: %s') % cmd)
            ret = self.fake_storage.execute_command(cmd, check_exit_code)
            (stdout, stderr) = ret
            LOG.debug(_('CLI output:\n stdout: %(out)s\n stderr: %(err)s') %
                        {'out': stdout, 'err': stderr})

        except exception.ProcessExecutionError as e:
            with excutils.save_and_reraise_exception():
                LOG.debug(_('CLI Exception output:\n stdout: %(out)s\n '
                            'stderr: %(err)s') % {'out': e.stdout,
                            'err': e.stderr})

        return ret

class StorwizeSVCDriverTestCase(test.TestCase):
    def setUp(self):
        super(StorwizeSVCDriverTestCase, self).setUp()
        self.USESIM = 1
        if self.USESIM == 1:
            self.sim = StorwizeSVCManagementSimulator("volpool")
            driver = StorwizeSVCFakeDriver()
            driver.set_fake_storage(self.sim)
            storwize_svc.FLAGS.san_ip = "hostname"
            storwize_svc.FLAGS.san_login = "user"
            storwize_svc.FLAGS.san_password = "pass"
            storwize_svc.FLAGS.storwize_svc_volpool_name = "volpool"
            storwize_svc.FLAGS.storwize_svc_flashcopy_timeout = "20"
        else:
            driver = storwize_svc.StorwizeSVCDriver()
            storwize_svc.FLAGS.san_ip = "-1.-1.-1.-1"
            storwize_svc.FLAGS.san_login = "user"
            storwize_svc.FLAGS.san_password = "password"
            storwize_svc.FLAGS.storwize_svc_volpool_name = "pool"

        self.driver = driver
        self.driver.do_setup(None)
        self.driver.check_for_setup_error()

    def test_storwize_svc_volume_tests(self):
        storwize_svc.FLAGS.storwize_svc_vol_rsize = "-1"
        volume = {}
        volume["name"] = "test1_volume%s" % random.randint(10000, 99999)
        volume["size"] = 10
        volume["id"] = 1
        self.driver.create_volume(volume)
        # Make sure that the volume has been created
        is_volume_defined = self.driver._is_volume_defined(volume["name"])
        self.assertEqual(is_volume_defined, True)
        self.driver.delete_volume(volume)

        if self.USESIM == 1:
            saved_rsize = storwize_svc.FLAGS.storwize_svc_vol_rsize
            saved_comp = storwize_svc.FLAGS.storwize_svc_vol_compression
            storwize_svc.FLAGS.storwize_svc_vol_rsize = "2%"
            storwize_svc.FLAGS.storwize_svc_vol_compression = True
            self.driver.create_volume(volume)
            is_volume_defined = self.driver._is_volume_defined(volume["name"])
            self.assertEqual(is_volume_defined, True)
            self.driver.delete_volume(volume)
            storwize_svc.FLAGS.storwize_svc_vol_rsize = saved_rsize
            storwize_svc.FLAGS.storwize_svc_vol_compression = saved_comp

    def test_storwize_svc_ip_connectivity(self):
        # Check for missing san_ip
        saved = storwize_svc.FLAGS.san_ip
        storwize_svc.FLAGS.san_ip = None
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.san_ip = saved

        if self.USESIM != 1:
            saved = storwize_svc.FLAGS.san_ip

            # Check for invalid ip
            storwize_svc.FLAGS.san_ip = "-1.-1.-1.-1"
            self.assertRaises(socket.gaierror,
                        self.driver.check_for_setup_error)

            # Check for unreachable IP
            storwize_svc.FLAGS.san_ip = "1.1.1.1"
            self.assertRaises(socket.error,
                        self.driver.check_for_setup_error)

            storwize_svc.FLAGS.san_ip = saved

    def test_storwize_svc_connectivity(self):
        # Make sure we detect if the pool doesn't exist
        orig_pool = getattr(storwize_svc.FLAGS, "storwize_svc_volpool_name")
        no_exist_pool = "i-dont-exist-%s" % random.randint(10000, 99999)
        storwize_svc.FLAGS.storwize_svc_volpool_name = no_exist_pool
        self.assertRaises(exception.InvalidParameterValue,
                self.driver.check_for_setup_error)
        storwize_svc.FLAGS.storwize_svc_volpool_name = orig_pool

        # Check the case where the user didn't configure IP addresses
        # as well as receiving unexpected results from the storage
        if self.USESIM == 1:
            self.sim.error_injection("lsnodecanister", "header_mismatch")
            self.assertRaises(exception.VolumeBackendAPIException,
                    self.driver.check_for_setup_error)
            self.sim.error_injection("lsnodecanister", "remove_field")
            self.assertRaises(exception.VolumeBackendAPIException,
                    self.driver.check_for_setup_error)
            self.sim.error_injection("lsportip", "ip_no_config")
            self.assertRaises(exception.VolumeBackendAPIException,
                    self.driver.check_for_setup_error)
            self.sim.error_injection("lsportip", "header_mismatch")
            self.assertRaises(exception.VolumeBackendAPIException,
                    self.driver.check_for_setup_error)
            self.sim.error_injection("lsportip", "remove_field")
            self.assertRaises(exception.VolumeBackendAPIException,
                    self.driver.check_for_setup_error)

        # Check with bad parameters
        saved_pass = storwize_svc.FLAGS.san_password
        saved_key = storwize_svc.FLAGS.san_private_key
        storwize_svc.FLAGS.san_password = None
        storwize_svc.FLAGS.san_private_key = None
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.san_password = saved_pass
        storwize_svc.FLAGS.san_private_key = saved_key

        saved = storwize_svc.FLAGS.storwize_svc_vol_vtype
        storwize_svc.FLAGS.storwize_svc_vol_vtype = "invalid"
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_vol_vtype = saved

        saved = storwize_svc.FLAGS.storwize_svc_vol_rsize
        storwize_svc.FLAGS.storwize_svc_vol_rsize = "invalid"
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_vol_rsize = saved

        saved = storwize_svc.FLAGS.storwize_svc_vol_warning
        storwize_svc.FLAGS.storwize_svc_vol_warning = "invalid"
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_vol_warning = saved

        saved = storwize_svc.FLAGS.storwize_svc_vol_autoexpand
        storwize_svc.FLAGS.storwize_svc_vol_autoexpand = "invalid"
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_vol_autoexpand = saved

        saved = storwize_svc.FLAGS.storwize_svc_vol_grainsize
        storwize_svc.FLAGS.storwize_svc_vol_grainsize = str(42)
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_vol_grainsize = saved

        saved = storwize_svc.FLAGS.storwize_svc_flashcopy_timeout
        storwize_svc.FLAGS.storwize_svc_flashcopy_timeout = str(601)
        self.assertRaises(exception.InvalidParameterValue,
                self.driver._check_flags)
        storwize_svc.FLAGS.storwize_svc_flashcopy_timeout = saved

        # Finally, check with good parameters
        self.driver.check_for_setup_error()

    def test_storwize_svc_flashcopy(self):
        volume1 = {}
        volume1["name"] = "test_volume%s" % random.randint(10000, 99999)
        volume1["size"] = 10
        volume1["id"] = 10
        self.driver.create_volume(volume1)

        snapshot = {}
        snapshot["name"] = "snap_volume%s" % random.randint(10000, 99999)
        snapshot["volume_name"] = volume1["name"]

        # Test timeout and volume cleanup
        saved = storwize_svc.FLAGS.storwize_svc_flashcopy_timeout
        storwize_svc.FLAGS.storwize_svc_flashcopy_timeout = str(1)
        self.assertRaises(exception.InvalidSnapshot,
                self.driver.create_snapshot, snapshot)
        is_volume_defined = self.driver._is_volume_defined(snapshot["name"])
        self.assertEqual(is_volume_defined, False)
        storwize_svc.FLAGS.storwize_svc_flashcopy_timeout = saved

        # Test bogus statuses
        if self.USESIM == 1:
            self.sim.error_injection("lsfcmap", "bogus_prepare")
            self.assertRaises(exception.VolumeBackendAPIException,
                self.driver.create_snapshot, snapshot)

        # Test prestartfcmap, startfcmap, and rmfcmap failing
        if self.USESIM == 1:
            self.sim.error_injection("prestartfcmap", "bad_id")
            self.assertRaises(exception.ProcessExecutionError,
                self.driver.create_snapshot, snapshot)
            self.sim.error_injection("lsfcmap", "speed_up")
            self.sim.error_injection("startfcmap", "bad_id")
            self.assertRaises(exception.ProcessExecutionError,
                self.driver.create_snapshot, snapshot)
            self.sim.error_injection("prestartfcmap", "bad_id")
            self.sim.error_injection("rmfcmap", "bad_id")
            self.assertRaises(exception.ProcessExecutionError,
                self.driver.create_snapshot, snapshot)

        # Test successful snapshot
        self.driver.create_snapshot(snapshot)

        # Ensure snapshot is defined
        is_volume_defined = self.driver._is_volume_defined(snapshot["name"])
        self.assertEqual(is_volume_defined, True)

        # Try to create a snapshot from an non-existing volume - should fail
        snapshot2 = {}
        snapshot2["name"] = "snap_volume%s" % random.randint(10000, 99999)
        snapshot2["volume_name"] = "undefined-vol"
        self.assertRaises(exception.VolumeNotFound,
                          self.driver.create_snapshot,
                          snapshot2)

        # Create volume from snapshot
        volume2 = {}
        volume2["name"] = "snap2vol_volume%s" % random.randint(10000, 99999)

        # Create volume from snapshot into an existsing volume
        self.assertRaises(exception.InvalidSnapshot,
                          self.driver.create_volume_from_snapshot,
                          volume1,
                          snapshot)

        # Try to create a volume from a non-existing snapshot
        self.assertRaises(exception.SnapshotNotFound,
                          self.driver.create_volume_from_snapshot,
                          volume2,
                          snapshot2)

        # Fail the snapshot
        if self.USESIM == 1:
            self.sim.error_injection("prestartfcmap", "bad_id")
            self.assertRaises(exception.ProcessExecutionError,
                self.driver.create_volume_from_snapshot, volume2, snapshot)

        # Succeed
        if self.USESIM == 1:
            self.sim.error_injection("lsfcmap", "speed_up")
        self.driver.create_volume_from_snapshot(volume2, snapshot)

        # Ensure volume is defined
        is_volume_defined = self.driver._is_volume_defined(volume2["name"])
        self.assertEqual(is_volume_defined, True)

        self.driver._delete_volume(volume2, True)
        self.driver._delete_snapshot(snapshot, True)

        # Check with target with different size
        volume3 = {}
        volume3["name"] = "test_volume%s" % random.randint(10000, 99999)
        volume3["size"] = 11
        volume3["id"] = 11
        self.driver.create_volume(volume3)
        snapshot["name"] = volume3["name"]
        self.assertRaises(exception.InvalidSnapshot,
                self.driver.create_snapshot, snapshot)
        self.driver._delete_volume(volume1, True)
        self.driver._delete_volume(volume3, True)

        # Snapshot volume that doesn't exist
        snapshot = {}
        snapshot["name"] = "snap_volume%s" % random.randint(10000, 99999)
        snapshot["volume_name"] = "no_exist"
        self.assertRaises(exception.VolumeNotFound,
                self.driver.create_snapshot, snapshot)

    def test_storwize_svc_volumes(self):
        # Create a first volume
        volume = {}
        volume["name"] = "test1_volume%s" % random.randint(10000, 99999)
        volume["size"] = 10
        volume["id"] = 1

        self.driver.create_volume(volume)

        self.driver.ensure_export(None, volume)

        # Do nothing
        self.driver.create_export(None, volume)
        self.driver.remove_export(None, volume)
        self.assertRaises(NotImplementedError,
                self.driver.check_for_export, None, volume["id"])

        # Make sure volume attributes are as they should be
        attributes = self.driver._get_volume_attributes(volume["name"])
        attr_size = float(attributes["capacity"]) / 1073741824  # bytes to GB
        self.assertEqual(attr_size, float(volume["size"]))
        pool = getattr(storwize_svc.FLAGS, "storwize_svc_volpool_name")
        self.assertEqual(attributes["mdisk_grp_name"], pool)
        vtype = getattr(storwize_svc.FLAGS, "storwize_svc_vol_vtype")
        self.assertEqual(attributes["type"], vtype)

        # Try to create the volume again (should fail)
        self.assertRaises(exception.ProcessExecutionError,
                self.driver.create_volume, volume)

        # Try to delete a volume that doesn't exist (should not fail)
        vol_no_exist = {"name": "i_dont_exist"}
        self.driver.delete_volume(vol_no_exist)
        # Ensure export for volume that doesn't exist (should not fail)
        self.driver.ensure_export(None, vol_no_exist)

        # Delete the volume
        self.driver.delete_volume(volume)

        # Check auto-expand option
        saved = storwize_svc.FLAGS.storwize_svc_vol_autoexpand
        storwize_svc.FLAGS.storwize_svc_vol_autoexpand = False
        self.driver.create_volume(volume)
        is_volume_defined = self.driver._is_volume_defined(volume["name"])
        self.assertEqual(is_volume_defined, True)
        self.driver.delete_volume(volume)
        storwize_svc.FLAGS.storwize_svc_vol_autoexpand = saved

    def test_storwize_svc_unicode_host_and_volume_names(self):
        volume1 = {}
        volume1["name"] = u"unicode1_volume%s" % random.randint(10000, 99999)
        volume1["size"] = 2
        volume1["id"] = 1
        self.driver.create_volume(volume1)
        # Make sure that the volumes have been created
        is_volume_defined = self.driver._is_volume_defined(volume1["name"])
        self.assertEqual(is_volume_defined, True)
        conn = {}
        conn["initiator"] = u"unicode:init:%s" % random.randint(10000, 99999)
        conn["ip"] = "10.10.10.10"  # Bogus ip for testing
        self.driver.initialize_connection(volume1, conn)
        self.driver.terminate_connection(volume1, conn)
        self.driver.delete_volume(volume1)

    def test_storwize_svc_host_maps(self):
        # Create two volumes to be used in mappings
        volume1 = {}
        volume1["name"] = "test1_volume%s" % random.randint(10000, 99999)
        volume1["size"] = 2
        volume1["id"] = 1
        self.driver.create_volume(volume1)
        volume2 = {}
        volume2["name"] = "test2_volume%s" % random.randint(10000, 99999)
        volume2["size"] = 2
        volume2["id"] = 1
        self.driver.create_volume(volume2)

        # Check case where no hosts exist
        if self.USESIM == 1:
            ret = self.driver._get_host_from_iscsiname("foo")
            self.assertEquals(ret, None)
            ret = self.driver._is_host_defined("foo")
            self.assertEquals(ret, False)

        # Make sure that the volumes have been created
        is_volume_defined = self.driver._is_volume_defined(volume1["name"])
        self.assertEqual(is_volume_defined, True)
        is_volume_defined = self.driver._is_volume_defined(volume2["name"])
        self.assertEqual(is_volume_defined, True)

        # Initialize connection from the first volume to a host
        # Add some characters to the initiator name that should be converted
        # when used for the host name
        conn = {}
        conn["initiator"] = "test:init:%s" % random.randint(10000, 99999)
        conn["ip"] = "10.10.10.10"  # Bogus ip for testing
        self.driver.initialize_connection(volume1, conn)

        # Initialize again, should notice it and do nothing
        self.driver.initialize_connection(volume1, conn)

        # Try to delete the 1st volume (should fail because it is mapped)
        self.assertRaises(exception.ProcessExecutionError,
                self.driver.delete_volume, volume1)

        # Test no preferred node
        self.driver.terminate_connection(volume1, conn)
        if self.USESIM == 1:
            self.sim.error_injection("lsvdisk", "no_pref_node")
            self.driver.initialize_connection(volume1, conn)

        # Initialize connection from the second volume to the host with no
        # preferred node set if in simulation mode, otherwise, just
        # another initialize connection.
        if self.USESIM == 1:
            self.sim.error_injection("lsvdisk", "blank_pref_node")
        self.driver.initialize_connection(volume2, conn)

        # Try to remove connection from host that doesn't exist (should fail)
        conn_no_exist = {"initiator": "i_dont_exist"}
        self.assertRaises(exception.VolumeBackendAPIException,
                self.driver.terminate_connection, volume1, conn_no_exist)

        # Try to remove connection from volume that isn't mapped (should print
        # message but NOT fail)
        vol_no_exist = {"name": "i_dont_exist"}
        self.driver.terminate_connection(vol_no_exist, conn)

        # Remove the mapping from the 1st volume and delete it
        self.driver.terminate_connection(volume1, conn)
        self.driver.delete_volume(volume1)
        vol_def = self.driver._is_volume_defined(volume1["name"])
        self.assertEqual(vol_def, False)

        # Make sure our host still exists
        host_name = self.driver._get_host_from_iscsiname(conn["initiator"])
        host_def = self.driver._is_host_defined(host_name)
        self.assertEquals(host_def, True)

        # Remove the mapping from the 2nd volume and delete it. The host should
        # be automatically removed because there are no more mappings.
        self.driver.terminate_connection(volume2, conn)
        self.driver.delete_volume(volume2)
        vol_def = self.driver._is_volume_defined(volume2["name"])
        self.assertEqual(vol_def, False)

        # Check if our host still exists (it should not)
        ret = self.driver._get_host_from_iscsiname(conn["initiator"])
        self.assertEquals(ret, None)
        ret = self.driver._is_host_defined(host_name)
        self.assertEquals(ret, False)

redpajama_set_name: RedPajamaGithub


text:
using namespace Urho3D;

void AppState_Benchmark01::OnEnter()
{
    assert(!scene_);
    LoadSceneXml("99_Benchmark/Scenes/Benchmark01.xml");

    GetSubsystem<Input>()->SetMouseVisible(false);
    SetupViewport();
    SubscribeToEvent(scene_, E_SCENEUPDATE, URHO3D_HANDLER(AppState_Benchmark01, HandleSceneUpdate));
    fpsCounter_.Clear();
}

void AppState_Benchmark01::OnLeave()
{
    UnsubscribeFromAllEvents();
    DestroyViewport();
    scene_ = nullptr;
}

void AppState_Benchmark01::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
{
    float timeStep = eventData[SceneUpdate::P_TIMESTEP].GetFloat();

    fpsCounter_.Update(timeStep);
    UpdateCurrentFpsElement();

    if (GetSubsystem<Input>()->GetKeyDown(KEY_ESCAPE))
    {
        GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
        return;
    }

    if (fpsCounter_.GetTotalTime() >= 25.f)
        GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_RESULTSCREEN);
}


text:
import simtk.unit as units

from intermol.decorators import accepts_compatible_units
from intermol.forces.abstract_bond_type import AbstractBondType

class ConnectionBondType(AbstractBondType):
    __slots__ = ['order', 'c']

    @accepts_compatible_units(None, None,
                              order=None,
                              c=None)
    def __init__(self, bondingtype1, bondingtype2,
                 order=1, c=False):
        AbstractBondType.__init__(self, bondingtype1, bondingtype2, order, c)

class ConnectionBond(ConnectionBondType):
    """
    stub documentation
    """
    def __init__(self, atom1, atom2, bondingtype1=None, bondingtype2=None,
                 order=1, c=False):
        self.atom1 = atom1
        self.atom2 = atom2
        ConnectionBondType.__init__(self, bondingtype1, bondingtype2,
                order=order, c=c)


text: public c<caret>lass Main { }


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.Common;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Task for pulling an SQL query into a stongly typed collection
    /// </summary>
    /// <typeparam name="TDatabaseHelperClass">
    /// The type for the database helper
    /// </typeparam>
    /// <typeparam name="TInformationClass">
    /// The type for the information class
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The type for the SQL Parameters
    /// </typeparam>
    /// <typeparam name="TAdoNetHelperClass">
    /// Type for the ADO.NET helper class
    /// </typeparam>
    public abstract class StronglyTypedCollection<TDatabaseHelperClass, TInformationClass, TParameterClass, TAdoNetHelperClass> :
        Base<StronglyTypedCollection<TInformationClass>, TDatabaseHelperClass, TParameterClass>
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TInformationClass : DataManager.Model.Info.Base<TInformationClass>, new()
        where TParameterClass : DbParameter
        where TAdoNetHelperClass : DataManager.Model.Helper.AdoNet.Base<TInformationClass>, new()
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StronglyTypedCollection{TDatabaseHelperClass,TInformationClass,TParameterClass,TAdoNetHelperClass}"/> class.
        /// </summary>
        /// <param name="taskName">
        /// The task name.
        /// </param>
        /// <param name="connectionString">
        /// The connection string.
        /// </param>
        protected StronglyTypedCollection(string taskName, string connectionString)
            : base(taskName, connectionString)
        {
        }

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override void OnProcess(StronglyTypedCollection<TInformationClass> result)
        {
            result.Result = this.DbHelper.GetStronglyTypedCollection<TInformationClass, TAdoNetHelperClass>(
                this.ConnectionString, this.GetSql(), this.GetParameters(), false);
        }
    }
}


text:
require 'spec_helper'
require 'application_helper'
require 'console_helper'
require 'droplet_helper'
require 'internet_availability_helper'
require 'logging_helper'
require 'java_buildpack/repository/configured_item'
require 'java_buildpack/util/cache/application_cache'
require 'java_buildpack/util/space_case'
require 'java_buildpack/util/tokenized_version'
require 'pathname'

shared_context 'with component help' do
  include_context 'with application help'
  include_context 'with console help'
  include_context 'with droplet help'
  include_context 'with internet availability help'
  include_context 'with logging help'

  let(:application_cache) { instance_double('ApplicationCache') }

  let(:component) { described_class.new context }
  let(:configuration) { {} }

  let(:context) do
    { application: application,
      component_name: described_class.to_s.split('::').last.space_case,
      configuration: configuration,
      droplet: droplet }
  end

  let(:uri) { 'test-uri' }
  let(:version) { '0.0.0' }

  # Mock application cache with cache fixture
  before do |example|
    allow(JavaBuildpack::Util::Cache::ApplicationCache).to receive(:new).and_return(application_cache)

    cache_fixture = example.metadata[:cache_fixture]
    if cache_fixture
      allow(application_cache).to receive(:get).with(uri)
                                               .and_yield(Pathname.new("spec/fixtures/#{cache_fixture}").open, false)
    end
  end

  # Mock repository
  before do
    tokenized_version = JavaBuildpack::Util::TokenizedVersion.new(version)

    allow(JavaBuildpack::Repository::ConfiguredItem).to receive(:find_item) do |&block|
      block&.call(tokenized_version)
    end.and_return([tokenized_version, uri])
  end

end


text:
package org.dcm4chex.wado.mbean.cache;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author franz.willer
 *
 * This Interface defines the basic methods of an WADO cache for DICOM images.
 *

 * The images are represented as File objects to allow streaming of the image data.
 */
public interface WADOCache {

	/**
	 * Get an image as BufferedImage from cache.
	 *

	 * This method returns the image from the default path of this cache.

	 *
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 *
	 * @return	The image if in cache or null.
	 */
	BufferedImage getImage( String studyUID, String seriesUID, String instanceUID );

	/**
	 * Get an image as file from cache.
	 *

	 * This method returns the image from the default path of this cache.

	 *
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 *
	 * @return	The File of the image if in cache or null.
	 */
	File getImageFile( String studyUID, String seriesUID, String instanceUID );

	/**
	 * Put an image as BufferedImage to the cache.
	 *

	 * Stores the image on the default path of this cache.
	 *
	 * @param image			The image.
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 *
	 * @return The File object of the image in this cache.
	 *
	 * @throws IOException
	 */
	File putImage( BufferedImage image, String studyUID, String seriesUID, String instanceUID ) throws IOException;

	/**
	 * Get an image of special size from cache.
	 *

	 * This method use a image size (rows and columns) to search on a special path of this cache.
	 *
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 * @param rows			Image height in pixel.
	 * @param columns		Image width in pixel.
	 *
	 * @return				The image if in cache or null.
	 */
	BufferedImage getImage( String studyUID, String seriesUID, String instanceUID, String rows, String columns );

	/**
	 * Get an image of special size from cache.
	 *

	 * This method use a image size (rows and columns) to search on a special path of this cache.
	 *
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 * @param rows			Image height in pixel.
	 * @param columns		Image width in pixel.
	 *
	 * @return				The File object of the image if in cache or null.
	 */
	File getImageFile( String studyUID, String seriesUID, String instanceUID, String rows, String columns );

	/**
	 * Put an image of special size to this cache.
	 *

	 * Stores the image on a special path of this cache.
	 *
	 * @param image			The image (with special size)
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 * @param rows			Image height in pixel.
	 * @param columns		Image width in pixel.
	 *
	 * @return The File object of the image in this cache.
	 * @throws IOException
     */
	File putImage( BufferedImage image, String studyUID, String seriesUID, String instanceUID, String pixelRows, String pixelColumns ) throws IOException;

	/**
	 * Puts a stream to this cache.
	 *
	 * @param stream		The InputStream to store.
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 * @param rows			Image height in pixel.
	 * @param columns		Image width in pixel.
	 *
	 * @return	The stored File object.
	 *
	 * @throws IOException
	 */
	File putStream( InputStream stream, String studyUID, String seriesUID, String instanceUID, String pixelRows, String pixelColumns ) throws IOException;

	/**
	 * Return the File object to get or store a file for given arguments.
	 *

	 * If the cache object referenced with arguments is'nt in this cache the returned file object
	 * exists() method will result false!
	 * @param studyUID		Unique identifier of the study.
	 * @param seriesUID		Unique identifier of the series.
	 * @param instanceUID	Unique identifier of the instance.
	 * @param key TODO
	 *
	 * @return File object to get or store a file.
	 */
	File getFileObject( String studyUID, String seriesUID, String instanceUID, String key );

	/**
	 * Clears this cache.
	 *

	 * Remove all images in this cache.
	 */
	void clearCache();

	/**
	 * Removes old entries to shrink this cache.
	 *
	 * @param background If true clean process runs in a seperate thread.
	 */
	void freeDiskSpace( boolean background );

	/**
	 * Setter for root directory of this cache.
	 *

	 * If a relative path is given, the resulting absolute path is relative to
	 * the servers home dir.
	 *
	 * @param newRoot The root directory for this cache.
	 */
	void setCacheRoot( String newRoot );

	/**
	 * Getter for root directory of this cache.
	 *

	 * This method returns always the same String used in setCacheRoot!
	 *
	 * @return The root directory for this cache (relative or absolute)
	 */
	String getCacheRoot();

	/**
	 * Getter for the absolute root directory of this cache.
	 *
	 * @return The root directory for this cache (absolute)
	 */
	File getAbsCacheRoot();

	/**
	 * Setter of minFreeSpace.
	 *
	 * @param minFreeSpace The minFreeSpace to set.
	 */
	public void setMinFreeSpace(long minFreeSpace);

	/**
	 * Returns the min drive space that must be available on the caches drive.
	 *

	 * This value is used to determine if this cache should be cleaned.
	 *
	 * @return Min allowed free diskspace in bytes.
	 */
	long getMinFreeSpace();

	/**
	 * Setter of preferredFreeSpace.
	 *
	 * @param preferredFreeSpace The preferredFreeSpace to set.
	 */
	void setPreferredFreeSpace(long preferredFreeSpace);

	/**
	 * Returns the free space that should be remain after cleaning the cache.
	 *

	 * This value is used as lower watermark of the cleaning process.
	 *
	 * @return Preferred free diskspace in bytes.
	 */
	long getPreferredFreeSpace();

	/**
	 * Returns the current free space on drive where this cache is stored.
	 *
	 * @return Current free diskspace in bytes
	 */
	long showFreeSpace();

	/**
	 * Returns true if a client redirect should be used if requewsted DICOM object is not local.
	 *
	 * @return True if client redirect should be used..
	 */
	public boolean isClientRedirect();

	/**
	 * Set the flag to determine if a client redirect should be used if  the requested DICOM object is not local.
	 *
	 * @param clientRedirect True for client side redirect, false for server side redirect.
	 */
	public void setClientRedirect(boolean clientRedirect);

	/**
	 * Returns true if a server side redirect should be cached.
	 *
	 * @return True if a server side redirected request should be cached.
	 */
	public boolean isRedirectCaching();

	/**
	 * Set the flag if caching is enabled for server side redirect.
	 *
	 * @param redirectCaching True to enable caching.
	 */
	public void setRedirectCaching(boolean redirectCaching);

}


text:
global.include = require('../lib/index').include;
global.define = require('../lib/index').define;

include('example.js', __dirname);

console.log(sprintf('%s v%s', example(), DECAUX_VERSION));
console.log(get_pi());
console.log(alt());


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Collections.Generic;
using System.Composition;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.LanguageServices.Implementation.Utilities;
using Microsoft.VisualStudio.Shell;
using IVsUIShell = Microsoft.VisualStudio.Shell.Interop.IVsUIShell;
using SVsUIShell = Microsoft.VisualStudio.Shell.Interop.SVsUIShell;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.PreviewPane
{
    [ExportWorkspaceServiceFactory(typeof(IPreviewPaneService), ServiceLayer.Host), Shared]
    internal class PreviewPaneService : ForegroundThreadAffinitizedObject, IPreviewPaneService, IWorkspaceServiceFactory
    {
        private readonly IVsUIShell _uiShell;

        [ImportingConstructor]
        [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
        public PreviewPaneService(IThreadingContext threadingContext, SVsServiceProvider serviceProvider)
            : base(threadingContext)
        {
            _uiShell = serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
        }

        IWorkspaceService IWorkspaceServiceFactory.CreateService(HostWorkspaceServices workspaceServices)
            => this;

        private static Image GetSeverityIconForDiagnostic(DiagnosticData diagnostic)
        {
            ImageMoniker? moniker = null;
            switch (diagnostic.Severity)
            {
                case DiagnosticSeverity.Error:
                    moniker = KnownMonikers.StatusError;
                    break;
                case DiagnosticSeverity.Warning:
                    moniker = KnownMonikers.StatusWarning;
                    break;
                case DiagnosticSeverity.Info:
                    moniker = KnownMonikers.StatusInformation;
                    break;
                case DiagnosticSeverity.Hidden:
                    moniker = KnownMonikers.StatusHidden;
                    break;
            }

            if (moniker.HasValue)
            {
                return new CrispImage
                {
                    Moniker = moniker.Value
                };
            }

            return null;
        }

        object IPreviewPaneService.GetPreviewPane(DiagnosticData data, IReadOnlyList<object> previewContent)
        {
            var title = data?.Message;

            if (string.IsNullOrWhiteSpace(title))
            {
                if (previewContent == null)
                {
                    // Bail out in cases where there is nothing to put in the header section
                    // of the preview pane and no preview content (i.e. no diff view) either.
                    return null;
                }

                return new PreviewPane(
                    severityIcon: null, id: null, title: null, description: null, helpLink: null, helpLinkToolTipText: null,
                    previewContent: previewContent, logIdVerbatimInTelemetry: false, uiShell: _uiShell);
            }

            var helpLinkUri = BrowserHelper.GetHelpLink(data);
            var helpLinkToolTip = BrowserHelper.GetHelpLinkToolTip(data.Id, helpLinkUri);

            Guid optionPageGuid = default;
            if (data.Properties.TryGetValue("OptionName", out var optionName))
            {
                data.Properties.TryGetValue("OptionLanguage", out var optionLanguage);
                optionPageGuid = GetOptionPageGuidForOptionName(optionName, optionLanguage);
            }

            return new PreviewPane(
                severityIcon: GetSeverityIconForDiagnostic(data),
                id: data.Id, title: title,
                description: data.Description.ToString(CultureInfo.CurrentUICulture),
                helpLink: helpLinkUri,
                helpLinkToolTipText: helpLinkToolTip,
                previewContent: previewContent,
                logIdVerbatimInTelemetry: data.CustomTags.Contains(WellKnownDiagnosticTags.Telemetry),
                uiShell: _uiShell,
                optionPageGuid: optionPageGuid);
        }

        private Guid GetOptionPageGuidForOptionName(string optionName, string optionLanguage)
        {
            if (optionName == nameof(NamingStyleOptions.NamingPreferences))
            {
                if (optionLanguage == LanguageNames.CSharp)
                {
                    return Guid.Parse(Guids.CSharpOptionPageNamingStyleIdString);
                }
                else if (optionLanguage == LanguageNames.VisualBasic)
                {
                    return Guid.Parse(Guids.VisualBasicOptionPageNamingStyleIdString);
                }
            }
            else if (optionName == nameof(CodeStyleOptions2.PreferIntrinsicPredefinedTypeKeywordInDeclaration))
            {
                if (optionLanguage == LanguageNames.CSharp)
                {
                    return Guid.Parse(Guids.CSharpOptionPageCodeStyleIdString);
                }
                else if (optionLanguage == LanguageNames.VisualBasic)
                {
                    return Guid.Parse(Guids.VisualBasicOptionPageVBSpecificIdString);
                }
            }

            return default;
        }
    }
}


text:
package br.ufrj.g2matricula.service.mapper;

import br.ufrj.g2matricula.domain.*;
import br.ufrj.g2matricula.service.dto.MateriaDTO;

import org.mapstruct.*;

/**
 * Mapper for the entity Materia and its DTO MateriaDTO.
 */
@Mapper(componentModel = "spring", uses = {CursoMapper.class})
public interface MateriaMapper extends EntityMapper<MateriaDTO, Materia> {

    @Mapping(source = "curso.id", target = "cursoId")
    @Mapping(source = "curso.nome", target = "cursoNome")
    MateriaDTO toDto(Materia materia);

    @Mapping(source = "cursoId", target = "curso")
    Materia toEntity(MateriaDTO materiaDTO);

    default Materia fromId(Long id) {
        if (id == null) {
            return null;
        }
        Materia materia = new Materia();
        materia.setId(id);
        return materia;
    }
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ask_amy.state_mgr package — ASK Amy  documentation</title>
    <link rel="stylesheet" href="../_static/sphinx13.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400,700'
          rel='stylesheet' type='text/css' />

Home

Get it

Docs

Extend/Develop

Navigation

          index

          modules |

ASK Amy home |

Documentation »

Table Of Contents

ask_amy.state_mgr package

Submodules

ask_amy.state_mgr.stack_dialog_mgr module

Module contents

This Page

Show Source

Quick search

    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>

ask_amy.state_mgr package¶

Submodules¶

ask_amy.state_mgr.stack_dialog_mgr module¶

class ask_amy.state_mgr.stack_dialog_mgr.StackDialogManager(dialog_dict=None)¶

Bases: ask_amy.core.default_dialog.DefaultDialog

confirmation_intent(confirmation)¶

get_expected_intent_for_data(data_name)¶

get_re_prompt_for_slot_data(data_name)¶

get_slot_data_details(data_name)¶

handle_session_end_confused()¶

Called if we are in an intent but don't have the info to move forward
and are not sure how or why alex called us here (obviously this should not
be a common occurrence.)
:return:

intent_attributes_to_request_attributes()¶

is_good_state()¶

Checks the state of the dialog and establish a conversation if this is the first
interaction on a multi step intent
:return:

is_valid_slot_data_type(name, value, type_validator)¶

Delegates to the appropriate validator if a type check is defined in the dialog_dict
:param name:
:param value:
:return:

need_valid_data(validation_errors)¶

no_intent()¶

peek_established_dialog()¶

peek at the current intent state
:return:

pop_established_dialog()¶

pop an intent state from the stack
:return:

push_established_dialog(intent_name)¶

Push an intent and its state  onto the stack
:param intent_name:
:return:

redirect_to_initialize_dialog(intent_name)¶

Simple redirect. We use this if an intent is called but a prior intent was expected
:param intent_name:
:return:

requested_value_intent()¶

required_fields_in_session_attributes_to_intent_attributes(required_fields)¶

Move session data into intent state data. This will stage the data we have already collected
that is required to execute the intent.
:return:

required_fields_process(required_fields)¶

review the required fields to process this intent if we have all the data move forward
if not create a reply that will call an appropriate intent to get the missing data
:param required_fields:
:return:

reset_established_dialog()¶

reset the whole intent dialog stack
:return:

slot_data_to_intent_attributes()¶

Move slot data into intent state data. This will collect the data required to
execute the initial intent. (i.e. similar to flow state in JSF)
:return:

yes_intent()¶

ask_amy.state_mgr.stack_dialog_mgr.required_fields(fields, user_managed=False)¶

Required fields decorator manages the state of the intent
:param fields:
:param user_managed:
:return:

ask_amy.state_mgr.stack_dialog_mgr.with_confirmation()¶

Required fields decorator manages the state of the intent
:return:

Module contents¶

Navigation

          index

          modules |

ASK Amy home |

Documentation »

        © Copyright 2017, Dan Higgins.
      Created using Sphinx 1.7.0.


text:
.. currentmodule:: statsmodels.iolib

.. _iolib:

Input-Output :mod:`iolib`
=========================

``statsmodels`` offers some functions for input and output. These include a
reader for STATA files, a class for generating tables for printing in several
formats and two helper functions for pickling.

Users can also leverage the powerful input/output functions provided by :ref:`pandas.io <pandas:io>`. Among other things, ``pandas`` (a ``statsmodels`` dependency) allows reading and writing to Excel, CSV, and HDF5 (PyTables).

Examples
--------

    `SimpleTable: Basic example <examples/generated/example_wls.html#ols-vs-wls>`_

Module Reference
----------------

.. autosummary::
   :toctree: generated/

   foreign.StataReader
   foreign.StataWriter
   foreign.genfromdta
   foreign.savetxt
   table.SimpleTable
   table.csv2st
   smpickle.save_pickle
   smpickle.load_pickle


text:
package com.dafrito.lua.script;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;

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

public class LuaScriptEngineTest {

	private LuaScriptEngine e;
	private LuaScriptEngineFactory factory;

	@Before
	public void setUp() {
		this.factory = new LuaScriptEngineFactory();
		this.e = (LuaScriptEngine)this.factory.getScriptEngine();
	}

	@Test
	public void testAnonymousEngineHasNoFactory() throws Exception {
		assertNull("Anonymous script engine has no factory", new LuaScriptEngine().getFactory());
	}

	@Test
	public void testEval() throws Exception {
		assertEquals(3.0, e.eval("a=2; return a+1"));
	}

	@Test
	public void testExplicitBindings() throws Exception {
		Bindings bindings = e.createBindings();
		assertNotNull(bindings);
		bindings.put("t", "42");
		assertEquals("42", bindings.get("t"));
	}

	@Test
	public void scriptEngineHasGlobalAndEngineBindings() throws Exception {
		assertNotNull(e.getBindings(ScriptContext.ENGINE_SCOPE));
		assertNotNull(e.getBindings(ScriptContext.GLOBAL_SCOPE));
		assertSame(e.getContext().getBindings(ScriptContext.ENGINE_SCOPE), e.getBindings(ScriptContext.ENGINE_SCOPE));
		assertSame(e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE), e.getBindings(ScriptContext.GLOBAL_SCOPE));
	}

	@Test
	public void testEngineBindingsCanBeChanged() throws Exception {
		Bindings b = e.createBindings();
		b.put("A", 2);
		e.setBindings(b, ScriptContext.ENGINE_SCOPE);
		assertEquals(2.0, e.get("A"));
	}

	@Test
	public void engineBindingsAreDefaultBindings() throws Exception {
		e.put("a", true);
		assertEquals(e.get("a"), e.getBindings(ScriptContext.ENGINE_SCOPE).get("a"));
	}

	@Test
	public void bindingsChangeScriptResults() throws Exception {
		e.put("a", true);
		assertEquals(true, e.get("a"));
		assertEquals(true, e.eval("return a"));
	}

	@Test
	public void testBindingsWithExplicitContext() throws Exception {
		Bindings bindings = e.createBindings();
		ScriptContext context = new SimpleScriptContext();

		context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
		bindings.put("a", true);
		assertEquals(true, e.eval("return a", context));

		bindings.put("a", false);
		assertEquals(false, e.eval("return a", context));

		bindings.remove("a");
		assertEquals(null, e.eval("return a", context));
	}

	@Test
	public void testAttributes() throws Exception {
		ScriptContext sc = e.getContext();
		sc.setAttribute("a", true, ScriptContext.ENGINE_SCOPE);
		assertEquals(true, sc.getAttribute("a"));
		assertEquals(true, e.eval("return a"));

		sc.removeAttribute("a", ScriptContext.ENGINE_SCOPE);
		assertEquals(null, sc.getAttribute("a"));
	}

	@Test
	public void testIOArguments() throws Exception {
		ScriptContext sc = e.getContext();
		assertSame(sc.getReader(), e.eval("return reader"));
		assertSame(sc.getWriter(), e.eval("return writer"));
		assertSame(sc.getErrorWriter(), e.eval("return errorWriter"));
	}

	@Test
	public void testPositionalArguments() throws Exception {
//		e.put(ScriptEngine.ARGV, new Object[] { 1.0d });
//		assertEquals(1.0d, e.eval("return ..."));
	}

	@Test
	public void testScriptExceptionFromSyntaxError() throws Exception {
//		e.put(ScriptEngine.FILENAME, "myfile");
//		try {
//			e.eval("a bad script");
//			fail("Syntax errors must cause exceptions");
//		} catch (ScriptException e) {
//			assertEquals("myfile", e.getFileName());
//			assertEquals(1, e.getLineNumber());
//		}
	}

	@Test
	public void testScriptExceptionFromExplicitError() throws Exception {
//		e.put(ScriptEngine.FILENAME, "myfile");
//		try {
//			e.eval("error(\"error\")");
//			fail("Explicit errors must cause exceptions");
//		} catch (ScriptException e) {
//			assertEquals("myfile", e.getFileName());
//			assertEquals(1, e.getLineNumber());
//		}
	}

	@Test
	public void testCompilable() throws Exception {
		if(!(e instanceof Compilable)) {
			return;
		}
		Compilable compilable = (Compilable) e;

		e.put("a", true);

		CompiledScript cs = compilable.compile("return a");
		assertEquals(true, cs.eval());

		Bindings b = new SimpleBindings();
		b.put("a", false);
		assertEquals(false, cs.eval(b));
	}

	@Test
	public void testInvocableFunction() throws Exception {
		if(!(e instanceof Invocable)) {
			return;
		}
		Invocable invocable = (Invocable) e;
		e.eval("function double(s) return s * 2 end");
		assertEquals(2.0d, invocable.invokeFunction("double", 1.0d));
	}

	@Test
	public void testInvocableMethod() throws Exception {
		if(!(e instanceof Invocable)) {
			return;
		}
		Invocable invocable = (Invocable) e;
		e.eval("runnable = { run = function() hasRun = true end }");
		assertEquals(null, e.get("hasRun"));
		invocable.invokeMethod(e.get("runnable"), "run");
		assertEquals(true, e.get("hasRun"));
	}

	@Test
	public void testInvocableInterfaceWithGlobalFunctions() throws Exception {
		if(!(e instanceof Invocable)) {
			return;
		}
		Invocable invocable = (Invocable) e;
		e.eval("function run() hasRun = true end }");
		assertEquals(null, e.get("hasRun"));
		Runnable runnable = invocable.getInterface(Runnable.class);
		runnable.run();
		assertEquals(true, e.get("hasRun"));
	}

	@Test
	public void testInvocableInterfaceWithObject() throws Exception {
		if(!(e instanceof Invocable)) {
			return;
		}
		Invocable invocable = (Invocable) e;
		e.eval("runnable = { run = function() hasRun = true end }");
		Object o = e.get("runnable");
		Runnable runnable = invocable.getInterface(o, Runnable.class);
		runnable.run();
		assertEquals(true, e.get("hasRun"));
	}
}


text:
module xlib.utils.database.column {

  var keywrods: string[] = [];

  export class Column {
    private _name: string = null;
    private _type: column.Type = null;
    private _default: any = null;
    constructor(options?: IOptions) {
      if (options && xlib.typeOf(options.name) !== 'undefined') {
        this.setName(options.name);
      }
      if (options && xlib.typeOf(options.type) !== 'undefined') {
        this.setType(options.type);
      }
      if (options && xlib.typeOf(options.defaults) !== 'undefined') {
        this.setDefault(options.defaults);
      }
    }
    getName(): string {
      return this._name;
    }
    setName(value: string): void {
      var temp: string = String(value || '');
      if (!/^[a-z_]\w*$/.test(temp)) {
        throw new Error('bla bla bla');
      } else if (keywrods.indexOf(temp) !== -1) {
        throw new Error('bla bla bla');
      }
      this._name = temp;
    }
    getType(): any {
      return this._type;
    }
    setType(value: any): void {
      var temp: string = String(value || '').toUpperCase(),
        keys: string[] = Object.keys(column.Type);
      if (value === null) {
        this._type = null;
      } else if (/^\d+$/.test(temp) &&
        keys.indexOf(temp) !== -1) {
        this._type = parseInt(temp);
      } else if (xlib.typeOf(value) === 'string' &&
        keys.indexOf(temp) !== -1) {
        this._type = column.Type[temp];
      } else {
        throw new Error('bla bla bla');
      }
    }

    getDefault(): any {
      return this._default;
    }
    setDefault(value: any): void {
      // todo: fix this
      this._default = value;
    }

  }

}