/** Testing {@link MediaType#valueOf(String)} and {@link MediaType#register(String, String)} */
  public void testValueOf() {
    assertSame(MediaType.APPLICATION_XML, MediaType.valueOf("application/xml"));
    assertSame(MediaType.ALL, MediaType.valueOf("*/*"));
    final MediaType newType = MediaType.valueOf("application/x-restlet-test");
    assertEquals("application", newType.getMainType());
    assertEquals("x-restlet-test", newType.getSubType());
    assertEquals("application/x-restlet-test", newType.getName());

    // Should not have got registered by call to valueOf() alone
    assertNotSame(newType, MediaType.valueOf("application/x-restlet-test"));

    final MediaType registeredType =
        MediaType.register("application/x-restlet-test", "Restlet testcase");
    assertNotSame(newType, registeredType); // didn't touch old value
    assertEquals("application/x-restlet-test", registeredType.getName());
    assertEquals("Restlet testcase", registeredType.getDescription());

    // Later valueOf calls always returns the registered type
    assertSame(registeredType, MediaType.valueOf("application/x-restlet-test"));
    assertSame(registeredType, MediaType.valueOf("application/x-restlet-test"));

    // Test toString() equivalence
    MediaType mediaType = MediaType.valueOf("application/atom+xml; name=value");
    assertEquals("application/atom+xml; name=value", mediaType.toString());
    assertEquals(MediaType.APPLICATION_ATOM, mediaType.getParent());
  }
  /**
   * Writes the current object as an XML element using the given SAX writer.
   *
   * @param writer The SAX writer.
   * @throws SAXException
   */
  public void writeElement(XmlWriter writer) throws SAXException {

    final AttributesImpl attributes = new AttributesImpl();
    if ((getHref() != null) && (getHref().toString() != null)) {
      attributes.addAttribute("", "href", null, "atomURI", getHref().toString());
    }

    writer.startElement(APP_NAMESPACE, "collection", null, attributes);

    if (getTitle() != null) {
      writer.dataElement(ATOM_NAMESPACE, "title", getTitle());
    }

    if (getAccept() != null) {
      StringBuilder sb = new StringBuilder();
      for (MediaType mediaType : getAccept()) {
        if (sb.length() > 0) {
          sb.append(", ");
        }

        sb.append(mediaType.toString());
      }

      writer.dataElement(APP_NAMESPACE, "accept", sb.toString());
    }

    try {
      if (getCategories() != null) {
        getCategories().writeElement(writer);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    writer.endElement(APP_NAMESPACE, "collection");
  }