Coder Social home page Coder Social logo

jcabi / jcabi-matchers Goto Github PK

View Code? Open in Web Editor NEW
14.0 6.0 15.0 1.15 MB

A few convenient Hamcrest matchers, mostly for XPath vs XHTML and fields in JAXB

Home Page: https://matchers.jcabi.com

License: Other

Java 100.00%
hamcrest hamcrest-matchers java jaxb xml xpath

jcabi-matchers's Introduction

logo

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

mvn PDD status Maven Central Hits-of-Code License

This project is just a parent POM for all other Java projects in "jcabi family".

See www.jcabi.com website for more details.

jcabi-matchers's People

Contributors

andreoss avatar carlosmiranda avatar cyberone avatar jrdalpra avatar renovate[bot] avatar romankisilenko avatar rultor avatar simonjenga avatar super132 avatar valery1707 avatar yegor256 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jcabi-matchers's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/codecov.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/cache v4
  • codecov/codecov-action v4.0.0-beta.3
  • ubuntu 22.04
.github/workflows/mvn.yml
  • actions/checkout v4@3df4ab11eba7bda6032a0b82a6bb43b11571feac
  • actions/setup-java v4
  • actions/cache v4
.github/workflows/pdd.yml
  • actions/checkout v4
  • ubuntu 22.04
.github/workflows/xcop.yml
  • actions/checkout v4@3df4ab11eba7bda6032a0b82a6bb43b11571feac
  • ubuntu 20.04
maven
pom.xml
  • com.jcabi:jcabi 1.39.0
  • org.projectlombok:lombok 1.18.32
  • org.hamcrest:hamcrest 2.2
  • com.jcabi:jcabi-log 0.24.1
  • com.jcabi:jcabi-aspects 0.26.0
  • com.jcabi:jcabi-xml 0.29.0
  • com.jcabi:jcabi-http 2.0.0
  • com.jcabi:jcabi-w3c 1.4.0
  • org.apache.commons:commons-lang3 3.14.0
  • commons-io:commons-io 2.16.1
  • xml-apis:xml-apis 2.0.2
  • javax.xml.bind:jaxb-api 2.4.0-b180830.0359
  • org.glassfish.jaxb:jaxb-runtime 2.3.9
  • javax.ws.rs:jsr311-api 1.1.1
  • net.sf.saxon:Saxon-HE 12.3
  • org.slf4j:slf4j-log4j12 2.0.13
  • log4j:log4j 1.2.17
  • org.glassfish:javax.json 1.1.4
  • com.sun.jersey:jersey-client 1.19.4
  • com.qulice:qulice-maven-plugin 0.22.0

  • Check this box to trigger a request for Renovate to run again on this repository

Indie tech review no.1

Review our existing source code and all other technical artifacts and report at least 8 bugs. More about such reviews you can read here. The task will be closed once we have that bugs reported and classified. Don't forget to add itr tag to each of them.

Make com.jcabi.matchers.XPathMatcher public

If someone wants to match XPath with jcabi-matchers the only way to do that is is via
static XhtmlMatchers.hasXPath()
however, it's static, not OO, not EO.
The jcabi already has a good OO/EO matcher com.jcabi.matchers.XPathMatcher, however, it's package-private and cannot be used by any project.

This issue is regarding making com.jcabi.matchers.XPathMatcher public.

XhtmlMatchers.hasXPath throws IllegalArgumentException

XhtmlMatchers.hasXPath throws IllegalArgumentException for a valid html fragment

MatcherAssert.assertThat(
            "<p>Here is a paragraph.</p><p>New paragraph.</p>",
            XhtmlMatchers.hasXPath("/p[.='Here is a paragraph']")
        );
[Fatal Error] :1:29: The markup in the document following the root element must be well-formed.

java.lang.IllegalArgumentException: Invalid XML: "<p>Here is a paragraph.</p><p>New paragraph.</p>"

RegexMatchers.java:65-67: Let's create a convenience method...

Puzzle 10-7b3f9a1b in src/main/java/com/jcabi/matchers/RegexMatchers.java:65-67 has to be resolved: Let's create a convenience method matchesAnyPattern(String...patterns) that should pass if a string matches any of the given patterns.

If you have any technical questions, don't ask me, submit new tickets instead

RegexMatchers

When I was working on jcabi/jcabi-aspects#88, I needed to create a unit test that used Regex matching as its assertion condition. I realized that Hamcrest did not have built-in matchers for regex. I think it will be very useful if we can have one as standard.

I propose that we create a class RegexMatchers, which will initially have the following methods:

  • Matcher<String> matchesPattern(String pattern): returns a Matcher that checks if the entire string matches the given pattern (should work like the String.matches() method).
  • Matcher<String> containsPattern(String pattern): returns a Matcher that checks if a string contains a subsequence that matches the given pattern (similar to Matcher.find() method).

RegexMatchers.java:85-89: Let's create the following convenience...

Puzzle 10-8e2f1dc4 in src/main/java/com/jcabi/matchers/RegexMatchers.java:85-89 has to be resolved: Let's create the following convenience methods: 1) containsAnyPattern(String...patterns), which should pass if a string contains ANY of the given patterns, and 2) containsAllPatterns(String...patterns). which should pass if a string contains ALL of the given patterns.

If you have any technical questions, don't ask me, submit new tickets instead

Allow matching extracted XPath

I'm trying to verify content that has been rendered in XHTML.
I can do that by looking for an XPath that contains the content, e.g.

assertThat(xhtml, XhtmlMatchers.hasXPath("//ns1:div[@id='content'][text()='Somecontent']", NS1));

However, if the structure is right but the content is wrong, I'm just informed that the XPath was not found. It would be much more comfortable, if I could add matchers to check the xpath result:

assertThat(xhtml, XhtmlMatchers.hasXPath("//ns1:div[@id='content']/text()", NS1, equalTo("Somecontent"));

An error would then show that the content that is wrong.

Do not require namespace for xmlns without prefix

As I have found out after quite some trial and error (see #32), to match XPath in an <html xmlns="http://www.w3.org/1999/xhtml"> tag, I need to use the "http://www.w3.org/1999/xhtml" namespace and need to prefix all tags.

Thus, I cannot just use any XPath tool to create the XPath expressions. It would be simpler if xmlns without prefix would not require a prefix in XhtmlMatchers either. Or more commonly, the prefixes in the XPath should be the same as those in xmlns.

Right now, it seems I'm better off removing the xmlns before feeding the xhtml to XhtmlMatchers.

<<< FAILURE! - in com.jcabi.matchers.NoBrokenLinksTest

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.jcabi.matchers.RegexMatchersTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.05 sec - in com.jcabi.matchers.RegexMatchersTest
Running com.jcabi.matchers.XhtmlMatchersTest
Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.47 sec - in com.jcabi.matchers.XhtmlMatchersTest
Running com.jcabi.matchers.JaxbConverterTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.26 sec - in com.jcabi.matchers.JaxbConverterTest
Running com.jcabi.matchers.W3CMatchersTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 12.83 sec - in com.jcabi.matchers.W3CMatchersTest
Running com.jcabi.matchers.StringSourceTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.jcabi.matchers.StringSourceTest
Running com.jcabi.matchers.NoBrokenLinksTest
[?[2;37mDEBUG?[m] pool-1-thread-16 com.jcabi.matchers.NoBrokenLinks: #assertThat(): 2 links found: ["/index.html", "http://img.rexsl.com/logo.png"]
[?[2;37mDEBUG?[m] pool-1-thread-16 com.jcabi.matchers.NoBrokenLinks: #http('http://www.rexsl.com/index.html'): response code is 404
[?[0;33mWARN?[m] pool-1-thread-16 com.jcabi.matchers.NoBrokenLinks: #isValid('http://www.rexsl.com/index.html'): not valid since response code is 404
[?[0;33mWARN?[m] pool-1-thread-16 com.jcabi.matchers.NoBrokenLinks: #http('http://img.rexsl.com/logo.png'): can't get response code: img.rexsl.com
[?[0;33mWARN?[m] pool-1-thread-16 com.jcabi.matchers.NoBrokenLinks: #isValid('http://img.rexsl.com/logo.png'): not valid since response code is 400
[?[0;31mERROR?[m] pool-1-thread-10 com.jcabi.aspects.aj.MethodValidator: JSR-303 validator failed to initialize: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath. (see http://aspects.jcabi.com/jsr-303.html)
[?[2;37mDEBUG?[m] pool-1-thread-10 com.jcabi.matchers.NoBrokenLinks: #assertThat(): 1 links found: [""]
[?[2;37mDEBUG?[m] pool-1-thread-8 com.jcabi.matchers.NoBrokenLinks: #assertThat(): 1 links found: ["/broken-link"]
[?[2;37mDEBUG?[m] pool-1-thread-8 com.jcabi.matchers.NoBrokenLinks: #http('http://google.com/broken-link'): response code is 404
[?[0;33mWARN?[m] pool-1-thread-8 com.jcabi.matchers.NoBrokenLinks: #isValid('http://google.com/broken-link'): not valid since response code is 404
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 9.72 sec <<< FAILURE! - in com.jcabi.matchers.NoBrokenLinksTest
passesWithoutBrokenLinks(com.jcabi.matchers.NoBrokenLinksTest)  Time elapsed: 5.56 sec  <<< FAILURE!
java.lang.AssertionError:
Expected: 2 broken link(s) found: ["http://www.rexsl.com/index.html", "http://img.rexsl.com/logo.png"]
     but: was <200 OK [http://localhost:12345/see-FakeRequest-class]

<html xmlns='http://www.w3.org/1999/xhtml'><body><a href='/index.html'/><a href='http://img.rexsl.com/logo.png'/></body></html>>
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
        at com.jcabi.matchers.NoBrokenLinksTest.passesWithoutBrokenLinks(NoBrokenLinksTest.java:91)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.apache.maven.surefire.junitcore.pc.Scheduler$1.run(Scheduler.java:318)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

Results :

Failed tests:
  NoBrokenLinksTest.passesWithoutBrokenLinks:91
Expected: 2 broken link(s) found: ["http://www.rexsl.com/index.html", "http://img.rexsl.com/logo.png"]
     but: was <200 OK [http://localhost:12345/see-FakeRequest-class]

<html xmlns='http://www.w3.org/1999/xhtml'><body><a href='/index.html'/><a href='http://img.rexsl.com/logo.png'/></body></html>>

Tests run: 32, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

1.4

Please, publish a new release 1.4 (or higher). Don't forget to update us here regularly (at least once a week), as explained in this article.

Update rultor configuration

See rubygems/rubygems#2354

@rultor merge

@andreoss @yegor256 Oops, I failed. You can see the full log here (spent 3min)

+ ls -al .
total 732
drwx--x--x   3 rultor rultor   4096 Jun 29 08:01 .
drwxrwxrwt 284 root   root   684032 Jun 29 08:03 ..
-rw-rw-r--   1 rultor rultor    444 Jun 29 08:03 end.sh
-rwxrwxr-x   1 rultor rultor    363 Jun 29 08:01 entry.sh
-rw-rw-r--   1 rultor rultor      6 Jun 29 08:01 pid
-rw-rw-r--   1 rultor rultor   1192 Jun 29 08:01 pubring.gpg
drwxrwxr-x   6 rultor rultor   4096 Jun 29 08:01 repo
-rwxrwxr-x   1 rultor rultor   6216 Jun 29 08:01 run.sh
-rw-rw-r--   1 rultor rultor     98 Jun 29 08:01 s3cfg
-rw-rw-r--   1 rultor rultor    683 Jun 29 08:01 script.sh
-rw-rw-r--   1 rultor rultor   2570 Jun 29 08:01 secring.gpg
-rw-rw-r--   1 rultor rultor   2464 Jun 29 08:01 settings.xml
-rw-rw-r--   1 rultor rultor  10539 Jun 29 08:03 stdout
++ pwd
++ pwd
+ docker run -t --rm -v /tmp/rultor-FQUM:/main '--env=MAVEN_OPTS=-XX:MaxPermSize=256m -Xmx1g' [email protected]:jcabi/jcabi-matchers.git --env=pull_id=41 [email protected]:andreoss/jcabi-matchers.git --env=fork_branch=issue-40 --env=head_branch=master '--env=pull_title=For #40: Update parent' --env=author=yegor256 '--env=scripts=( '\''export '\''\'\'''\''MAVEN_OPTS=-XX:MaxPermSize=256m -Xmx1g'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''[email protected]:jcabi/jcabi-matchers.git'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''pull_id=41'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''[email protected]:andreoss/jcabi-matchers.git'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''fork_branch=issue-40'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''head_branch=master'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''pull_title=For #40: Update parent'\''\'\'''\'''\'' '\'';'\'' '\''export '\''\'\'''\''author=yegor256'\''\'\'''\'''\'' '\'';'\'' '\''sudo gem install --no-ri --no-rdoc pdd'\'' '\'';'\'' '\''sudo gem install --no-ri --no-rdoc est'\'' '\'';'\'' '\''mvn clean install -Pqulice --errors --settings ../settings.xml'\'' '\'';'\'' '\''mvn clean'\'' '\'';'\'' '\''pdd --source=$(pwd) --verbose --file=/dev/null'\'' '\'';'\'' '\''est --dir=est --file=/dev/null'\'' '\'';'\'' )' --hostname=docker --privileged --memory=6g --memory-swap=16g --oom-kill-disable --cidfile=/tmp/rultor-FQUM/cid -w=/main -v /var/run/docker.sock:/var/run/docker.sock --name=jcabi_jcabi-matchers_41 yegor256/rultor-image /main/entry.sh
WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
+ set -e
+ set -o pipefail
+ shopt -s dotglob
+ useradd -m -G sudo r
+ usermod -s /bin/bash r
+ echo '%sudo ALL=(ALL) NOPASSWD:ALL'
+ cp -R /root/.bashrc /root/.cache /root/.composer /root/.gem /root/.gnupg /root/.m2 /root/.profile /root/texmf /home/r
+ cp -R ./cid ./end.sh ./entry.sh ./pid ./pubring.gpg ./repo ./run.sh ./s3cfg ./script.sh ./secring.gpg ./settings.xml ./stdout /home/r
+ rm -rf repo
+ chown -R r:r /home/r
+ chmod a+x /home/r/script.sh
+ su --login r --command /home/r/script.sh
mesg: cannot open /dev/pts/0: Permission denied
+ set -e
+ set -o pipefail
+ shopt -s expand_aliases
+ alias 'sudo=sudo -i'
+ export HOME=/home/r
+ HOME=/home/r
+ cd /home/r/repo
+ export 'MAVEN_OPTS=-XX:MaxPermSize=256m -Xmx1g'
+ MAVEN_OPTS='-XX:MaxPermSize=256m -Xmx1g'
+ export [email protected]:jcabi/jcabi-matchers.git
+ [email protected]:jcabi/jcabi-matchers.git
+ export pull_id=41
+ pull_id=41
+ export [email protected]:andreoss/jcabi-matchers.git
+ [email protected]:andreoss/jcabi-matchers.git
+ export fork_branch=issue-40
+ fork_branch=issue-40
+ export head_branch=master
+ head_branch=master
+ export 'pull_title=For #40: Update parent'
+ pull_title='For #40: Update parent'
+ export author=yegor256
+ author=yegor256
+ sudo -i gem install --no-ri --no-rdoc pdd
ERROR:  While executing gem ... (OptionParser::InvalidOption)
    invalid option: --no-ri
container 57aa624cc8196fae206b78b1aa797ceaffc3a77995f9b8808c8d73af14b8d362 is dead
Mon Jun 29 08:04:25 CEST 2020

Originally posted by @rultor in #41 (comment)

rultor fails build but travis passes

During merge of PR rultor failed build due to test NoBrokenLinksTest.findsBrLinksInHtmlWithNamespace but locally and on travis this test passes successfully. I think these two systems should always provide the same results.

Logs are here and here.

W3C matchers

Let's create a new utility class W3CMatchers with two methods: validHtml() and validCss()

They should use jcabi-w3c for validation against W3C servers

Qulice configuration - no license specified.

  1. Checkout master branch
  2. run mvn qulice:check -Pqulice
  3. There are multiple Line does not match expected header line of ' * BSD License'. (HeaderCheck) messages.

This is a known bug of qulice configuration in parent-0.31 (jcabi/jcabi-parent#19), accessed via jcabi-0.16. Suggested action is waiting until jcabi/jcabi#297 fix is released and upgrading jcabi version.

Please add xhtml example

The example page is very short and is missing an xhtml example. This would be helpful to show how to use namespace for xmlns without prefix.

Example
Xhtml file:

<?xml version="1.0"  encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>
<body>
<div id="content">Somecontent</div>
</body>
</html>

Matcher usage:

final String NS1 = "http://www.w3.org/1999/xhtml";
String html="<?xml version='1.0'  encoding='utf-8'?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>Example</title></head><body><div id='content'>Somecontent</div></body></html>";
Source xhtml = XhtmlMatchers.xhtml(html);
		
assertThat(xhtml, XhtmlMatchers.hasXPath("//ns1:div[@id='content']", NS1));
assertThat(xhtml, XhtmlMatchers.hasXPath("//ns1:div[@id='content'][text()='Somecontent']", NS1));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.