Changeset 1929 for trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe
- Timestamp:
- 03/20/15 16:00:10 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtilsTest.java
r1927 r1929 17 17 import java.io.File; 18 18 import java.io.FileInputStream; 19 import java.io.FileOutputStream; 19 20 import java.util.Collection; 20 21 import java.util.HashSet; … … 29 30 //import static org.junit.Assert.*; 30 31 31 import org.apache.commons.lang.SerializationUtils; 32 33 34 32 35 import org.eclipse.uml2.uml.Interaction; 33 36 import org.eclipse.uml2.uml.Model; … … 43 46 import de.ugoe.cs.autoquest.plugin.http.SOAPUtils; 44 47 import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; 48 import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 45 49 import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator; 46 50 import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel; 47 51 import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; 52 import de.ugoe.cs.util.SerializationUtils; 48 53 import de.ugoe.cs.util.console.TextConsole; 49 54 … … 58 63 59 64 private final static String OUTPUT_DIR = "target/tmp/test-outputs/"; 60 65 66 private final static boolean DELETE_OUTPUTS = false; 67 61 68 // for RLUS 62 69 private final static TestData deda_1 = new TestData("deda_rlus_properties.prop", 63 "deda_usagejournal.log",64 "deda_rlus_usageprofile.dat",65 "deda_model.uml",66 "deda_rlus_model_testsuite.uml",67 "deda_rlus_model_scheduling.uml");70 "deda_usagejournal.log", 71 "deda_rlus_usageprofile.dat", 72 "deda_model.uml", 73 "deda_rlus_model_testsuite.uml", 74 "deda_rlus_model_scheduling.uml"); 68 75 69 76 // for IXS 70 77 private final static TestData deda_2 = new TestData("deda_ixs_properties.prop", 71 "deda_usagejournal.log", 72 "deda_ixs_usageprofile.dat", 73 "deda_model.uml", 74 "deda_ixs_model_testsuite.uml", 75 "deda_ixs_model_scheduling.uml"); 76 77 private final static TestData ita_1 = new TestData("ita_properties.prop", "ita_usagejournal.log", 78 "ita_usageprofile.dat", "ita_model.uml", 79 "ita_model_testsuite.uml", 80 "ita_model_scheduling.uml"); 81 78 "deda_usagejournal.log", 79 "deda_ixs_usageprofile.dat", 80 "deda_model.uml", 81 "deda_ixs_model_testsuite.uml", 82 "deda_ixs_model_scheduling.uml"); 83 84 private final static TestData ita_1 = new TestData("ita_properties.prop", 85 "ita_usagejournal.log", 86 "ita_usageprofile.dat", "ita_model.uml", 87 "ita_model_testsuite.uml", 88 "ita_model_scheduling.uml"); 89 82 90 private static class TestData { 83 91 public final String propertiesFile; … … 89 97 90 98 public TestData(String propertiesFile, 91 String usageJournalFile,92 String usageProfileFile,93 String dslModelFile,94 String testSuiteFile,95 String schedulingFile)99 String usageJournalFile, 100 String usageProfileFile, 101 String dslModelFile, 102 String testSuiteFile, 103 String schedulingFile) 96 104 { 97 105 this.propertiesFile = propertiesFile; … … 101 109 this.testSuiteFile = testSuiteFile; 102 110 this.schedulingFile = schedulingFile; 103 111 104 112 } 105 113 … … 119 127 @BeforeClass 120 128 public static void setUpBeforeClass() throws Exception { 121 new TextConsole(Level. FINE);129 new TextConsole(Level.SEVERE); 122 130 } 123 131 124 132 @After 125 133 public void tearDown() throws Exception { 126 deleteFiles(new File(OUTPUT_DIR)); 134 if (DELETE_OUTPUTS) { 135 deleteFiles(new File(OUTPUT_DIR)); 136 } 127 137 } 128 138 … … 130 140 public void testCreateUMLTransitionSequence_ITA_1() throws Exception { 131 141 TestData testdata = ita_1; 132 142 133 143 Properties properties = loadProperties(testdata); 134 144 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); … … 137 147 138 148 StateMachine stateMachine = 139 (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 140 UMLPackage.Literals.STATE_MACHINE, true); 141 142 149 (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 150 UMLPackage.Literals.STATE_MACHINE, true); 151 143 152 Collection<List<Event>> umlSequences = new LinkedList<>(); 144 153 145 154 // remove everything but transport from sequences 146 155 for (List<Event> sequence : sequences) { … … 159 168 public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception { 160 169 TestData testdata = ita_1; 161 170 162 171 Properties properties = loadProperties(testdata); 163 172 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); … … 190 199 createInteractionFromEventSequenceWorkflow(deda_1); 191 200 } 192 201 193 202 @Test 194 203 public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception { 195 204 createInteractionFromEventSequenceWorkflow(deda_2); 196 205 } 197 206 198 207 @Test 199 208 public void testCreateInteractionFromEventSequence_ITA_1() throws Exception { … … 205 214 calculateUsageScoreWorkflow(deda_1); 206 215 } 207 216 208 217 @Test 209 218 public void testCalculateUsageScore_DEDA_2() throws Exception { … … 211 220 } 212 221 213 214 222 @Test 215 223 public void testCalculateUsageScore_ITA_1() throws Exception { … … 221 229 createSchedulingWorkflow(deda_1); 222 230 } 223 231 224 232 @Test 225 233 public void testCreateScheduling_DEDA_2() throws Exception { 226 234 createSchedulingWorkflow(deda_2); 227 235 } 228 236 229 237 @Test 230 238 public void testCreateScheduling_ITA() throws Exception { … … 236 244 validateModelWithLogWorkflow(deda_1); 237 245 } 238 239 246 240 247 @Test … … 247 254 validateModelWithLogWorkflow(ita_1); 248 255 } 249 256 250 257 private void validateModelWithLogWorkflow(TestData testdata) throws Exception { 251 258 Properties properties = loadProperties(testdata); 252 259 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 253 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 254 260 Model model = 261 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 262 255 263 // run validation 256 int violations = UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 264 int violations = 265 UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 257 266 if (violations == 0) { 258 267 System.out.println("No problems found."); … … 262 271 } 263 272 } 264 273 265 274 private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception { 266 275 Properties properties = loadProperties(testdata); 267 276 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 268 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 269 277 Model model = 278 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 279 270 280 // create a test case for each observed sequence 271 int i=0; 272 for( List<Event> sequence : sequences ) { 273 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 274 properties.getProperty("test.context")); 281 int i = 0; 282 for (List<Event> sequence : sequences) { 283 UMLUtils.createInteractionFromEventSequence(sequence, model, 284 properties.getProperty("testcases.prefix") + 285 "_" + i, 286 properties.getProperty("test.context")); 275 287 i++; 276 288 } 277 } 278 289 290 ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile); 291 } 292 279 293 private void calculateUsageScoreWorkflow(TestData testdata) throws Exception { 280 294 Properties properties = loadProperties(testdata); 281 295 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 282 296 Model model = 283 ModelUtils.loadModel(ClassLoader 284 .getSystemResourceAsStream(testdata.dslModelFile));285 IStochasticProcess usageProfile = createUsageProfile(sequences);286 Collection<List<Event>> generatedSequences =createRandomSequences(usageProfile, properties);287 297 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 298 IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 299 Collection<List<Event>> generatedSequences = 300 createRandomSequences(usageProfile, properties); 301 288 302 int i = 1; 303 List<Interaction> interactions = new LinkedList<>(); 289 304 int[] lengths = new int[generatedSequences.size()]; 290 305 for (List<Event> sequence : generatedSequences) { 291 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 292 properties.getProperty("test.context")); 306 interactions.add(UMLUtils.createInteractionFromEventSequence(sequence, model, 307 properties.getProperty("testcases.prefix") + 308 "_" + i, 309 properties.getProperty("test.context"))); 293 310 lengths[i - 1] = sequence.size(); 294 311 i++; 295 312 } 296 for (int j = 0; j < generatedSequences.size(); j++) { 297 Interaction interaction = 298 (Interaction) model.getPackagedElement(properties.getProperty("testcases.prefix") + j, true, 299 UMLPackage.Literals.INTERACTION, true); 300 double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile); 313 for (int j = 0; j < interactions.size(); j++) { 314 double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile); 301 315 System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]); 302 316 } 303 317 } 304 318 305 319 private void createSchedulingWorkflow(TestData testdata) throws Exception { 306 320 Properties properties = loadProperties(testdata); 307 321 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 308 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 309 IStochasticProcess usageProfile = createUsageProfile(sequences); 310 Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties); 322 Model model = 323 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 324 IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 325 Collection<List<Event>> generatedSequences = 326 createRandomSequences(usageProfile, properties); 311 327 int i = 1; 312 328 for (List<Event> sequence : generatedSequences) { 313 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 329 UMLUtils.createInteractionFromEventSequence(sequence, model, 330 properties.getProperty("testcases.prefix") + 331 "_" + i, 314 332 properties.getProperty("test.context")); 315 333 i++; 316 334 } 317 318 UMLUtils.createScheduling(model, usageProfile, null);319 320 //ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");321 } 322 335 336 UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context")); 337 338 ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile); 339 } 340 323 341 private Properties loadProperties(TestData testdata) throws Exception { 324 342 Properties properties = new Properties(); 325 properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile).getFile())); 343 properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile) 344 .getFile())); 326 345 return properties; 327 346 } 328 329 private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, Properties properties) throws Exception { 347 348 private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, 349 Properties properties) 350 throws Exception 351 { 330 352 // load usage journal 331 353 HTTPLogParser parser = 332 354 new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile) 333 355 .getFile())); 334 parser 335 .parseFile(new File(ClassLoader 336 .getSystemResource(testdata.usageJournalFile) 337 .getFile())); 356 parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile) 357 .getFile())); 338 358 Collection<List<Event>> sequences = parser.getSequences(); 339 359 340 // remove non SOAP events 360 // remove non SOAP events and convert to SimpleSOAPEventType 361 Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 341 362 for (List<Event> sequence : sequences) { 342 SOAPUtils.removeNonSOAPEvents(sequence);343 } 344 363 simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 364 } 365 345 366 // remove calls to ingored services 346 367 Set<String> ignoredServices = new HashSet<>(); 347 368 String ignoredServicesString = properties.getProperty("test.ignored.services"); 348 if ( ignoredServicesString!=null) {349 for ( String service : ignoredServicesString.split(",")) {369 if (ignoredServicesString != null) { 370 for (String service : ignoredServicesString.split(",")) { 350 371 ignoredServices.add(service.trim()); 351 372 } 352 373 } 353 354 for (List<Event> sequence : s equences) {374 375 for (List<Event> sequence : simpleSOAPSequences) { 355 376 for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 356 377 Event event = eventIter.next(); 357 S OAPEventType eventType = (SOAPEventType) event.getType();378 SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType(); 358 379 if (ignoredServices.contains(eventType.getServiceName())) { 359 380 eventIter.remove(); … … 361 382 } 362 383 } 363 return sequences; 364 } 365 366 private IStochasticProcess createUsageProfile(Collection<List<Event>> sequences) throws Exception { 367 Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 368 for (List<Event> sequence : sequences) { 369 simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 370 } 371 384 return simpleSOAPSequences; 385 } 386 387 private IStochasticProcess createUsageProfile(TestData testdata, Collection<List<Event>> sequences) 388 throws Exception 389 { 372 390 FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1)); 373 usageProfile.train(simpleSOAPSequences); 391 usageProfile.train(sequences); 392 FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile); 393 SerializationUtils.serialize(usageProfile, fos); 394 fos.close(); 374 395 return usageProfile; 375 396 } 376 377 private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, Properties properties) throws Exception { 397 398 private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, 399 Properties properties) throws Exception 400 { 378 401 int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number")); 379 402 int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1")); 380 int testCaseMaxLength = Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 403 int testCaseMaxLength = 404 Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 381 405 int maxIter = numberOfTestCases * 100; 382 RandomWalkGenerator testGenerator = new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, maxIter); 406 RandomWalkGenerator testGenerator = 407 new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, 408 maxIter); 383 409 return testGenerator.generateTestSuite(usageProfile); 384 410 } 385 411 386 412 private void deleteFiles(File file) { 387 413 if (file.exists()) {
Note: See TracChangeset
for help on using the changeset viewer.