aboutsummaryrefslogtreecommitdiffstats
blob: f0eb3ec29dd223439e8f7b8c06e7f3c16e9c03b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GsonUtils;
import org.junit.Test;

/**
 * Unit tests of {@link Repository}
 */
public class RepositoryTest {

	/**
	 * Test default state of repository
	 */
	@Test
	public void defaultState() {
		Repository repo = new Repository();
		assertNull(repo.getCloneUrl());
		assertNull(repo.getCreatedAt());
		assertNull(repo.getDescription());
		assertEquals(0, repo.getForks());
		assertNull(repo.getGitUrl());
		assertNull(repo.getHomepage());
		assertNull(repo.getHtmlUrl());
		assertNull(repo.getLanguage());
		assertNull(repo.getDefaultBranch());
		assertNull(repo.getMirrorUrl());
		assertNull(repo.getName());
		assertEquals(0, repo.getOpenIssues());
		assertNull(repo.getOwner());
		assertNull(repo.getParent());
		assertNull(repo.getPushedAt());
		assertEquals(0, repo.getSize());
		assertNull(repo.getSource());
		assertNull(repo.getSshUrl());
		assertNull(repo.getSvnUrl());
		assertNull(repo.getUrl());
		assertEquals(0, repo.getWatchers());
		assertEquals(0, repo.getStars());
		assertFalse(repo.isFork());
		assertFalse(repo.isHasDownloads());
		assertFalse(repo.isHasIssues());
		assertFalse(repo.isHasWiki());
		assertFalse(repo.isPrivate());
		assertEquals(0, repo.getId());
		assertNull(repo.getUpdatedAt());
	}

	/**
	 * Test updating fields of repository
	 */
	@Test
	public void updateFields() {
		Repository repo = new Repository();
		assertEquals("clone://", repo.setCloneUrl("clone://").getCloneUrl());
		assertEquals(new Date(2500), repo.setCreatedAt(new Date(2500))
				.getCreatedAt());
		assertEquals("a repo", repo.setDescription("a repo").getDescription());
		assertEquals(10, repo.setForks(10).getForks());
		assertEquals("git://", repo.setGitUrl("git://").getGitUrl());
		assertEquals("home", repo.setHomepage("home").getHomepage());
		assertEquals("html", repo.setHtmlUrl("html").getHtmlUrl());
		assertEquals("java", repo.setLanguage("java").getLanguage());
		assertEquals("master", repo.setDefaultBranch("master").getDefaultBranch());
		assertEquals("project", repo.setName("project").getName());
		assertEquals(20, repo.setOpenIssues(20).getOpenIssues());
		User owner = new User().setLogin("owner");
		assertEquals(owner, repo.setOwner(owner).getOwner());
		Repository parent = new Repository().setName("parent");
		assertEquals(parent, repo.setParent(parent).getParent());
		assertEquals(new Date(3000), repo.setPushedAt(new Date(3000))
				.getPushedAt());
		assertEquals(100, repo.setSize(100).getSize());
		Repository source = new Repository().setName("source");
		assertEquals(source, repo.setSource(source).getSource());
		assertEquals("ssh://", repo.setSshUrl("ssh://").getSshUrl());
		assertEquals("svn://", repo.setSvnUrl("svn://").getSvnUrl());
		assertEquals("url://", repo.setUrl("url://").getUrl());
		assertEquals(200, repo.setWatchers(200).getWatchers());
		assertEquals(42, repo.setStars(42).getStars());
		assertTrue(repo.setFork(true).isFork());
		assertTrue(repo.setHasDownloads(true).isHasDownloads());
		assertTrue(repo.setHasIssues(true).isHasIssues());
		assertTrue(repo.setHasWiki(true).isHasWiki());
		assertTrue(repo.setPrivate(true).isPrivate());
		assertEquals("mirror", repo.setMirrorUrl("mirror").getMirrorUrl());
		assertEquals(14, repo.setId(14).getId());
		assertEquals(new Date(50000), repo.setUpdatedAt(new Date(50000))
				.getUpdatedAt());
	}

	/**
	 * Generate id with null name
	 */
	@Test
	public void generateIdNullName() {
		Repository repo = new Repository();
		repo.setName(null);
		repo.setOwner(new User().setLogin("tuser"));
		assertNull(repo.generateId());
	}

	/**
	 * Generate id with empty name
	 */
	@Test
	public void generateIdEmptyName() {
		Repository repo = new Repository();
		repo.setName("");
		repo.setOwner(new User().setLogin("tuser"));
		assertNull(repo.generateId());
	}

	/**
	 * Generate id with null owner
	 */
	@Test
	public void generateIdNullOwner() {
		Repository repo = new Repository();
		repo.setName("myproject");
		assertNull(repo.generateId());
	}

	/**
	 * Generate id with null owner login
	 */
	@Test
	public void generateIdNullOwnerLogin() {
		Repository repo = new Repository();
		repo.setName("myproject");
		repo.setOwner(new User().setLogin(null));
		assertNull(repo.generateId());
	}

	/**
	 * Generate id with empty owner login
	 */
	@Test
	public void generateIdEmptyOwnerLogin() {
		Repository repo = new Repository();
		repo.setName("myproject");
		repo.setOwner(new User().setLogin(""));
		assertNull(repo.generateId());
	}

	/**
	 * Generate id for repository
	 */
	@Test
	public void generateId() {
		Repository repo = new Repository();
		repo.setName("myproject");
		repo.setOwner(new User().setLogin("tuser"));
		assertEquals("tuser/myproject", repo.generateId());
	}

	@Test
	public void fromJson() throws IOException {
		try (Reader r = new BufferedReader(new InputStreamReader (
				this.getClass().getResourceAsStream("test_repo.json"),
				StandardCharsets.UTF_8))) {
			Repository repo = GsonUtils.fromJson(r, Repository.class);
			assertEquals(9,repo.getWatchers());
			assertEquals(21, repo.getStars());
		}
	}
}