View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github.mock;
31  
32  import com.jcabi.github.Coordinates;
33  import java.io.IOException;
34  import java.util.Arrays;
35  import java.util.Collections;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.Test;
39  
40  /**
41   * Test case for {@link MkRepoCommits).
42   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
43   * @version $Id: 118b2cb4c7fd10eaa61cce9eb5752fe462853be1 $
44   */
45  public final class MkRepoCommitsTest {
46  
47      /**
48       * MkRepoCommits can return commits' iterator.
49       * @throws IOException If some problem inside
50       */
51      @Test
52      public void returnIterator() throws IOException {
53          final String user =  "testuser1";
54          MatcherAssert.assertThat(
55              new MkRepoCommits(
56                  new MkStorage.InFile(),
57                  user,
58                  new Coordinates.Simple(user, "testrepo1")
59              ).iterate(Collections.<String, String>emptyMap()),
60              Matchers.notNullValue()
61          );
62      }
63  
64      /**
65       * MkRepoCommits can get a commit.
66       * @throws IOException if some problem inside
67       */
68      @Test
69      public void getCommit() throws IOException {
70          final String user =  "testuser2";
71          final String sha = "6dcb09b5b57875f334f61aebed695e2e4193db5e";
72          MatcherAssert.assertThat(
73              new MkRepoCommits(
74                  new MkStorage.InFile(),
75                  user,
76                  new Coordinates.Simple(user, "testrepo2")
77              ).get(sha),
78              Matchers.notNullValue()
79          );
80      }
81  
82      /**
83       * MkRepoCommits can compare commits.
84       * @throws IOException if some problem inside
85       */
86      @Test
87      public void canCompare() throws IOException {
88          final String user =  "testuser3";
89          MatcherAssert.assertThat(
90              new MkRepoCommits(
91                  new MkStorage.InFile(),
92                  user,
93                  new Coordinates.Simple(user, "testrepo3")
94              ).compare("5339b8e35b", "9b2e6efde9"),
95              Matchers.notNullValue()
96          );
97      }
98  
99      /**
100      * MkRepoCommits can compare commits as diff fromat.
101      * @throws Exception if some problem inside
102      */
103     @Test
104     public void canCompareAsDiffFormat() throws Exception {
105         final String user =  "testuser4";
106         final String base =  "c034abc";
107         final String head =  "a0ed832";
108         MatcherAssert.assertThat(
109             new MkRepoCommits(
110                 new MkStorage.InFile(),
111                 user,
112                 new Coordinates.Simple(user, "testrepo4")
113             ).diff(base, head),
114             Matchers.stringContainsInOrder(Arrays.asList(base, head))
115         );
116     }
117 
118     /**
119      * MkRepoCommits can compare commits as patch.
120      * @throws Exception if some problem inside
121      */
122     @Test
123     public void canCompareAsPatch() throws Exception {
124         final String user =  "testuser5";
125         final String head = "9b2e6e7de9";
126         MatcherAssert.assertThat(
127             new MkRepoCommits(
128                 new MkStorage.InFile(), user,
129                 new Coordinates.Simple(user, "testrepo5")
130             ).patch("5c39b8e35b", head),
131             Matchers.stringContainsInOrder(
132                 Arrays.asList(
133                     head, "From:", "Date:", "Subject:", "files changed",
134                     "insertions", "deletions"
135                 )
136             )
137         );
138     }
139 }