Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JVM] Pretty Formatter error #619

Closed
TarekSaid opened this issue Nov 1, 2013 · 9 comments
Closed

[JVM] Pretty Formatter error #619

TarekSaid opened this issue Nov 1, 2013 · 9 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@TarekSaid
Copy link

Hi,

I am getting a StringIndexOutOfBoundsException when using the pretty formatter. When I remove the formatter, all my scenarios pass.

UPDATE:
it seems this problem has to do with the regular expressions I use for my feature steps.

I have the following feature file, in portuguese (UPDATED):

# language: pt
# encoding: iso-8859-1
@copia-ncm
Funcionalidade: copiar o NCM do produto para cada empresa do grupo
  Como uma empresa cadastrando um novo produto 
  Para gerar corretamente as tributações para as minhas outras empresas do grupo
  Eu quero que o NCM do produto seja copiado

  Contexto: Dado que eu estou logado

  @copia-ncm-existente
  Cenário: cópia de NCM existente
    Dado que eu estou cadastrando o produto "12341" com NCM iniciado por "9999.99.99"
    E a empresa 1 possui esse NCM
    Quando eu salvo o produto
    Então eu deveria ver o ncm no produto dessa empresa com código "9999.99.99-01"

  @copia-ncm-enquad-igual
  Cenário: cópia de NCM inexistente para mesmo enquadramento tributário
    Dado que eu estou cadastrando o produto "12343" com NCM iniciado por "8888.88.88-60"
    E a empresa 16 não possui esse NCM e tem mesmo enquadramento tributário
    Quando eu salvo o produto
    Então eu deveria ver o ncm no produto dessa empresa com código "8888.88.88-01" e a mesma situação tributária

  @copia-ncm-enquad-diferente
  Cenário: cópia de NCM inexistente para enquadramento tributário diferente
    Dado que eu estou cadastrando o produto "12342" com NCM iniciado por "1234.56.78"
    E a empresa 1 não possui esse NCM e não tem mesmo enquadramento tributário
    Quando eu salvo o produto
    Então eu deveria ver o ncm no produto dessa empresa com código "1234.56.78-01" e a mesma situação tributária

  @copia-ncm-importacao-existente
  Cenário: cópia de NCM existente durante a importação de dados
    Dado que eu estou importando o produto "999994" com NCM iniciado por "9999.99.99"
    E que eu tenho o arquivo "test/resources/0200-ncm-existente.txt"
    E que eu quero importar "Produtos"
    E a empresa 1 possui esse NCM
    Quando eu importo o arquivo
    Então eu deveria ver o ncm no produto dessa empresa com código "9999.99.99-01"

  @copia-ncm-importacao-inexistente
  Cenário: cópia de NCM inexistente durante a importação de dados
    Dado que eu estou importando o produto "999995" com NCM iniciado por "1314.15.16"
    E que eu tenho o arquivo "test/resources/0200-cria-ncm.txt"
    E que eu quero importar "Produtos"
    E a empresa 1 não possui esse NCM e não tem mesmo enquadramento tributário
    Quando eu importo o arquivo
    Então eu deveria ver o ncm no produto dessa empresa com código "1314.15.16-01" e a mesma situação tributária

I am using these regular expressions for my step defs:

@Dado("^que eu estou (cadastrando|importando) o produto \"([^\"]*)\" com NCM iniciado por \"([^\"]*)\"$")

@Dado("^a empresa (\\d+) (não )?possui esse NCM( e (não )?tem mesmo enquadramento tributário)?$")

@Quando("^eu salvo o produto$")

@Entao("^eu deveria ver o ncm no produto dessa empresa com código \"([^\"]*)\"( e a mesma situação tributária)?$")

Here's the full stack trace:

java.lang.StringIndexOutOfBoundsException: String index out of range: -38
 at java.lang.String.substring(String.java:1911)
 at gherkin.formatter.StepPrinter.writeStep(StepPrinter.java:11)
 at gherkin.formatter.PrettyFormatter.printStep(PrettyFormatter.java:254)
 at gherkin.formatter.PrettyFormatter.printSteps(PrettyFormatter.java:120)
 at gherkin.formatter.PrettyFormatter.replay(PrettyFormatter.java:111)
 at gherkin.formatter.PrettyFormatter.background(PrettyFormatter.java:92)
 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 cucumber.runtime.Utils$1.call(Utils.java:35)
 at cucumber.runtime.Timeout.timeout(Timeout.java:12)
 at cucumber.runtime.Utils.invoke(Utils.java:31)
 at cucumber.runtime.RuntimeOptions$1.invoke(RuntimeOptions.java:151)
 at com.sun.proxy.$Proxy15.background(Unknown Source)
 at cucumber.runtime.junit.JUnitReporter.background(JUnitReporter.java:156)
 at gherkin.formatter.model.Background.replay(Background.java:16)
 at cucumber.runtime.model.StepContainer.format(StepContainer.java:31)
 at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:48)
 at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:38)
 at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:83)
 at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
 at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
 at cucumber.api.junit.Cucumber.runChild(Cucumber.java:82)
 at cucumber.api.junit.Cucumber.runChild(Cucumber.java:41)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at cucumber.api.junit.Cucumber.run(Cucumber.java:87)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

When I remove the pretty formatter, all my scenarios pass.

@aslakhellesoy
Copy link
Contributor

What cucumber-jvm version?

@TarekSaid
Copy link
Author

Sorry, I forgot to add that info. Note: we don't use any dependency management tool, I've added those by hand...

java: 1.7
cucumber-core: 1.1.5
cucumber-html: 0.2.3
cucumber-java: 1.1.5
cucumber-junit: 1.1.5
cucumber-jvm-deps: 1.0.3
gherkin: 2.12.1
junit: 4.11

I don't know if you need these, but here goes:
OS: arch-linux
architecture: x86_64
kernel: 3.11.6-1-ARCH
Eclipse: 4.3.1

Thanks!
Tarek.

@TarekSaid
Copy link
Author

I've updated the issue with some more info. I've added two scenarios, and I've noticed the following:

  • If I run the scenarios using the feature above, or I place the @copia-ncm-enquad-igual and @copia-ncm-enquad-diferente scenarios last, it runs the first 3 features the stops;
  • If I comment scenarios, my other features don't run.
  • If I leave only one scenario, it works fine.

This lead me to believe it is related to the regular expressions I used (updated in my first post).

@brasmusson
Copy link
Contributor

The problem lies in:

@Dado("^a empresa (\\d+) (não )?possui esse NCM( e (não )?tem mesmo enquadramento tributário)?$")

Nested capture groups cannot be handled by the PrettyFormatter. Since each capture groups maps to an argument, this sort of gives an argument in an argument, the third argument (e não tem mesmo enquadramento tributário), will end at a position after the forth argument starts (não). Exactly 38 positions before the end the third argument thereby the String index out of range: -38

@mpkorstanje mpkorstanje added Bug good first issue Good for newcomers labels Aug 10, 2017
@mpkorstanje
Copy link
Contributor

@mlvandijk if you want to try this one you can start by trying to reproduce the issue. It might not be possible to reproduce the problem in 2.0.0-SNAPSHOT as the Formatters have changed quite drastically.

If it is possible to reproduce the issue you can add a unit test to PrettyFormatter. Because this is a nested capture group I think it will be okay do boundary checking and do nothing if the index is out of bounds.

@mlvandijk
Copy link
Member

@mpkorstanje Sure, will give it a try. Thanks!

@mlvandijk mlvandijk self-assigned this Aug 10, 2017
@mlvandijk
Copy link
Member

mlvandijk commented Aug 12, 2017

@mpkorstanje @brasmusson I've reproduced this issue, (added a test for it) and made some small changes to PrettyFormatter to prevent StringIndexOutOfBoundsException from being thrown for nested arguments. This does not break any other tests in Core. However, I am not sure if this is how PrettyFormatter should work.
Created PR for review; Travis build fails due to failing test (added to reproduce issue as per request).

@mlvandijk
Copy link
Member

Fixed by PR #1200

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants