package org.mixer2.xhtml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.mixer2.Mixer2Engine;
import org.mixer2.jaxb.xhtml.Div;
import org.mixer2.jaxb.xhtml.Html;
import org.mixer2.jaxb.xhtml.Span;
import org.mixer2.xhtml.exception.TagTypeUnmatchException;


public class StyleAttrTest {

    private String templateFileName = "StyleAttrTest.html";
    private String templateFilePath;
    private static Mixer2Engine m2e = Mixer2EngineSingleton.getInstance();

    @AfterClass
    public static void afterClass() {
        m2e = null;
    }

    @Before
    public void init() throws IOException {
        templateFilePath = getClass().getResource(templateFileName).toString();
        String osname = System.getProperty("os.name");
        if(osname.indexOf("Windows")>=0){
            templateFilePath = templateFilePath.replaceFirst("file:/", "");
        } else {
            templateFilePath = templateFilePath.replaceFirst("file:", "");
        }
    }

    @Test
    public void getStyle() throws IOException, TagTypeUnmatchException {
        Html html = m2e.loadHtmlTemplate(new File(templateFilePath));
        TreeMap<String,String> styleMap = html.getById("span1",Span.class).getStyleAsTreeMap();
        assertEquals("large", styleMap.get("font-weight"));
        assertEquals("0", styleMap.get("margin"));
        assertEquals("0", styleMap.get("padding"));
    }

    @Test
    public void setStyle() throws IOException, TagTypeUnmatchException {
        Html html = m2e.loadHtmlTemplate(new File(templateFilePath));
        TreeMap<String,String> styleMap = html.getById("div1",Div.class).getStyleAsTreeMap();
        assertEquals("solid", styleMap.get("border-style"));
        styleMap.put("font-weight", "bold");
        html = m2e.loadHtmlTemplate(m2e.saveToString(html));
        assertEquals("solid", styleMap.get("border-style"));
        assertEquals("bold", styleMap.get("font-weight"));

    }

    @Test
    public void setStyleNew() throws IOException, TagTypeUnmatchException {
        Html html;
        TreeMap<String,String> styleMap;

        html = m2e.loadHtmlTemplate(new File(templateFilePath));
        styleMap = html.getById("div1",Div.class).getStyleAsTreeMap();
        assertEquals("solid", styleMap.get("border-style"));

        TreeMap<String,String> newStyleMap = new TreeMap<String,String>();
        newStyleMap.put("border-color", "red");
        html.getById("div1",Div.class).setStyleByTreeMap(newStyleMap);

        html = m2e.loadHtmlTemplate(m2e.saveToString(html));
        styleMap = html.getById("div1",Div.class).getStyleAsTreeMap();
        assertNull(styleMap.get("border-style"));
        assertEquals("red", styleMap.get("border-color"));

    }

}

@@ -200,23 +200,38 @@
 org.
-junit.AfterClas
+apache.commons.lang.SystemUtil
 s;%0Ai
@@ -681,89 +681,8 @@
 );%0A%0A
-    @AfterClass%0A    public static void afterClass() %7B%0A        m2e = null;%0A    %7D%0A%0A
     
@@ -825,94 +825,36 @@
     
-String osname = System.getProperty(%22os.name%22);%0A        if(osname.indexOf(%22Windows%22)%3E=0
+if(SystemUtils.IS_OS_WINDOWS
 )%7B%0A 
@@ -1631,32 +1631,50 @@
 p%3CString,String%3E
+ styleMap;%0A       
  styleMap = html
@@ -1774,32 +1774,33 @@
 order-style%22));%0A
+%0A
         styleMap
@@ -1844,54 +1844,193 @@
 html
- = m2e.loadHtmlTemplate(m2e.saveToString(html)
+.getById(%22div1%22,Div.class).setStyleByTreeMap(styleMap);%0A        html = m2e.loadHtmlTemplate(m2e.saveToString(html));%0A        styleMap = html.getById(%22div1%22,Div.class).getStyleAsTreeMap(
 );%0A 

fix test.