Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MkBranches |
|
| 1.0;1 | ||||
MkBranches$1 |
|
| 1.0;1 | ||||
MkBranches$AjcClosure1 |
|
| 1.0;1 | ||||
MkBranches$AjcClosure3 |
|
| 1.0;1 | ||||
MkBranches$AjcClosure5 |
|
| 1.0;1 | ||||
MkBranches$AjcClosure7 |
|
| 1.0;1 |
1 | 42 | /** |
2 | * Copyright (c) 2013-2017, 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.aspects.Immutable; | |
33 | import com.jcabi.aspects.Loggable; | |
34 | import com.jcabi.github.Branch; | |
35 | import com.jcabi.github.Branches; | |
36 | import com.jcabi.github.Coordinates; | |
37 | import com.jcabi.github.Repo; | |
38 | import com.jcabi.xml.XML; | |
39 | import java.io.IOException; | |
40 | import lombok.EqualsAndHashCode; | |
41 | import lombok.ToString; | |
42 | import org.xembly.Directives; | |
43 | ||
44 | /** | |
45 | * Mock Git branches. | |
46 | * | |
47 | * @author Chris Rebert (github@rebertia.com) | |
48 | * @version $Id: a21d76ba8a40d7e0facbff7847d506cfce28f9a4 $ | |
49 | * @since 0.24 | |
50 | */ | |
51 | @Immutable | |
52 | @Loggable(Loggable.DEBUG) | |
53 | 0 | @ToString |
54 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords" }) |
55 | 12 | public final class MkBranches implements Branches { |
56 | /** | |
57 | * XPath from a given branch to its commit SHA string. | |
58 | */ | |
59 | private static final String XPATH_TO_SHA = "sha/text()"; | |
60 | ||
61 | /** | |
62 | * Storage. | |
63 | */ | |
64 | private final transient MkStorage storage; | |
65 | ||
66 | /** | |
67 | * Login of the user logged in. | |
68 | */ | |
69 | private final transient String self; | |
70 | ||
71 | /** | |
72 | * Repo name. | |
73 | */ | |
74 | private final transient Coordinates coords; | |
75 | ||
76 | /** | |
77 | * Public ctor. | |
78 | * @param stg Storage | |
79 | * @param login Username | |
80 | * @param rep Repo | |
81 | * @throws IOException If there is any I/O problem | |
82 | */ | |
83 | MkBranches( | |
84 | final MkStorage stg, | |
85 | final String login, | |
86 | final Coordinates rep | |
87 | 17 | ) throws IOException { |
88 | 17 | this.storage = stg; |
89 | 17 | this.self = login; |
90 | 17 | this.coords = rep; |
91 | 34 | this.storage.apply( |
92 | 17 | new Directives().xpath( |
93 | 17 | String.format( |
94 | "/github/repos/repo[@coords='%s']", | |
95 | this.coords | |
96 | ) | |
97 | 17 | ).addIf("branches") |
98 | ); | |
99 | 17 | } |
100 | ||
101 | @Override | |
102 | public Repo repo() { | |
103 | 0 | return new MkRepo(this.storage, this.self, this.coords); |
104 | } | |
105 | ||
106 | @Override | |
107 | public Iterable<Branch> iterate() { | |
108 | 6 | return new MkIterable<Branch>( |
109 | this.storage, | |
110 | 2 | String.format("%s/branch", this.xpath()), |
111 | 6 | new MkIterable.Mapping<Branch>() { |
112 | @Override | |
113 | public Branch map(final XML xml) { | |
114 | 8 | return new MkBranch( |
115 | 4 | MkBranches.this.storage, |
116 | 4 | MkBranches.this.self, |
117 | 4 | MkBranches.this.coords, |
118 | 4 | xml.xpath("@name").get(0), |
119 | 4 | xml.xpath(MkBranches.XPATH_TO_SHA).get(0) |
120 | ); | |
121 | } | |
122 | } | |
123 | ); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Creates a new branch. | |
128 | * @param name Name of branch | |
129 | * @param sha Commit SHA | |
130 | * @return New branch | |
131 | * @throws IOException if there is an I/O problem | |
132 | */ | |
133 | public Branch create( | |
134 | final String name, | |
135 | final String sha) | |
136 | throws IOException { | |
137 | 42 | final Directives directives = new Directives() |
138 | 21 | .xpath(this.xpath()) |
139 | 21 | .add("branch") |
140 | 21 | .attr("name", name) |
141 | 21 | .add("sha").set(sha).up(); |
142 | 21 | this.storage.apply(directives); |
143 | 21 | return new MkBranch(this.storage, this.self, this.coords, name, sha); |
144 | } | |
145 | ||
146 | /** | |
147 | * Gets a branch by name. | |
148 | * @param name Name of branch. | |
149 | * @return The branch with the given name | |
150 | * @throws IOException If there is an I/O problem | |
151 | */ | |
152 | public Branch get( | |
153 | final String name | |
154 | ) throws IOException { | |
155 | 6 | return new MkBranch( |
156 | this.storage, | |
157 | this.self, | |
158 | this.coords, | |
159 | name, | |
160 | 2 | this.storage.xml() |
161 | 2 | .nodes( |
162 | 2 | String.format( |
163 | "%s/branch[@name='%s']", | |
164 | 2 | this.xpath(), |
165 | name | |
166 | ) | |
167 | ) | |
168 | 2 | .get(0) |
169 | 2 | .xpath(MkBranches.XPATH_TO_SHA) |
170 | 2 | .get(0) |
171 | ); | |
172 | } | |
173 | ||
174 | /** | |
175 | * XPath of this element in XML tree. | |
176 | * @return XPath | |
177 | */ | |
178 | private String xpath() { | |
179 | 25 | return String.format( |
180 | "/github/repos/repo[@coords='%s']/branches", | |
181 | this.coords | |
182 | ); | |
183 | } | |
184 | } |