
es.upm.dit.gsi.beast.reader.mas.CreateMASTestStory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of beast-tool Show documentation
Show all versions of beast-tool Show documentation
BEhavioural Agents Simple Testing Tool - BEAST Tool
The aim of this project is the development of a system which allows Behavior Driven Development (BDD) in Multi-Agent Systems (MAS), to make testing practices more accessible and intuitive to everybody.
In one hand, in order to let tests be writable by newcomers and experts alike, system must allow the redaction of tests in plain text, because client does not need to have knowledge of our code. This plain text will be traduced to software later.
The definition of test will be realized with the terminology Given-When-Then, which allows trace an easy guide of the behavior of a given scenario when something happened.
In the other hand, due to the complexity of MAS, making unit testing of an agent that needs the interaction with others is almost impossible until the whole system is finished. This implies to leave testing issues to the end of the project, generating big troubles in case of malfunction. Consequently, its necessary to carry out a tool to allow the creation of mock agents and to perform tests during the whole development process. Therefore another objective of our systems is to include a mocking tool which permits testing continuously.
Definitively, our tool allows the testing of any MAS in the development process, increasing its modularity and decreasing its elaboration and testing cost. These tests will be written in plain text so that anyone would be able to understand them.
For further reading, a paper published in ITMAS2012 workshop can be found in: http://scholar.google.es/citations?view_op=view_citation&hl=es&user=mT3KgXUAAAAJ&citation_for_view=mT3KgXUAAAAJ:Tyk-4Ss8FVUC
The newest version!
package es.upm.dit.gsi.beast.reader.mas;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Logger;
import es.upm.dit.gsi.beast.exception.BeastException;
/**
* Project: beast
* File: es.upm.dit.gsi.beast.reader.mas.CreateMASTestStory.java
*
* Grupo de Sistemas Inteligentes
* Departamento de Ingeniería de Sistemas Telemáticos
* Universidad Politécnica de Madrid (UPM)
*
* @author Alberto Mardomingo
* @author Jorge Solitario
*
* @author alvarocarrera
* @email [email protected]
* @twitter @alvarocarrera
* @version 0.1
*
*/
public class CreateMASTestStory {
/**
* Method to create the java file that it's executed from caseManager. Its
* name comes from the Scenario that it's testing. Its behaviour is written
* in the .story file allocated in the same folder, which is the plain text
* given by the client.
*
* @param story_name - the name of the Story
* @param platform_name - the name of the platform
* @param package_path - the package path
* @param dest_dir the main folder (typically src/main/java)
* @param loggingPropFile
* @param storyUser - The user launching the Story
* @param userFeature - The feature requested by the user
* @param userBenefit - The benefit the feature will provide
* @param scenarios - A list with the tests to launch in the testSuite.
* @throws BeastException
*/
public static void createMASTestStory(String story_name,
String platform_name, String package_path, String dest_dir,
String loggingPropFile, String storyUser,
String userFeature, String userBenefit, HashMap scenarios) throws BeastException {
Logger logger = Logger.getLogger(CreateMASTestStory.class.getName());
File f = MASReader.createFolder(package_path, dest_dir);
String storyClass = MASReader.createClassName(story_name);
File javaFile = new File(f, storyClass + ".java");
try {
FileWriter fw = new FileWriter(javaFile);
fw.write("package " + package_path + ";\n");
fw.write("\n");
// Adds all the necessary imports.
fw.write("import es.upm.dit.gsi.beast.story.logging.LogActivator;\n");
fw.write("import es.upm.dit.gsi.beast.story.BeastTestCaseRunner;\n");
fw.write("import java.io.FileInputStream;\n");
fw.write("import java.io.IOException;\n");
fw.write("import java.util.logging.Logger;\n");
fw.write("import java.util.logging.Level;\n");
fw.write("import java.util.logging.LogManager;\n");
fw.write("import java.util.Properties;\n");
fw.write("import org.junit.Test;\n");
fw.write("\n");
// Class header
fw.write("/**\n");
fw.write(" * Main class to translate plain text into code, following the Given-When-Then\n");
fw.write(" * language. In the GIVEN part it launchs the platform In the WHEN part it\n");
fw.write(" * configures the state of its agents. In the THEN part it checks the correct\n");
fw.write(" * behaviour. The main purpose of it consists of knowing agents' state/properties\n");
fw.write(" * without changing its code.\n");
fw.write(" * \n");
fw.write(" * \n");
fw.write(" * This \"AgentStory\" is described as follows:\n");
fw.write(" * Story: " + story_name + "\n");
fw.write(" * As a " + storyUser + "\n");
fw.write(" * I want to " + userFeature + "\n");
fw.write(" * So that " + userBenefit + "\n");
fw.write(" * \n");
fw.write(" * @author es.upm.dit.gsi.beast\n");
fw.write(" */\n");
fw.write("public class " + storyClass + "{\n");
fw.write("\n");
fw.write(" public Logger logger = Logger.getLogger("
+ storyClass + ".class.getName());\n");
fw.write("\n");
// Creates the constructor
fw.write(" /**\n");
fw.write(" * Constructor to configure logging\n");
fw.write(" */\n");
fw.write(" public " + storyClass + "() {\n");
if (loggingPropFile == null) {
// If there is no properties, creates a "Standard" logger.
fw.write(" LogActivator.logToFile(logger, "
+ storyClass + ".class.getName(), Level.ALL);\n");
} else {
fw.write(" Properties preferences = new Properties();\n");
fw.write(" try {\n");
fw.write(" FileInputStream configFile = new FileInputStream(\""
+ loggingPropFile + "\");\n");
fw.write(" preferences.load(configFile);\n");
fw.write(" LogManager.getLogManager().readConfiguration(configFile);\n");
fw.write(" LogActivator.logToFile(logger, "
+ storyClass + ".class.getName(), Level.ALL);\n");
fw.write(" } catch (IOException ex) {\n");
fw.write(" logger.severe(\"WARNING: Could not open configuration file\");\n");
fw.write(" }\n");
}
fw.write(" }\n");
fw.write("\n");
// Run each test
// Remenber; scenarios:
// { scenarioID1 => ["Given", "When", "then"],
// scenarioID2 => ["Given", "When", "then"], ...}
// BE AWARE: The scenarios are plain test WITH SPACES!!
for(String scenario : scenarios.keySet()){
fw.write(" /**\n");
fw.write(" * This is the scenario: " + scenario
+ "\n");
fw.write(" * where the GIVEN is described as: "
+ scenarios.get(scenario)[0] + "\n");
fw.write(" * the WHEN is described as: " + scenarios.get(scenario)[1]
+ "\n");
fw.write(" * and the THEN is described as: "
+ scenarios.get(scenario)[2] + "\n");
fw.write(" */\n");
fw.write(" @Test\n");
fw.write(" public void " + MASReader.changeFirstLetterToLowerCase(
MASReader.createClassName(scenario)) + "() {\n");
fw.write(" BeastTestCaseRunner.executeBeastTestCase(\"" +
package_path + "." + MASReader.createFirstLowCaseName(story_name) +
"." + MASReader.createClassName(scenario) + "\");\n");
fw.write(" }\n");
}
// This is how I can launch the jbehave test from a TestSuite,
// since they are not junit TestCases.
// This is not my fault, don't kill me, please.
// Ends the class.
// You don't say.
fw.write("\n");
fw.write("}\n");
fw.write("\n");
fw.flush();
fw.close();
// logger.info(scenario_name+" has been created in "+dest_dir+" "+Reader.createFolderPath(package_path));
} catch (IOException e) {
logger.severe("ERROR: The file " + story_name
+ ".java can not be writed");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy