// Copyright (C) 2009 The Android Open Source Project
//
// 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.gerrit.mail;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import org.junit.Test;

public class AddressTest {
  @Test
  public void parse_NameEmail1() {
    final Address a = Address.parse("A U Thor <author@example.com>");
    assertThat(a.getName()).isEqualTo("A U Thor");
    assertThat(a.getEmail()).isEqualTo("author@example.com");
  }

  @Test
  public void parse_NameEmail2() {
    final Address a = Address.parse("A <a@b>");
    assertThat(a.getName()).isEqualTo("A");
    assertThat(a.getEmail()).isEqualTo("a@b");
  }

  @Test
  public void parse_NameEmail3() {
    final Address a = Address.parse("<a@b>");
    assertThat(a.getName()).isNull();
    assertThat(a.getEmail()).isEqualTo("a@b");
  }

  @Test
  public void parse_NameEmail4() {
    final Address a = Address.parse("A U Thor<author@example.com>");
    assertThat(a.getName()).isEqualTo("A U Thor");
    assertThat(a.getEmail()).isEqualTo("author@example.com");
  }

  @Test
  public void parse_NameEmail5() {
    final Address a = Address.parse("A U Thor  <author@example.com>");
    assertThat(a.getName()).isEqualTo("A U Thor");
    assertThat(a.getEmail()).isEqualTo("author@example.com");
  }

  @Test
  public void parse_Email1() {
    final Address a = Address.parse("author@example.com");
    assertThat(a.getName()).isNull();
    assertThat(a.getEmail()).isEqualTo("author@example.com");
  }

  @Test
  public void parse_Email2() {
    final Address a = Address.parse("a@b");
    assertThat(a.getName()).isNull();
    assertThat(a.getEmail()).isEqualTo("a@b");
  }

  @Test
  public void parse_NewTLD() {
    Address a = Address.parse("A U Thor <author@example.systems>");
    assertThat(a.getName()).isEqualTo("A U Thor");
    assertThat(a.getEmail()).isEqualTo("author@example.systems");
  }

  @Test
  public void parseInvalid() {
    assertInvalid("");
    assertInvalid("a");
    assertInvalid("a<");
    assertInvalid("<a");
    assertInvalid("<a>");
    assertInvalid("a<a>");
    assertInvalid("a <a>");

    assertInvalid("a");
    assertInvalid("a<@");
    assertInvalid("<a@");
    assertInvalid("<a@>");
    assertInvalid("a<a@>");
    assertInvalid("a <a@>");
    assertInvalid("a <@a>");
  }

  private void assertInvalid(String in) {
    try {
      Address.parse(in);
      fail("Expected IllegalArgumentException for " + in);
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage()).isEqualTo("Invalid email address: " + in);
    }
  }

  @Test
  public void toHeaderString_NameEmail1() {
    assertThat(format("A", "a@a")).isEqualTo("A <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail2() {
    assertThat(format("A B", "a@a")).isEqualTo("A B <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail3() {
    assertThat(format("A B. C", "a@a")).isEqualTo("\"A B. C\" <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail4() {
    assertThat(format("A B, C", "a@a")).isEqualTo("\"A B, C\" <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail5() {
    assertThat(format("A \" C", "a@a")).isEqualTo("\"A \\\" C\" <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail6() {
    assertThat(format("A \u20ac B", "a@a")).isEqualTo("=?UTF-8?Q?A_=E2=82=AC_B?= <a@a>");
  }

  @Test
  public void toHeaderString_NameEmail7() {
    assertThat(format("A \u20ac B (Code Review)", "a@a"))
        .isEqualTo("=?UTF-8?Q?A_=E2=82=AC_B_=28Code_Review=29?= <a@a>");
  }

  @Test
  public void toHeaderString_Email1() {
    assertThat(format(null, "a@a")).isEqualTo("a@a");
  }

  @Test
  public void toHeaderString_Email2() {
    assertThat(format(null, "a,b@a")).isEqualTo("<a,b@a>");
  }

  private static String format(String name, String email) {
    return new Address(name, email).toHeaderString();
  }
}

@@ -709,29 +709,58 @@
 tic 
-org.ju
+com.google.gerrit.testing.GerritJU
 nit.
-A
+a
 ssert
-.fail
+Throws
 ;%0A%0Ai
@@ -2935,143 +2935,121 @@
     
-try %7B%0A      Address.parse(in);%0A      fail(%22Expected IllegalArgumentException for %22 + in);%0A    %7D catch (IllegalArgumentException e) %7B%0A  
+IllegalArgumentException thrown =%0A        assertThrows(IllegalArgumentException.class, () -%3E Address.parse(in));%0A
     
@@ -3063,22 +3063,31 @@
 hat(
-e.get
+thrown).has
 Message
+That
 (
-)
 ).is
@@ -3131,14 +3131,8 @@
 n);%0A
-    %7D%0A
   %7D%0A

Migrate from try-catch-fail to assertThrows