View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo.avatars;
20  
21  import java.io.File;
22  
23  import org.junit.Rule;
24  import org.junit.Test;
25  import org.junit.rules.TemporaryFolder;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  
30  public class GravatarProviderTest {
31  
32      @Rule
33      public TemporaryFolder tmpFolder = new TemporaryFolder();
34  
35      @Test
36      public void urlShouldBeCorrect() {
37          GravatarProvider gravatarProvider = new GravatarProvider();
38          gravatarProvider.setBaseUrl("https://www.gravatar.com/avatar");
39          String externalAvatarUrl = gravatarProvider.getAvatarUrl("email@example.com");
40  
41          assertEquals(
42                  "https://www.gravatar.com/avatar/5658ffccee7f0ebfda2b226238b1eb6e.jpg?d=blank&s=60", externalAvatarUrl);
43      }
44  
45      @Test
46      public void urlForEmptyEmailShouldBeCorrect() {
47          GravatarProvider gravatarProvider = new GravatarProvider();
48          gravatarProvider.setBaseUrl("https://www.gravatar.com/avatar");
49  
50          String externalAvatarUrl = gravatarProvider.getAvatarUrl(null);
51  
52          assertEquals(
53                  "https://www.gravatar.com/avatar/00000000000000000000000000000000.jpg?d=blank&f=y&s=60",
54                  externalAvatarUrl);
55  
56          externalAvatarUrl = gravatarProvider.getAvatarUrl("");
57  
58          assertEquals(
59                  "https://www.gravatar.com/avatar/00000000000000000000000000000000.jpg?d=blank&f=y&s=60",
60                  externalAvatarUrl);
61      }
62  
63      @Test
64      public void localAvatarPathShouldBeCorrect() throws Exception {
65          GravatarProvider gravatarProvider = new GravatarProvider();
66          gravatarProvider.setBaseUrl("https://www.gravatar.com/avatar");
67          gravatarProvider.setOutputDirectory(tmpFolder.getRoot());
68          String localAvatarUrl = gravatarProvider.getLocalAvatarPath("sjaranowski@apache.org");
69          assertEquals("avatars/90cc13b765c79d2d55ca64388ea2bc5f.jpg", localAvatarUrl);
70          assertTrue(new File(tmpFolder.getRoot(), "avatars/90cc13b765c79d2d55ca64388ea2bc5f.jpg").exists());
71      }
72  
73      @Test
74      public void localAvatarPathShouldHaveDefaultForNotExisting() throws Exception {
75          GravatarProvider gravatarProvider = new GravatarProvider();
76          gravatarProvider.setBaseUrl("https://www.gravatar.com/avatar");
77          gravatarProvider.setOutputDirectory(tmpFolder.getRoot());
78          String localAvatarUrl = gravatarProvider.getLocalAvatarPath("test@example.com");
79          assertEquals("avatars/00000000000000000000000000000000.jpg", localAvatarUrl);
80          assertTrue(new File(tmpFolder.getRoot(), "avatars/00000000000000000000000000000000.jpg").exists());
81      }
82  
83      @Test
84      public void localAvatarPathShouldBeCorrectForDefault() throws Exception {
85          GravatarProvider gravatarProvider = new GravatarProvider();
86          gravatarProvider.setBaseUrl("https://www.gravatar.com/avatar");
87          gravatarProvider.setOutputDirectory(tmpFolder.getRoot());
88          String localAvatarUrl = gravatarProvider.getLocalAvatarPath(null);
89          assertEquals("avatars/00000000000000000000000000000000.jpg", localAvatarUrl);
90          assertTrue(new File(tmpFolder.getRoot(), "avatars/00000000000000000000000000000000.jpg").exists());
91      }
92  }