package com.atlassian.plugin.util.validation;

import junit.framework.TestCase;
import org.dom4j.Element;
import org.dom4j.DocumentFactory;
import static com.atlassian.plugin.util.validation.ValidatePattern.createPattern;
import static com.atlassian.plugin.util.validation.ValidatePattern.test;
import com.atlassian.plugin.PluginParseException;

public class TestValidatePattern extends TestCase
{
    Element root;

    @Override
    public void setUp()
    {
        DocumentFactory factory = DocumentFactory.getInstance();
        root = factory.createElement("root");
        root.addAttribute("foo", "bar");
        Element child = factory.createElement("child");
        child.addAttribute("some", "thing");
        child.setText("mybody");
        Element emptyChild = factory.createElement("child");
        root.add(child);
        root.add(emptyChild);
    }

    public void testSuccess()
    {
        createPattern().
                rule(".",
                        test("child").withError("Child is required"),
                        test("not(baz)").withError("Baz should not exist")).
                rule("child[1]",
                        test(".[@some = 'thing']").withError("Need some attribute")).
                evaluate(root);

    }

    public void testErrorMessageWithEmptyList()
    {
        try
        {
            createPattern().
                    rule(".",
                            test("baz").withError("Baz should exist")).
                    evaluate(root);
            fail();
        }
        catch (PluginParseException ex)
        {
            assertEquals("Baz should exist", ex.getMessage());
        }
    }

    public void testErrorMessageWithNull()
    {
        try
        {
            createPattern().
                    rule(".",
                            test("baz[1]").withError("Baz should exist")).
                    evaluate(root);
            fail();
        }
        catch (PluginParseException ex)
        {
            assertEquals("Baz should exist", ex.getMessage());
        }
    }

    public void testErrorMessageWithBoolean()
    {
        try
        {
            createPattern().
                    rule(".",
                            test("not(not(baz))").withError("Baz should exist")).
                    evaluate(root);
            fail();
        }
        catch (PluginParseException ex)
        {
            assertEquals("Baz should exist", ex.getMessage());
        }
    }


}

@@ -1248,32 +1248,763 @@
 (root);%0A%0A    %7D%0A%0A
+    /*public void testSuccessPerfTest()%0A    %7B%0A        ValidatePattern ptn = createPattern().%0A                rule(%22.%22,%0A                        test(%22child%22).withError(%22Child is required%22),%0A                        test(%22not(baz)%22).withError(%22Baz should not exist%22)).%0A                rule(%22child%5B1%5D%22,%0A                        test(%22.%5B@some = 'thing'%5D%22).withError(%22Need some attribute%22));%0A%0A        for (int x=0; x%3C1000; x++)%0A        %7B%0A            ptn.evaluate(root);%0A        %7D%0A%0A        long start = System.currentTimeMillis();%0A        for (int x=0; x%3C5000; x++)%0A        %7B%0A            ptn.evaluate(root);%0A        %7D%0A        long end = System.currentTimeMillis();%0A        System.out.println(%22Time elapsed: %22+(end-start)+%22 ms%22);%0A%0A    %7D*/%0A%0A
     public void 

Add commented-out micro-benchmark for validations PLUG-298