Best JGiven code snippet using com.tngtech.jgiven.impl.tag.TagCreator.getHref
Source:TagCreator.java
...87 } else {88 setIfNotNullOrEmpty(tagConfig.getDefaultValue(), tag::setValue);89 }90 tag.setDescription(getDescriptionFromGenerator(tagConfig, annotation, valueOptional.orElse(null)));91 tag.setHref(getHref(tagConfig, annotation, valueOptional.orElse(null)));92 return Collections.singletonList(tag);93 }94 private List<Tag> processConfiguredAnnotation(TagConfiguration tagConfig) {95 if (!tagConfig.isIgnoreValue()) {96 log.warn(97 "Tag configuration 'ignoreValue', set to 'false' is ignored, "98 + "because no annotation that could be respected was given.");99 }100 Tag tag = createStyledTag(tagConfig);101 tag.setTags(tagConfig.getTags());102 String value = tagConfig.getDefaultValue();103 setIfNotNullOrEmpty(value, tag::setValue);104 tag.setDescription(getDescriptionFromGenerator(tagConfig, null, value));105 tag.setHref(getHref(tagConfig, null, value));106 return Collections.singletonList(tag);107 }108 private Tag createStyledTag(TagConfiguration tagConfig) {109 Tag tag = new Tag(tagConfig.getAnnotationFullType());110 tag.setType(tagConfig.getAnnotationType());111 tag.setPrependType(tagConfig.isPrependType());112 tag.setShowInNavigation(tagConfig.showInNavigation());113 setIfNotNullOrEmpty(tagConfig.getName(), tag::setName);114 setIfNotNullOrEmpty(tagConfig.getCssClass(), tag::setCssClass);115 setIfNotNullOrEmpty(tagConfig.getColor(), tag::setColor);116 setIfNotNullOrEmpty(tagConfig.getStyle(), tag::setStyle);117 return tag;118 }119 private void setIfNotNullOrEmpty(String value, Consumer<String> setter) {120 if (!Strings.isNullOrEmpty(value)) {121 setter.accept(value);122 }123 }124 private Optional<Object> getValuesFromAnnotation(Annotation annotation) {125 try {126 Method method = annotation.annotationType().getMethod("value");127 return Optional.ofNullable(method.invoke(annotation));128 } catch (NoSuchMethodException ignoreAnnotationsThatAreNotTags) {129 return Optional.empty();130 } catch (Exception e) {131 log.error("Error while getting 'value' method of annotation " + annotation, e);132 return Optional.empty();133 }134 }135 TagConfiguration toTagConfiguration(Class<? extends Annotation> annotationType) {136 IsTag isTag = annotationType.getAnnotation(IsTag.class);137 if (isTag != null) {138 return fromIsTag(isTag, annotationType);139 }140 return configuration.getTagConfiguration(annotationType);141 }142 private TagConfiguration fromIsTag(IsTag isTag, Class<? extends Annotation> annotationType) {143 String name = isTag.name();144 return TagConfiguration.builder(annotationType)145 .defaultValue(isTag.value())146 .description(isTag.description())147 .explodeArray(isTag.explodeArray())148 .ignoreValue(isTag.ignoreValue())149 .prependType(isTag.prependType())150 .name(name)151 .descriptionGenerator(isTag.descriptionGenerator())152 .cssClass(isTag.cssClass())153 .color(isTag.color())154 .style(isTag.style())155 .tags(getNamesOfParentTags(annotationType))156 .href(isTag.href())157 .hrefGenerator(isTag.hrefGenerator())158 .showInNavigation(isTag.showInNavigation())159 .build();160 }161 private List<String> getNamesOfParentTags(Class<? extends Annotation> annotationType) {162 return getTagAnnotationsOn(annotationType)163 .flatMap(resolvedTags -> resolvedTags.getDeclaredTags().stream())164 .map(Tag::toIdString)165 .collect(Collectors.toList());166 }167 private List<Tag> getAllAncestorTags(Class<? extends Annotation> annotationType) {168 return getTagAnnotationsOn(annotationType)169 .flatMap(resolvedTags -> resolvedTags.resolvedTags.stream())170 .flatMap(tag -> {171 Stream<Tag> tagStream = Stream.of(tag.tag);172 return Stream.concat(tagStream, tag.ancestors.stream());173 })174 .collect(Collectors.toList());175 }176 private Stream<ResolvedTags> getTagAnnotationsOn(Class<? extends Annotation> annotationType) {177 return Arrays.stream(annotationType.getAnnotations())178 .filter(a -> a.annotationType().isAnnotationPresent(IsTag.class))179 .map(this::toTags);180 }181 private List<String> toStringList(Object[] value) {182 List<String> values = Lists.newArrayList();183 for (Object v : value) {184 values.add(String.valueOf(v));185 }186 return values;187 }188 private String getDescriptionFromGenerator(TagConfiguration tagConfiguration, Annotation annotation, Object189 value) {190 try {191 return tagConfiguration.getDescriptionGenerator().getDeclaredConstructor().newInstance()192 .generateDescription(tagConfiguration, annotation, value);193 } catch (Exception e) {194 throw new JGivenWrongUsageException(195 "Error while trying to generate the description for annotation " + annotation196 + " using DescriptionGenerator class "197 + tagConfiguration.getDescriptionGenerator() + ": " + e.getMessage(),198 e);199 }200 }201 private String getHref(TagConfiguration tagConfiguration, Annotation annotation, Object value) {202 try {203 return tagConfiguration.getHrefGenerator().getDeclaredConstructor().newInstance()204 .generateHref(tagConfiguration, annotation, value);205 } catch (Exception e) {206 throw new JGivenWrongUsageException(207 "Error while trying to generate the href for annotation " + annotation208 + " using HrefGenerator class "209 + tagConfiguration.getHrefGenerator() + ": " + e.getMessage(),210 e);211 }212 }213 private List<Tag> getExplodedTags(Tag originalTag, Object[] values, Annotation annotation,214 TagConfiguration tagConfig) {215 List<Tag> result = Lists.newArrayList();216 for (Object singleValue : values) {217 Tag newTag = originalTag.copy();218 newTag.setValue(String.valueOf(singleValue));219 newTag.setDescription(getDescriptionFromGenerator(tagConfig, annotation, singleValue));220 newTag.setHref(getHref(tagConfig, annotation, singleValue));221 result.add(newTag);222 }223 return result;224 }225}...
getHref
Using AI Code Generation
1[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jgiven-html5-report ---2[INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jgiven-html5-report ---3[INFO] [INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ jgiven-html5-report ---4[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jgiven-html5-report ---5[INFO] [INFO] --- maven-source-plugin:3.0.0:jar-no-fork (attach-sources) @ jgiven-html5-report ---6[INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ jgiven-html5-report ---
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!!