<commit_before>/*
 * Copyright (C) 2011 The Guava Authors
 *
 * 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 com.google.common.net;

import static com.google.common.base.Charsets.UTF_16;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.net.MediaType.ANY_APPLICATION_TYPE;
import static com.google.common.net.MediaType.ANY_AUDIO_TYPE;
import static com.google.common.net.MediaType.ANY_IMAGE_TYPE;
import static com.google.common.net.MediaType.ANY_TEXT_TYPE;
import static com.google.common.net.MediaType.ANY_TYPE;
import static com.google.common.net.MediaType.ANY_VIDEO_TYPE;
import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.google.common.net.MediaType.JPEG;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import static java.util.Arrays.asList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import junit.framework.TestCase;

/**
 * Tests for {@link MediaType}.
 *
 * @author <NAME>
 */
@GwtCompatible(emulated = true)
public class MediaTypeTest extends TestCase {
  @GwtIncompatible // reflection
  public void testParse_useConstants() throws Exception {
    for (MediaType constant : getConstants()) {
      assertSame(constant, MediaType.parse(constant.toString()));
    }
  }

  @GwtIncompatible // reflection
  public void testCreate_useConstants() throws Exception {
    for (MediaType constant : getConstants()) {
      assertSame(constant, MediaType.create(constant.type(), constant.subtype())
          .withParameters(constant.parameters()));
    }
  }

  @GwtIncompatible // reflection
  public void testConstants_charset() throws Exception {
    for (Field field : getConstantFields()) {
      Optional<Charset> charset = ((MediaType) field.get(null)).charset();
      if (field.getName().endsWith("_UTF_8")) {
        assertThat(charset).hasValue(UTF_8);
      } else {
        assertThat(charset).isAbsent();
      }
    }
  }

  @GwtIncompatible // reflection
  private static FluentIterable<Field> getConstantFields() {
    return FluentIterable.from(asList(MediaType.class.getDeclaredFields()))
        .filter(new Predicate<Field>() {
          @Override public boolean apply(Field input) {
            int modifiers = input.getModifiers();
            return isPublic(modifiers) && isStatic(modifiers) && isFinal(modifiers)
                && MediaType.class.equals(input.getType());
          }
        });<commit_msg>Test that all MediaType constants are unique.<commit_after>/*
 * Copyright (C) 2011 The Guava Authors
 *
 * 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 com.google.common.net;

import static com.google.common.base.Charsets.UTF_16;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.net.MediaType.ANY_APPLICATION_TYPE;
import static com.google.common.net.MediaType.ANY_AUDIO_TYPE;
import static com.google.common.net.MediaType.ANY_IMAGE_TYPE;
import static com.google.common.net.MediaType.ANY_TEXT_TYPE;
import static com.google.common.net.MediaType.ANY_TYPE;
import static com.google.common.net.MediaType.ANY_VIDEO_TYPE;
import static com.google.common.net.MediaType.HTML_UTF_8;
import static com.google.common.net.MediaType.JPEG;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.reflect.Modifier.isFinal;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import static java.util.Arrays.asList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import junit.framework.TestCase;

/**
 * Tests for {@link MediaType}.
 *
 * @author <NAME>
 */
@GwtCompatible(emulated = true)
public class MediaTypeTest extends TestCase {
  @GwtIncompatible // reflection
  public void testParse_useConstants() throws Exception {
    for (MediaType constant : getConstants()) {
      assertSame(constant, MediaType.parse(constant.toString()));
    }
  }

  @GwtIncompatible // reflection
  public void testCreate_useConstants() throws Exception {
    for (MediaType constant : getConstants()) {
      assertSame(constant, MediaType.create(constant.type(), constant.subtype())
          .withParameters(constant.parameters()));
    }
  }

  @GwtIncompatible // reflection
  public void testConstants_charset() throws Exception {
    for (Field field : getConstantFields()) {
      Optional<Charset> charset = ((MediaType) field.get(null)).charset();
      if (field.getName().endsWith("_UTF_8")) {
        assertThat(charset).hasValue(UTF_8);
      } else {
        assertThat(charset).isAbsent();
      }
    }
  }

  @GwtIncompatible // reflection
  public void testConstants_areUnique() {
    assertThat(getConstants()).containsNoDuplicates();
  }

  @GwtIncompatible // reflection
  private static FluentIterable<Field> getConstantFields() {
    return FluentIterable.from(asList(MediaType.class.getDeclaredFields()))
        .filter(new Predicate<Field>() {
          @Override public boolean apply(Field input) {
            int modifiers = input.getModifiers();
            return isPublic(modifiers) && isStatic(modifiers) && isFinal(modifiers)
                && MediaType.class.equals(input.getType());
          }
        });
{'fraction_non_alphanumeric': 0.10619699934768428, 'fraction_numerical': 0.003913894324853229, 'mean_word_length': 4.410021171489062, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5104512', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2186, 'n_words': 510}