text:
package com.intellij.util.xmlb;

import com.intellij.openapi.util.JDOMExternalizableStringList;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.SmartList;
import com.intellij.util.xmlb.annotations.AbstractCollection;
import com.intellij.util.xmlb.annotations.*;
import gnu.trove.THashMap;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.intellij.lang.annotations.Language;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author mike
 */
public class XmlSerializerTest extends TestCase {
  private static final String XML_PREFIX = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

  public static class EmptyBean {
  }

  public void testEmptyBeanSerialization() {
    doSerializerTest("<EmptyBean />", new EmptyBean());
  }

  @Tag("Bean")
  public static class EmptyBeanWithCustomName {
  }

  public void testEmptyBeanSerializationWithCustomName() {
    doSerializerTest("<Bean />", new EmptyBeanWithCustomName());
  }

  public static class BeanWithPublicFields implements Comparable<BeanWithPublicFields> {
    public int INT_V = 1;
    public String STRING_V = "hello";

    public BeanWithPublicFields(final int INT_V, final String STRING_V) {
      this.INT_V = INT_V;
      this.STRING_V = STRING_V;
    }

    public BeanWithPublicFields() {
    }

    @Override
    public int compareTo(final BeanWithPublicFields o) {
      return STRING_V.compareTo(o.STRING_V);
    }
  }

  public void testPublicFieldSerialization() {
    BeanWithPublicFields bean = new BeanWithPublicFields();

    doSerializerTest(
      "<BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "  <option name=\"STRING_V\" value=\"hello\" />\n" +
      "</BeanWithPublicFields>", bean);

    bean.INT_V = 2;
    bean.STRING_V = "bye";

    doSerializerTest(
      "<BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "  <option name=\"STRING_V\" value=\"bye\" />\n" +
      "</BeanWithPublicFields>", bean);
  }

  public static class BeanWithPublicFieldsDescendant extends BeanWithPublicFields {
    public String NEW_S = "foo";
  }

  public void testPublicFieldSerializationWithInheritance() {
    BeanWithPublicFieldsDescendant bean = new BeanWithPublicFieldsDescendant();

    doSerializerTest(
      "<BeanWithPublicFieldsDescendant>\n" +
      "  <option name=\"NEW_S\" value=\"foo\" />\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "  <option name=\"STRING_V\" value=\"hello\" />\n" +
      "</BeanWithPublicFieldsDescendant>",
      bean);

    bean.INT_V = 2;
    bean.STRING_V = "bye";
    bean.NEW_S = "bar";

    doSerializerTest(
      "<BeanWithPublicFieldsDescendant>\n" +
      "  <option name=\"NEW_S\" value=\"bar\" />\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "  <option name=\"STRING_V\" value=\"bye\" />\n" +
      "</BeanWithPublicFieldsDescendant>",
      bean);
  }

  public static class BeanWithSubBean {
    public EmptyBeanWithCustomName BEAN1 = new EmptyBeanWithCustomName();
    public BeanWithPublicFields BEAN2 = new BeanWithPublicFields();
  }

  public void testSubBeanSerialization() {
    BeanWithSubBean bean = new BeanWithSubBean();
    doSerializerTest(
      "<BeanWithSubBean>\n" +
      "  <option name=\"BEAN1\">\n" +
      "    <Bean />\n" +
      "  </option>\n" +
      "  <option name=\"BEAN2\">\n" +
      "    <BeanWithPublicFields>\n" +
      "      <option name=\"INT_V\" value=\"1\" />\n" +
      "      <option name=\"STRING_V\" value=\"hello\" />\n" +
      "    </BeanWithPublicFields>\n" +
      "  </option>\n" +
      "</BeanWithSubBean>",
      bean);
    bean.BEAN2.INT_V = 2;
    bean.BEAN2.STRING_V = "bye";

    doSerializerTest(
      "<BeanWithSubBean>\n" +
      "  <option name=\"BEAN1\">\n" +
      "    <Bean />\n" +
      "  </option>\n" +
      "  <option name=\"BEAN2\">\n" +
      "    <BeanWithPublicFields>\n" +
      "      <option name=\"INT_V\" value=\"2\" />\n" +
      "      <option name=\"STRING_V\" value=\"bye\" />\n" +
      "    </BeanWithPublicFields>\n" +
      "  </option>\n" +
      "</BeanWithSubBean>",
      bean);
  }

  public void testSubBeanSerializationAndSkipDefaults() {
    BeanWithSubBean bean = new BeanWithSubBean();
    doSerializerTest(
      "<BeanWithSubBean />",
      bean, new SkipDefaultsSerializationFilter());
  }

  public final static class BeanWithEquals {
    public String STRING_V = "hello";

    public BeanWithEquals() {
    }

    @Override
    public boolean equals(Object o) {
      // any instance of this class is equal
      return this == o || (o != null && getClass() == o.getClass());
    }
  }

  public static class BeanWithSubBeanWithEquals {
    public EmptyBeanWithCustomName BEAN1 = new EmptyBeanWithCustomName();
    public BeanWithEquals BEAN2 = new BeanWithEquals();
  }

  public void testSubBeanWithEqualsSerializationAndSkipDefaults() {
    BeanWithSubBeanWithEquals bean = new BeanWithSubBeanWithEquals();
    SkipDefaultsSerializationFilter filter = new SkipDefaultsSerializationFilter();
    doSerializerTest(
      "<BeanWithSubBeanWithEquals />",
      bean, filter);

    bean.BEAN2.STRING_V = "new";
    doSerializerTest(
      "<BeanWithSubBeanWithEquals />",
      bean, filter);
  }

  public void testNullFieldValue() {
    BeanWithPublicFields bean1 = new BeanWithPublicFields();

    doSerializerTest(
      "<BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "  <option name=\"STRING_V\" value=\"hello\" />\n" +
      "</BeanWithPublicFields>",
      bean1);

    bean1.STRING_V = null;

    doSerializerTest(
      "<BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "  <option name=\"STRING_V\" />\n" +
      "</BeanWithPublicFields>", bean1);

    BeanWithSubBean bean2 = new BeanWithSubBean();
    bean2.BEAN1 = null;
    bean2.BEAN2 = null;

    doSerializerTest(
      "<BeanWithSubBean>\n" +
      "  <option name=\"BEAN1\" />\n" +
      "  <option name=\"BEAN2\" />\n" +
      "</BeanWithSubBean>", bean2);

  }

  public static class BeanWithList {
    public List<String> VALUES = new ArrayList<String>(Arrays.asList("a", "b", "c"));
  }

  public void testListSerialization() {
    BeanWithList bean = new BeanWithList();

    doSerializerTest(
      "<BeanWithList>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <list>\n" +
      "      <option value=\"a\" />\n" +
      "      <option value=\"b\" />\n" +
      "      <option value=\"c\" />\n" +
      "    </list>\n" +
      "  </option>\n" +
      "</BeanWithList>",
      bean);

    bean.VALUES = new ArrayList<String>(Arrays.asList("1", "2", "3"));

    doSerializerTest(
      "<BeanWithList>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <list>\n" +
      "      <option value=\"1\" />\n" +
      "      <option value=\"2\" />\n" +
      "      <option value=\"3\" />\n" +
      "    </list>\n" +
      "  </option>\n" +
      "</BeanWithList>",
      bean);
  }

  public static class BeanWithSet {
    public Set<String> VALUES = new LinkedHashSet<String>(Arrays.asList("a", "b", "w"));
  }

  public void testSetSerialization() {
    BeanWithSet bean = new BeanWithSet();
    doSerializerTest(
      "<BeanWithSet>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <set>\n" +
      "      <option value=\"a\" />\n" +
      "      <option value=\"b\" />\n" +
      "      <option value=\"w\" />\n" +
      "    </set>\n" +
      "  </option>\n" +
      "</BeanWithSet>",
      bean);
    bean.VALUES = new LinkedHashSet<String>(Arrays.asList("1", "2", "3"));

    doSerializerTest(
      "<BeanWithSet>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <set>\n" +
      "      <option value=\"1\" />\n" +
      "      <option value=\"2\" />\n" +
      "      <option value=\"3\" />\n" +
      "    </set>\n" +
      "  </option>\n" +
      "</BeanWithSet>",
      bean);
  }

  public static class BeanWithMap {
    public Map<String, String> VALUES = new LinkedHashMap<String, String>();

    {
      VALUES.put("a", "1");
      VALUES.put("b", "2");
      VALUES.put("c", "3");
    }
  }

  public void testMapSerialization() {
    BeanWithMap bean = new BeanWithMap();
    doSerializerTest(
      "<BeanWithMap>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <map>\n" +
      "      <entry key=\"a\" value=\"1\" />\n" +
      "      <entry key=\"b\" value=\"2\" />\n" +
      "      <entry key=\"c\" value=\"3\" />\n" +
      "    </map>\n" +
      "  </option>\n" +
      "</BeanWithMap>",
      bean);
    bean.VALUES.clear();
    bean.VALUES.put("1", "a");
    bean.VALUES.put("2", "b");
    bean.VALUES.put("3", "c");

    doSerializerTest(
      "<BeanWithMap>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <map>\n" +
      "      <entry key=\"1\" value=\"a\" />\n" +
      "      <entry key=\"2\" value=\"b\" />\n" +
      "      <entry key=\"3\" value=\"c\" />\n" +
      "    </map>\n" + "  </option>\n" +
      "</BeanWithMap>",
      bean);
  }

  public static class BeanWithMapWithAnnotations {
    @Property(surroundWithTag = false)
    @MapAnnotation(
      surroundWithTag = false,
      entryTagName = "option",
      keyAttributeName = "name",
      valueAttributeName = "value"
    )
    public Map<String, String> VALUES = new LinkedHashMap<String, String>();

    {
      VALUES.put("a", "1");
      VALUES.put("b", "2");
      VALUES.put("c", "3");
    }
  }

  public void testMapSerializationWithAnnotations() {
    BeanWithMapWithAnnotations bean = new BeanWithMapWithAnnotations();
    doSerializerTest(
      "<BeanWithMapWithAnnotations>\n" +
      "  <option name=\"a\" value=\"1\" />\n" +
      "  <option name=\"b\" value=\"2\" />\n" +
      "  <option name=\"c\" value=\"3\" />\n" +
      "</BeanWithMapWithAnnotations>",
      bean);
    bean.VALUES.clear();
    bean.VALUES.put("1", "a");
    bean.VALUES.put("2", "b");
    bean.VALUES.put("3", "c");

    doSerializerTest(
      "<BeanWithMapWithAnnotations>\n" +
      "  <option name=\"1\" value=\"a\" />\n" +
      "  <option name=\"2\" value=\"b\" />\n" +
      "  <option name=\"3\" value=\"c\" />\n" +
      "</BeanWithMapWithAnnotations>",
      bean);
  }

  public static class BeanWithMapWithBeanValue {
    public Map<String, BeanWithProperty> VALUES = new LinkedHashMap<String, BeanWithProperty>();
  }

  public void testMapWithBeanValue() {
    BeanWithMapWithBeanValue bean = new BeanWithMapWithBeanValue();

    bean.VALUES.put("a", new BeanWithProperty("James"));
    bean.VALUES.put("b", new BeanWithProperty("Bond"));
    bean.VALUES.put("c", new BeanWithProperty("Bill"));

    doSerializerTest(
      "<BeanWithMapWithBeanValue>\n" +
      "  <option name=\"VALUES\">\n" +
      "    <map>\n" +
      "      <entry key=\"a\">\n" +
      "        <value>\n" +
      "          <BeanWithProperty>\n" +
      "            <option name=\"name\" value=\"James\" />\n" +
      "          </BeanWithProperty>\n" +
      "        </value>\n" +
      "      </entry>\n" +
      "      <entry key=\"b\">\n" +
      "        <value>\n" +
      "          <BeanWithProperty>\n" +
      "            <option name=\"name\" value=\"Bond\" />\n" +
      "          </BeanWithProperty>\n" +
      "        </value>\n" +
      "      </entry>\n" +
      "      <entry key=\"c\">\n" +
      "        <value>\n" +
      "          <BeanWithProperty>\n" +
      "            <option name=\"name\" value=\"Bill\" />\n" +
      "          </BeanWithProperty>\n" +
      "        </value>\n" +
      "      </entry>\n" +
      "    </map>\n" +
      "  </option>\n" +
      "</BeanWithMapWithBeanValue>",
      bean);
  }

  public static class BeanWithMapWithBeanValue2 {
    @MapAnnotation(surroundWithTag = false, surroundKeyWithTag = false, surroundValueWithTag = false)
    public Map<String, BeanWithProperty> values = new THashMap<String, BeanWithProperty>();
  }

  public void testMapWithBeanValueUsingSkipDefaultsFilter() {
    BeanWithMapWithBeanValue2 bean = new BeanWithMapWithBeanValue2();
    doSerializerTest(
      "<BeanWithMapWithBeanValue2 />",
      bean, new SkipDefaultsSerializationFilter());
  }

  public static class BeanWithOption {
    @OptionTag("path")
    public String PATH;
  }

  public void testOptionTag() {
    BeanWithOption bean = new BeanWithOption();
    bean.PATH = "123";
    doSerializerTest("<BeanWithOption>\n" +
                     "  <option name=\"path\" value=\"123\" />\n" +
                     "</BeanWithOption>", bean);
  }

  public static class BeanWithCustomizedOption {
    @OptionTag(tag = "setting", nameAttribute = "key", valueAttribute = "saved")
    public String PATH;
  }

  public void testCustomizedOptionTag() {
    BeanWithCustomizedOption bean = new BeanWithCustomizedOption();
    bean.PATH = "123";
    doSerializerTest("<BeanWithCustomizedOption>\n" +
                     "  <setting key=\"PATH\" saved=\"123\" />\n" +
                     "</BeanWithCustomizedOption>", bean);
  }

  public static class BeanWithProperty {
    private String name = "James";

    public BeanWithProperty() {
    }

    public BeanWithProperty(final String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }

  public void testPropertySerialization() {
    BeanWithProperty bean = new BeanWithProperty();

    doSerializerTest(
      "<BeanWithProperty>\n" +
      "  <option name=\"name\" value=\"James\" />\n" +
      "</BeanWithProperty>",
      bean);

    bean.setName("Bond");

    doSerializerTest(
      "<BeanWithProperty>\n" +
      "  <option name=\"name\" value=\"Bond\" />\n" +
      "</BeanWithProperty>", bean);
  }

  public static class BeanWithFieldWithTagAnnotation {
    @Tag("name")
    public String STRING_V = "hello";
  }

  public void testParallelDeserialization() throws InterruptedException {
    final Element e = new Element("root").addContent(new Element("name").setText("x"));
    XmlSerializer.deserialize(e, BeanWithArray.class);//to initialize XmlSerializerImpl.ourBindings
    Thread[] threads = new Thread[5];
    final AtomicReference<AssertionFailedError> exc = new AtomicReference<AssertionFailedError>();
    for (int i = 0; i < threads.length; i++) {
      threads[i] = new Thread("XmlSerializerTest#testParallelDeserialization-" + i) {
        @Override
        public void run() {
          try {
            for (int j = 0; j < 10; j++) {
              BeanWithFieldWithTagAnnotation bean = XmlSerializer.deserialize(e, BeanWithFieldWithTagAnnotation.class);
              assertNotNull(bean);
              assertEquals("x", bean.STRING_V);
            }
          }
          catch (AssertionFailedError e) {
            exc.set(e);
          }
        }
      };
    }
    for (Thread thread : threads) {
      thread.start();
    }
    for (Thread thread : threads) {
      thread.join();
    }
    AssertionFailedError error = exc.get();
    if (error != null) {
      throw error;
    }
  }

  public void testFieldWithTagAnnotation() {
    BeanWithFieldWithTagAnnotation bean = new BeanWithFieldWithTagAnnotation();

    doSerializerTest(
      "<BeanWithFieldWithTagAnnotation>\n" +
      "  <name>hello</name>\n" +
      "</BeanWithFieldWithTagAnnotation>",
      bean);

    bean.STRING_V = "bye";

    doSerializerTest(
      "<BeanWithFieldWithTagAnnotation>\n" +
      "  <name>bye</name>\n" +
      "</BeanWithFieldWithTagAnnotation>", bean);
  }

  public void testEscapeCharsInTagText() {
    BeanWithFieldWithTagAnnotation bean = new BeanWithFieldWithTagAnnotation();
    bean.STRING_V = "a\nb\"<";

    doSerializerTest(
      "<BeanWithFieldWithTagAnnotation>\n" +
      "  <name>a\nb"<</name>\n" +
      "</BeanWithFieldWithTagAnnotation>", bean);
  }

  public void testEscapeCharsInAttributeValue() {
    final BeanWithPropertiesBoundToAttribute bean = new BeanWithPropertiesBoundToAttribute();
    bean.name = "a\nb\"<";
    doSerializerTest("<BeanWithPropertiesBoundToAttribute count=\"3\" name=\"a
b"<\" />", bean);
  }

  public void testShuffledDeserialize() {
    BeanWithPublicFields bean = new BeanWithPublicFields();
    bean.INT_V = 987;
    bean.STRING_V = "1234";

    Element element = serialize(bean, null);

    Element node = element.getChildren().get(0);
    element.removeContent(node);
    element.addContent(node);

    bean = XmlSerializer.deserialize(element, bean.getClass());
    assert bean != null;
    assertEquals(987, bean.INT_V);
    assertEquals("1234", bean.STRING_V);
  }

  public void testFilterSerializer() {
    BeanWithPublicFields bean = new BeanWithPublicFields();
    assertSerializer(bean,
                     "<BeanWithPublicFields>\n" +
                     "  <option name=\"INT_V\" value=\"1\" />\n" +
                     "</BeanWithPublicFields>",
                     new SerializationFilter() {
      @Override
      public boolean accepts(@NotNull Accessor accessor, @NotNull Object bean) {
        return accessor.getName().startsWith("I");
      }
    });
  }

  public static class BeanWithArray {
    public String[] ARRAY_V = new String[] {"a", "b"};
  }

  public void testArray() {
    BeanWithArray bean = new BeanWithArray();
    doSerializerTest(
      "<BeanWithArray>\n" +
      "  <option name=\"ARRAY_V\">\n" +
      "    <array>\n" +
      "      <option value=\"a\" />\n" +
      "      <option value=\"b\" />\n" +
      "    </array>\n" +
      "  </option>\n" +
      "</BeanWithArray>", bean);

    bean.ARRAY_V = new String[] {"1", "2", "3", ""};
    doSerializerTest(
      "<BeanWithArray>\n" +
      "  <option name=\"ARRAY_V\">\n" +
      "    <array>\n" +
      "      <option value=\"1\" />\n" +
      "      <option value=\"2\" />\n" +
      "      <option value=\"3\" />\n" +
      "      <option value=\"\" />\n" +
      "    </array>\n" + "  </option>\n" +
      "</BeanWithArray>", bean);
  }

  public static class BeanWithTransient {
    @Transient
    public int INT_V = 1;

    @Transient
    public String getValue() {
      return "foo";
    }
  }
  public void testTransient() {
    final BeanWithTransient bean = new BeanWithTransient();
    doSerializerTest("<BeanWithTransient />", bean);
  }

  public static class BeanWithArrayWithoutTagName {
    @AbstractCollection(surroundWithTag = false)
    public String[] V = new String[]{"a"};
  }

  public void testArrayAnnotationWithoutTagNAmeGivesError() {
    final BeanWithArrayWithoutTagName bean = new BeanWithArrayWithoutTagName();

    try {
      doSerializerTest("<BeanWithArrayWithoutTagName><option name=\"V\"><option value=\"a\"/></option></BeanWithArrayWithoutTagName>", bean);
    }
    catch (XmlSerializationException e) {
      return;
    }

    fail("No Exception");
  }

  public static class BeanWithArrayWithElementTagName {
    @AbstractCollection(elementTag = "vvalue", elementValueAttribute = "v")
    public String[] V = new String[]{"a", "b"};
  }
  public void testArrayAnnotationWithElementTag() {
    final BeanWithArrayWithElementTagName bean = new BeanWithArrayWithElementTagName();

    doSerializerTest(
      "<BeanWithArrayWithElementTagName>\n" +
      "  <option name=\"V\">\n" +
      "    <array>\n" +
      "      <vvalue v=\"a\" />\n" +
      "      <vvalue v=\"b\" />\n" +
      "    </array>\n" +
      "  </option>\n" +
      "</BeanWithArrayWithElementTagName>",
      bean);

    bean.V = new String[] {"1", "2", "3"};

    doSerializerTest(
      "<BeanWithArrayWithElementTagName>\n" +
      "  <option name=\"V\">\n" +
      "    <array>\n" +
      "      <vvalue v=\"1\" />\n" +
      "      <vvalue v=\"2\" />\n" +
      "      <vvalue v=\"3\" />\n" +
      "    </array>\n" +
      "  </option>\n" +
      "</BeanWithArrayWithElementTagName>", bean);
  }

  public static class BeanWithArrayWithoutTag {
    @AbstractCollection(elementTag = "vvalue", elementValueAttribute = "v", surroundWithTag = false)
    public String[] V = new String[]{"a", "b"};
    public int INT_V = 1;
  }
  public void testArrayWithoutTag() {
    final BeanWithArrayWithoutTag bean = new BeanWithArrayWithoutTag();

    doSerializerTest(
      "<BeanWithArrayWithoutTag>\n" +
      "  <option name=\"V\">\n" +
      "    <vvalue v=\"a\" />\n" +
      "    <vvalue v=\"b\" />\n" +
      "  </option>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "</BeanWithArrayWithoutTag>", bean);

    bean.V = new String[] {"1", "2", "3"};

    doSerializerTest(
      "<BeanWithArrayWithoutTag>\n" +
      "  <option name=\"V\">\n" +
      "    <vvalue v=\"1\" />\n" +
      "    <vvalue v=\"2\" />\n" +
      "    <vvalue v=\"3\" />\n" +
      "  </option>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "</BeanWithArrayWithoutTag>", bean);
  }

  public static class BeanWithPropertyWithoutTagOnPrimitiveValue {
    @Property(surroundWithTag = false)
    public int INT_V = 1;
  }
  public void testPropertyWithoutTagWithPrimitiveType() {
    final BeanWithPropertyWithoutTagOnPrimitiveValue bean = new BeanWithPropertyWithoutTagOnPrimitiveValue();

    try {
      doSerializerTest("<BeanWithFieldWithTagAnnotation><name>hello</name></BeanWithFieldWithTagAnnotation>", bean);
    }
    catch (XmlSerializationException e) {
      return;
    }

    fail("No Exception");
  }

  public static class BeanWithPropertyWithoutTag {
    @Property(surroundWithTag = false)
    public BeanWithPublicFields BEAN1 = new BeanWithPublicFields();
    public int INT_V = 1;
  }
  public void testPropertyWithoutTag() {
    final BeanWithPropertyWithoutTag bean = new BeanWithPropertyWithoutTag();

    doSerializerTest(
      "<BeanWithPropertyWithoutTag>\n" +
      "  <BeanWithPublicFields>\n" +
      "    <option name=\"INT_V\" value=\"1\" />\n" +
      "    <option name=\"STRING_V\" value=\"hello\" />\n" +
      "  </BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "</BeanWithPropertyWithoutTag>",
      bean);

    bean.INT_V = 2;
    bean.BEAN1.STRING_V = "junk";

    doSerializerTest(
      "<BeanWithPropertyWithoutTag>\n" +
      "  <BeanWithPublicFields>\n" +
      "    <option name=\"INT_V\" value=\"1\" />\n" +
      "    <option name=\"STRING_V\" value=\"junk\" />\n" +
      "  </BeanWithPublicFields>\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "</BeanWithPropertyWithoutTag>", bean);
  }

  public static class BeanWithArrayWithoutAllsTag {
    @Property(surroundWithTag = false)
    @AbstractCollection(elementTag = "vvalue", elementValueAttribute = "v", surroundWithTag = false)
    public String[] V = new String[]{"a", "b"};

    public int INT_V = 1;
  }

  public void testArrayWithoutAllTags() {
    final BeanWithArrayWithoutAllsTag bean = new BeanWithArrayWithoutAllsTag();

    doSerializerTest(
      "<BeanWithArrayWithoutAllsTag>\n" +
      "  <vvalue v=\"a\" />\n" +
      "  <vvalue v=\"b\" />\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "</BeanWithArrayWithoutAllsTag>", bean);

    bean.INT_V = 2;
    bean.V = new String[] {"1", "2", "3"};

    doSerializerTest(
      "<BeanWithArrayWithoutAllsTag>\n" +
      "  <vvalue v=\"1\" />\n" +
      "  <vvalue v=\"2\" />\n" +
      "  <vvalue v=\"3\" />\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "</BeanWithArrayWithoutAllsTag>", bean);
  }

  public static class BeanWithArrayWithoutAllsTag2 {
    @Property(surroundWithTag = false)
    @AbstractCollection(elementTag = "vvalue", elementValueAttribute = "", surroundWithTag = false)
    public String[] V = new String[]{"a", "b"};
    public int INT_V = 1;
  }
  public void testArrayWithoutAllTags2() {
    final BeanWithArrayWithoutAllsTag2 bean = new BeanWithArrayWithoutAllsTag2();

    doSerializerTest(
      "<BeanWithArrayWithoutAllsTag2>\n" +
      "  <vvalue>a</vvalue>\n" +
      "  <vvalue>b</vvalue>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "</BeanWithArrayWithoutAllsTag2>", bean);

    bean.INT_V = 2;
    bean.V = new String[] {"1", "2", "3"};

    doSerializerTest(
      "<BeanWithArrayWithoutAllsTag2>\n" +
      "  <vvalue>1</vvalue>\n" +
      "  <vvalue>2</vvalue>\n" +
      "  <vvalue>3</vvalue>\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "</BeanWithArrayWithoutAllsTag2>", bean);
  }

  public void testDeserializeFromFormattedXML() throws Exception {
    String xml = "<BeanWithArrayWithoutAllsTag>\n" +
                 "  <option name=\"INT_V\" value=\"2\"/>\n" +
                 "  <vvalue v=\"1\"/>\n" +
                 "  <vvalue v=\"2\"/>\n" +
                 "  <vvalue v=\"3\"/>\n" +
                 "</BeanWithArrayWithoutAllsTag>";

    BeanWithArrayWithoutAllsTag bean = XmlSerializer.deserialize(JDOMUtil.loadDocument(xml).getRootElement(), BeanWithArrayWithoutAllsTag.class);

    assertEquals(2, bean.INT_V);
    assertEquals("[1, 2, 3]", Arrays.asList(bean.V).toString());
  }

  public static class BeanWithPolymorphicArray {
    @AbstractCollection(elementTypes = {BeanWithPublicFields.class, BeanWithPublicFieldsDescendant.class})
    public BeanWithPublicFields[] V = new BeanWithPublicFields[] {};
  }

  public void testPolymorphicArray() {
    final BeanWithPolymorphicArray bean = new BeanWithPolymorphicArray();

    doSerializerTest(
      "<BeanWithPolymorphicArray>\n" +
      "  <option name=\"V\">\n" +
      "    <array />\n" +
      "  </option>\n" +
      "</BeanWithPolymorphicArray>", bean);

    bean.V = new BeanWithPublicFields[] {new BeanWithPublicFields(), new BeanWithPublicFieldsDescendant(), new BeanWithPublicFields()};

    doSerializerTest(
      "<BeanWithPolymorphicArray>\n" +
      "  <option name=\"V\">\n" +
      "    <array>\n" +
      "      <BeanWithPublicFields>\n" +
      "        <option name=\"INT_V\" value=\"1\" />\n" +
      "        <option name=\"STRING_V\" value=\"hello\" />\n" +
      "      </BeanWithPublicFields>\n" +
      "      <BeanWithPublicFieldsDescendant>\n" +
      "        <option name=\"NEW_S\" value=\"foo\" />\n" +
      "        <option name=\"INT_V\" value=\"1\" />\n" +
      "        <option name=\"STRING_V\" value=\"hello\" />\n" +
      "      </BeanWithPublicFieldsDescendant>\n" +
      "      <BeanWithPublicFields>\n" +
      "        <option name=\"INT_V\" value=\"1\" />\n" +
      "        <option name=\"STRING_V\" value=\"hello\" />\n" +
      "      </BeanWithPublicFields>\n" +
      "    </array>\n" +
      "  </option>\n" +
      "</BeanWithPolymorphicArray>", bean);
  }

  public static class BeanWithPropertiesBoundToAttribute {
    @Attribute( "count")
    public int COUNT = 3;
    @Attribute("name")
    public String name = "James";
    @Attribute("occupation")
    public String occupation;
  }
  public void testBeanWithPrimitivePropertyBoundToAttribute() {
    final BeanWithPropertiesBoundToAttribute bean = new BeanWithPropertiesBoundToAttribute();

    doSerializerTest("<BeanWithPropertiesBoundToAttribute count=\"3\" name=\"James\" />", bean);

    bean.COUNT = 10;
    bean.name = "Bond";

    doSerializerTest("<BeanWithPropertiesBoundToAttribute count=\"10\" name=\"Bond\" />", bean);
  }

  public static class BeanWithPropertyFilter {
    @Property(
      filter = PropertyFilterTest.class
    )
    public String STRING_V = "hello";
  }
  public static class PropertyFilterTest implements SerializationFilter {
    @Override
    public boolean accepts(@NotNull Accessor accessor, @NotNull Object bean) {
      return !accessor.read(bean).equals("skip");
    }
  }
  public void testPropertyFilter() {
    BeanWithPropertyFilter bean = new BeanWithPropertyFilter();

    doSerializerTest(
      "<BeanWithPropertyFilter>\n" +
      "  <option name=\"STRING_V\" value=\"hello\" />\n" +
      "</BeanWithPropertyFilter>", bean);

    bean.STRING_V = "bye";

    doSerializerTest(
      "<BeanWithPropertyFilter>\n" +
      "  <option name=\"STRING_V\" value=\"bye\" />\n" +
      "</BeanWithPropertyFilter>", bean);

    bean.STRING_V = "skip";

    assertSerializer(bean, "<BeanWithPropertyFilter />", null);
  }

  public static class BeanWithJDOMElement {
    public String STRING_V = "hello";
    @Tag("actions")
    public org.jdom.Element actions;
  }

  public void testSerializeJDOMElementField() {
    BeanWithJDOMElement element = new BeanWithJDOMElement();
    element.STRING_V = "a";
    element.actions = new Element("x").addContent(new Element("a")).addContent(new Element("b"));
    assertSerializer(element, "<BeanWithJDOMElement>\n" +
                              "  <option name=\"STRING_V\" value=\"a\" />\n" +
                              "  <actions>\n" +
                              "    <a />\n" +
                              "    <b />\n" +
                              "  </actions>\n" +
                              "</BeanWithJDOMElement>", null);

    element.actions = null;
    assertSerializer(element, "<BeanWithJDOMElement>\n" +
                              "  <option name=\"STRING_V\" value=\"a\" />\n" +
                              "</BeanWithJDOMElement>", null);
  }

  public void testDeserializeJDOMElementField() throws Exception {

    final BeanWithJDOMElement bean = XmlSerializer.deserialize(JDOMUtil.loadDocument(
      "<BeanWithJDOMElement><option name=\"STRING_V\" value=\"bye\"/><actions><action/><action/></actions></BeanWithJDOMElement>"
    ).getRootElement(), BeanWithJDOMElement.class);

    assertEquals("bye", bean.STRING_V);
    assertNotNull(bean.actions);
    assertEquals(2, bean.actions.getChildren("action").size());
  }

  public static class BeanWithJDOMElementArray {
    public String STRING_V = "hello";
    @Tag("actions")
    public org.jdom.Element[] actions;
  }

  public void testJDOMElementArrayField() throws Exception {
    String text = "<BeanWithJDOMElementArray>\n" +
                  "  <option name=\"STRING_V\" value=\"bye\" />\n" +
                  "  <actions>\n" +
                  "    <action />\n" +
                  "    <action />\n" +
                  "  </actions>\n" +
                  "  <actions>\n" +
                  "    <action />\n" +
                  "  </actions>\n" +
                  "</BeanWithJDOMElementArray>";
    final BeanWithJDOMElementArray bean = XmlSerializer.deserialize(JDOMUtil.loadDocument(text
    ).getRootElement(), BeanWithJDOMElementArray.class);

    assertEquals("bye", bean.STRING_V);
    assertNotNull(bean.actions);
    assertEquals(2, bean.actions.length);
    assertEquals(2, bean.actions[0].getChildren().size());
    assertEquals(1, bean.actions[1].getChildren().size());

    assertSerializer(bean, text, null);

    bean.actions = null;
    String newText = "<BeanWithJDOMElementArray>\n" +
                     "  <option name=\"STRING_V\" value=\"bye\" />\n" +
                     "</BeanWithJDOMElementArray>";
    doSerializerTest(newText, bean);

    bean.actions = new Element[0];
    doSerializerTest(newText, bean);
  }

  public static class BeanWithTextAnnotation {
    public int INT_V = 1;
    @Text
    public String STRING_V = "hello";

    public BeanWithTextAnnotation(final int INT_V, final String STRING_V) {
      this.INT_V = INT_V;
      this.STRING_V = STRING_V;
    }

    public BeanWithTextAnnotation() {
    }
  }

  public void testTextAnnotation() {
    BeanWithTextAnnotation bean = new BeanWithTextAnnotation();

    doSerializerTest(
      "<BeanWithTextAnnotation>\n" +
      "  <option name=\"INT_V\" value=\"1\" />\n" +
      "  hello\n" +
      "</BeanWithTextAnnotation>", bean);

    bean.INT_V = 2;
    bean.STRING_V = "bye";

    doSerializerTest(
      "<BeanWithTextAnnotation>\n" +
      "  <option name=\"INT_V\" value=\"2\" />\n" +
      "  bye\n" +
      "</BeanWithTextAnnotation>", bean);
  }

  public static enum TestEnum {
    VALUE_1,
    VALUE_2,
    VALUE_3;
  }

  public static class BeanWithEnum {
    public TestEnum FLD = TestEnum.VALUE_1;
  }

  public void testEnums() {
    BeanWithEnum bean = new BeanWithEnum();

    doSerializerTest(
      "<BeanWithEnum>\n" + "  <option name=\"FLD\" value=\"VALUE_1\" />\n" + "</BeanWithEnum>", bean);

    bean.FLD = TestEnum.VALUE_3;

    doSerializerTest(
      "<BeanWithEnum>\n" + "  <option name=\"FLD\" value=\"VALUE_3\" />\n" + "</BeanWithEnum>", bean);
  }

  public static class BeanWithSetKeysInMap {
    public Map<Collection<String>, String> myMap = new LinkedHashMap<Collection<String>, String>();
  }

  public void testSetKeysInMap() {
    final BeanWithSetKeysInMap bean = new BeanWithSetKeysInMap();
    bean.myMap.put(new LinkedHashSet<String>(Arrays.asList("a", "b", "c")), "letters");
    bean.myMap.put(new LinkedHashSet<String>(Arrays.asList("1", "2", "3")), "numbers");

    BeanWithSetKeysInMap bb = doSerializerTest(
      "<BeanWithSetKeysInMap>\n" +
      "  <option name=\"myMap\">\n" +
      "    <map>\n" +
      "      <entry value=\"letters\">\n" +
      "        <key>\n" +
      "          <set>\n" +
      "            <option value=\"a\" />\n" +
      "            <option value=\"b\" />\n" +
      "            <option value=\"c\" />\n" +
      "          </set>\n" +
      "        </key>\n" +
      "      </entry>\n" +
      "      <entry value=\"numbers\">\n" +
      "        <key>\n" +
      "          <set>\n" +
      "            <option value=\"1\" />\n" +
      "            <option value=\"2\" />\n" +
      "            <option value=\"3\" />\n" +
      "          </set>\n" +
      "        </key>\n" +
      "      </entry>\n" +
      "    </map>\n" +
      "  </option>\n" +
      "</BeanWithSetKeysInMap>",
      bean);

    for (Collection<String> collection : bb.myMap.keySet()) {
      assertTrue(collection instanceof Set);
    }
  }

  public static class ConversionFromTextToAttributeBean {
    @Property(surroundWithTag = false)
    public ConditionBean myConditionBean = new ConditionBean();
  }
  @Tag("condition")
  public static class ConditionBean {
    @Attribute("expression")
    public String myNewCondition;
    @Text
    public String myOldCondition;
  }

  public void testConversionFromTextToAttribute() {
    ConversionFromTextToAttributeBean bean = new ConversionFromTextToAttributeBean();
    bean.myConditionBean.myOldCondition = "2+2";
    doSerializerTest("<ConversionFromTextToAttributeBean>\n" +
                     "  <condition>2+2</condition>\n" +
                     "</ConversionFromTextToAttributeBean>", bean);

    bean = new ConversionFromTextToAttributeBean();
    bean.myConditionBean.myNewCondition = "2+2";
    doSerializerTest("<ConversionFromTextToAttributeBean>\n" +
                     "  <condition expression=\"2+2\" />\n" +
                     "</ConversionFromTextToAttributeBean>", bean);
  }

  public void testDeserializeInto() throws Exception {
    BeanWithPublicFields bean = new BeanWithPublicFields();
    bean.STRING_V = "zzz";

    String xml = "<BeanWithPublicFields><option name=\"INT_V\" value=\"999\"/></BeanWithPublicFields>";
    XmlSerializer.deserializeInto(bean, JDOMUtil.loadDocument(xml).getRootElement());

    assertEquals(999, bean.INT_V);
    assertEquals("zzz", bean.STRING_V);
  }

  public static class BeanWithMapWithoutSurround {
    @Tag("map")
    @MapAnnotation(surroundWithTag = false, entryTagName = "pair", surroundKeyWithTag = false, surroundValueWithTag = false)
    public Map<BeanWithPublicFields, BeanWithTextAnnotation> MAP = new LinkedHashMap<BeanWithPublicFields, BeanWithTextAnnotation>();
  }

  public void testMapWithNotSurroundingKeyAndValue() {
    BeanWithMapWithoutSurround bean = new BeanWithMapWithoutSurround();

    bean.MAP.put(new BeanWithPublicFields(1, "a"), new BeanWithTextAnnotation(2, "b"));
    bean.MAP.put(new BeanWithPublicFields(3, "c"), new BeanWithTextAnnotation(4, "d"));
    bean.MAP.put(new BeanWithPublicFields(5, "e"), new BeanWithTextAnnotation(6, "f"));

    doSerializerTest(
      "<BeanWithMapWithoutSurround>\n" +
      "  <map>\n" +
      "    <pair>\n" +
      "      <BeanWithPublicFields>\n" +
      "        <option name=\"INT_V\" value=\"1\" />\n" +
      "        <option name=\"STRING_V\" value=\"a\" />\n" +
      "      </BeanWithPublicFields>\n" +
      "      <BeanWithTextAnnotation>\n" +
      "        <option name=\"INT_V\" value=\"2\" />\n" +
      "        b\n" +
      "      </BeanWithTextAnnotation>\n" +
      "    </pair>\n" + "    <pair>\n" +
      "      <BeanWithPublicFields>\n" +
      "        <option name=\"INT_V\" value=\"3\" />\n" +
      "        <option name=\"STRING_V\" value=\"c\" />\n" +
      "      </BeanWithPublicFields>\n" +
      "      <BeanWithTextAnnotation>\n" +
      "        <option name=\"INT_V\" value=\"4\" />\n" +
      "        d\n" +
      "      </BeanWithTextAnnotation>\n" +
      "    </pair>\n" +
      "    <pair>\n" +
      "      <BeanWithPublicFields>\n" +
      "        <option name=\"INT_V\" value=\"5\" />\n" +
      "        <option name=\"STRING_V\" value=\"e\" />\n" +
      "      </BeanWithPublicFields>\n" +
      "      <BeanWithTextAnnotation>\n" +
      "        <option name=\"INT_V\" value=\"6\" />\n" +
      "        f\n" +
      "      </BeanWithTextAnnotation>\n" +
      "    </pair>\n" +
      "  </map>\n" +
      "</BeanWithMapWithoutSurround>",
      bean);
  }

  public static class BeanWithMapAtTopLevel {
    @Property(surroundWithTag = false)
    @MapAnnotation(surroundWithTag = false, surroundKeyWithTag = false, surroundValueWithTag = false)
    public Map<String, String> map = new LinkedHashMap<String, String>();

    public String option;
  }

  public void testMapAtTopLevel() {
    BeanWithMapAtTopLevel bean = new BeanWithMapAtTopLevel();
    bean.map.put("a", "b");
    bean.option = "xxx";
    doSerializerTest("<BeanWithMapAtTopLevel>\n" +
                     "  <entry key=\"a\" value=\"b\" />\n" +
                     "  <option name=\"option\" value=\"xxx\" />\n" +
                     "</BeanWithMapAtTopLevel>", bean);
  }

  private static class BeanWithConverter {
    private static class MyConverter extends Converter<Ref<String>> {
      @Nullable
      @Override
      public Ref<String> fromString(@NotNull String value) {
        return Ref.create(value);
      }

      @NotNull
      @Override
      public String toString(@NotNull Ref<String> o) {
        return StringUtil.notNullize(o.get());
      }
    }

    @Attribute(converter = MyConverter.class)
    public Ref<String> foo;

    @OptionTag(converter = MyConverter.class)
    public Ref<String> bar;
  }

  public void testConverter() {
    BeanWithConverter bean = new BeanWithConverter();
    doSerializerTest("<BeanWithConverter>\n" +
                     "  <option name=\"bar\" />\n" +
                     "</BeanWithConverter>", bean);

    bean.foo = Ref.create("testValue");
    doSerializerTest("<BeanWithConverter foo=\"testValue\">\n" +
                     "  <option name=\"bar\" />\n" +
                     "</BeanWithConverter>", bean);

    bean.foo = Ref.create();
    bean.bar = Ref.create("testValue2");
    doSerializerTest("<BeanWithConverter foo=\"\">\n" +
                     "  <option name=\"bar\" value=\"testValue2\" />\n" +
                     "</BeanWithConverter>", bean);
  }

  public void testConverterUsingSkipDefaultsFilter() {
    BeanWithConverter bean = new BeanWithConverter();
    doSerializerTest("<BeanWithConverter />", bean, new SkipDefaultsSerializationFilter());

    bean.foo = Ref.create("testValue");
    doSerializerTest("<BeanWithConverter foo=\"testValue\" />", bean, new SkipDefaultsSerializationFilter());

    bean.foo = Ref.create();
    bean.bar = Ref.create("testValue2");
    doSerializerTest("<BeanWithConverter foo=\"\">\n" +
                     "  <option name=\"bar\" value=\"testValue2\" />\n" +
                     "</BeanWithConverter>", bean);
  }

  private static class BeanWithDefaultAttributeName {
    @Attribute
    public String getFoo() {
      return "foo";
    }

    public void setFoo(@SuppressWarnings("UnusedParameters") String value) {
    }
  }

  public void testDefaultAttributeName() {
    BeanWithDefaultAttributeName bean = new BeanWithDefaultAttributeName();
    doSerializerTest("<BeanWithDefaultAttributeName foo=\"foo\" />", bean);
  }

  static class Bean2 {
    @Attribute
    public String ab;

    @Attribute
    public String module;

    @Attribute
    public String ac;
  }

  public void testOrdered() throws IOException, JDOMException {
    Bean2 bean = new Bean2();
    bean.module = "module";
    bean.ab = "ab";
    doSerializerTest("<Bean2 ab=\"ab\" module=\"module\" />", bean, new SkipDefaultsSerializationFilter());

    checkSmartSerialization(new Bean2(), "<Bean2 module=\"1\" ab=\"2\" ac=\"32\" />");
    checkSmartSerialization(new Bean2(), "<Bean2 ab=\"2\" module=\"1\" ac=\"32\" />");
    checkSmartSerialization(new Bean2(), "<Bean2 ac=\"2\" module=\"1\" ab=\"32\" />");
    checkSmartSerialization(new Bean2(), "<Bean2 ac=\"2\" ab=\"32\" />");
    checkSmartSerialization(new Bean2(), "<Bean2 ac=\"2\" ab=\"32\" module=\"\" />");
  }

  @SuppressWarnings("deprecation")
  @Tag("b")
  static class Bean3 {
    public JDOMExternalizableStringList list = new JDOMExternalizableStringList();
  }

  @Tag("b")
  static class Bean4 {
    @CollectionBean
    public final List<String> list = new SmartList<String>();
  }

  @SuppressWarnings("deprecation")
  public void testJDOMExternalizableStringList() throws IOException, JDOMException {
    Bean3 bean = new Bean3();
    bean.list.add("one");
    bean.list.add("two");
    bean.list.add("three");
    doSerializerTest("<b>\n" +
                     "  <list>\n" +
                     "    <item value=\"one\" />\n" +
                     "    <item value=\"two\" />\n" +
                     "    <item value=\"three\" />\n" +
                     "  </list>\n" +
                     "</b>", bean, new SkipDefaultsSerializationFilter());
  }

  public void testCollectionBean() throws IOException, JDOMException {
    Bean4 bean = new Bean4();
    bean.list.add("one");
    bean.list.add("two");
    bean.list.add("three");
    doSerializerTest("<b>\n" +
                     "  <list>\n" +
                     "    <item value=\"one\" />\n" +
                     "    <item value=\"two\" />\n" +
                     "    <item value=\"three\" />\n" +
                     "  </list>\n" +
                     "</b>", bean, new SkipDefaultsSerializationFilter());
  }

  public void testCollectionBeanReadJDOMExternalizableStringList() throws IOException, JDOMException {
    @SuppressWarnings("deprecation")
    JDOMExternalizableStringList list = new JDOMExternalizableStringList();
    list.add("one");
    list.add("two");
    list.add("three");

    Element value = new Element("value");
    list.writeExternal(value);
    Bean4 o = XmlSerializer.deserialize(new Element("state").addContent(new Element("option").setAttribute("name", "myList").addContent(value)), Bean4.class);
    assertSerializer(o, "<b>\n" +
                        "  <list>\n" +
                        "    <item value=\"one\" />\n" +
                        "    <item value=\"two\" />\n" +
                        "    <item value=\"three\" />\n" +
                        "  </list>\n" +
                        "</b>", "Deserialization failure", new SkipDefaultsSerializationFilter());
  }

  private static void checkSmartSerialization(@NotNull Bean2 bean, @NotNull String serialized) throws IOException, JDOMException {
    SmartSerializer serializer = new SmartSerializer();
    serializer.readExternal(bean, JDOMUtil.loadDocument(serialized).getRootElement());
    Element serializedState = new Element("Bean2");
    serializer.writeExternal(bean, serializedState);
    assertEquals(serialized, JDOMUtil.writeElement(serializedState));
  }

  //---------------------------------------------------------------------------------------------------
  private static Element assertSerializer(Object bean, String expected, SerializationFilter filter) {
    return assertSerializer(bean, expected, "Serialization failure", filter);
  }

  private static <T> T doSerializerTest(@Language("XML") String expectedText, T bean) {
    return doSerializerTest(expectedText, bean, null);
  }

  private static <T> T doSerializerTest(@Language("XML") String expectedText, T bean, @Nullable SerializationFilter filter) {
    Element element = assertSerializer(bean, expectedText, filter);

    //test deserializer
    @SuppressWarnings("unchecked")
    Class<T> aClass = (Class<T>)bean.getClass();
    T o = XmlSerializer.deserialize(element, aClass);
    assertSerializer(o, expectedText, "Deserialization failure", filter);
    return o;
  }

  private static Element assertSerializer(Object bean, String expectedText, String message, SerializationFilter filter) throws XmlSerializationException {
    Element element = serialize(bean, filter);
    String actualString = JDOMUtil.writeElement(element, "\n").trim();
    if (!expectedText.startsWith(XML_PREFIX)) {
      if (actualString.startsWith(XML_PREFIX)) actualString = actualString.substring(XML_PREFIX.length()).trim();
    }

    assertEquals(message, expectedText, actualString);
    return element;
  }

  public static class BeanWithMapWithSetValue {
    @MapAnnotation(entryTagName = "entry-tag", keyAttributeName = "key-attr", surroundWithTag = false)
    public Map<String, Set<String>> myValues = new LinkedHashMap<String, Set<String>>();
  }

  public void testBeanWithMapWithSetValue() {
    BeanWithMapWithSetValue bean = new BeanWithMapWithSetValue();

    bean.myValues.put("a", new LinkedHashSet<String>(Arrays.asList("first1", "second1")));
    bean.myValues.put("b", new LinkedHashSet<String>(Arrays.asList("first2", "second2")));

    doSerializerTest(
      "<BeanWithMapWithSetValue>\n" +
      "  <option name=\"myValues\">\n" +
      "    <entry-tag key-attr=\"a\">\n" +
      "      <value>\n" +
      "        <set>\n" +
      "          <option value=\"first1\" />\n" +
      "          <option value=\"second1\" />\n" +
      "        </set>\n" +
      "      </value>\n" +
      "    </entry-tag>\n" +
      "    <entry-tag key-attr=\"b\">\n" +
      "      <value>\n" +
      "        <set>\n" +
      "          <option value=\"first2\" />\n" +
      "          <option value=\"second2\" />\n" +
      "        </set>\n" +
      "      </value>\n" +
      "    </entry-tag>\n" +
      "  </option>\n" +
      "</BeanWithMapWithSetValue>",
      bean);
  }

  private static Element serialize(Object bean, SerializationFilter filter) {
    return XmlSerializer.serialize(bean, filter);
  }
}


text:
from textx.metamodel import metamodel_from_file, metamodel_from_str
from textx.model import get_children_of_type, get_parent_of_type, \
    get_model, get_metamodel, get_children, get_location, textx_isinstance, \
    textxerror_wrap
from textx.exceptions import TextXError, TextXSyntaxError, \
    TextXSemanticError, TextXRegistrationError
from textx.registration import (LanguageDesc, GeneratorDesc,
                                register_language, register_generator,
                                language_descriptions, language_description,
                                generator_descriptions, generator_description,
                                clear_language_registrations,
                                clear_generator_registrations,
                                languages_for_file,
                                language_for_file,
                                metamodel_for_language,
                                metamodel_for_file,
                                metamodels_for_file,
                                generator_for_language_target,
                                generator, language)

__version__ = "3.1.0.dev"

redpajama_set_name: RedPajamaGithub


text:
/* 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.flowable.bpmn.model;

/**
 * @author Tijs Rademakers
 */
public abstract class Artifact extends BaseElement {

    @Override
    public abstract Artifact clone();
}


text:
transcript_list=$1
stemmer=$2
gramSize=$3

for transcript in $transcript_list; do
	python audio_features/cue.py  $transcript $stemmer $gramSize
done


text:
<?php

namespace ChrisNoden\Tests\Graph\Object;

use ChrisNoden\Facebook\Graph\AccessToken\AppAccessToken;
use ChrisNoden\Facebook\Graph\Object\Application;
use ChrisNoden\Facebook\Graph\Object\Application\Subscription;

class ApplicationTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var int The Graffiti app
     */
    private $test_app_id = 2439131959;

    private $testSubscriptionArray = array(
        array(
            "object"       => "user",
            "callback_url" => "http://realtime.myapp.com/callback",
            "fields"       => array("hometown", "friends"),
            "active"       => true
        ),
        array(
            "object"       => "page",
            "callback_url" => "http://realtime.myapp.com/callback",
            "fields"       => array("checkins"),
            "active"       => true
        )
    );

    public function testBasicObject()
    {
        $obj = new Application();
        $this->assertInstanceOf('ChrisNoden\Facebook\Graph\Object\Application', $obj);
        $this->assertEquals('Application', $obj->__toString());
    }

    public function testFactoryMethod()
    {
        $obj = new Application();
        $obj->load($this->test_app_id);
        $this->assertInstanceOf('ChrisNoden\Facebook\Graph\Object\Application', $obj);
        $this->assertEquals('Graffiti ', $obj->getName());
        $this->assertEquals(
            'http://www.facebook.com/apps/application.php?id=2439131959',
            $obj->getLink()
        );
        $this->assertEquals('graffitiwall', $obj->getNamespace());
    }

    /**
     * Ask for specific fields from Graph and ensure they are the only ones returned
     * (plus ID which is always returned)
     */
    public function testInclusiveFieldRequest()
    {
        $obj            = new Application();
        $include_fields = array(
            'description',
            'subcategory'
        );
        $obj->load($this->test_app_id, $include_fields);
        $fields = $obj->getFieldList();
        $this->assertEquals($this->test_app_id, $obj->getId());
        $this->assertEquals('Draw for your friends.', $obj->getDescription());
        $this->assertEquals('Other', $obj->getSubcategory());
        foreach ($fields as $field => $aData) {
            if ($field != 'id' && isset($aData['value']) && !in_array($field, $include_fields)) {
                $this->fail('ObjectAbstract->load() has fetched an unnecessary field from Graph - ' . $field);
            }
        }
    }

    public function testSetSubscriptionsFromArray()
    {
        $obj = new Application();
        $obj->setSubscriptions($this->testSubscriptionArray);
        $subs = $obj->getSubscriptions();
        $this->assertEquals(
            count($this->testSubscriptionArray),
            count($subs),
            'Expecting ' . count($this->testSubscriptionArray) . ' Subscriptions'
        );
        /** @var Subscription $subscription */
        foreach ($subs as $key => $subscription) {
            foreach ($subscription as $name => $value) {
                if (!isset($this->testSubscriptionArray[$key])) {
                    $this->fail('Extra Subscription object created');
                } elseif (!isset($this->testSubscriptionArray[$key][$name])) {
                    $this->fail('Extra Subscription parameter created');
                }
                $this->assertEquals($this->testSubscriptionArray[$key][$name], $value);
            }
        }
    }

    public function testSetSubscriptionsFromJson()
    {
        $obj = new Application();
        $obj->setSubscriptions(json_encode($this->testSubscriptionArray));
        $subs = $obj->getSubscriptions();
        $this->assertEquals(
            count($this->testSubscriptionArray),
            count($subs),
            'Expecting ' . count($this->testSubscriptionArray) . ' Subscriptions'
        );
        /** @var Subscription $subscription */
        foreach ($subs as $key => $subscription) {
            foreach ($subscription as $name => $value) {
                if (!isset($this->testSubscriptionArray[$key])) {
                    $this->fail('Extra Subscription object created');
                } elseif (!isset($this->testSubscriptionArray[$key][$name])) {
                    $this->fail('Extra Subscription parameter created');
                }
                $this->assertEquals($this->testSubscriptionArray[$key][$name], $value);
            }
        }
    }

    public function testSubscriptionLoadFromFacebook()
    {
        if (defined('TEST_APP_ID') && defined('TEST_APP_SECRET')) {
            $obj = new Application();
            $obj->setId(TEST_APP_ID);
            $obj->setSecret(TEST_APP_SECRET);
            $obj->fetchSubscriptions();
            $arr = $obj->getSubscriptions();
            if (!is_array($arr)) {
                $this->fail('Application::getSubscriptions() not returning an array');
            }
        }
    }

}


text:
from sqlalchemy import create_engine, MetaData, Column, Table, Integer, String, Float
from sqlalchemy.orm import sessionmaker, mapper

__author__ = 'dolel'

SQLALCHEMY_DATABASE_URI = 'mysql://root:FdtWeb2013@localhost/geeknight_recommender'
#Engine, which the Session will use for connection resources
database_engine = create_engine(SQLALCHEMY_DATABASE_URI)
#Creating the Configured Session Class
Session = sessionmaker(bind=database_engine)
session = Session()

class User(object):
    def __init__(self, user_name=None, user_id=None):
        self.user_name = user_name
        self.user_id = user_id

    def save_user(self):
        session.add(self)
        session.commit()
        return True

    def retrieve_users(self):
        return session.query(User).all()

    def retrieve_user_information(self):
        return session.query(User).filter(User.user_id == self.user_id).all()

    def delete_user(self):
        session.query(User).filter(User.user_id == self.user_id).delete()
        session.commit()
        return True

    def retrieve_user_name(self):
        user_name = None
        for user in session.query(User).filter(User.user_id == self.user_id):
            user_name = user.user_name
        return user_name

metadata = MetaData()
user_table = Table('users', metadata,
                   Column('user_id', Integer, primary_key=True, autoincrement=True),
                   Column('user_name', String(20), nullable=False))
mapper(User, user_table)

class Ratings(object):
    def __init__(self, user_id=None, olympus_has_fallen=None, m300=None, expendables=None, lord_of_the_rings=None,
                 pirates_of_the_carribbean=None, harry_potter=None, grown_ups=None, hangover=None, pacific_rim=None,
                 transformers=None, man_of_steel=None, now_you_see_me=None,
                 django_unchained=None, pain_and_gain=None, madagascar=None, despicable_me=None, smurfs=None):
        self.user_id = user_id
        self.olympus_has_fallen = olympus_has_fallen
        self.m300 = m300
        self.expendables = expendables
        self.lord_of_the_rings = lord_of_the_rings
        self.pirates_of_the_carribbean = pirates_of_the_carribbean
        self.harry_potter = harry_potter
        self.grown_ups = grown_ups
        self.hangover = hangover
        self.pacific_rim = pacific_rim
        self.transformers = transformers
        self.man_of_steel = man_of_steel
        self.now_you_see_me = now_you_see_me
        self.django_unchained = django_unchained
        self.pain_and_gain = pain_and_gain
        self.madagascar = madagascar
        self.despicable_me = despicable_me
        self.smurfs = smurfs

    def save_ratings(self):
        if session.query(Ratings).filter(Ratings.user_id == self.user_id).all().__len__() == 0:
            session.add(self)
            session.commit()
            return True
        else:
            return False

    def retrieve_user_ratings(self):
        return session.query(Ratings).filter(Ratings.user_id == self.user_id).all()

    def retrieve_all_user_ratings(self):
        user_ratings = {}
        new_user_ratings = {}
        movies_to_remove = []
        for user_rating in session.query(Ratings).all():
            user_ratings[user_rating.user_id] = {'olympus_has_fallen': user_rating.olympus_has_fallen,
                                                 'm300': user_rating.m300,
                                                 'lord_of_the_rings': user_rating.lord_of_the_rings,
                                                 'expendables': user_rating.expendables,
                                                 'pirates_of_the_carribbean': user_rating.pirates_of_the_carribbean,
                                                 'harry_potter': user_rating.harry_potter,
                                                 'grown_ups': user_rating.grown_ups,
                                                 'hangover': user_rating.hangover,
                                                 'pacific_rim': user_rating.pacific_rim,
                                                 'transformers': user_rating.transformers,
                                                 'man_of_steel': user_rating.man_of_steel,
                                                 'now_you_see_me': user_rating.now_you_see_me,
                                                 'django_unchained': user_rating.django_unchained,
                                                 'pain_and_gain': user_rating.pain_and_gain,
                                                 'madagascar': user_rating.madagascar,
                                                 'despicable_me': user_rating.despicable_me,
                                                 'smurfs': user_rating.smurfs}

        for user_id in user_ratings:
            for movie_name, movie_rating in user_ratings[user_id].items():
                if movie_rating < 1.0:
                    del user_ratings[user_id][movie_name]
        return user_ratings

metadata = MetaData()
ratings_table = Table('ratings', metadata,
                      Column('user_id', Integer, primary_key=True, autoincrement=True),
                      Column('olympus_has_fallen', Float, nullable=True),
                      Column('m300', Float, nullable=True),
                      Column('expendables', Float, nullable=True),
                      Column('lord_of_the_rings', Float, nullable=True),
                      Column('pirates_of_the_carribbean', Float, nullable=True),
                      Column('harry_potter', Float, nullable=True),
                      Column('grown_ups', Float, nullable=True),
                      Column('hangover', Float, nullable=True),
                      Column('pacific_rim', Float, nullable=True),
                      Column('transformers', Float, nullable=True),
                      Column('man_of_steel', Float, nullable=True),
                      Column('now_you_see_me', Float, nullable=True),
                      Column('django_unchained', Float, nullable=True),
                      Column('pain_and_gain', Float, nullable=True),
                      Column('madagascar', Float, nullable=True),
                      Column('despicable_me', Float, nullable=True),
                      Column('smurfs', Float, nullable=True))
mapper(Ratings, ratings_table)


text:
ES5Harness.registerTest( {
id: "12.2.1-13-s",

path: "TestCases/chapter12/12.2/12.2.1/12.2.1-13-s.js",

description: "arguments as global var identifier throws SyntaxError in strict mode",

test: function testcase() {
    var indirectEval = eval;
    try {indirectEval("'use strict'; var arguments;");}
    catch (e) {
       return e instanceof SyntaxError;}
 },

precondition: function () {
   //indirect eval must be globally scoped
   var localVar = "12.2.1-13-s";
   var indirectEval = eval;
   try {
	if (indirectEval("localVar;")==="12.2.1-13-s" ) return false;
   } catch (e) {};
   return fnSupportsStrict();
}

});


text:
namespace google { namespace protobuf { namespace compiler { namespace swift {

    namespace {
        void SetMessageVariables(const FieldDescriptor* descriptor,
                                 map<string, string>* variables) {
            std::string name = UnderscoresToCamelCase(descriptor);
            (*variables)["classname"] = ClassName(descriptor->containing_type());
            (*variables)["name"] = name;
            (*variables)["capitalized_name"] = UnderscoresToCapitalizedCamelCase(descriptor);
            (*variables)["number"] = SimpleItoa(descriptor->number());
            (*variables)["type"] = ClassName(descriptor->message_type());
            if (IsPrimitiveType(GetSwiftType(descriptor))) {
                (*variables)["storage_type"] = ClassName(descriptor->message_type());
                (*variables)["storage_attribute"] = "";
            } else {
                (*variables)["storage_type"] = string(ClassName(descriptor->message_type()));
                (*variables)["storage_attribute"] = "";
            }
            (*variables)["group_or_message"] =
            (descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
            "Group" : "Message";

            (* variables)["acontrol"] = GetAccessControlTypeForFields(descriptor->file());
            (* variables)["acontrolFunc"] = GetAccessControlType(descriptor->file());

            if (isOneOfField(descriptor)) {
                const OneofDescriptor* oneof = descriptor->containing_oneof();
                (*variables)["oneof_name"] = UnderscoresToCapitalizedCamelCase(oneof->name());
                (*variables)["oneof_class_name"] = ClassNameOneof(oneof);
            }
        }
    }  // namespace

    MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor)
    : descriptor_(descriptor) {
        SetMessageVariables(descriptor, &variables_);
    }

    MessageFieldGenerator::~MessageFieldGenerator() {
    }

    void MessageFieldGenerator::GenerateExtensionSource(io::Printer* printer) const {
        //
        //      printer->Print(variables_,"@property (strong)$storage_attribute$ $storage_type$ $name$;\n");
    }

    void MessageFieldGenerator::GenerateSynthesizeSource(io::Printer* printer) const {
        if (isOneOfField(descriptor_)) {

            printer->Print(variables_,"$acontrol$private(set) var has$capitalized_name$:Bool {\n"
                           "      get {\n"
                           "           if $oneof_class_name$.get$capitalized_name$(storage$oneof_name$) == nil {\n"
                           "               return false\n"
                           "           }\n"
                           "           return true\n"
                           "      }\n"
                           "      set(newValue) {\n"
                           "      }\n"
                           "}\n");

            printer->Print(variables_,"$acontrol$private(set) var $name$:$type$!{\n"
                           "     get {\n"
                           "          return $oneof_class_name$.get$capitalized_name$(storage$oneof_name$)\n"
                           "     }\n"
                           "     set (newvalue) {\n"
                           "          storage$oneof_name$ = $oneof_class_name$.$capitalized_name$(newvalue)\n"
                           "     }\n"
                           "}\n");
        }
        else {
            printer->Print(variables_, "$acontrol$private(set) var has$capitalized_name$:Bool = false\n");
            printer->Print(variables_, "$acontrol$private(set) var $name$:$type$ = $type$()\n");
        }

    }

    void MessageFieldGenerator::GenerateInitializationSource(io::Printer* printer) const {
    }

    void MessageFieldGenerator::GenerateBuilderMembersSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "$acontrol$var has$capitalized_name$:Bool {\n"
                       "     get {\n"
                       "         return builderResult.has$capitalized_name$\n"
                       "     }\n"
                       "}\n"
                       "$acontrol$var $name$:$storage_type$ {\n"
                       "     get {\n"
                       "         return builderResult.$name$\n"
                       "     }\n"
                       "     set (value) {\n"
                       "         builderResult.has$capitalized_name$ = true\n"
                       "         builderResult.$name$ = value\n"
                       "     }\n"
                       "}\n"
                       "func set$capitalized_name$(value:$storage_type$)-> $classname$Builder {\n"
                       "  self.$name$ = value\n"
                       "  return self\n"
                       "}\n"
                       "$acontrolFunc$ func merge$capitalized_name$(value:$storage_type$) -> $classname$Builder {\n"
                       "  if (builderResult.has$capitalized_name$) {\n"
                       "    builderResult.$name$ = $type$.builderWithPrototype(builderResult.$name$).mergeFrom(value).buildPartial()\n"
                       "  } else {\n"
                       "    builderResult.$name$ = value\n"
                       "  }\n"
                       "  builderResult.has$capitalized_name$ = true\n"
                       "  return self\n"
                       "}\n"
                       "$acontrolFunc$ func clear$capitalized_name$() -> $classname$Builder {\n"
                       "  builderResult.has$capitalized_name$ = false\n"
                       "  builderResult.$name$ = $type$()\n"
                       "  return self\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateMergingCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "if (other.has$capitalized_name$) {\n"
                       "    merge$capitalized_name$(other.$name$)\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateBuildingCodeSource(io::Printer* printer) const {
    }

    void MessageFieldGenerator::GenerateParsingCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "var subBuilder:$type$Builder = $type$.builder()\n"
                       "if has$capitalized_name$ {\n"
                       "  subBuilder.mergeFrom($name$)\n"
                       "}\n");

        if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
            printer->Print(variables_,
                           "input.readGroup($number$, builder:subBuilder, extensionRegistry:extensionRegistry)\n");
        } else {
            printer->Print(variables_,
                           "input.readMessage(subBuilder, extensionRegistry:extensionRegistry)\n");
        }

        printer->Print(variables_,
                       "$name$ = subBuilder.buildPartial()\n");
    }

    void MessageFieldGenerator::GenerateSerializationCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "if has$capitalized_name$ {\n"
                       "  output.write$group_or_message$($number$, value:$name$)\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateSerializedSizeCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "if has$capitalized_name$ {\n"
                       "  serialize_size += $name$.compute$group_or_message$Size($number$)\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateDescriptionCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "if has$capitalized_name$ {\n"
                       "  output += \"\\(indent) $name$ {\\n\"\n"
                       "  $name$.writeDescriptionTo(&output, indent:\"\\(indent)  \")\n"
                       "  output += \"\\(indent) }\\n\"\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateIsEqualCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "(lhs.has$capitalized_name$ == rhs.has$capitalized_name$) && (!lhs.has$capitalized_name$ || lhs.$name$ == rhs.$name$)");
    }

    void MessageFieldGenerator::GenerateHashCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "if has$capitalized_name$ {\n"
                       "  hashCode = (hashCode &* 31) &+ $name$.hashValue\n"
                       "}\n");
    }

    void MessageFieldGenerator::GenerateMembersSource(io::Printer* printer) const {

    }

    string MessageFieldGenerator::GetBoxedType() const {
        return ClassName(descriptor_->message_type());
    }

    RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor)
    : descriptor_(descriptor) {
        SetMessageVariables(descriptor, &variables_);
    }

    RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {
    }

    //TODO
    void RepeatedMessageFieldGenerator::GenerateExtensionSource(io::Printer* printer) const {
    }

    void RepeatedMessageFieldGenerator::GenerateSynthesizeSource(io::Printer* printer) const {

    }

    void RepeatedMessageFieldGenerator::GenerateInitializationSource(io::Printer* printer) const {
        //      printer->Print(variables_, "$name$ = [$storage_type$]()\n");
    }

    void RepeatedMessageFieldGenerator::GenerateMembersSource(io::Printer* printer) const {

        printer->Print(variables_, "$acontrol$private(set) var $name$:Array<$storage_type$>  = Array<$storage_type$>()\n");
    }

    void RepeatedMessageFieldGenerator::GenerateBuilderMembersSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "$acontrol$var $name$:Array<$storage_type$> {\n"
                       "     get {\n"
                       "         return builderResult.$name$\n"
                       "     }\n"
                       "     set (value) {\n"
                       "         builderResult.$name$ = value\n"
                       "     }\n"
                       "}\n"
                       "func set$capitalized_name$(value:Array<$storage_type$>)-> $classname$Builder {\n"
                       "  self.$name$ = value\n"
                       "  return self\n"
                       "}\n"
                       "$acontrolFunc$ func clear$capitalized_name$() -> $classname$Builder {\n"
                       "  builderResult.$name$.removeAll(keepCapacity: false)\n"
                       "  return self\n"
                       "}\n");
    }

    void RepeatedMessageFieldGenerator::GenerateMergingCodeSource(io::Printer* printer) const {

        printer->Print(variables_,
                       "if !other.$name$.isEmpty  {\n"
                       "   builderResult.$name$ += other.$name$\n"
                       "}\n");
    }

    void RepeatedMessageFieldGenerator::GenerateBuildingCodeSource(io::Printer* printer) const {
    }

    void RepeatedMessageFieldGenerator::GenerateParsingCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "var subBuilder = $type$.builder()\n");

        if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
            printer->Print(variables_,
                           "input.readGroup($number$,builder:subBuilder,extensionRegistry:extensionRegistry)\n");
        } else {
            printer->Print(variables_,
                           "input.readMessage(subBuilder,extensionRegistry:extensionRegistry)\n");
        }

        printer->Print(variables_,
                       "$name$ += [subBuilder.buildPartial()]\n");
    }

    void RepeatedMessageFieldGenerator::GenerateSerializationCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "for oneElement$name$ in $name$ {\n"
                       "    output.write$group_or_message$($number$, value:oneElement$name$)\n"
                       "}\n");
    }

    void RepeatedMessageFieldGenerator::GenerateSerializedSizeCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "for oneElement$name$ in $name$ {\n"
                       "    serialize_size += oneElement$name$.compute$group_or_message$Size($number$)\n"
                       "}\n");
    }

    void RepeatedMessageFieldGenerator::GenerateDescriptionCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "var $name$ElementIndex:Int = 0\n"
                       "for oneElement$name$ in $name$ {\n"
                       "    output += \"\\(indent) $name$[\\($name$ElementIndex)] {\\n\"\n"
                       "    oneElement$name$.writeDescriptionTo(&output, indent:\"\\(indent)  \")\n"
                       "    output += \"\\(indent)}\\n\"\n"
                       "    $name$ElementIndex++\n"
                       "}\n");
    }

    void RepeatedMessageFieldGenerator::GenerateIsEqualCodeSource(io::Printer* printer) const {
        printer->Print(variables_, "(lhs.$name$ == rhs.$name$)");
    }

    void RepeatedMessageFieldGenerator::GenerateHashCodeSource(io::Printer* printer) const {
        printer->Print(variables_,
                       "for oneElement$name$ in $name$ {\n"
                       "    hashCode = (hashCode &* 31) &+ oneElement$name$.hashValue\n"
                       "}\n");
    }

    string RepeatedMessageFieldGenerator::GetBoxedType() const {
        return ClassName(descriptor_->message_type());
    }
}  // namespace swift
}  // namespace compiler
}  // namespace protobuf
}  // namespace google


text:
AMS Short Course on Interacting with Radar Data in The Cloud
============================================================

.. image:: images/evolution.png
   :scale: 150 %
   :alt: Evolution of storage
   :align: center
   :target: http://www.gusmorais.com/

**08:30 - 09:00** `Arrival, introductions and setting up of environments`_::

    Participants will arrive at Amazon by 8:30 and will be shown to the tutorial
    room

    Wifi will be set up and course materials distributed

**09:00 - 10:00** `How to set up an instance in the Amazon Cloud`_::

    An introduction to the concepts of virtualization and Elastic Cloud Compute
    (EC2) instances

    Installation on the EC2 instance of software required for the tutorial

**10:00 - 11:00** `An introduction and primer on Python and Scientific Python`_::

    Python is a general purpose, high-level, interpreted programming language
    that is widely used for scientific computing. During this session Python
    will breifly be introduced as a programming language for working with
    scientific data.  Much of the time will be spent introducing and providing
    background on some of the key libraries used in the scientific Python
    ecosystem including NumPy, SciPy, and matplotlib.

**11:00 - 12:00** `An introduction to Boto, Unidata tools and Py-ART`_::

    During this section additional Python tools, Boto, Siphon, THREDDS, and
    Py-ART, are introducts. These tools are needed or useful for accessing the
    NEXRAD Level 2 archive on Amazon Simple Storage Server (S3). This section
    will walk through using Boto, Amazon's Python library for talking to AWS,
    to access files on S3. Unidata's THREDDS server, and its Siphon Python
    client, will be introduced as a way for accessing the radar data using
    OPeNDAP and for querying available data files. Finally, Py-ART will be
    discussed as a Pythonic solution for analyzing and displaying radar data.

**12:00 - 13:30** `Lunch`_::

    On your own.

**13:30 - 16:00** `Tying it together, using Boto, Py-ART and Siphon to fetch and plot radar data and observations in the cloud`_::

    Group activities where we will use code discussed from earlier to build an
    EC2 instance and python file that can fetch radar and other observations and
    produce a visualization for any site and time in the archive.

**16:00 - 17:00** `Getting it to scale, how to use AWS to set up many processes`_::

    Radar data processing, in general tends to be very pleasantly parallel. This
    section will discuss how you can use instances and Amazon's scheduling tools
    to map many time steps or many sites into a cloud of processes.

**17:00 - 17:30** `Questions answers and feedback`_::

    A chance to ask the tutors questions, present your own use cases and find
    out what is next.

.. raw:: pdf

      PageBreak

Arrival, introductions and setting up of environments
-----------------------------------------------------

Scott Collis, Jonathan Helmus and Mark Korver

Getting hooked into the wifi, testing connections, dissemination of course
materials.

How to set up an instance in the Amazon Cloud
---------------------------------------------

Mark Korver

An introduction to how to set up an EC2 instance and start it running. How to
log into it via SSH and how to run code and get results.

An introduction and primer on Python and Scientific Python
----------------------------------------------------------

Jonathan Helmus and Ryan May

Python is a general purpose, high-level, interpreted programming language
that is widely used for scientific computing. During this session Python
will breifly be introduced as a programming language for working with
scientific data.  Much of the time will be spent introducing and providing
background on some of the key libraries used in the scientific Python
ecosystem including NumPy, SciPy, and matplotlib.

An introduction to Boto, Unidata tools and Py-ART
-------------------------------------------------

Scott Collis and Ryan May

During this section additional Python tools, Boto, Siphon, THREDDS, and
Py-ART, are introducts. These tools are needed or useful for accessing the
NEXRAD Level 2 archive on Amazon Simple Storage Server (S3). This section
will walk through using Boto, Amazon's Python library for talking to AWS,
to access files on S3. Unidata's THREDDS server, and its Siphon Python
client, will be introduced as a way for accessing the radar data using
OPeNDAP and for querying available data files. Finally, Py-ART will be
discussed as a Pythonic solution for analyzing and displaying radar data.

Lunch
-----

Details and suggestions will go here closer to the workshop date.

Tying it together, using Boto, Py-ART and Siphon to fetch and plot radar data and observations in the cloud
-----------------------------------------------------------------------------------------------------------

All Tutors

Group project to build and run some code to create a visualization for one of a
number of specific historical cases. Pulling radar data from S3, using Siphon to
fetch observational data and Matplotlib to plot data overlay.
This is the hands on component where students will be building code. Students
will be provided with a python text file with missing blocks and using the
knowledge gained during the course and with the guidance of the tutors will
create a nice plot.

Getting it to scale, how to use AWS to set up many processes
------------------------------------------------------------

Mark Korver and Scott Collis

Radar data processing, in general tends to be very pleasantly parallel. This
section will discuss how you can use instances and Amazon's scheduling tools
to map many time steps or many sites into a cloud of processes.

Questions answers and feedback
------------------------------

All Tutors

Feedback and further discussion

Render this document as pdf by ``rst2pdf course_outline.rst``


text:
layout: post
title: jooq
date:   2017-08-01 00:00:00 +0800
categories: learning
tag: jooq
---

* content
{:toc}

JOOQ
=======

* DSLContext reference configuration(connection, datasource, etc) like sessionFactory
* sql dialect
* curl -H "Content-Type:application/json" -d '{"id":"1","jsonrpc":"2.0","method":"query"}' http://localhost:8080/disbursement


text: 1. ../data/roast_pork.png. Background black* color result in rgb(255,0,0) exception.


text:
<?php

namespace Hooklife\ThinkphpWechat\Command;
/**
 * Created by PhpStorm.
 * User: hooklife
 * Date: 2017/1/12
 * Time: 15:39
 */
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SendConfig extends Command
{
    public function configure()
    {
        $this->setName('wechat:config')
             ->setDescription('send config to tp folder');
    }
    public function execute(Input $input, Output $output)
    {
        //获取默认配置文件
        $content = file_get_contents(VENDOR_PATH .'hooklife/thinkphp5-wechat/src/config.php');

        $configPath = CONF_PATH.'extra/';
        $configFile = $configPath.'wechat.php';

        //判断目录是否存在
        if (!file_exists($configPath)) {
            mkdir($configPath, 0755, true);
        }

        //判断文件是否存在
        if (is_file($configFile)) {
            throw new \InvalidArgumentException(sprintf('The config file "%s" already exists', $configFile));
        }

        if (false === file_put_contents($configFile, $content)) {
            throw new \RuntimeException(sprintf('The config file "%s" could not be written to "%s"', $configFile,$configPath));
        }

        $output->writeln('create wechat config ok');
    }

}


text:
import PropTypes from 'prop-types';
import React from 'react';
import { findDOMNode } from 'react-dom';
import cn from 'classnames';
import _ from 'lodash';

import dates from './utils/dates';
import { segStyle } from './utils/eventLevels';
import { notify } from './utils/helpers';
import { elementType } from './utils/propTypes';
import { dateCellSelection, slotWidth, getCellAtX, pointInBox } from './utils/selection';
import Selection, { getBoundsForNode, isEvent } from './Selection';

class BackgroundCells extends React.Component {

  static propTypes = {
    cellWrapperComponent: elementType,
    container: PropTypes.func,
    selectable: PropTypes.oneOf([true, false, 'ignoreEvents']),

    onSelectSlot: PropTypes.func.isRequired,
    onSelectEnd: PropTypes.func,
    onSelectStart: PropTypes.func,

    range: PropTypes.arrayOf(
      PropTypes.instanceOf(Date)
    ),

    // same as range, just for disabled days
    disabledRange: PropTypes.arrayOf(
      PropTypes.instanceOf(Date)
    ),

    rtl: PropTypes.bool,
    type: PropTypes.string,

  }

  static defaultProps = {
    disabledRange: [],
  }

  constructor(props, context) {
    super(props, context);

    this.state = {
      selecting: false
    };
  }

  componentDidMount(){
    this.props.selectable
      && this._selectable()
  }

  componentWillUnmount() {
    this._teardownSelectable();
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.selectable && !this.props.selectable)
      this._selectable();

    if (!nextProps.selectable && this.props.selectable)
      this._teardownSelectable();
  }

  render(){
    let { range, cellWrapperComponent: Wrapper, disabledRange } = this.props;
    let { selecting, startIdx, endIdx } = this.state;

    return (
      <div className='rbc-row-bg'>
        {range.map((date, index) => {
          let selected =  selecting && index >= startIdx && index <= endIdx;
          return (
            <Wrapper
              key={index}
              value={date}
              range={range}
            >
              <div
                style={segStyle(1, range.length)}
                className={cn(
                  'rbc-day-bg',
                  selected && 'rbc-selected-cell',
                  dates.isToday(date) && 'rbc-today',
                  disabledRange.some(enabledDate => enabledDate.getTime() === date.getTime()) && 'blocked',
                )}
              />
            </Wrapper>
          )
        })}
      </div>
    )
  }

  _selectable(){
    let node = findDOMNode(this);
    let selector = this._selector = new Selection(this.props.container)

    selector.on('selecting', box => {
      let { range, rtl } = this.props;

      let startIdx = -1;
      let endIdx = -1;

      if (!this.state.selecting) {
        notify(this.props.onSelectStart, [box]);
        this._initial = { x: box.x, y: box.y };
      }
      if (selector.isSelected(node)) {
        let nodeBox = getBoundsForNode(node);

        ({ startIdx, endIdx } = dateCellSelection(
            this._initial
          , nodeBox
          , box
          , range.length
          , rtl));
      }

      this.setState({
        selecting: true,
        startIdx, endIdx
      })
    })

    selector.on('mousedown', (box) => {
      if (this.props.selectable !== 'ignoreEvents') return

      return !isEvent(findDOMNode(this), box)
    })

    selector
      .on('click', point => {
        if (!isEvent(findDOMNode(this), point)) {
          let rowBox = getBoundsForNode(node)
          let { range, rtl } = this.props;

          if (pointInBox(rowBox, point)) {
            let width = slotWidth(getBoundsForNode(node),  range.length);
            let currentCell = getCellAtX(rowBox, point.x, width, rtl, range.length);

            this._selectSlot({
              startIdx: currentCell,
              endIdx: currentCell,
              action: 'click',
            })
          }
        }

        this._initial = {}
        this.setState({ selecting: false })
      })

    selector
      .on('select', () => {
        this._selectSlot({ ...this.state, action: 'select' })
        this._initial = {}
        this.setState({ selecting: false })
        notify(this.props.onSelectEnd, [this.state]);
      })
  }

  _teardownSelectable() {
    if (!this._selector) return
    this._selector.teardown();
    this._selector = null;
  }

  _selectSlot({ endIdx, startIdx, action }) {
    if (endIdx !== -1 && startIdx !== -1)
      this.props.onSelectSlot &&
        this.props.onSelectSlot({
          start: startIdx,
          end: endIdx,
          action
        })
  }
}

export default BackgroundCells;


text:
const {intersectionBy, sortBy} = require('lodash')
const GeoIndex = require('./indexes/geo')
const TokenIndex = require('./indexes/token')
const TextIndex = require('./indexes/text')

const createIndexByType = {
  token: (key, params) => new TokenIndex(key, params),
  tokenList: (key, params) => new TokenIndex(key, {...params, array: true}),
  text: (key, params) => new TextIndex(key, params),
  geo: key => new GeoIndex(key)
}

class SearchableCollection {
  constructor(schema, options = {}) {
    if (!schema) {
      throw new Error('schema is required')
    }

    this._schema = schema
    this._options = options
    this._indexes = {}
    this._queryKeys = {}
    Object.keys(schema).forEach(key => {
      this.processSchemaEntry(key, schema[key])
    })
  }

  processSchemaEntry(key, params) {
    const queryWith = params.queryWith || key
    const index = createIndexByType[params.type](key, params)
    this._indexes[key] = index
    this._queryKeys[queryWith] = index
  }

  load(items) {
    this._data = items
    Object.keys(this._indexes).forEach(indexName => {
      this._indexes[indexName].load(items)
    })
  }

  search(query) {
    const rawResults = []
    for (const key in query) {
      if (key in this._queryKeys) {
        rawResults.push(this._queryKeys[key].find(query[key], query))
      }
    }

    if (rawResults.length === 0) {
      return this._data
    }

    if (rawResults.length === 1) {
      return rawResults[0]
    }

    const orderedResults = sortBy(rawResults, r => {
      return r.length > 0 && '_score' in r[0] ? -1 : 0
    })
    return intersectionBy(...orderedResults, 'code')
  }
}

module.exports = SearchableCollection


text:
import { message } from 'antd'

export const apiAddress = '/api'

export function checkStatus (response) {
  if (response.status >= 200 && response.status < 300) {
    return response
  } else {
    message.error('加载数据失败')
    const error = new Error(response.statusText)
    error.response = response
    throw error
  }
}


text:
namespace {
const char kExtensionScheme[] = "chrome-extension://";
}  // namespace

namespace notifier {

COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kUnread) ==
               sync_pb::CoalescedSyncedNotification_ReadState_UNREAD,
               local_enum_must_match_protobuf_enum);
COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kRead) ==
               sync_pb::CoalescedSyncedNotification_ReadState_READ,
               local_enum_must_match_protobuf_enum);
COMPILE_ASSERT(static_cast<sync_pb::CoalescedSyncedNotification_ReadState>(
                   SyncedNotification::kDismissed) ==
               sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED,
               local_enum_must_match_protobuf_enum);

SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) {
  Update(sync_data);
}

SyncedNotification::~SyncedNotification() {}

void SyncedNotification::Update(const syncer::SyncData& sync_data) {
  specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification());
}

sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const {
  sync_pb::EntitySpecifics entity_specifics;
  entity_specifics.mutable_synced_notification()->CopyFrom(specifics_);
  return entity_specifics;
}

std::string SyncedNotification::title() const {
  return ExtractTitle();
}

std::string SyncedNotification::heading() const {
  return ExtractHeading();
}

std::string SyncedNotification::description() const {
  return ExtractDescription();
}

std::string SyncedNotification::app_id() const {
  return ExtractAppId();
}

std::string SyncedNotification::key() const {
  return ExtractKey();
}

GURL SyncedNotification::origin_url() const {
  return ExtractOriginUrl();
}

GURL SyncedNotification::app_icon_url() const {
  return ExtractAppIconUrl();
}

GURL SyncedNotification::image_url() const {
  return ExtractImageUrl();
}

std::string SyncedNotification::first_external_id() const {
  return ExtractFirstExternalId();
}

std::string SyncedNotification::notification_id() const {
  return ExtractNotificationId();
}

std::string SyncedNotification::text() const {
  return ExtractText();
}

SyncedNotification::ReadState SyncedNotification::read_state() const {
  return ExtractReadState();
}

// TODO(petewil): Consider the timestamp too once it gets added to the protobuf.
bool SyncedNotification::EqualsIgnoringReadState(
    const SyncedNotification& other) const {
  return (title() == other.title() &&
          app_id() == other.app_id() &&
          key() == other.key() &&
          text() == other.text() &&
          origin_url() == other.origin_url() &&
          app_icon_url() == other.app_icon_url() &&
          image_url() == other.image_url() );
}

bool SyncedNotification::IdMatches(const SyncedNotification& other) const {
  // Two notifications have the same ID if the <appId/coalescingKey> pair
  // matches.
  return (notification_id() == other.notification_id());
}

// Set the read state on the notification, returns true for success.
void SyncedNotification::SetReadState(const ReadState& read_state) {

  // convert the read state to the protobuf type for read state
  if (kDismissed == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED);
  else if (kUnread == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_UNREAD);
  else if (kRead == read_state)
    specifics_.mutable_coalesced_notification()->set_read_state(
        sync_pb::CoalescedSyncedNotification_ReadState_READ);
  else
    NOTREACHED();
}

void SyncedNotification::NotificationHasBeenRead() {
  SetReadState(kRead);
}

void SyncedNotification::NotificationHasBeenDismissed() {
  SetReadState(kDismissed);
}

std::string SyncedNotification::ExtractFirstExternalId() const {
  if (!specifics_.has_coalesced_notification() ||
      specifics_.coalesced_notification().notification_size() < 1 ||
      !specifics_.coalesced_notification().notification(0).has_external_id())
    return std::string();

  return specifics_.coalesced_notification().notification(0).external_id();
}

std::string SyncedNotification::ExtractTitle() const {
  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().has_title())
    return "";

  return specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().title();
}

std::string SyncedNotification::ExtractHeading() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().has_heading())
    return "";

  return specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().heading();
}

std::string SyncedNotification::ExtractDescription() const {
  if (!specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().has_description())
    return "";

  return specifics_.coalesced_notification().render_info().collapsed_info().
      simple_collapsed_layout().description();
}

std::string SyncedNotification::ExtractAppId() const {
  if (!specifics_.coalesced_notification().has_app_id())
    return "";
  return specifics_.coalesced_notification().app_id();
}

std::string SyncedNotification::ExtractKey() const {
  if (!specifics_.coalesced_notification().has_key())
    return "";
  return specifics_.coalesced_notification().key();
}

GURL SyncedNotification::ExtractOriginUrl() const {
  std::string origin_url(kExtensionScheme);
  origin_url += app_id();
  return GURL(origin_url);
}

GURL SyncedNotification::ExtractAppIconUrl() const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info_size() == 0)
    return GURL();

  if (!specifics_.coalesced_notification().render_info().expanded_info().
      collapsed_info(0).simple_collapsed_layout().has_app_icon())
    return GURL();

  return GURL(specifics_.coalesced_notification().render_info().
              expanded_info().collapsed_info(0).simple_collapsed_layout().
              app_icon().url());
}

// TODO(petewil): This currenly only handles the first image from the first
// collapsed item, someday return all images.
GURL SyncedNotification::ExtractImageUrl() const {
  if (specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().media_size() == 0)
    return GURL();

  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().media(0).image().has_url())
    return GURL();

  return GURL(specifics_.coalesced_notification().render_info().
              expanded_info().simple_expanded_layout().media(0).image().url());
}

std::string SyncedNotification::ExtractText() const {
  if (!specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().has_text())
    return "";

  return specifics_.coalesced_notification().render_info().expanded_info().
      simple_expanded_layout().text();
}

SyncedNotification::ReadState SyncedNotification::ExtractReadState() const {
  DCHECK(specifics_.coalesced_notification().has_read_state());

  sync_pb::CoalescedSyncedNotification_ReadState found_read_state =
      specifics_.coalesced_notification().read_state();

  if (found_read_state ==
      sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) {
    return kDismissed;
  } else if (found_read_state ==
             sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) {
    return kUnread;
  } else if (found_read_state ==
             sync_pb::CoalescedSyncedNotification_ReadState_READ) {
    return kRead;
  } else {
    NOTREACHED();
    return static_cast<SyncedNotification::ReadState>(found_read_state);
  }
}

std::string SyncedNotification::ExtractNotificationId() const {
  return key();
}

}  // namespace notifier


text:
NSString * const TI_APPLICATION_DEPLOYTYPE = @"production";
NSString * const TI_APPLICATION_ID = @"com.knightfinderapp";
NSString * const TI_APPLICATION_PUBLISHER = @"Knight Application Systems";
NSString * const TI_APPLICATION_URL = @"http://www.knightfinderapp.com";
NSString * const TI_APPLICATION_NAME = @"KnightFinder";
NSString * const TI_APPLICATION_VERSION = @"3.4";
NSString * const TI_APPLICATION_DESCRIPTION = @"Knight Finder for Android and iPhone";
NSString * const TI_APPLICATION_COPYRIGHT = @"2011 by Knight Application Systems";
NSString * const TI_APPLICATION_GUID = @"76f1037c-f371-4982-925c-514695f2e21d";
BOOL const TI_APPLICATION_ANALYTICS = true;

#ifdef TARGET_IPHONE_SIMULATOR
NSString * const TI_APPLICATION_RESOURCE_DIR = @"";
#endif

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

#ifdef __LOG__ID__
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *logPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%s.log",STRING(__LOG__ID__)]];
	freopen([logPath cStringUsingEncoding:NSUTF8StringEncoding],"w+",stderr);
	fprintf(stderr,"[INFO] Application started\n");
#endif

	int retVal = UIApplicationMain(argc, argv, nil, @"TiApp");
    [pool release];
    return retVal;
}


text:
<resources>
    <string name="app_name">mAttendance</string>
    <string name="navigation_drawer_open">Open navigation drawer</string>
    <string name="navigation_drawer_close">Close navigation drawer</string>
    <string name="title_activity_today_attendance">TodayAttendance</string>
    <string name="title_activity_info">InfoActivity</string>

</resources>