summaryrefslogtreecommitdiffstats
path: root/Essentials/test/com/earth2me/essentials/EconomyTest.java
blob: ff9a6b6b7927eca9d5724ff0068a20ed0b65085b (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
package com.earth2me.essentials;

import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;
import java.io.IOException;
import junit.framework.TestCase;
import org.bukkit.plugin.InvalidDescriptionException;
import org.junit.Test;


public class EconomyTest extends TestCase
{
	private final transient Essentials ess;
	private final static String NPCNAME = "npc1";
	private final static String PLAYERNAME = "TestPlayer1";

	public EconomyTest(final String testName)
	{
		super(testName);
		ess = new Essentials();
		final FakeServer server = new FakeServer();
		try
		{
			ess.setupForTesting(server);
		}
		catch (InvalidDescriptionException ex)
		{
			fail("InvalidDescriptionException");
		}
		catch (IOException ex)
		{
			fail("IOException");
		}
		server.addPlayer(new OfflinePlayer(PLAYERNAME, ess));
	}

	// only one big test, since we use static instances
	@Test
	public void testEconomy()
	{
		// test NPC
		assertFalse("NPC does not exists", Economy.playerExists(NPCNAME));
		assertTrue("Create NPC", Economy.createNPC(NPCNAME));
		assertTrue("NPC exists", Economy.playerExists(NPCNAME));
		assertNotNull("NPC can be accessed", ess.getOfflineUser(NPCNAME));
		try
		{
			Economy.removeNPC(NPCNAME);
		}
		catch (UserDoesNotExistException ex)
		{
			fail(ex.getMessage());
		}
		assertFalse("NPC can be removed", Economy.playerExists(NPCNAME));

		//test Math
		try
		{

			assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
			Economy.resetBalance(PLAYERNAME);
			assertEquals("Player has no money", 0.0, Economy.getMoney(PLAYERNAME));
			Economy.add(PLAYERNAME, 10.0);
			assertEquals("Add money", 10.0, Economy.getMoney(PLAYERNAME));
			Economy.subtract(PLAYERNAME, 5.0);
			assertEquals("Subtract money", 5.0, Economy.getMoney(PLAYERNAME));
			Economy.multiply(PLAYERNAME, 2.0);
			assertEquals("Multiply money", 10.0, Economy.getMoney(PLAYERNAME));
			Economy.divide(PLAYERNAME, 2.0);
			assertEquals("Divide money", 5.0, Economy.getMoney(PLAYERNAME));
			Economy.setMoney(PLAYERNAME, 10.0);
			assertEquals("Set money", 10.0, Economy.getMoney(PLAYERNAME));
		}
		catch (NoLoanPermittedException ex)
		{
			fail(ex.getMessage());
		}
		catch (UserDoesNotExistException ex)
		{
			fail(ex.getMessage());
		}

		//test Format
		assertEquals("Format $1000", "$1000", Economy.format(1000.0));
		assertEquals("Format $10", "$10", Economy.format(10.0));
		assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
		assertEquals("Format $10.10", "$10.10", Economy.format(10.102));
		assertEquals("Format $10.11", "$10.11", Economy.format(10.109));


		//test Exceptions
		try
		{
			assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
			Economy.resetBalance(PLAYERNAME);
			assertEquals("Reset balance", 0.0, Economy.getMoney(PLAYERNAME));
			Economy.subtract(PLAYERNAME, 5.0);
			fail("Did not throw exception");
		}
		catch (NoLoanPermittedException ex)
		{
		}
		catch (UserDoesNotExistException ex)
		{
			fail(ex.getMessage());
		}

		try
		{
			Economy.resetBalance("UnknownPlayer");
			fail("Did not throw exception");
		}
		catch (NoLoanPermittedException ex)
		{
			fail(ex.getMessage());
		}
		catch (UserDoesNotExistException ex)
		{
		}
	}
}