/*******************************************************************************
 * Copyright (c) 2013 University of Twente, Bielefeld University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
package saiba.bml.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import hmi.xml.XMLFormatting;
import hmi.xml.XMLNameSpace;

import java.io.IOException;

import org.junit.Test;

import saiba.bml.BMLInfo;

import com.google.common.collect.ImmutableList;

/**
 * Shared testcases for all Behaviours
 * @author welberge
 *
 */
public abstract class AbstractBehaviourTest
{
    protected abstract Behaviour createBehaviour(String bmlId, String extraAttributeString) throws IOException;
    protected abstract Behaviour parseBehaviour(String bmlId, String bmlString) throws IOException;
    private static final double PARAMETER_PRECISION = 0.0001;
    
    @Test
    public void testCustomFloatAttribute() throws IOException
    {
        BMLInfo.addCustomFloatAttribute(createBehaviour("bml1","").getClass(), "customfaceattributes","attr2");
        Behaviour beh = createBehaviour("bml1", "xmlns:custom=\"customfaceattributes\" custom:attr2=\"10\"");
        assertEquals(10, beh.getFloatParameterValue("customfaceattributes:attr2"),PARAMETER_PRECISION);
        assertTrue(beh.specifiesParameter("customfaceattributes:attr2"));        
    }
    
    @Test
    public void testCustomStringAttribute() throws IOException
    {
        BMLInfo.addCustomStringAttribute(createBehaviour("bml1","").getClass(), "customfaceattributes","attr1");
        Behaviour beh = createBehaviour("bml1", "xmlns:custom=\"customfaceattributes\" custom:attr1=\"blah\"");        
        assertEquals("blah", beh.getStringParameterValue("customfaceattributes:attr1"));    
        assertTrue(beh.specifiesParameter("customfaceattributes:attr1"));
        assertTrue(beh.satisfiesConstraint("customfaceattributes:attr1", "blah"));
    }
    
    @Test 
    public void testWriteCustomFloatAttribute()throws IOException
    {
        BMLInfo.addCustomFloatAttribute(createBehaviour("bml1","").getClass(), "customfaceattributes","attr2");
        Behaviour behIn = createBehaviour("bml1", "xmlns:custom=\"customfaceattributes\" custom:attr2=\"10\"");
        
        StringBuilder buf = new StringBuilder();        
        behIn.appendXML(buf, new XMLFormatting(), ImmutableList.of(new XMLNameSpace("custom","customfaceattributes")));        
        
        Behaviour behOut = parseBehaviour("bml1",buf.toString());
        assertEquals(10, behOut.getFloatParameterValue("customfaceattributes:attr2"),PARAMETER_PRECISION);
        assertTrue(behOut.specifiesParameter("customfaceattributes:attr2"));        
    }
    
    @Test
    public void testWriteCustomStringAttribute()throws IOException
    {
        BMLInfo.addCustomStringAttribute(createBehaviour("bml1","").getClass(), "customfaceattributes","attr1");
        Behaviour behIn = createBehaviour("bml1", "xmlns:custom=\"customfaceattributes\" custom:attr1=\"blah\"");
        
        StringBuilder buf = new StringBuilder();        
        behIn.appendXML(buf, new XMLFormatting(), ImmutableList.of(new XMLNameSpace("custom","customfaceattributes")));
        
        Behaviour behOut = parseBehaviour("bml1",buf.toString());
        assertEquals("blah", behOut.getStringParameterValue("customfaceattributes:attr1"));    
        assertTrue(behOut.specifiesParameter("customfaceattributes:attr1"));
    }
}

@@ -1368,16 +1368,60 @@
 Equals;%0A
+import static org.junit.Assert.assertFalse;%0A
 import s
@@ -1455,16 +1455,16 @@
 rtTrue;%0A
-
 import h
@@ -2538,32 +2538,402 @@
 %0A    %0A    @Test%0A
+    public void testNoFloatAttribute() throws IOException%0A    %7B%0A        BMLInfo.addCustomFloatAttribute(createBehaviour(%22bml1%22,%22%22).getClass(), %22customfaceattributes%22,%22attr2%22);%0A        Behaviour beh = createBehaviour(%22bml1%22, %22xmlns:custom=%5C%22customfaceattributes%5C%22%22);%0A        assertFalse(beh.specifiesParameter(%22customfaceattributes:attr2%22));        %0A    %7D%0A    %0A    @Test%0A
     public void 
@@ -3381,24 +3381,24 @@
 s:attr1%22));%0A
-
         asse
@@ -3471,32 +3471,415 @@
 %22));%0A    %7D%0A    %0A
+    @Test%0A    public void testNoCustomStringAttribute() throws IOException%0A    %7B%0A        BMLInfo.addCustomStringAttribute(createBehaviour(%22bml1%22,%22%22).getClass(), %22customfaceattributes%22,%22attr1%22);%0A        Behaviour beh = createBehaviour(%22bml1%22, %22xmlns:custom=%5C%22customfaceattributes%5C%22 %22);    %0A        assertFalse(beh.specifiesParameter(%22customfaceattributes:attr2%22));        %0A    %7D%0A    %0A
     @Test %0A    p

test for no specification of a parameter