package com.intellij.util.xml.stubs;

import com.intellij.openapi.extensions.Extensions;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.xml.XmlName;
import com.intellij.util.xml.reflect.DomExtender;
import com.intellij.util.xml.reflect.DomExtenderEP;
import com.intellij.util.xml.reflect.DomExtensionsRegistrar;
import com.intellij.util.xml.stubs.model.Bar;
import com.intellij.util.xml.stubs.model.Custom;
import org.jetbrains.annotations.NotNull;

/**
 * @author Dmitry Avdeev
 *         Date: 8/3/12
 */
public class DomStubBuilderTest extends DomStubTest {

  public void testDomLoading() throws Exception {
    getRootStub("foo.xml");
  }

  public void testFoo() throws Exception {
    doBuilderTest("foo.xml", "File:foo\n" +
                             "  Element:foo\n" +
                             "    Element:bar\n" +
                             "      Attribute:string:xxx\n" +
                             "      Attribute:int:666\n" +
                             "    Element:bar\n");
  }

  public void testIncompleteAttribute() throws Exception {
    doBuilderTest("incompleteAttribute.xml", "File:foo\n" +
                                             "  Element:foo\n" +
                                             "    Element:bar\n" +
                                             "      Attribute:string:\n");
  }

  public void testDomExtension() throws Exception {
    DomExtenderEP ep = new DomExtenderEP();
    ep.domClassName = Bar.class.getName();
    ep.extenderClassName = TestExtender.class.getName();
    PlatformTestUtil.registerExtension(Extensions.getRootArea(), DomExtenderEP.EP_NAME, ep, myTestRootDisposable);

    doBuilderTest("extender.xml", "File:foo\n" +
                                  "  Element:foo\n" +
                                  "    Element:bar\n" +
                                  "      Attribute:extend:xxx\n" +
                                  "    Element:bar\n");
  }

  public void testNullTag() throws Exception {
    doBuilderTest("nullTag.xml", "File:foo\n" +
                                 "  Element:foo\n");
  }

  public static class TestExtender extends DomExtender<Bar> {

    @Override
    public void registerExtensions(@NotNull Bar bar, @NotNull DomExtensionsRegistrar registrar) {
      registrar.registerAttributeChildExtension(new XmlName("extend"), Custom.class);
    }
  }
}

@@ -82,16 +82,186 @@
 nsions;%0A
+import com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.stubs.ObjectStubTree;%0Aimport com.intellij.psi.stubs.StubTreeLoader;%0A
 import c
@@ -2187,104 +2187,343 @@
     
-doBuilderTest(%22nullTag.xml%22, %22File:foo%5Cn%22 +%0A                                 %22  Element:foo%5Cn%22);
+PsiFile psiFile = myFixture.configureByFile(%22nullTag.xml%22);%0A%0A    StubTreeLoader loader = StubTreeLoader.getInstance();%0A    VirtualFile file = psiFile.getVirtualFile();%0A    assertTrue(loader.canHaveStub(file));%0A    ObjectStubTree stubTree = loader.readFromVFile(getProject(), file);%0A    assertNull(stubTree); // no stubs for invalid XML
 %0A  %7D

test fixed