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
|
package com.gitblit.instance;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.nio.file.Files;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class GitblitInstanceIdTest
{
@Rule
public TemporaryFolder baseFolder = new TemporaryFolder();
/**
* Tests that the version nibble is set to 0x8.
*/
@Test
public void testUuidVersion() throws Exception
{
GitblitInstanceId id = new GitblitInstanceId();
UUID uuid = id.getId();
assertNotNull(uuid);
long upper = uuid.getMostSignificantBits();
assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L));
}
/**
* Tests that the variant nibble is set to 0x8.
*/
@Test
public void testUuidVariant() throws Exception
{
GitblitInstanceId id = new GitblitInstanceId();
UUID uuid = id.getId();
assertNotNull(uuid);
long lower = uuid.getLeastSignificantBits();
assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L));
}
/**
* Test that the first four bytes hold a timestamp in a newly generated id.
*/
@Test
public void testDatePart() throws Exception
{
GitblitInstanceId id = new GitblitInstanceId();
UUID uuid = id.getId();
assertNotNull(uuid);
long upper = uuid.getMostSignificantBits();
long ts = System.currentTimeMillis();
assertEquals("Date part of UUID does not equal current date/time.", ts >> 2*8, upper >> 4*8);
}
/**
* Test that a new id is generated and stored in a file, when none existed.
*/
@Test
public void testStoreId() throws Exception
{
GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot());
UUID uuid = id.getId();
assertNotNull(uuid);
long lower = uuid.getLeastSignificantBits();
assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L));
long upper = uuid.getMostSignificantBits();
assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L));
File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE);
assertTrue("Id file was not created", idFile.exists());
String string = Files.readAllLines(idFile.toPath()).get(0);
try {
UUID uuidFromFile = UUID.fromString(string);
assertEquals("Returned id and id read from file are not equal.", uuid, uuidFromFile);
} catch (IllegalArgumentException e) {
fail("UUID read from file is not valid: " + string);
}
}
/**
* Test that a new id is generated and stored in a file, when none existed.
*/
@Test
public void testStoreIdNonexistingFolder() throws Exception
{
File baseSubFolder = new File(baseFolder.getRoot(), "nonexisting");
GitblitInstanceId id = new GitblitInstanceId(baseSubFolder);
UUID uuid = id.getId();
assertNotNull(uuid);
long lower = uuid.getLeastSignificantBits();
assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L));
long upper = uuid.getMostSignificantBits();
assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L));
File idFile = new File(baseSubFolder, GitblitInstanceId.STORAGE_FILE);
assertTrue("Id file was not created", idFile.exists());
String string = Files.readAllLines(idFile.toPath()).get(0);
try {
UUID uuidFromFile = UUID.fromString(string);
assertEquals("Returned id and id read from file are not equal.", uuid, uuidFromFile);
} catch (IllegalArgumentException e) {
fail("UUID read from file is not valid: " + string);
}
}
/**
* Test that an existing id is read from an existing id file.
*/
@Test
public void testReadId() throws Exception
{
File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE);
String uuidString = "0196e409-c664-82f3-88f1-e963d16c7b8a";
Files.write(idFile.toPath(), uuidString.getBytes());
GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot());
UUID uuid = id.getId();
assertNotNull(uuid);
UUID refUuid = UUID.fromString(uuidString);
assertEquals("Returned id is not equal to reference id", refUuid, uuid);
}
/**
* Test reading id from a file with whitespace
*/
@Test
public void testReadIdWhitespace() throws Exception
{
File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE);
String uuidString = "0196e409-c664-82f3-88f1-e963d16c7b8a";
String fileString = "\n " + uuidString + " \n \n";
Files.write(idFile.toPath(), fileString.getBytes());
GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot());
UUID uuid = id.getId();
assertNotNull(uuid);
UUID refUuid = UUID.fromString(uuidString);
assertEquals("Returned id is not equal to reference id", refUuid, uuid);
}
}
|