Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.shared.StringSpecializationInfo
Source:DateFormatClassReplacement.java
...4import org.evomaster.client.java.instrumentation.coverage.methodreplacement.Replacement;5import org.evomaster.client.java.instrumentation.heuristic.Truthness;6import org.evomaster.client.java.instrumentation.shared.ReplacementType;7import org.evomaster.client.java.instrumentation.shared.StringSpecialization;8import org.evomaster.client.java.instrumentation.shared.StringSpecializationInfo;9import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;10import java.text.DateFormat;11import java.text.ParseException;12import java.text.SimpleDateFormat;13import java.util.Date;14import java.util.Objects;15public class DateFormatClassReplacement implements MethodReplacementClass {16 public static final String YYYY_MM_DD = "yyyy-MM-dd";17 public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";18 @Override19 public Class<?> getTargetClass() {20 return DateFormat.class;21 }22 private static Date parseSimpleDateFormat(SimpleDateFormat caller, String input, String idTemplate) throws ParseException {23 final String pattern = caller.toPattern();24 if (ExecutionTracer.isTaintInput(input)) {25 final StringSpecializationInfo specializationInfo;26 switch (pattern) {27 case YYYY_MM_DD:28 specializationInfo = new StringSpecializationInfo(StringSpecialization.DATE_YYYY_MM_DD, pattern);29 break;30 case YYYY_MM_DD_HH_MM:31 specializationInfo = new StringSpecializationInfo(StringSpecialization.DATE_YYYY_MM_DD_HH_MM, pattern);32 break;33 default:34 specializationInfo = new StringSpecializationInfo(StringSpecialization.DATE_FORMAT_PATTERN, pattern);35 }36 ExecutionTracer.addStringSpecialization(input,37 specializationInfo);38 }39 if (idTemplate == null) {40 return caller.parse(input);41 }42 try {43 Date res = caller.parse(input);44 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.EXCEPTION, new Truthness(1, 0));45 return res;46 } catch (ParseException e) {47 final double h;48 switch (pattern) {49 case YYYY_MM_DD:50 h = DateTimeParsingUtils.getHeuristicToISOLocalDateParsing(input);51 break;52 case YYYY_MM_DD_HH_MM:53 h = DateTimeParsingUtils.getHeuristicToDateTimeParsing(input);54 break;55 default:56 h = DateTimeParsingUtils.getHeuristicToDateTimePatternParsing(input, pattern);57 }58 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.EXCEPTION, new Truthness(h, 1));59 throw e;60 }61 }62 @Replacement(type = ReplacementType.EXCEPTION)63 public static Date parse(DateFormat caller, String input, String idTemplate) throws ParseException {64 Objects.requireNonNull(caller);65 if (caller instanceof SimpleDateFormat) {66 SimpleDateFormat sdf = (SimpleDateFormat) caller;67 return parseSimpleDateFormat(sdf, input, idTemplate);68 } else {69 if (ExecutionTracer.isTaintInput(input)) {70 ExecutionTracer.addStringSpecialization(input,71 new StringSpecializationInfo(StringSpecialization.DATE_FORMAT_UNKNOWN_PATTERN, null));72 }73 if (idTemplate == null) {74 return caller.parse(input);75 }76 try {77 Date res = caller.parse(input);78 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.EXCEPTION, new Truthness(1, 0));79 return res;80 } catch (ParseException e) {81 // we do not have much guidance since we cannot access any pattern82 ExecutionTracer.executedReplacedMethod(idTemplate, ReplacementType.EXCEPTION, new Truthness(0, 1));83 throw e;84 }85 }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!