/**
 *  Copyright 2015 Sven Ewald
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.xmlbeam.tests.xpath.variables;

import static org.junit.Assert.*;

import org.junit.Test;
import org.xmlbeam.XBProjector;
import org.xmlbeam.annotation.XBRead;

@SuppressWarnings("javadoc")
public class TestXPathVariables {

    public static String XML="<root><value id=\"1\">first</value><value id=\"2\">second</value></root>";
    
    public interface Projection {
        @XBRead("//value[@id=$PARAM0]")
        String getValue(int id);
    }
    
    @Test
    public void testVariableSubstitution() {
        Projection projection = new XBProjector().onXMLString(XML).createProjection(Projection.class);
        long before1 = System.nanoTime();
        assertEquals("first",projection.getValue(1));
        long duration1 = System.nanoTime() - before1;
        assertEquals("second",projection.getValue(2));
        long before2 = System.nanoTime();
        assertEquals("first",projection.getValue(1));
        long duration2 = System.nanoTime() - before2;
        System.out.println(duration1/duration2);
        // Assert caching the invocation context has some effect.
        assertTrue(duration1> 2*duration2);
    }
}

@@ -673,17 +673,28 @@
 .Assert.
-*
+assertEquals
 ;%0A%0Aimpor
@@ -874,17 +874,19 @@
 ring XML
-=
+ = 
 %22%3Croot%3E%3C
@@ -953,20 +953,16 @@
 root%3E%22;%0A
-    
 %0A    pub
@@ -1067,20 +1067,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te
@@ -1289,32 +1289,33 @@
 tEquals(%22first%22,
+ 
 projection.getVa
@@ -1407,16 +1407,17 @@
 second%22,
+ 
 projecti
@@ -1504,16 +1504,17 @@
 %22first%22,
+ 
 projecti
@@ -1615,18 +1615,128 @@
 tln(
-duration1/
+%22Duration1:%22 + duration1);%0A        System.out.println(%22Duration2:%22 + duration2);%0A        System.out.println(duration1 / 
 dura
@@ -1735,32 +1735,33 @@
 1 / duration2);%0A
+%0A
         // Asser
@@ -1814,24 +1814,26 @@
 ct.%0A        
+//
 assertTrue(d

fix junit test