You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by "gallon.fizik@gmail.com" <ga...@gmail.com> on 2017/12/21 14:36:42 UTC

WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Hello, I have a simple test class: 

import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.Function;
import org.junit.Test;

public class FunctionRegistratorIT1 {
    @Test
    public void testRegisterFunction1() {
        WorkbookEvaluator.registerFunction("TREND", new DummyExcelFunction());
    }

    @Test
    public void testRegisterFunction2() {
        WorkbookEvaluator.registerFunction("TREND", new DummyExcelFunction());
    }

    private class DummyExcelFunction implements Function {
        @Override
        public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
            return new NumberEval(0);
        }
    }
}

Oddly enough, the test being executed second fails with 'IllegalArgumentException: POI already implements TREND. You cannot POI's implementations of Excel functions'. If I comment one of the tests out, the other one passes. This behavior persists if the same function is registered in different tests across a test suite.

I expected that every @Test is run in isolation, but it seems that WorkbookEvaluator stores registered functions between invocations of different tests within a suite.

Although it is possible that it is probably related to JUnit rather than POI, perhaps someone here has already witnessed this behavior.

To sum up, the questions are:
* why does this happen?
* how can I have get several tests registering the same function names work?

Regards, 
Vladislav

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Posted by "gallon.fizik@gmail.com" <ga...@gmail.com>.
Thank you for the detailed explanation.

On 2017-12-21 16:35, Yegor Kozlov <ye...@dinom.ru> wrote: 
> UDFs are registered in a static map and JUnit does not reset  static
> variables.
> Use the @BeforeClass annotation to register your functions.  This can be a
> problem if you have many functions, but if it is just one a or few  it
> should be okay.
> If @BeforeClass  is not an option then the only alternative is to reset
> static variables via reflection or class loaded in @After.
> 
> Yegor
> 
> On Thu, Dec 21, 2017 at 5:36 PM, gallon.fizik@gmail.com <
> gallon.fizik@gmail.com> wrote:
> 
> > Hello, I have a simple test class:
> >
> > import org.apache.poi.ss.formula.WorkbookEvaluator;
> > import org.apache.poi.ss.formula.eval.NumberEval;
> > import org.apache.poi.ss.formula.eval.ValueEval;
> > import org.apache.poi.ss.formula.functions.Function;
> > import org.junit.Test;
> >
> > public class FunctionRegistratorIT1 {
> >     @Test
> >     public void testRegisterFunction1() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     @Test
> >     public void testRegisterFunction2() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     private class DummyExcelFunction implements Function {
> >         @Override
> >         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> > srcColumnIndex) {
> >             return new NumberEval(0);
> >         }
> >     }
> > }
> >
> > Oddly enough, the test being executed second fails with
> > 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> > implementations of Excel functions'. If I comment one of the tests out, the
> > other one passes. This behavior persists if the same function is registered
> > in different tests across a test suite.
> >
> > I expected that every @Test is run in isolation, but it seems that
> > WorkbookEvaluator stores registered functions between invocations of
> > different tests within a suite.
> >
> > Although it is possible that it is probably related to JUnit rather than
> > POI, perhaps someone here has already witnessed this behavior.
> >
> > To sum up, the questions are:
> > * why does this happen?
> > * how can I have get several tests registering the same function names
> > work?
> >
> > Regards,
> > Vladislav
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> > For additional commands, e-mail: user-help@poi.apache.org
> >
> >
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Posted by "gallon.fizik@gmail.com" <ga...@gmail.com>.
Actually, this was my initial approach. It works perfect in the production application but still doesn't allow to register a function with the same name several times across a test suite (which should be legitimate).

Anyway, thanks for your reply

On 2017-12-21 18:13, Greg Woolsey <gr...@gmail.com> wrote: 
> You could also wrap your function registration in code that first looks to
> see if it is already defined, using
> WorkbookEvaluator.getSupportedFunctionNames().contains("TREND")
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Posted by Greg Woolsey <gr...@gmail.com>.
You could also wrap your function registration in code that first looks to
see if it is already defined, using
WorkbookEvaluator.getSupportedFunctionNames().contains("TREND")

On Thu, Dec 21, 2017 at 7:35 AM Yegor Kozlov <ye...@dinom.ru> wrote:

> UDFs are registered in a static map and JUnit does not reset  static
> variables.
> Use the @BeforeClass annotation to register your functions.  This can be a
> problem if you have many functions, but if it is just one a or few  it
> should be okay.
> If @BeforeClass  is not an option then the only alternative is to reset
> static variables via reflection or class loaded in @After.
>
> Yegor
>
> On Thu, Dec 21, 2017 at 5:36 PM, gallon.fizik@gmail.com <
> gallon.fizik@gmail.com> wrote:
>
> > Hello, I have a simple test class:
> >
> > import org.apache.poi.ss.formula.WorkbookEvaluator;
> > import org.apache.poi.ss.formula.eval.NumberEval;
> > import org.apache.poi.ss.formula.eval.ValueEval;
> > import org.apache.poi.ss.formula.functions.Function;
> > import org.junit.Test;
> >
> > public class FunctionRegistratorIT1 {
> >     @Test
> >     public void testRegisterFunction1() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     @Test
> >     public void testRegisterFunction2() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     private class DummyExcelFunction implements Function {
> >         @Override
> >         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> > srcColumnIndex) {
> >             return new NumberEval(0);
> >         }
> >     }
> > }
> >
> > Oddly enough, the test being executed second fails with
> > 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> > implementations of Excel functions'. If I comment one of the tests out,
> the
> > other one passes. This behavior persists if the same function is
> registered
> > in different tests across a test suite.
> >
> > I expected that every @Test is run in isolation, but it seems that
> > WorkbookEvaluator stores registered functions between invocations of
> > different tests within a suite.
> >
> > Although it is possible that it is probably related to JUnit rather than
> > POI, perhaps someone here has already witnessed this behavior.
> >
> > To sum up, the questions are:
> > * why does this happen?
> > * how can I have get several tests registering the same function names
> > work?
> >
> > Regards,
> > Vladislav
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> > For additional commands, e-mail: user-help@poi.apache.org
> >
> >
>

Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Posted by Yegor Kozlov <ye...@dinom.ru>.
UDFs are registered in a static map and JUnit does not reset  static
variables.
Use the @BeforeClass annotation to register your functions.  This can be a
problem if you have many functions, but if it is just one a or few  it
should be okay.
If @BeforeClass  is not an option then the only alternative is to reset
static variables via reflection or class loaded in @After.

Yegor

On Thu, Dec 21, 2017 at 5:36 PM, gallon.fizik@gmail.com <
gallon.fizik@gmail.com> wrote:

> Hello, I have a simple test class:
>
> import org.apache.poi.ss.formula.WorkbookEvaluator;
> import org.apache.poi.ss.formula.eval.NumberEval;
> import org.apache.poi.ss.formula.eval.ValueEval;
> import org.apache.poi.ss.formula.functions.Function;
> import org.junit.Test;
>
> public class FunctionRegistratorIT1 {
>     @Test
>     public void testRegisterFunction1() {
>         WorkbookEvaluator.registerFunction("TREND", new
> DummyExcelFunction());
>     }
>
>     @Test
>     public void testRegisterFunction2() {
>         WorkbookEvaluator.registerFunction("TREND", new
> DummyExcelFunction());
>     }
>
>     private class DummyExcelFunction implements Function {
>         @Override
>         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> srcColumnIndex) {
>             return new NumberEval(0);
>         }
>     }
> }
>
> Oddly enough, the test being executed second fails with
> 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> implementations of Excel functions'. If I comment one of the tests out, the
> other one passes. This behavior persists if the same function is registered
> in different tests across a test suite.
>
> I expected that every @Test is run in isolation, but it seems that
> WorkbookEvaluator stores registered functions between invocations of
> different tests within a suite.
>
> Although it is possible that it is probably related to JUnit rather than
> POI, perhaps someone here has already witnessed this behavior.
>
> To sum up, the questions are:
> * why does this happen?
> * how can I have get several tests registering the same function names
> work?
>
> Regards,
> Vladislav
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>