text:
"""Utilities for Federation Extension."""

import re

import jsonschema
from oslo.utils import timeutils
import six

from keystone.common import config
from keystone import exception
from keystone.i18n import _, _LW
from keystone.openstack.common import log

CONF = config.CONF
LOG = log.getLogger(__name__)

MAPPING_SCHEMA = {
    "type": "object",
    "required": ['rules'],
    "properties": {
        "rules": {
            "minItems": 1,
            "type": "array",
            "items": {
                "type": "object",
                "required": ['local', 'remote'],
                "additionalProperties": False,
                "properties": {
                    "local": {
                        "type": "array"
                    },
                    "remote": {
                        "minItems": 1,
                        "type": "array",
                        "items": {
                            "type": "object",
                            "oneOf": [
                                {"$ref": "#/definitions/empty"},
                                {"$ref": "#/definitions/any_one_of"},
                                {"$ref": "#/definitions/not_any_of"}
                            ],
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "empty": {
            "type": "object",
            "required": ['type'],
            "properties": {
                "type": {
                    "type": "string"
                },
            },
            "additionalProperties": False,
        },
        "any_one_of": {
            "type": "object",
            "additionalProperties": False,
            "required": ['type', 'any_one_of'],
            "properties": {
                "type": {
                    "type": "string"
                },
                "any_one_of": {
                    "type": "array"
                },
                "regex": {
                    "type": "boolean"
                }
            }
        },
        "not_any_of": {
            "type": "object",
            "additionalProperties": False,
            "required": ['type', 'not_any_of'],
            "properties": {
                "type": {
                    "type": "string"
                },
                "not_any_of": {
                    "type": "array"
                },
                "regex": {
                    "type": "boolean"
                }
            }
        }
    }
}

def validate_mapping_structure(ref):
    v = jsonschema.Draft4Validator(MAPPING_SCHEMA)

    messages = ''
    for error in sorted(v.iter_errors(ref), key=str):
        messages = messages + error.message + "\n"

    if messages:
        raise exception.ValidationError(messages)

def validate_expiration(token_ref):
    if timeutils.utcnow() > token_ref.expires:
        raise exception.Unauthorized(_('Federation token is expired'))

def validate_groups_cardinality(group_ids, mapping_id):
    """Check if groups list is non-empty.

    :param group_ids: list of group ids
    :type group_ids: list of str

    :raises exception.MissingGroups: if ``group_ids`` cardinality is 0

    """
    if not group_ids:
        raise exception.MissingGroups(mapping_id=mapping_id)

def validate_groups_in_backend(group_ids, mapping_id, identity_api):
    """Iterate over group ids and make sure they are present in the backend/

    This call is not transactional.
    :param group_ids: IDs of the groups to be checked
    :type group_ids: list of str

    :param mapping_id: id of the mapping used for this operation
    :type mapping_id: str

    :param identity_api: Identity Manager object used for communication with
                         backend
    :type identity_api: identity.Manager

    :raises: exception.MappedGroupNotFound

    """
    for group_id in group_ids:
        try:
            identity_api.get_group(group_id)
        except exception.GroupNotFound:
            raise exception.MappedGroupNotFound(
                group_id=group_id, mapping_id=mapping_id)

def validate_groups(group_ids, mapping_id, identity_api):
    """Check group ids cardinality and check their existence in the backend.

    This call is not transactional.
    :param group_ids: IDs of the groups to be checked
    :type group_ids: list of str

    :param mapping_id: id of the mapping used for this operation
    :type mapping_id: str

    :param identity_api: Identity Manager object used for communication with
                         backend
    :type identity_api: identity.Manager

    :raises: exception.MappedGroupNotFound
    :raises: exception.MissingGroups

    """
    validate_groups_cardinality(group_ids, mapping_id)
    validate_groups_in_backend(group_ids, mapping_id, identity_api)

# TODO(marek-denis): Optimize this function, so the number of calls to the
# backend are minimized.
def transform_to_group_ids(group_names, mapping_id,
                           identity_api, assignment_api):
    """Transform groups identitified by name/domain to their ids

    Function accepts list of groups identified by a name and domain giving
    a list of group ids in return.

    Example of group_names parameter::

        [
            {
                "name": "group_name",
                "domain": {
                    "id": "domain_id"
                },
            },
            {
                "name": "group_name_2",
                "domain": {
                    "name": "domain_name"
                }
            }
        ]

    :param group_names: list of group identified by name and its domain.
    :type group_names: list

    :param mapping_id: id of the mapping used for mapping assertion into
        local credentials
    :type mapping_id: str

    :param identity_api: identity_api object
    :param assignment_api: assignment_api object

    :returns: generator object with group ids

    :raises: excepton.MappedGroupNotFound: in case asked group doesn't
        exist in the backend.

    """

    def resolve_domain(domain):
        """Return domain id.

        Input is a dictionary with a domain identified either by a ``id`` or a
        ``name``. In the latter case system will attempt to fetch domain object
        from the backend.

        :returns: domain's id
        :rtype: str

        """
        domain_id = (domain.get('id') or
                     assignment_api.get_domain_by_name(
                     domain.get('name')).get('id'))
        return domain_id

    for group in group_names:
        try:
            group_dict = identity_api.get_group_by_name(
                group['name'], resolve_domain(group['domain']))
            yield group_dict['id']
        except exception.GroupNotFound:
            raise exception.MappedGroupNotFound(
                group_id=group['name'], mapping_id=mapping_id)

def get_assertion_params_from_env(context):
    LOG.debug('Environment variables: %s', context['environment'])
    prefix = CONF.federation.assertion_prefix
    for k, v in context['environment'].items():
        if k.startswith(prefix):
            yield (k, v)

class RuleProcessor(object):
    """A class to process assertions and mapping rules."""

    class _EvalType(object):
        """Mapping rule evaluation types."""
        ANY_ONE_OF = 'any_one_of'
        NOT_ANY_OF = 'not_any_of'

    def __init__(self, rules):
        """Initialize RuleProcessor.

        Example rules can be found at:
        :class:`keystone.tests.mapping_fixtures`

        :param rules: rules from a mapping
        :type rules: dict

        """

        self.rules = rules

    def process(self, assertion_data):
        """Transform assertion to a dictionary of user name and group ids
        based on mapping rules.

        This function will iterate through the mapping rules to find
        assertions that are valid.

        :param assertion_data: an assertion containing values from an IdP
        :type assertion_data: dict

        Example assertion_data::

            {
                'Email': 'testacct@example.com',
                'UserName': 'testacct',
                'FirstName': 'Test',
                'LastName': 'Account',
                'orgPersonType': 'Tester'
            }

        :returns: dictionary with user and group_ids

        The expected return structure is::

            {
                'name': 'foobar',
                'group_ids': ['abc123', 'def456'],
                'group_names': [
                    {
                        'name': 'group_name_1',
                        'domain': {
                            'name': 'domain1'
                        }
                    },
                    {
                        'name': 'group_name_1_1',
                        'domain': {
                            'name': 'domain1'
                        }
                    },
                    {
                        'name': 'group_name_2',
                        'domain': {
                            'id': 'xyz132'
                        }
                    }
                ]
            }

        """

        # Assertions will come in as string key-value pairs, and will use a
        # semi-colon to indicate multiple values, i.e. groups.
        # This will create a new dictionary where the values are arrays, and
        # any multiple values are stored in the arrays.
        LOG.debug('assertion data: %s', assertion_data)
        assertion = dict((n, v.split(';')) for n, v in assertion_data.items()
                         if isinstance(v, six.string_types))
        LOG.debug('assertion: %s', assertion)
        identity_values = []

        LOG.debug('rules: %s', self.rules)
        for rule in self.rules:
            direct_maps = self._verify_all_requirements(rule['remote'],
                                                        assertion)

            # If the compare comes back as None, then the rule did not apply
            # to the assertion data, go on to the next rule
            if direct_maps is None:
                continue

            # If there are no direct mappings, then add the local mapping
            # directly to the array of saved values. However, if there is
            # a direct mapping, then perform variable replacement.
            if not direct_maps:
                identity_values += rule['local']
            else:
                for local in rule['local']:
                    new_local = self._update_local_mapping(local, direct_maps)
                    identity_values.append(new_local)

        LOG.debug('identity_values: %s', identity_values)
        mapped_properties = self._transform(identity_values)
        LOG.debug('mapped_properties: %s', mapped_properties)
        return mapped_properties

    def _transform(self, identity_values):
        """Transform local mappings, to an easier to understand format.

        Transform the incoming array to generate the return value for
        the process function. Generating content for Keystone tokens will
        be easier if some pre-processing is done at this level.

        :param identity_values: local mapping from valid evaluations
        :type identity_values: array of dict

        Example identity_values::

            [{'group': {'id': '0cd5e9'}, 'user': {'email': 'bob@example.com'}}]

        :returns: dictionary with user name, group_ids and group_names.

        """

        def extract_groups(groups_by_domain):
            for groups in groups_by_domain.values():
                for group in {g['name']: g for g in groups}.values():
                    yield group

        # initialize the group_ids as a set to eliminate duplicates
        user_name = None
        group_ids = set()
        group_names = list()
        groups_by_domain = dict()

        for identity_value in identity_values:
            if 'user' in identity_value:
                # if a mapping outputs more than one user name, log it
                if user_name is not None:
                    LOG.warning(_LW('Ignoring user name %s'),
                                identity_value['user']['name'])
                else:
                    user_name = identity_value['user']['name']
            if 'group' in identity_value:
                group = identity_value['group']
                if 'id' in group:
                    group_ids.add(group['id'])
                elif 'name' in group:
                    domain = (group['domain'].get('name') or
                              group['domain'].get('id'))
                    groups_by_domain.setdefault(domain, list()).append(group)
                group_names.extend(extract_groups(groups_by_domain))

        return {'name': user_name,
                'group_ids': list(group_ids),
                'group_names': group_names}

    def _update_local_mapping(self, local, direct_maps):
        """Replace any {0}, {1} ... values with data from the assertion.

        :param local: local mapping reference that needs to be updated
        :type local: dict
        :param direct_maps: list of identity values, used to update local
        :type direct_maps: list

        Example local::

            {'user': {'name': '{0} {1}', 'email': '{2}'}}

        Example direct_maps::

            ['Bob', 'Thompson', 'bob@example.com']

        :returns: new local mapping reference with replaced values.

        The expected return structure is::

            {'user': {'name': 'Bob Thompson', 'email': 'bob@example.org'}}

        """

        LOG.debug('direct_maps: %s', direct_maps)
        LOG.debug('local: %s', local)
        new = {}
        for k, v in six.iteritems(local):
            if isinstance(v, dict):
                new_value = self._update_local_mapping(v, direct_maps)
            else:
                new_value = v.format(*direct_maps)
            new[k] = new_value
        return new

    def _verify_all_requirements(self, requirements, assertion):
        """Go through the remote requirements of a rule, and compare against
        the assertion.

        If a value of ``None`` is returned, the rule with this assertion
        doesn't apply.
        If an array of zero length is returned, then there are no direct
        mappings to be performed, but the rule is valid.
        Otherwise, then it will return the values, in order, to be directly
        mapped, again, the rule is valid.

        :param requirements: list of remote requirements from rules
        :type requirements: list

        Example requirements::

            [
                {
                    "type": "UserName"
                },
                {
                    "type": "orgPersonType",
                    "any_one_of": [
                        "Customer"
                    ]
                }
            ]

        :param assertion: dict of attributes from an IdP
        :type assertion: dict

        Example assertion::

            {
                'UserName': ['testacct'],
                'LastName': ['Account'],
                'orgPersonType': ['Tester'],
                'Email': ['testacct@example.com'],
                'FirstName': ['Test']
            }

        :returns: list of direct mappings or None.

        """

        direct_maps = []

        for requirement in requirements:
            requirement_type = requirement['type']
            regex = requirement.get('regex', False)

            any_one_values = requirement.get(self._EvalType.ANY_ONE_OF)
            if any_one_values is not None:
                if self._evaluate_requirement(any_one_values,
                                              requirement_type,
                                              self._EvalType.ANY_ONE_OF,
                                              regex,
                                              assertion):
                    continue
                else:
                    return None

            not_any_values = requirement.get(self._EvalType.NOT_ANY_OF)
            if not_any_values is not None:
                if self._evaluate_requirement(not_any_values,
                                              requirement_type,
                                              self._EvalType.NOT_ANY_OF,
                                              regex,
                                              assertion):
                    continue
                else:
                    return None

            # If 'any_one_of' or 'not_any_of' are not found, then values are
            # within 'type'. Attempt to find that 'type' within the assertion.
            direct_map_values = assertion.get(requirement_type)
            if direct_map_values:
                LOG.debug('updating a direct mapping: %s', direct_map_values)
                direct_maps += direct_map_values

        return direct_maps

    def _evaluate_requirement(self, values, requirement_type,
                              eval_type, regex, assertion):
        """Evaluate the incoming requirement and assertion.

        If the requirement type does not exist in the assertion data, then
        return False. If regex is specified, then compare the values and
        assertion values. Otherwise, grab the intersection of the values
        and use that to compare against the evaluation type.

        :param values: list of allowed values, defined in the requirement
        :type values: list
        :param requirement_type: key to look for in the assertion
        :type requirement_type: string
        :param eval_type: determine how to evaluate requirements
        :type eval_type: string
        :param regex: perform evaluation with regex
        :type regex: boolean
        :param assertion: dict of attributes from the IdP
        :type assertion: dict

        :returns: boolean, whether requirement is valid or not.

        """

        assertion_values = assertion.get(requirement_type)
        if not assertion_values:
            return False

        if regex:
            for value in values:
                for assertion_value in assertion_values:
                    if re.search(value, assertion_value):
                        return True
            return False

        any_match = bool(set(values).intersection(set(assertion_values)))
        if any_match and eval_type == self._EvalType.ANY_ONE_OF:
            return True
        if not any_match and eval_type == self._EvalType.NOT_ANY_OF:
            return True

        return False


text:
The maven plugin can be included in the pom.xml file of your artifact in the following way (requires maven >= 3.0.3):

```
<plugin>
	<groupId>com.github.siom79.japicmp</groupId>
	<artifactId>japicmp-maven-plugin</artifactId>
	<version>0.11.0</version>
	<configuration>
		<oldVersion>
			<dependency>
				<groupId>${project.groupId}</groupId>
				<artifactId>${project.artifactId}</artifactId>
				<version>${oldversion}</version>
				<type>jar</type>
			</dependency>
		</oldVersion>
		<newVersion>
			<file>
				<path>${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}</path>
			</file>
		</newVersion>
		<parameter>
			<!-- see documentation -->
		</parameter>
	</configuration>
	<executions>
		<execution>
			<phase>verify</phase>
			<goals>
				<goal>cmp</goal>
			</goals>
		</execution>
	</executions>
</plugin>
```

You can also leave out the <oldVersion> and <newVersion> elements:

```
<plugin>
	<groupId>com.github.siom79.japicmp</groupId>
	<artifactId>japicmp-maven-plugin</artifactId>
	<version>0.11.0</version>
	<configuration>
		<parameter>
			<!-- see documentation -->
		</parameter>
	</configuration>
	<executions>
		<execution>
			<phase>verify</phase>
			<goals>
				<goal>cmp</goal>
			</goals>
		</execution>
	</executions>
</plugin>
```

This way the plugin tries to determine the artifacts output (new version) and the latest released version (no SNAPSHOT version). If you need
to configure the latest version more precisely (e.g. only GA versions), then you can use the parameter <oldVersionPattern>:

```
<plugin>
	<groupId>com.github.siom79.japicmp</groupId>
	<artifactId>japicmp-maven-plugin</artifactId>
	<version>0.11.0</version>
	<configuration>
		<parameter>
        	<oldVersionPattern>\d+\.\d+\.\d+\.GA</oldVersionPattern>
        </parameter>
	</configuration>
	<executions>
		<execution>
			<phase>verify</phase>
			<goals>
				<goal>cmp</goal>
			</goals>
		</execution>
	</executions>
</plugin>
```

##Advance Usage##

An advanced configuration can utilize the following parameters:

```
<build>
	<plugins>
		<plugin>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-maven-plugin</artifactId>
			<version>0.11.0</version>
			<configuration>
				<oldVersion>
					<dependency>
						<groupId>japicmp</groupId>
						<artifactId>japicmp-test-v1</artifactId>
						<version>0.11.0</version>
						<type>jar</type>
					</dependency>
				</oldVersion>
				<newVersion>
					<file>
						<path>${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}</path>
					</file>
				</newVersion>
				<parameter>
					<onlyModified>true</onlyModified>
					<includes>
						<include>package.to.include</include>
						<include>package.ClassToInclude</include>
						<include>package.Class#methodToInclude(long,int)</include>
						<include>package.Class#fieldToInclude</include>
						<include>@my.AnnotationToInclude</include>
					</includes>
					<excludes>
						<exclude>package.to.exclude</exclude>
						<exclude>package.ClassToExclude</exclude>
						<exclude>package.Class#methodToExclude(long,int)</exclude>
						<exclude>package.Class#fieldToExclude</exclude>
						<exclude>@my.AnnotationToExcluce</exclude>
					</excludes>
					<includeExclusively>false</includeExclusively>
					<excludeExclusively>false</excludeExclusively>
					<accessModifier>public</accessModifier>
					<breakBuildOnModifications>false</breakBuildOnModifications>
					<breakBuildOnBinaryIncompatibleModifications>false</breakBuildOnBinaryIncompatibleModifications>
					<breakBuildOnSourceIncompatibleModifications>false</breakBuildOnSourceIncompatibleModifications>
					<breakBuildBasedOnSemanticVersioning>false</breakBuildBasedOnSemanticVersioning>
					<breakBuildBasedOnSemanticVersioningForMajorVersionZero>false</breakBuildBasedOnSemanticVersioningForMajorVersionZero>
					<breakBuildIfCausedByExclusion>true</breakBuildIfCausedByExclusion>
					<onlyBinaryIncompatible>false</onlyBinaryIncompatible>
					<includeSynthetic>false</includeSynthetic>
					<ignoreMissingClasses>false</ignoreMissingClasses>
					<ignoreMissingClassesByRegularExpressions>
						<ignoreMissingClassesByRegularExpression>org.apache.commons.math3.*</ignoreMissingClassesByRegularExpression>
					</ignoreMissingClassesByRegularExpressions>
					<skipPomModules>true</skipPomModules>
					<htmlStylesheet>path/to/stylesheet.css</htmlStylesheet>
					<htmlTitle>Title of report</htmlTitle>
					<noAnnotations>false</noAnnotations>
					<ignoreNonResolvableArtifacts>false</ignoreNonResolvableArtifacts>
					<packagingSupporteds>
						<packagingSupported>jar</packagingSupported>
					</packagingSupporteds>
					<postAnalysisScript>${project.basedir}/src/main/groovy/postAnalysisScript.groovy</postAnalysisScript>
					<reportOnlyFilename>false</reportOnlyFilename>
					<skipXmlReport>false</skipXmlReport>
					<skipHtmlReport>false</skipHtmlReport>
					<skipDiffReport>false</skipDiffReport>
					<includeModules>
						<includeModule>.*incl.*</includeModule>
					</includeModules>
					<excludeModules>
						<excludeModule>.*incl.*</excludeModule>
					</excludeModules>
				</parameter>
				<dependencies>
					<dependency>
						<groupId>org.apache.commons</groupId>
						<artifactId>commons-math3</artifactId>
						<version>3.4</version>
					</dependency>
				</dependencies>
				<skip>false</skip>
			</configuration>
			<executions>
				<execution>
					<phase>verify</phase>
					<goals>
						<goal>cmp</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>
```

The elements <oldVersion> and <newVersion> elements let you specify which version you want to compare. Both elements
support either a <dependency> or a <file> element. If necessary you can select the artifact by providing a <classifier> element inside
the <dependency> element. Through the <parameter> element you can provide the following options:

| Parameter | Optional | Default | Description |
|-----------|----------|---------|-------------|
| breakBuildOnModifications						         | true  | false		| If set to true, the build breaks in case a modification has been detected.|
| breakBuildOnBinaryIncompatibleModifications	         | true  | false		| If set to true, the build breaks in case a binary incompatible modification has been detected.|
| breakBuildOnSourceIncompatibleModifications	         | true  | false		| If set to true, the build breaks in case a source incompatible modification has been detected.|
| breakBuildBasedOnSemanticVersioning			         | true  | false		| If set to true, the plugin analyzes the versions of the old and new archives and decides based on these versions if binary compatible or incompatible changes are allowed or not. This option expects versions in the form Major.Minor.Patch (e.g. 1.2.3 or 1.2.3-SNAPSHOT).|
| breakBuildBasedOnSemanticVersioningForMajorVersionZero | true  | false        | If set to true, the plugin will apply the option breakBuildBasedOnSemanticVersioning also for projects with major version zero (which is not required by the Semantic Versioning Specification).|
| breakBuildIfCausedByExclusion					         | true  | true		    | If set to false, the plugin won't break the build if the incompatible change was caused by an excluded class (e.g. excluded interface removed from not excluded class).|
| onlyBinaryIncompatible						         | true  | false		| If set to true, only binary incompatible changes are reported.|
| onlyModified									         | true  | false 	    | Outputs only modified classes/methods. If not set to true, all classes and methods are printed.|
| includeSynthetic								         | true  | false		| If set to true, changes for synthetic classes and class members are tracked.|
| noAnnotations									         | true  | false		| Setting this option to true disables the evaluation of annotations completely.|
| reportOnlyFilename							         | true  | false		| If set to true, report will include only the artifact filename, not the complete artifact file path.|
| ignoreMissingClasses							         | true  | false		| If set to true, superclasses and interfaces that cannot be resolved are ignored. Pleases note that in this case the results for the affected classes may not be accurate.|
| ignoreMissingClassesByRegularExpressions		         | true	 | n.a.		    | List of regular expressions for superclasses and interfaces that cannot be resolved and that should be ignored. In contrast to the option ignoreMissingClasses, which ignores all missing classes, this options allows a fine-grained selection.|
| accessModifier								         | true  | protected	| Sets the access modifier level (public, package, protected, private).|
| includes										         | true  | n.a.  	    | List of package, classes, methods and field that should be included. The syntax is similar to the one used for javadoc references. Annotations can also be used for filtering, just let the fully qualified name start with @.|
| excludes										         | true  | n.a.		    | List of package, classes, methods and field that should be excluded. The syntax is similar to the one used for javadoc references. Annotations can also be used for filtering, just let the fully qualified name start with @.|
| includeExclusively								     | true  | false		| Include only packages specified in the "includes" parameter, exclude their sub-packages.|
| excludeExclusively								     | true  | false		| Exclude only packages specified in the "excludes" parameter, include their sub-packages.|
| htmlStylesheet								         | true  | n.a.		    | Path to an individual CSS stylesheet for the HTML report.|
| htmlTitle										         | true  | n.a.		    | A title for the HTML report (optional).|
| skipPomModules								         | true  | true		    | Setting this parameter to false (default: true) will not skip execution in modules with packaging type pom.|
| skip											         | true  | false		| Setting this parameter to true will skip execution of the plugin.|
| ignoreNonResolvableArtifacts					         | true  | false		| Set this to true in order to ignore artifacts that cannot be resolved, i.e. the build does not break in case a dependency cannot be resolved to a file.|
| ignoreMissingOldVersion						         | true  | false		| If set to true, not resolvable artifacts for the old version do not break the build.|
| ignoreMissingNewVersion						         | true  | false		| If set to true, not resolvable artifacts for the new version do not break the build.|
| packagingSupported							         | true  | n.a.			| List all packaging type for which the plugin should be executed. Helpful if you define the plugin in a root pom.|
| postAnalysisScript							         | true  | n.a.			| A [Groovy](http|//www.groovy-lang.org/) script that gets invoked after analysis is completed and before the output is written. This way it can be used to filter the output or break the build on specific conditions.|
| skipXmlReport									         | true  | false		| If set to true, no XML report will be generated.|
| skipHtmlReport								         | true  | false		| If set to true, no HTML report will be generated.|
| skipDiffReport								         | true  | false		| If set to true, no diff report will be generated.|
| oldVersionPattern								         | true  | n.a.			| If <oldVersion> is not used, the old version compared against must match this regular expression.|
| includeModules								         | true  | n.a.			| List of regular expression that specify if an artifact should be excluded based on its artifact id.|
| excludeModules								         | true  | n.a.			| List of regular expression that specify if an artifact should be included based on its artifact id.|

If your library implements interfaces or extends classes from other libraries than the JDK, you can add these dependencies by using the
<dependencies> element:

```
<dependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-math3</artifactId>
		<version>3.4</version>
	</dependency>
</dependencies>
```

Dependencies declared in the enclosing pom.xml and its parents are added automatically. The dependencies declared explicitly for this plugin
are appended to the classpath before the ones from the enclosing pom.xml, hence you can override them.

In case the classpath between both versions differs, you can add the dependencies for the new and old version separately:

```
<oldClassPathDependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-math3</artifactId>
		<version>3.4</version>
	</dependency>
</oldClassPathDependencies>
<newClassPathDependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-math3</artifactId>
		<version>3.5</version>
	</dependency>
</newClassPathDependencies>
```

The maven plugin produces the two files `japicmp.diff` and `japicmp.xml` within the directory `${project.build.directory}/japicmp`
of your artifact. If you run the plugin multiple times within the same module using the <executions> element, the reports
are named after the execution id.

##Site report##

Alternatively it can be used inside the `<reporting/>` tag in order to be invoked by the
[maven-site-plugin](https://maven.apache.org/plugins/maven-site-plugin/) and therewith to be integrated into the site report:

```
<reporting>
	<plugins>
		<plugin>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-maven-plugin</artifactId>
			<version>0.11.0</version>
			<reportSets>
				<reportSet>
					<reports>
						<report>cmp-report</report>
					</reports>
				</reportSet>
			</reportSets>
			<configuration>
				<!-- see above -->
			</configuration>
		</plugin>
	</plugins>
</reporting>
```
To create a summary report, you can also provide multiple old and new versions:

```
<configuration>
	<oldVersions>
		<dependency>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-test-v1</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-test2-v1</artifactId>
			<version>${project.version}</version>
		</dependency>
	</oldVersions>
	<newVersions>
		<dependency>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-test-v2</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>com.github.siom79.japicmp</groupId>
			<artifactId>japicmp-test2-v2</artifactId>
			<version>${project.version}</version>
		</dependency>
	</newVersions>
	...
</configuration>
```
The configuration above will create one report for all the declared dependencies.

##Using Groovy scripts for post analysis##

The parameter <postAnalysisScript/> can be used to invoke a [Groovy](http://www.groovy-lang.org/) script after the analysis but before the output is written.
This is helpful if you want to apply some custom filtering that is not possible with the standard means of japicmp. The following script for example filters out
all classes that reside within the package `japicmp.test.annotation` and all methods that start with `get...()` and `set...()`.

```
def it = jApiClasses.iterator()
while (it.hasNext()) {
	def jApiClass = it.next()
	def fqn = jApiClass.getFullyQualifiedName()
	if (fqn.startsWith("japicmp.test.annotation")) {
		it.remove()
	}
	def methodIt = jApiClass.getMethods().iterator()
	while (methodIt.hasNext()) {
		def method = methodIt.next()
		if (method.getName().startsWith("get") || method.getName().startsWith("set")) {
			methodIt.remove()
		}
	}
}
return jApiClasses
```

Please note that the script has to return a list of `JApiClass` objects, otherwise the maven plugin will report an error.

Beyond that the script can also be used to break the build on some project specific requirement. Let's assume that for the next release no classes within the package
`japicmp.test.annotation` should be modified in any way. The following Groovy script iterates over all classes and throws an exception if a class is not `UNCHANGED`.

```
import static japicmp.model.JApiChangeStatus.*

def it = jApiClasses.iterator()
while (it.hasNext()) {
	def jApiClass = it.next()
	def fqn = jApiClass.getFullyQualifiedName()
	if (fqn.startsWith("japicmp.test.annotation")) {
		if (jApiClass.getChangeStatus() != UNCHANGED) {
			throw new Exception("Class in package japicmp.test.annotation has been modified.")
		}
	}
}
return jApiClasses
```

redpajama_set_name: RedPajamaGithub


text: This is a simple quasiquoter to include Sass code in yesod.


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
org.springframework.scheduling Class Hierarchy (Spring Framework API 2.5)
</TITLE>

<META NAME="date" CONTENT="2007-11-19">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style">

      Overview
      Package
      Class
      Use
    Tree
      Deprecated
      Index
      Help

The Spring Framework

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Hierarchy For Package org.springframework.scheduling

Package Hierarchies:
All Packages

Class Hierarchy

java.lang.Object

java.lang.Throwable (implements java.io.Serializable)

java.lang.Exception

java.lang.RuntimeException

org.springframework.core.NestedRuntimeException

org.springframework.scheduling.SchedulingException

Interface Hierarchy

java.lang.Runnable

org.springframework.scheduling.SchedulingAwareRunnable

org.springframework.core.task.TaskExecutor

org.springframework.scheduling.SchedulingTaskExecutor

      Overview
      Package
      Class
      Use
    Tree
      Deprecated
      Index
      Help

The Spring Framework

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Copyright © 2002-2007 The Spring Framework.


text:
angular.module('mwUI.Inputs', ['mwUI.i18n', 'mwUI.Backbone']);

// @include ./directives/mw_checkbox.js
// @include ./directives/mw_checkbox_group.js
// @include ./directives/mw_input_defaults.js
// @include ./directives/mw_radio.js
// @include ./directives/mw_radio_group.js
// @include ./directives/mw_select.js
// @include ./directives/mw_select_box.js
// @include ./directives/mw_toggle.js

angular.module('mwUI.Inputs')
  .config(function(i18nProvider) {
    i18nProvider.addResource('mw-inputs/i18n', 'uikit');
  });


text:
/**
 * Based on https://github.com/zml2008/configurate/blob/0b6b95818a2cf19a6d46d46c09ec7d637a17b7b9/configurate-core/src/main/java/ninja/leaping/configurate/objectmapping/serialize/TypeSerializers.java
 * by zml and Configurate contributors
 *
 * modification of the EnumValueSerializer class to support non uppercase only enums by Malte Laukötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.lergin.laboruspixelmon;

import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;

public class CustomEnumTypeSerializer implements TypeSerializer<Enum> {
    @Override
    public Enum deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
        String enumConstant = value.getString();
        if (enumConstant == null) {
            throw new ObjectMappingException("No value present in node " + value);
        }

        Enum ret;
        try {
            ret = Enum.valueOf(type.getRawType().asSubclass(Enum.class), value.getString());
        } catch (IllegalArgumentException ex) {
            try {
                enumConstant = enumConstant.toUpperCase();

                ret = Enum.valueOf(type.getRawType().asSubclass(Enum.class), value.getString().toUpperCase());
            } catch (IllegalArgumentException e) {
                throw new ObjectMappingException("Invalid enum constant provided for " + value.getKey() + ": Expected a value of enum " + type + ", got " + enumConstant);
            }
        }

        return ret;
    }

    @Override
    public void serialize(TypeToken<?> type, Enum obj, ConfigurationNode value) throws ObjectMappingException {
        value.setValue(obj.name());
    }
}


text:
package org.basex.query.expr.path;

import org.basex.query.*;
import org.basex.query.expr.*;
import org.basex.query.iter.*;
import org.basex.query.value.*;
import org.basex.query.value.item.*;
import org.basex.query.value.node.*;
import org.basex.query.value.seq.*;
import org.basex.query.var.*;
import org.basex.util.*;
import org.basex.util.hash.*;

/**
 * Iterative step expression with one or more simple numeric predicates.
 *
 * @author BaseX Team 2005-22, BSD License
 * @author Christian Gruen
 */
public final class IterPosStep extends Step {
  /**
   * Constructor.
   * @param info input info
   * @param axis axis
   * @param test node test
   * @param preds predicates
   */
  IterPosStep(final InputInfo info, final Axis axis, final Test test, final Expr... preds) {
    super(info, axis, test, preds);
  }

  @Override
  public NodeIter iter(final QueryContext qc) {
    return new NodeIter() {
      final CmpPos[] posExpr = new CmpPos[exprs.length];
      final long[] cPos = new long[exprs.length];
      BasicNodeIter iter;
      boolean skip;

      @Override
      public ANode next() throws QueryException {
        if(skip) return null;
        if(iter == null) {
          iter = axis.iter(checkNode(qc));
          final int el = exprs.length;
          for(int e = 0; e < el; e++) {
            final Expr expr = exprs[e];
            if(expr instanceof CmpPos) {
              posExpr[e] = (CmpPos) expr;
            } else if(numeric(expr)) {
              // pre-evaluate numeric position
              final Item item = expr.item(qc, info);
              if(item == Empty.VALUE) return null;
              final Expr ex = IntPos.get(toDouble(item), info);
              if(!(ex instanceof CmpPos)) return null;
              posExpr[e] = (CmpPos) ex;
            }
          }
        }

        for(final ANode node : iter) {
          qc.checkStop();
          if(test.matches(node) && preds(node)) return node.finish();
        }
        return null;
      }

      private boolean preds(final ANode node) throws QueryException {
        final QueryFocus qf = qc.focus;
        final Value cv = qf.value;
        qf.value = node;
        try {
          final int pl = exprs.length;
          for(int p = 0; p < pl; p++) {
            final Expr pred = exprs[p];
            final CmpPos pos = posExpr[p];
            if(pos == null) {
              final Item tst = pred.test(qc, info);
              if(tst == null) return false;
            } else {
              final long ps = ++cPos[p];
              final int t = pos.test(ps, qc);
              if(t == 0) return false;
              if(t == 2) skip = true;
            }
          }
        } finally {
          qf.value = cv;
        }
        return true;
      }
    };
  }

  @Override
  public Step copy(final CompileContext cc, final IntObjMap<Var> vm) {
    return copyType(new IterPosStep(info, axis, test.copy(), Arr.copyAll(cc, vm, exprs)));
  }
}


text:
@implementation OverlayViewController

@synthesize captureManager;

- (void)viewDidLoad {

	[self setCaptureManager:[[CaptureSessionManager alloc] init] ];

	[[self captureManager] addVideoInput];

	[[self captureManager] addVideoPreviewLayer];
	CGRect layerRect = [[[self view] layer] bounds];
	[[[self captureManager] previewLayer] setBounds:layerRect];
	[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                                CGRectGetMidY(layerRect))];
	[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

	[[captureManager captureSession] startRunning];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

@end


text:
namespace Miruken.Map
{
    using System.Linq;
    using Callback;
    using Callback.Policy;
    using Callback.Policy.Bindings;

    public class Maps : CategoryAttribute
    {
        public Maps()
        {
            Strict = true;
        }

        public override CallbackPolicy CallbackPolicy => Policy;

        public override bool Approve(object callback, PolicyMemberBinding binding)
        {
            var mapping = (Mapping)callback;
            var format  = mapping.Format;
            if (format == null) return true;
            var dispatch = binding.Dispatcher;
            return dispatch.Attributes.OfType<IFormatMatching>()
                       .Any(f => f.Matches(format))
                || dispatch.Owner.Attributes.OfType<IFormatMatching>()
                       .Any(f => f.Matches(format));
        }

        public static void AddFilters(params IFilterProvider[] providers) =>
            Policy.AddFilters(providers);

        public static readonly CallbackPolicy Policy =
            BivariantPolicy.Create<Mapping>(m => m.Type, m => m.Source,
                x => x.MatchMethod(x.ReturnKey.Alias("to"),
                          x.Target, x.Extract(m => m.Target).Alias("to"))
                      .MatchCallbackMethod(x.ReturnKey, x.Target)
                      .MatchMethod(x.ReturnKey.OrVoid, x.Callback)
            );
    }
}


text:
<?php

class Ess_M2ePro_Model_Resource_Ebay_Account_PickupStore_Log_Collection
    extends Ess_M2ePro_Model_Resource_Collection_Abstract
{
    //########################################

    public function _construct()
    {
        parent::_construct();
        $this->_init('M2ePro/Ebay_Account_PickupStore_Log');
    }

    //########################################
}


text:
using System;
using System.Collections.Generic;
using System.Text;
using wojilu.Members.Users.Domain;
using wojilu.Members.Interface;

namespace wojilu.Members.Common.Service {

    public class AdminLogService<T> : IAdminLogService<T> where T : ObjectBase<T>, IAdminLog {

        public DataPage<T> GetPage( int pageSize ) {
            return db.findPage<T>( "", pageSize );
        }

        public DataPage<T> GetPage( String condition ) {
            return db.findPage<T>( condition, 20 );
        }

        private void Add( IAdminLog log ) {
            db.insert( log );
        }

        public void Add( User user, String action, String ip ) {
            this.Add( user, action, ip, 0 );
        }

        public void Add( User user, String action, String ip, int categoryId ) {
            this.Add( user, action, "", "", ip, categoryId );
        }

        public void Add( User user, String action, String dataInfo, String dataType, String ip ) {
            this.Add( user, action, dataInfo, dataType, ip, 0 );
        }

        private void Add( User user, String action, String dataInfo, String dataType, String ip, int categoryId ) {
            IAdminLog log = Entity.New( typeof( T ).FullName ) as IAdminLog;
            log.User = user;
            log.Message = action;
            log.Ip = ip;
            log.CategoryId = categoryId;
            log.DataInfo = dataInfo;
            log.DataType = dataType;

            this.Add( log );
        }

    }
}


text:
function varargout = Dynamics(varargin)
% DYNAMICS M-file for Dynamics.fig
%      DYNAMICS, by itself, creates a new DYNAMICS or raises the existing
%      singleton*.
%
%      H = DYNAMICS returns the handle to a new DYNAMICS or the handle to
%      the existing singleton*.
%
%      DYNAMICS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DYNAMICS.M with the given input arguments.
%
%      DYNAMICS('Property','Value',...) creates a new DYNAMICS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Dynamics_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Dynamics_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Dynamics_OpeningFcn, ...
                   'gui_OutputFcn',  @Dynamics_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function Dynamics_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

guidata(hObject, handles);

function varargout = Dynamics_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.compute.fluent.models;

import com.azure.core.annotation.Fluent;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.compute.models.InstanceViewStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/** The RunCommandResult model. */
@Fluent
public final class RunCommandResultInner {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(RunCommandResultInner.class);

    /*
     * Run command operation response.
     */
    @JsonProperty(value = "value")
    private List<InstanceViewStatus> value;

    /**
     * Get the value property: Run command operation response.
     *
     * @return the value value.
     */
    public List<InstanceViewStatus> value() {
        return this.value;
    }

    /**
     * Set the value property: Run command operation response.
     *
     * @param value the value value to set.
     * @return the RunCommandResultInner object itself.
     */
    public RunCommandResultInner withValue(List<InstanceViewStatus> value) {
        this.value = value;
        return this;
    }

    /**
     * Validates the instance.
     *
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (value() != null) {
            value().forEach(e -> e.validate());
        }
    }
}


text:
/* Generated by Font Squirrel (http://www.fontsquirrel.com) on January 31, 2013 04:24:29 PM America/New_York */

@font-face {
    font-family: 'RalewayThin';
    src: url('raleway_thin-webfont.eot');
    src: url('raleway_thin-webfont.eot?#iefix') format('embedded-opentype'),
         url('raleway_thin-webfont.woff') format('woff'),
         url('raleway_thin-webfont.ttf') format('truetype'),
         url('raleway_thin-webfont.svg#RalewayThin') format('svg');
    font-weight: normal;
    font-style: normal;

}


text:
"""
    tests/test_email_queue.py

"""
import time
import unittest
import threading
import Queue
from functools import wraps

from mock import patch
from pretend import stub
from faker import Faker

from trytond.tests.test_tryton import (
    POOL, USER, DB_NAME, CONTEXT, ModuleTestCase
)
from trytond.transaction import Transaction
from trytond.cache import Cache
from trytond import backend
import trytond.tests.test_tryton

def clear_email_queue(function):
    @wraps(function)
    def wrapper(*args, **kwargs):
        EmailQueue = POOL.get('email.queue')
        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            EmailQueue.delete(EmailQueue.search([]))
            transaction.commit()
        return function(*args, **kwargs)
    return wrapper

class BadSMTPServerException(Exception):
    pass

class TestEmailQueue(ModuleTestCase):
    '''
    Test Email Queue
    '''

    module = 'email_queue'

    @patch("smtplib.SMTP")
    def test_0010_send_mails(self, mock_smtp):
        """
        Tests send_mails functionality.
        """
        EmailQueue = POOL.get('email.queue')

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            # Put some emails in queue
            f = Faker()
            for item in xrange(10):
                EmailQueue.queue_mail(f.email(), f.email(), f.text())

            transaction.commit()

            self.assertEqual(EmailQueue.search([], count=True), 10)
            self.assertEqual(
                EmailQueue.search([('state', '=', 'outbox')], count=True), 10
            )

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            # Run cron method to send mails
            EmailQueue.send_all()

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            self.assertEqual(
                EmailQueue.search([('state', '=', 'sent')], count=True), 10
            )

    @patch("smtplib.SMTP")
    @clear_email_queue
    def test_0015_max_attempts(self, mock_smtp):
        """
        After five attempts email state changes to failed.
        """
        EmailQueue = POOL.get('email.queue')

        # Mock sendmail to raise exception
        mock_smtp.return_value.sendmail.side_effect = BadSMTPServerException()

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            # Put some emails in queue
            f = Faker()
            for item in xrange(10):
                EmailQueue.queue_mail(f.email(), f.email(), f.text())

            transaction.commit()

            self.assertEqual(
                EmailQueue.search([('state', '=', 'outbox')], count=True), 10
            )

        # Try sending the emails. The first 3 attempts will result in
        # failures and the email should then be in failed state.
        for i in xrange(3):
            with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
                # Run cron method to send mails
                with self.assertRaises(BadSMTPServerException):
                    EmailQueue.send_all()

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            self.assertEqual(
                EmailQueue.search([('state', '=', 'failed')], count=True), 1
            )
            self.assertEqual(
                EmailQueue.search([('state', '=', 'outbox')], count=True), 9
            )

        # Lets make the smtp server work again ;) now the remaining emails
        # should be sent well while the failed one remains failed
        mock_smtp.return_value.sendmail.side_effect = None

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            EmailQueue.send_all()

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            self.assertEqual(
                EmailQueue.search([('state', '=', 'failed')], count=True), 1
            )
            self.assertEqual(
                EmailQueue.search([('state', '=', 'outbox')], count=True), 0
            )
            self.assertEqual(
                EmailQueue.search([('state', '=', 'sent')], count=True), 9
            )

    @unittest.skipIf(
        backend.name() == 'sqlite', "Skip txn safety test on SQlite"
    )
    @clear_email_queue
    def test_9999_transaction_safety(self):
        """
        Test the transaction safety of email sender.

        * This test is expected to work only on postgres
        * This should be the last test since this breaks the rule to commit
          within the test creating records
        """
        EmailQueue = POOL.get('email.queue')

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            # Put some emails in queue
            f = Faker()
            for item in xrange(10):
                EmailQueue.queue_mail(f.email(), f.email(), f.text())

            transaction.commit()

        # A queue is used to handle the ones which errored.
        searialization_error_q = Queue.Queue(3)

        # A fake smtp server which just sleeps for 5 seconds when sendmail
        # is called.
        smtp_server = stub(sendmail=lambda *args: time.sleep(5))

        def threaded_send_email(email, smtp_server):
            """
            A new threaded email sender. This is required because there is
            no transaction in the new thread that is spawned and sendemail
            tries to create a new cursor from an existing transaction.

            So create the new transaction here, refresh the active record
            objects and call sendmail like the cron would have
            """
            with Transaction().start(DB_NAME, USER, context=CONTEXT):
                # email active record is from old transaction, so referesh it.
                email = EmailQueue(email.id)

                database = backend.get('database')

                try:
                    # Now send the email
                    email.send(smtp_server)
                except database.DatabaseOperationalError:
                    # This specific email could not be sent because of a
                    # transaction serialization error
                    searialization_error_q.put(email.id)
                finally:
                    Cache.drop(DB_NAME)

        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            email1, email2 = EmailQueue.search(
                [('state', '=', 'outbox')], limit=2
            )

        t1 = threading.Thread(
            target=threaded_send_email, args=(email1, smtp_server)
        )
        t2 = threading.Thread(
            target=threaded_send_email, args=(email2, smtp_server)
        )
        # create another thread with **email1** again. This is expected to
        # fail, though even t1 might fail and this would succeed. Either
        # way we dont care because we only make sure that there is 1
        # failure and that both email1 and 2 are sent.
        t3 = threading.Thread(
            target=threaded_send_email, args=(email1, smtp_server)
        )

        # start all the threads. Since there is a time.sleep of 5 seconds
        # in the sendmail call, it simulates a case of delayed execution.
        # thread3 is guaranteed to start within 5 seconds of thread1 and
        # the error that is asserted also specifically looks for a
        # concurrency triggered transaction serialisation exception.
        t1.start()
        t2.start()
        t3.start()

        # Blockingly wait till the threads complete
        t1.join()
        t2.join()
        t3.join()

        # 1: Assert that the email1's ID is in the serialization_error_q
        self.assertEqual(searialization_error_q.qsize(), 1)

        # 1B: Ensure that the ID is of email1 which was the one sent twice
        self.assertEqual(searialization_error_q.get(), email1.id)

        # 2: Assert that both email 1 and 2 have the sent state
        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            self.assertEqual(EmailQueue(email1.id).state, 'sent')
            self.assertEqual(EmailQueue(email2.id).state, 'sent')

        # 3: Assert that there are 8 emails left in outbox
        with Transaction().start(DB_NAME, USER, CONTEXT) as transaction:
            self.assertEqual(
                EmailQueue.search([('state', '=', 'outbox')], count=True), 8
            )

def suite():
    """
    Define suite
    """
    test_suite = trytond.tests.test_tryton.suite()
    test_suite.addTests(
        unittest.TestLoader().loadTestsFromTestCase(TestEmailQueue)
    )
    return test_suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())


text:
const products = ['products']

const cartProducts = [
  ['products'],
  ['cart', 'itemQty'],
  (products, itemQty) => {
    return itemQty.map((quantity, productId) => {
      var product = products.get(productId)
      return product
        .set('quantity', quantity)
        .remove('inventory') // inventory shouldnt be known in cart
    }).toList()
  }
]

const cartTotal = [
  cartProducts,
  (items) => {
    const total = items.reduce((total, item) => {
      return total + (item.get('quantity')* item.get('price'))
    }, 0) || 0
    return total.toFixed(2)
  }
]

export default { products, cartProducts, cartTotal }


text:
void
cli_setup_readline();

char*
cli_readline(char* prompt);

#endif


text:
define(function (require, exports) {
	'use strict';

	const oop = require('../lib/oop');
	const TextMode = require('./text').Mode;
	const NSISHighlightRules = require('./nsis_highlight_rules').NSISHighlightRules;

	// TODO: pick appropriate fold mode
	const FoldMode = require('./folding/cstyle').FoldMode;

	const Mode = function () {
		this.HighlightRules = NSISHighlightRules;
		this.foldingRules = new FoldMode();
	};

	oop.inherits(Mode, TextMode);

	(function () {
		this.lineCommentStart = [';', '#'];
		this.blockComment = { start: '/*', end: '*/' };
		this.$id = 'ace/mode/nsis';
	}.call(Mode.prototype));

	exports.Mode = Mode;
});


text:
#region Usings

using System;
using NoSQL.GraphDB.Model;

#endregion

namespace NoSQL.GraphDB
{
    /// <summary>
    ///   Fallen8 write interface.
    /// </summary>
    public interface IWrite
    {
        #region create

        /// <summary>
        ///   Creates a vertex
        /// </summary>
        /// <param name="creationDate"> The creation date as Unix timestamp (seconds from 01/01/1971) </param>
        /// <param name="properties"> The properties. </param>
        /// <returns> The created vertex </returns>
        VertexModel CreateVertex(UInt32 creationDate, PropertyContainer[] properties = null);

        /// <summary>
        ///   Creates an edge.
        /// </summary>
        /// <returns> The edge model. </returns>
        /// <param name='sourceVertexId'> Source vertex identifier. </param>
        /// <param name='edgePropertyId'> Edge property identifier. </param>
        /// <param name='targetVertexId'> Target vertex identifier. </param>
        /// <param name='creationDate'> The creation date as Unix timestamp (seconds from 01/01/1971) </param>
        /// <param name='properties'> Properties. </param>
        EdgeModel CreateEdge(Int32 sourceVertexId, UInt16 edgePropertyId, Int32 targetVertexId, UInt32 creationDate,
                             PropertyContainer[] properties = null);

        #endregion

        #region update

        /// <summary>
        ///   Tries to add a property.
        /// </summary>
        /// <returns> <c>true</c> if the property was added; otherwise, <c>false</c> . </returns>
        /// <param name='graphElementId'> Graph element identifier. </param>
        /// <param name='propertyId'> Property identifier. </param>
        /// <param name='property'> The to be added property. </param>
        Boolean TryAddProperty(Int32 graphElementId, UInt16 propertyId, Object property);

        /// <summary>
        ///   Tries to remove a property.
        /// </summary>
        /// <returns> <c>true</c> if the property was removed; otherwise, <c>false</c> . </returns>
        /// <param name='graphElementId'> Graph element identifier. </param>
        /// <param name='propertyId'> Property identifier. </param>
        Boolean TryRemoveProperty(Int32 graphElementId, UInt16 propertyId);

        #endregion

        #region delete

        /// <summary>
        ///   Tries the remove graph element.
        /// </summary>
        /// <returns> <c>true</c> if the graph element was removed; otherwise, <c>false</c> . </returns>
        /// <param name='graphElementId'> Graph element identifier. </param>
        Boolean TryRemoveGraphElement(Int32 graphElementId);

        #endregion

        #region Tabula rasa

        /// <summary>
        ///   Put the database in its initial state.
        /// </summary>
        void TabulaRasa();

        #endregion

        #region Trim

        /// <summary>
        ///   Trims Fallen-8 to its minimum memory usage
        /// </summary>
        void Trim();

        #endregion

        #region Load

        /// <summary>
        /// Load a Fallen-8 from a specified path
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="startServices">Start the services?</param>
        void Load(String path, Boolean startServices = false);

        #endregion
    }
}


text:
package wait;

/**
 * Created by Ivan Sliusar on 03.11.2017.
 * Red Line Soft corp.
 */
public class Work implements Runnable {
    /**
     * Name of work.
     */
    private String nameOfWork;

    public Work(String nameOfWork) {
        this.nameOfWork = nameOfWork;
    }

    /**
     * Override run.
     */
    @Override
    public void run() {
        System.out.println("Work " + this.nameOfWork + " start");
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Getter.
     * @return String
     */
    public String getNameOfWork() {
        return nameOfWork;
    }
}


text:
/*
 -------------------------------------------------------------------------------
 search.h
 PlanningProject1 Project

 DESCRIPTION:
    description...

 FILES:
    search.h
    search.cpp

 DEPENDENCIES:

 FUNCTIONS:
    type method1(type arg1);
        Description... .
            arg1 - description... .

 NOTES:

 EXAMPLES:
    Example 1: description
    ----------------------------------------------------------------------------
    code...
    ----------------------------------------------------------------------------

 VERSIONS:
    1.0 - 9/19/12 - Rowland O'Flaherty, Tapomayukh Bhattacharjee, and Daehyung Park

 -------------------------------------------------------------------------------
 */

#ifndef _search_h_
#define _search_h_

//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "sokoban.h"
#include "sokobanState.h"
#include "output.h"
#include <vector>
#include <iterator>

#include <algorithm>
using namespace std;

//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
vector<sokoban::moveType> search(sokoban, int (*algorithm)(sokoban &, int (*heuristic)(sokoban &), vector<sokobanState> &, vector<sokoban::moveType> &), int (*heuristic)(sokoban &) );

//------------------------------------------------------------------------------
// Helper Typedefs and Functions -----------------------------------------------
typedef std::pair<sokoban::moveType, int> moveCost; // Cost associated with a move
typedef std::pair<sokobanState, sokobanState> cameFrom; // The second state is a parent to the first
typedef std::pair<sokobanState, int> stateCost; // Cost associated with the a state

bool moveComparator( const moveCost &l, const moveCost &r);

//------------------------------------------------------------------------------
// Heuristic Algorithms --------------------------------------------------------

// Zero heuristic --------------------------------------------------------------
int zero(sokoban &game);

// Heuristic 1 dist --------------------------------------------------------------
int heuristic1(sokoban &game);

// Heuristic 2 dist --------------------------------------------------------------
int heuristic2(sokoban &game);

// Heuristic 1 dist with no stuck corners ----------------------------------------
int heuristic1NoCorners(sokoban &game);

// Heuristic 2 dist with no stuck corners ----------------------------------------
int heuristic2NoCorners(sokoban &game);

//------------------------------------------------------------------------------
// Search Algorithms -----------------------------------------------------------

// Depth first search ----------------------------------------------------------
int depthFirst(sokoban &game, int (*heuristic)(sokoban &), vector<sokobanState> &stateList,  vector<sokoban::moveType> &moveList);

// Best first search -----------------------------------------------------------

int bestFirst(sokoban &game, int (*heuristic)(sokoban &), vector<sokobanState> &stateList,  vector<sokoban::moveType> &moveList);

// A star search ---------------------------------------------------------------

int AStar(sokoban &game, int (*heuristic)(sokoban &), vector<sokobanState> &stateList,  vector<sokoban::moveType> &moveList);
void lowestCostState(vector<sokobanState>::iterator &bestStateIt, vector<sokobanState> &stateSet, vector<stateCost> &stateCosts);
int getStateScore(sokobanState &aState, vector<stateCost> &stateCosts);
void reconstructePath(sokobanState &aState, vector<cameFrom> &cameFromMapping, vector<sokoban::moveType> &cameFromMoves, vector<sokoban::moveType> &moveList);

#endif


text:
import textGeneratorBackend
import unittest

class TestNormalWorking(unittest.TestCase):
    def setUp(self):
        self.o = textGeneratorBackend.StatisticsTextGenerator(
            penalty=10, gramN=3, wordNum=18)

    def test_wordsWillBeRepeatedAfterExhaustion(self):
        self.assertEqual(
            self.o.generateText(['A', 'b', 'c', 'A', 'b']),
            ['A', 'b', 'c', 'A', 'b', 'c', 'A', 'b', 'c',
             'A', 'b', 'c', 'A', 'b', 'c', 'A', 'b', 'c'])

    def test_wordIsChoosenFromListWhenNoNgrmaIsAvaliable(self):
        # Note: this came from the actual result.
        self.assertEqual(
            self.o.generateText(['A', 'b', 'c']),
            ['A', 'b', 'c', 'b', 'c', 'A', 'b', 'c',
             'A', 'b', 'c', 'A', 'b', 'c', 'A', 'b', 'c', 'A'])

class TestDegenerateCases(unittest.TestCase):
    def setUp(self):
        self.o = textGeneratorBackend.StatisticsTextGenerator(
            penalty=10, gramN=4, wordNum=1940)

    def test_tooFewWords(self):
        # What to do in this case?
        self.assertEqual(
            self.o.generateText(['A', 'b']),
            [])

if __name__ == '__main__':
    unittest.main()


text:
import dilap.core.model as dmo
import dilap.core.tools as dpr
import dilap.core.vector as dpv

import pdb

class cone(dmo.model):

    def __init__(self,*args,**kwargs):
        dmo.model.__init__(self,*args,**kwargs)
        self._geo(**kwargs)

    def _geo(self,n = 8):
        bottom = dpr.point_ring(1,n)
        dpv.translate_coords_z(bottom,-0.5)
        top = dpv.center_of_mass(bottom).translate_z(1)
        bottom.reverse()
        self._tripie(bottom)
        bottom.append(bottom[0])
        bottom.reverse()
        self._trifan(top,bottom)


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.zalando.stups</groupId>
    <artifactId>booties-reactor</artifactId>
    <version>0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>booties-parent-poms/booties-parent</module>
        <module>data-jpa-eclipselink-spring-boot-starter</module>
        <module>guava-eventbus-spring-boot-starter</module>
        <module>stups-twintip-spring-boot-starter</module>
        <module>tomcat-proxies-spring-boot-starter</module>
        <module>workarounds-spring-boot</module>
        <module>misc/github-client-parent</module>
        <module>misc/junit-postgres</module>
        <module>misc/junit-redis</module>
        <module>misc/mcb</module>
        <module>async-spring-boot-starter</module>
        <module>booties-examples</module>
    </modules>

    <properties>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <coveralls.dryRun>true</coveralls.dryRun>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eluder.coveralls</groupId>
                <artifactId>coveralls-maven-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <dryRun>${coveralls.dryRun}</dryRun>
                    <jacocoReports>
                        <jacocoReport>spring-boot-zalando-stups-twintip/target/site/jacoco-it/jacoco.xml</jacocoReport>
                        <jacocoReport>spring-boot-starter-tomcat-proxies/target/site/jacoco-it/jacoco.xml</jacocoReport>
                        <jacocoReport>spring-boot-starter-guava-eventbus/target/site/jacoco-it/jacoco.xml</jacocoReport>
                        <jacocoReport>spring-boot-starter-data-jpa-eclipselink/target/site/jacoco-it/jacoco.xml</jacocoReport>
                    </jacocoReports>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>travis</id>
            <activation>
                <property>
                    <name>TRAVIS</name>
                    <value>true</value>
                </property>
            </activation>
            <properties>
                <coveralls.dryRun>false</coveralls.dryRun>
            </properties>
        </profile>
    </profiles>

    <scm>
        <url>https://github.com/zalando-stups/booties.git</url>
        <connection>scm:git:https://github.com/zalando-stups/booties.git</connection>
        <developerConnection>scm:git:https://github.com/zalando-stups/booties.git</developerConnection>
        <tag>HEAD</tag>
    </scm>
</project>


text:
var http = require('http')
var createHandler = require('github-webhook-handler')
require('shelljs/global')
var winston =   require('winston')

var config = require('./config').config

var DOC_SOURCE_PATH = config.DOC_SOURCE_PATH
var SERVER_SECRET = config.SERVER_SECRET
var DOC_BRANCH = config.DOC_BRANCH
var DEPLOY_BRANCH = config.DEPLOY_BRANCH
var DEPLOY_PATH = config.DEPLOY_PATH
var SERVER_PATH = config.SERVER_PATH
var TRIGGER_GIT_REF = config.TRIGGER_GIT_REF

winston.level = config.LOG_LEVEL
winston.add(winston.transports.File, {filename: config.LOG_FILENAME})

function documentUpdate(){
    if (!which('git')) {
        echo('Sorry, this script requires git');
        exit(1);
    }

    if (!which('gitbook')) {
        echo('Sorry, this script requires gitbook');
        exit(1);
    }

    cd(DOC_SOURCE_PATH)
    exec('git reset --hard')
    exec('git checkout ' + DOC_BRANCH )
    exec('git checkout -- .')

    var res = exec('git pull origin ' + DOC_BRANCH)
    if (res.code != 0 ){
        winston.error(res.stderr)
        return
    }

    winston.debug("source pull success")

    var res = exec('gitbook build')
    if (res.code != 0 ){
        winston.error(res.stderr)
        return
    }
    rm("-rf" , tempdir() + "/_book" )
    mv("_book" , tempdir() )
    exec('git checkout ' + DEPLOY_BRANCH )
    exec('git checkout -- .')
    exec('git reset --hard')
    var res = exec('git pull origin ' + DEPLOY_BRANCH )
    if (res.code != 0 ){
        winston.error(res.stderr)
        return
    }
    winston.debug("doc pull success")

    cd(DEPLOY_PATH)
    cp("-rf" , tempdir() + "/_book" + "/*"  , "." )

    exec("git add  -A;")
    exec("git commit -m '* [site] document auto deployment' ")
    exec("git push origin " +  DEPLOY_BRANCH )
    winston.info("doc deploy over")
}

var handler = createHandler({ path: SERVER_PATH , secret: SERVER_SECRET })

http.createServer(function (req, res) {
  handler(req, res, function (err) {
    res.statusCode = 202
    res.end('deloy doc only')
  })
}).listen(7778)

handler.on('error', function (err) {
  winston.error('Error:', err.message)
})

var IN_TRANSACTION = false
handler.on('push', function (event) {
    winston.info('Received a push event for %s to %s',
                 event.payload.repository.name,
                 event.payload.ref)

    if (TRIGGER_GIT_REF == event.payload.ref){
        winston.info("start deployment transaction")

        if (IN_TRANSACTION){
            winston.error('got push event in deployment TRANSACTION')
            return
        }else{
            IN_TRANSACTION = true
            winston.debug('IN_TRANSACTION true')

            documentUpdate()

            IN_TRANSACTION = false
            winston.debug('IN_TRANSACTION false')
        }
    }
})


text:
input_dir = ARGV.shift
output_dir = ARGV.shift
unless output_dir
    output_dir = input_dir
end

eml_files = []
Dir.foreach(input_dir) do |file_name|
    if file_name !~ /^\./
        eml_files << file_name
    end
end

puts eml_files.inspect if $DEBUG

eml_files.each do |file_name|
    puts "Input File: #{input_dir}#{file_name}" if $DEBUG
    lines = File.readlines("#{input_dir}#{file_name}")
    while true
        puts lines[0] if $DEBUG
        if lines[0] !~ /^>From/
            lines.shift
        else
            lines.shift
            break
        end
    end
    out_file = File.new("#{output_dir}alt-#{file_name}", "w")
    lines.each do |line|
        out_file.write(line)
    end
    out_file.close
end


text:
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({

  fetchAuth(databaseName, tableName) {
    const url = this.buildURL() + '/system/ranger/auth';
    return this.ajax(url, "GET", {data: {database: databaseName, table: tableName}});
  }
});


text:
import { IInt64, Int64 } from '../../main'

/**
 * Test class compatible with Int64.
 */
export class LikeInt64 implements IInt64 {
    /** @inheritDoc */
    public readonly buffer: Buffer

    private readonly i64: Int64

    constructor(value: string) {
        this.i64 = Int64.fromDecimalString(value)
        this.buffer = this.i64.buffer
    }

    /** @inheritDoc */
    public toDecimalString(): string {
        return this.i64.toDecimalString()
    }
}

/**
 * Test class with no buffer.
 */
export class NoBufferInt64 implements Omit<IInt64, 'buffer'> {
    private readonly i64: Int64

    constructor(value: string) {
        this.i64 = Int64.fromDecimalString(value)
    }

    /** @inheritDoc */
    public toDecimalString(): string {
        return this.i64.toDecimalString()
    }
}

/**
 * Test class with no toDecimalString().
 */
export class NoStringInt64 implements Omit<IInt64, 'toDecimalString'> {
    /** @inheritDoc */
    public readonly buffer: Buffer

    private readonly i64: Int64

    constructor(value: string) {
        this.i64 = Int64.fromDecimalString(value)
        this.buffer = this.i64.buffer
    }
}

/**
 * Test class with a shorter buffer.
 */
export class Int56 implements IInt64 {
    /** @inheritDoc */
    public readonly buffer: Buffer

    private readonly i64: Int64

    constructor(value: string) {
        this.i64 = Int64.fromDecimalString(value)
        this.buffer = this.i64.buffer.slice(1)
    }

    /** @inheritDoc */
    public toDecimalString(): string {
        return this.i64.toDecimalString()
    }
}

/**
 * Test class with a longer buffer.
 */
export class Int72 implements IInt64 {
    /** @inheritDoc */
    public readonly buffer: Buffer

    private readonly i64: Int64

    constructor(value: string) {
        this.i64 = Int64.fromDecimalString(value)
        this.buffer = Buffer.alloc(9)
        this.i64.buffer.copy(this.buffer, 1)
    }

    /** @inheritDoc */
    public toDecimalString(): string {
        return this.i64.toDecimalString()
    }
}