GithubHelp home page GithubHelp logo

mutation-tool-for-annotations's Introduction

Mutation Tool for Annotations

Build Status codecov

This a lib to mutate code annotations from Java or attributes from C# source code. From a set of operators, this will create one javaMutant for each change on original code, related to code annotations. For example, consider the following code (Java):

@RequestMapping(value = "/foo")
public String form() {
  return "form";
}

The ADAT (Add Attribute) operator can create the following javaMutants:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String form() {
  return "form";
}
@RequestMapping(value = "/foo", method = RequestMethod.GET)
public String form() {
  return "form";
}

With these javaMutants, the developer can evaluate your test suite. The ultimate goal is kill these javaMutants with tests, improving the test suite. This project was based on theory of Mutation Testing. For more details, see An Analysis and Survey of the Development of Mutation Testing and Mutation Operators for Code Annotations.

Operators

This project have a set of 9 operators:

Operator Description
ADA Add annotation
ADAT Add attributes to annotation
CHODR Change order of annotations
RMA Remove annotations
RMAT Remove attributes of annotations
RPA Replace one annotation for another
RPAT Replace one attribute for another
RPAV Replace one attribute value for another
SWTG Change location of one annotation

To use this project, it is necessary to choose one or more of these operators.

Configuration

You will need the srcML tool: https://www.srcml.org/. Install on your OS.

First, create a Maven project with java.

On pom.xml, Add the Jitpack repository:

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

And the Dependency:

<dependencies>
  <dependency>
    <groupId>com.github.easy-software-ufal</groupId>
    <artifactId>mutation-tool-for-annotations</artifactId>
    <version>Tag</version>
  </dependency>
</dependencies>

Replace the Tag with target release.

How to use

First you need to fill the annotations.json of the project (located on the config folder). This is a configuration file, with information of annotations. The following code is a example of this file:

{
  "annotations": [
    {
      "annotation": "@org.springframework.web.bind.annotation.RequestMapping",
      "replaceableBy": [],
      "targets": ["type", "method"],
      "attributes": [
        {
          "name": "value",
          "type": "java.lang.String[]",
          "validValues": ["\"/ex/foo\""],
          "default": "true"
        },
        {
          "name": "method",
          "type": "org.springframework.web.bind.annotation.RequestMethod[]",
          "validValues": ["org.springframework.web.bind.annotation.RequestMethod.POST"]
        },
        {
          "name": "headers",
          "type": "java.lang.String[]",
          "validValues": ["\"content-type=text/*\""]
        }
      ]
    },
    {
      "annotation": "@org.springframework.beans.factory.annotation.Autowired",
      "replaceableBy": ["@org.springframework.beans.factory.annotation.Qualifier"],
      "targets": ["field", "method"],
      "attributes": [{}]
    },
    {
      "annotation": "@org.springframework.beans.factory.annotation.Qualifier",
      "replaceableBy": ["@org.springframework.beans.factory.annotation.Autowired"],
      "targets": ["field", "parameter"],
      "attributes": [{}]
    }
  ]
}

For each annotation, we have the following fields:

annotation: the base of annotation, with @, the package and his name. Mandatory.

replaceableBy: list of replacements of the annotation. Each replacement is a base of annotation, such as the annotation field. Optional.

targets: list of targets of the annotation. Targets are locations where the annotations can be placed. valid values: type, method, field, parameter. Mandatory (at least one target).

attributes: list of attributes of annotations. Optional.

For each attribute, we have the following fields:

name: name of the attribute. Mandatory.

type: type of the attribute. Can be any Java type. Mandatory.

validValues: list of valid values. Mandatory.

default: this mark the attribute as default, which means that the annotation will receive only the value without the attribute name. Only works when the annotation have one attribute. Can be true or false. Optional.

This file is quite similar for C#. The name of attribute include the namespace, without the @.


After fill the annotations.json, it is necessary to create a Java code to use this project to generate the javaMutants. The following code is an example:

MutationToolConfig config = new MutationToolConfig(new File(SOURCE_PATH));
config.setProjectName(PROJECT_NAME);
config.getOperators().addAll(Arrays.asList(OperatorsEnum.RMA, OperatorsEnum.RMAT));
config.setThreads(2);
config.setLanguage(Language.JAVA) // or Language.C_SHARP
new MutationTool(config).run();

Basic configuration

The constructor of the MutationToolConfig class have the following field:

pathSources: directory of the source code. This doesn't include the test code.

This class have the following methods:

setProjectName: set the name of original project.

setThreads: set the number of threads.

setLanguage: set the language to generate the mutants (Java or C#).

getOperators: get the list of operators.

Setting the operators

The method getOperators of MutationToolConfig class retrieve the list of operators. It is possible to add operators to this list:

Add all operators

config.getOperators().addAll(Arrays.asList(OperatorsEnum.values()));

Add some operators

config.getOperators().addAll(Arrays.asList(OperatorsEnum.RMA, OperatorsEnum.RMAT));

Generate the javaMutants

The last thing is the use the method run of the MutationTool class:

new MutationTool(config).run();

All the javaMutants will be generated in the data/javaMutants folder.

mutation-tool-for-annotations's People

Contributors

jcarlosadm avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jcarlosadm

mutation-tool-for-annotations's Issues

Create new JSON config file

Create new JSON configuration file on basis of current JSON configuration files and the new model:

{
    "annotation": "@Consumes",
    "replaceableBy": ["@Produces"],
    "targets": ["type", "method"],
    "attributes": [{
        "name": "value",
        "type": "string[]",
        "validValues": ["image/jpeg", "image/png"]
    }]
}

Implement the AnnotationInfo class

Implement the following AnnotationInfo class (on package mutation.tool.util.json):

AnnotationInfo

  • name:String
  • replaceableBy:List<String>
  • targets:List<InsertionPoint>
  • attributes:List<AttributeInfo>

AttributeInfo

  • name:String
  • type:String
  • validValues:List<String>

Some operators may identify or catch wrong annotations

Each annotation have a name and a package. Operators who need the configuration file don't identify the package information, that is, they can identify or capture an annotation by name, but not by package.

One possible solution is to consider the name of the annotation and imports in the source file.

Affected operators: ADA, ADAT, RPA, RPAT, RPAV, SWTG.

Add AnnotationInfos in MutationToolConfig

Add AnnotationInfos object in MutationToolConfig class. Instantiate this field on constructor. If occur any error, must be null, and related operators must be removed (warning on log).

Change Context to Target

Change the class Context to Target. And package context to target. Rename all related instances (maybe Intellij idea will do it automatically).

rename tests

rename tests to begin with "test". Necessary change to maven catch tests.

check src and test folders

Verify src and test folders, both settled by user, on method init on class MutationTool.
Basic checks:

  • The folders exists?
  • Exists intersection between the folders?

Create log class

Create log class, or use some lib to do that. maybe a report class

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.