summaryrefslogtreecommitdiffstats
path: root/Essentials/test/com/earth2me/essentials/UtilTest.java
blob: 497d26d7a57b87b89c4b0537c0e93c4cea3d5f89 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.earth2me.essentials;

import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException;


public class UtilTest extends TestCase
{
	private final Essentials ess;
	private final FakeServer server;

	public UtilTest()
	{
		server = new FakeServer();
		server.createWorld("testWorld", Environment.NORMAL);
		ess = new Essentials(server);
		try
		{
			ess.setupForTesting(server);
		}
		catch (InvalidDescriptionException ex)
		{
			fail("InvalidDescriptionException");
		}
		catch (IOException ex)
		{
			fail("IOException");
		}
	}

	public void testSafeLocation()
	{
		Set<String> testSet = new HashSet<String>();
		int count = 0;
		int x, y, z, origX, origY, origZ;
		x = y = z = origX = origY = origZ = 0;
		int i = 0;
		while (true)
		{
			testSet.add(x + ":" + y + ":" + z);
			count++;
			i++;
			if (i >= LocationUtil.VOLUME.length)
			{
				break;
			}
			x = origX + LocationUtil.VOLUME[i].x;
			y = origY + LocationUtil.VOLUME[i].y;
			z = origZ + LocationUtil.VOLUME[i].z;
		}
		assertTrue(testSet.contains("0:0:0"));
		assertTrue(testSet.contains("3:3:3"));
		assertEquals(testSet.size(), count);
		int diameter = LocationUtil.RADIUS * 2 + 1;
		assertEquals(diameter * diameter * diameter, count);
	}

	public void testFDDnow()
	{
		Calendar c = new GregorianCalendar();
		String resp = DateUtil.formatDateDiff(c, c);
		assertEquals(resp, "now");
	}

	public void testFDDfuture()
	{
		Calendar a, b;
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 0, 1);
		assertEquals("1 second", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 0, 2);
		assertEquals("2 seconds", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 0, 3);
		assertEquals("3 seconds", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 1, 0);
		assertEquals("1 minute", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 2, 0);
		assertEquals("2 minutes", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 10, 3, 0);
		assertEquals("3 minutes", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 11, 0, 0);
		assertEquals("1 hour", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 12, 0, 0);
		assertEquals("2 hours", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 13, 0, 0);
		assertEquals("3 hours", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 2, 10, 0, 0);
		assertEquals("1 day", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 3, 10, 0, 0);
		assertEquals("2 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 4, 10, 0, 0);
		assertEquals("3 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 2, 1, 10, 0, 0);
		assertEquals("1 month", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 3, 1, 10, 0, 0);
		assertEquals("2 months", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 4, 1, 10, 0, 0);
		assertEquals("3 months", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2011, 1, 1, 10, 0, 0);
		assertEquals("1 year", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2012, 1, 1, 10, 0, 0);
		assertEquals("2 years", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2013, 1, 1, 10, 0, 0);
		assertEquals("3 years", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2011, 4, 5, 23, 38, 12);
		assertEquals("1 year 3 months 4 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 9, 17, 23, 45, 45);
		b = new GregorianCalendar(2015, 3, 7, 10, 0, 0);
		assertEquals("4 years 5 months 20 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2011, 4, 31, 10, 0, 0);
		b = new GregorianCalendar(2011, 4, 31, 10, 5, 0);
		assertEquals("5 minutes", DateUtil.formatDateDiff(a, b));
	}

	public void testFDDpast()
	{
		Calendar a, b;
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 59, 59);
		assertEquals("1 second", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 59, 58);
		assertEquals("2 seconds", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 59, 57);
		assertEquals("3 seconds", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 59, 0);
		assertEquals("1 minute", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 58, 0);
		assertEquals("2 minutes", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 57, 0);
		assertEquals("3 minutes", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 9, 0, 0);
		assertEquals("1 hour", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 8, 0, 0);
		assertEquals("2 hours", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 1, 7, 0, 0);
		assertEquals("3 hours", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 5, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 4, 10, 0, 0);
		assertEquals("1 day", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 5, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 3, 10, 0, 0);
		assertEquals("2 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 5, 10, 0, 0);
		b = new GregorianCalendar(2010, 1, 2, 10, 0, 0);
		assertEquals("3 days", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 5, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 4, 1, 10, 0, 0);
		assertEquals("1 month", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 5, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 3, 1, 10, 0, 0);
		assertEquals("2 months", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 5, 1, 10, 0, 0);
		b = new GregorianCalendar(2010, 2, 1, 10, 0, 0);
		assertEquals("3 months", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2009, 1, 1, 10, 0, 0);
		assertEquals("1 year", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2008, 1, 1, 10, 0, 0);
		assertEquals("2 years", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2007, 1, 1, 10, 0, 0);
		assertEquals("3 years", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 1, 1, 10, 0, 0);
		b = new GregorianCalendar(2009, 4, 5, 23, 38, 12);
		assertEquals("8 months 26 days 10 hours", DateUtil.formatDateDiff(a, b));
		a = new GregorianCalendar(2010, 9, 17, 23, 45, 45);
		b = new GregorianCalendar(2000, 3, 7, 10, 0, 0);
		assertEquals("10 years 6 months 10 days", DateUtil.formatDateDiff(a, b));
	}
}