private static String checkTestNamesUnique(Map<String, List<Scenario>> scenarioMap) { StringBuilder stringBuilder = new StringBuilder(); for (String feature : scenarioMap.keySet()) { List<Scenario> scenarios = scenarioMap.get(feature); for (Scenario scenario : scenarios) { for (Scenario scenario1 : scenarios) { if (!scenario1.equals(scenario) && scenario1.getScenarioName().startsWith(scenario.getScenarioName())) { stringBuilder .append("Feature '") .append(feature) .append("' scenario '") .append(scenario.getScenarioName()) .append("' name crosses with scenario '") .append(scenario1.getScenarioName()) .append("'"); stringBuilder.append("\n"); } } } } String result = stringBuilder.toString(); if (result.length() > 0) { return result; } return null; }
private ScenarioModel namedScenario(Description description) { Scenario scenarioAnnotation = description.getAnnotation(Scenario.class); Parameters parametersAnnotation = description.getAnnotation(Parameters.class); String name = null; if (scenarioHasDefinedName(scenarioAnnotation)) name = scenarioAnnotation.value(); else name = createScenarioNameFromTestMethodName(description, parametersAnnotation); if (parametersAnnotation != null && !scenarioAnnotation.pending()) name = name.substring(name.indexOf('(') + 1, name.indexOf(')')) + " (" + description .getMethodName() .substring(0, description.getMethodName().indexOf('(') - 1) + ")"; return ScenarioManager.currentScenario().withName(name).withDescription(description); }
public static void main(String[] args) { String dir = "d:\\PP-rad\\poznan\\"; String networkFile = dir + "network.xml"; String linkStats = dir + "40.linkstats.txt.gz"; String polygonFile = dir + "poznan_polygon\\poznan_city_polygon.shp"; boolean includeBorderLinks = false; String filteredLinkStats = dir + "40.linkstats-filtered.txt.gz"; Geometry polygonGeometry = PolygonBasedFilter.readPolygonGeometry(polygonFile); Predicate<Link> linkInsidePolygonPredicate = PolygonBasedFilter.createLinkInsidePolygonPredicate(polygonGeometry, includeBorderLinks); Scenario scenario = ScenarioUtils.createScenario(VrpConfigUtils.createConfig()); MatsimNetworkReader nr = new MatsimNetworkReader(scenario); nr.readFile(networkFile); Map<Id<Link>, ? extends Link> linkMap = scenario.getNetwork().getLinks(); try (BufferedReader br = IOUtils.getBufferedReader(linkStats); PrintWriter pw = new PrintWriter(IOUtils.getBufferedWriter(filteredLinkStats))) { String header = br.readLine(); pw.println(header); String line; while ((line = br.readLine()) != null) { String linkId = new StringTokenizer(line).nextToken(); // linkId - first column Link link = linkMap.get(Id.create(linkId, Link.class)); if (linkInsidePolygonPredicate.apply(link)) { pw.println(line); } } } catch (IOException e) { throw new RuntimeException(e); } }
public void printContent(PrintWriter itsWriter, String delimiter) { Collection steps = getstepsValue(); if (steps != null) { for (Iterator i = steps.iterator(); i.hasNext(); ) { Object step = i.next(); if (step instanceof Scenario) { ((Scenario) step).printContent(itsWriter, delimiter); } } itsWriter.println(); for (Iterator j = steps.iterator(); j.hasNext(); ) { Object step = j.next(); if (step instanceof Action_Choice) { ((Action_Choice) step).printContent(itsWriter, delimiter); } } } }
private void prepareNetwork(String networkFile, String zoneShpFile, String zoneXmlFile) { scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); new MatsimNetworkReader(scenario.getNetwork()).readFile(networkFile); zones = BerlinZoneUtils.readZones(zoneXmlFile, zoneShpFile); }
private void setStatusToPendingIfAnnotatedAsPending(ScenarioModel scenario) { Scenario scenarioAnnotation = scenario.description().getAnnotation(Scenario.class); if (scenarioAnnotation != null && scenarioAnnotation.pending()) scenario.withStatus(ScenarioStatus.PENDING); }
private boolean scenarioHasDefinedName(Scenario scenarioAnnotation) { return scenarioAnnotation != null && scenarioAnnotation.value() != null && !scenarioAnnotation.value().equals(""); };
/** * Checks if there is no TestNG tests for Cucumber Scenarios * * @return String error message if there is any problem. If no problem found returns null; */ public static String checkTests() { try { File featuresDir = new File(RESOURCES); Collection<File> featuresFiles = FileUtils.listFiles(featuresDir, new String[] {"feature"}, true); ArrayList<String> featurePaths = new ArrayList<>(); for (File featuresFile : featuresFiles) { String featureDir = featuresFile.getParent(); if (!featurePaths.contains(featureDir)) { featurePaths.add(featureDir); } } List<CucumberFeature> features = CucumberFeature.load(new FileResourceLoader(), featurePaths, new ArrayList<>()); for (CucumberFeature cucumberFeature : features) { for (CucumberTagStatement scenarioTagStatement : cucumberFeature.getFeatureElements()) { String featureName = cucumberFeature.getUri(); featureName = featureName.substring(0, featureName.indexOf(".")); if (scenarioMap.get(featureName) == null) { scenarioMap.put(featureName, new ArrayList<Scenario>()); } String scenarioName = scenarioTagStatement.getVisualName(); scenarioName = scenarioName.substring(scenarioName.indexOf(":") + 1).trim(); scenarioMap.get(featureName).add(new Scenario(scenarioName)); } } String uniqueCheck = checkTestNamesUnique(scenarioMap); Collection<File> testngTests = FileUtils.listFiles(new File(TEST_DIR), new String[] {"java"}, true); for (File testngTest : testngTests) { String classPath = testngTest.getCanonicalPath(); classPath = classPath.substring(classPath.indexOf("java") + 5).replace(File.separator, "."); classPath = classPath.substring(0, classPath.lastIndexOf(".")); Class<?> test = classLoader.loadClass(classPath); if (test.getAnnotation(Feature.class) != null) { String featureName = test.getAnnotation(Feature.class).value(); for (Method method : test.getMethods()) { if (method.getAnnotation(com.legalmonkeys.test.annotation.Scenario.class) != null) { String scenarioName = method.getAnnotation(com.legalmonkeys.test.annotation.Scenario.class).value(); List<Scenario> scenarios = scenarioMap.get(featureName); for (Scenario scenario : scenarios) { if (scenario.getScenarioName().equals(scenarioName)) { if (method.getAnnotation(org.testng.annotations.Test.class) != null) { scenario.setFoundInTestNG(true); } } } } } } } int testCount = 0; boolean fail = false; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("\n"); if (uniqueCheck != null) { fail = true; stringBuilder.append(uniqueCheck); } for (String featureName : scenarioMap.keySet()) { for (Scenario scenario : scenarioMap.get(featureName)) { testCount++; if (!scenario.isFoundInTestNG()) { stringBuilder .append("Feature: \"") .append(featureName) .append(".feature\". Scenario: \"") .append(scenario.getScenarioName()) .append("\" TestNG test not found.") .append("\n"); fail = true; } } } if (fail) { return stringBuilder.toString(); } if (testCount == 0) { return "Error: 0 scenarios found."; } } catch (ClassNotFoundException e) { e.printStackTrace(); return "Error checking tests state." + e.getLocalizedMessage(); } catch (IOException e) { e.printStackTrace(); return "Error checking tests state." + e.getLocalizedMessage(); } return null; }
public void readNetwork(String dir, String networkFile) { Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); new MatsimNetworkReader(scenario).readFile(dir + "\\" + networkFile); idToLinkMap = scenario.getNetwork().getLinks(); }