View Javadoc
1   /******************************************************************************
2    * TestDatabaseImpl.java - Test the BuckoFIBS database 
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2011 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.2  2011/05/10 05:49:06  dick
10   * More GroupOfPlayers testing.
11   *
12   * Revision 1.1  2011/05/09 17:23:14  dick
13   * Test the BuckoFIBS database.
14   *
15   */
16  
17  /* 
18   * This program is free software: you can redistribute it and/or modify
19   * it under the terms of the GNU General Public License as published by
20   * the Free Software Foundation, either version 3 of the License, or
21   * (at your option) any later version.
22   *
23   * This program is distributed in the hope that it will be useful,
24   * but WITHOUT ANY WARRANTY; without even the implied warranty of
25   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26   * GNU General Public License for more details.
27   *
28   * You should have received a copy of the GNU General Public License
29   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30   *
31   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
32   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
33   * 
34   */
35  package com.buckosoft.fibs.BuckoFIBS.db;
36  
37  import java.awt.Color;
38  import java.util.Date;
39  import java.util.List;
40  
41  import junit.framework.TestCase;
42  
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  import com.buckosoft.fibs.BuckoFIBS.db.DatabaseImpl;
47  import com.buckosoft.fibs.BuckoFIBS.db.HibernateUtil;
48  import com.buckosoft.fibs.domain.FinishedMatch;
49  import com.buckosoft.fibs.domain.GroupOfPlayers;
50  import com.buckosoft.fibs.domain.Player;
51  import com.buckosoft.fibs.domain.PlayerGroup;
52  import com.buckosoft.fibs.domain.WinLoss;
53  
54  /** Test the BuckoFIBS database
55   * @author Dick Balaska
56   * @since 2011/05/08
57   * @version $Revision$ <br> $Date$
58   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/test/java/com/buckosoft/fibs/test/BuckoFIBS/db/TestDatabaseImpl.java">cvs TestDatabaseImpl.java</a>
59   *
60   */
61  public class TestDatabaseImpl extends TestCase {
62  	DatabaseImpl db = null;
63  	
64  	private final static String ps1 = "5 BuckoFIBSa - - 0 1 1682.77 5010 2 1304717383 75-144-150-169-NewEngland.hfc.comcastbusiness.net BuckoFIBS-0.7b -";
65  	private final static String ps2 = "5 TestPlayer - - 0 1 1582.77 1010 2 1304717383 75-144-150-169-NewEngland.hfc.comcastbusiness.net BuckoFIBS-0.7b -";
66  	private final static String ps3 = "5 Player3 - - 0 1 1482.77 1020 2 1304717383 75-144-150-169-NewEngland.hfc.comcastbusiness.net BuckoFIBS-0.7b -";
67  
68  	private final static String s_group1 = "Group1"; 
69  	private final static String s_group2 = "Group2"; 
70  	/**
71  	 * @throws java.lang.Exception
72  	 */
73  	@Before
74  	public void setUp() throws Exception {
75  		initNewDatabase();
76  	}
77  
78  	private void initNewDatabase() {
79  		db = new DatabaseImpl();
80  		assertNotNull(db);
81  		db.setURL("mem");		
82  		//db.setURL("testfile");	// doesn't work		
83  	}
84  
85  	private FinishedMatch getFM0() {
86  		Date d = new Date();
87  		d.setTime(d.getTime()-100);
88  		FinishedMatch fm = new FinishedMatch();
89  		fm.setOpponentId(2);
90  		fm.setDuration(100);
91  		fm.setYourScore(3);
92  		fm.setOpponentScore(1);
93  		fm.setMatchPoints(3);
94  		fm.setRatingAfterMatch(1600.0);
95  		fm.setDate(d);
96  		return(fm);
97  	}
98  
99  	private FinishedMatch getFM1() {
100 		Date d = new Date();
101 		FinishedMatch fm = new FinishedMatch();
102 		fm.setOpponentId(2);
103 		fm.setDuration(1000);
104 		fm.setYourScore(0);
105 		fm.setOpponentScore(1);
106 		fm.setMatchPoints(1);
107 		fm.setRatingAfterMatch(1500.0);
108 		d.setTime(d.getTime()+100);
109 		fm.setDate(d);
110 		return(fm);
111 	}
112 
113 	private FinishedMatch getFM2() {
114 		Date d = new Date();
115 		FinishedMatch fm = new FinishedMatch();
116 		fm.setOpponentId(3);
117 		fm.setDuration(1500);
118 		fm.setYourScore(0);
119 		fm.setOpponentScore(1);
120 		fm.setMatchPoints(1);
121 		fm.setRatingAfterMatch(1500.0);
122 		d.setTime(d.getTime()+100);
123 		fm.setDate(d);
124 		return(fm);
125 	}
126 
127 	/**
128 	 * Test method for {@link com.buckosoft.fibs.BuckoFIBS.db.DatabaseImpl#DatabaseImpl()}.
129 	 */
130 	@Test
131 	public void testDatabaseImpl() {
132 		this.initNewDatabase();
133 	}
134 
135 	/**
136 	 * Test method for {@link com.buckosoft.fibs.BuckoFIBS.db.DatabaseImpl#store(com.buckosoft.fibs.domain.Player)}.
137 	 */
138 	@Test
139 	public void testStorePlayer() {
140 		this.initNewDatabase();
141 		Player p = db.getPlayer(1);
142 		assertNull(p);
143 		p = new Player();
144 		p.parsePlayer(ps1);
145 		db.store(p);
146 		Player q = db.getPlayer(1);
147 		assertEquals(p.getName(), q.getName());
148 		q = db.getPlayer("BuckoFIBSa");
149 		assertEquals(p.getName(), q.getName());
150 		p = new Player();
151 		p.parsePlayer(ps2);
152 		db.store(p);
153 		q = db.getPlayer(2);
154 		assertEquals(p.getName(), q.getName());
155 		q = db.getPlayer("TestPlayer");
156 		assertEquals(p.getName(), q.getName());
157 	}
158 
159 	/**
160 	 * Test method for {@link com.buckosoft.fibs.BuckoFIBS.db.DatabaseImpl#store(com.buckosoft.fibs.domain.FinishedMatch)}.
161 	 */
162 	@Test
163 	public void testFinishedMatches() {
164 		this.initNewDatabase();
165 		List<FinishedMatch> lfm = db.getFinishedMatches(1);
166 		assertEquals(0, lfm.size());
167 		FinishedMatch fm = getFM0();
168 		db.store(fm);
169 		lfm = db.getFinishedMatches(2);
170 		assertEquals(1, lfm.size());
171 		FinishedMatch fn = getFM1();
172 		db.store(fn);
173 		lfm = db.getFinishedMatches(2);
174 		assertEquals(2, lfm.size());
175 		fn = getFM2();
176 		db.store(fn);
177 
178 		double[] dd = db.getAllRatingsByDate();
179 		assertEquals(1600.0, dd[0]);
180 		assertEquals(1500.0, dd[1]);
181 	}
182 
183 	/**
184 	 * Test method for {@link com.buckosoft.fibs.BuckoFIBS.db.DatabaseImpl#updateOpponent(com.buckosoft.fibs.domain.Player)}.
185 	 */
186 	@Test
187 	public void testUpdateOpponent() {
188 		this.initNewDatabase();
189 		Player p = db.getPlayer(1);
190 		assertNull(p);
191 
192 		p = new Player();
193 		p.parsePlayer(ps1);
194 		db.store(p);
195 		p = new Player();
196 		p.parsePlayer(ps2);
197 		db.store(p);
198 		p = new Player();
199 		p.parsePlayer(ps3);
200 
201 		p = new Player();
202 		p.setName("UnknownPlayer");
203 		db.updateOpponent(p);
204 		assertEquals(0, p.getId());
205 
206 		FinishedMatch fm = getFM0();
207 		db.store(fm);
208 		List<FinishedMatch> lfm;
209 		lfm = db.getFinishedMatches(2);
210 		assertEquals(1, lfm.size());
211 		FinishedMatch fn = getFM1();
212 		db.store(fn);
213 
214 		p = new Player();
215 		db.updateOpponent(p);		// should do nothing really, just code coverage
216 		assertEquals(0, p.getId());
217 		p.setName("TestPlayer");
218 		WinLoss wl = p.getWinLoss();
219 		assertNull(wl);
220 		db.updateOpponent(p);
221 		assertEquals(2, p.getId());
222 		p.parsePlayer(ps2);
223 		p.setId(2);
224 		db.updateOpponent(p);
225 		assertEquals("1-1", p.getWinLoss().toString());
226 		
227 	}
228 
229 	/**
230 	 * Test method for GroupsOfPlayers access.
231 	 */
232 	@Test
233 	public void testGroupsOfPlayers() {
234 		this.initNewDatabase();
235 		Player p = db.getPlayer(1);
236 		assertNull(p);
237 
238 		p = new Player();
239 		p.parsePlayer(ps1);
240 		db.store(p);
241 		Player q = new Player();
242 		q.parsePlayer(ps2);
243 		db.store(q);
244 		p = new Player();
245 		p.parsePlayer(ps3);
246 		
247 		List<GroupOfPlayers> ll = db.getGroupsOfPlayers();
248 		assertEquals(0, ll.size());
249 
250 		GroupOfPlayers gop = new GroupOfPlayers();
251 		assertNotNull(gop);
252 		assertTrue(gop.isDirty());
253 		gop = new GroupOfPlayers(s_group1, Color.black);
254 		ll.add(gop);
255 		db.store(ll);
256 		assertFalse(gop.isDirty());
257 		List<PlayerGroup>lpg = gop.getPlayerGroups();
258 		assertEquals(0, lpg.size());
259 		gop.addPlayer(2);
260 		assertTrue(gop.isDirty());
261 		lpg = gop.getPlayerGroups();
262 		db.store(ll);
263 		ll = db.getGroupsOfPlayers();
264 		assertEquals(1, ll.size());
265 		GroupOfPlayers gop1 = new GroupOfPlayers(s_group2, Color.red);
266 		gop1.addPlayer(2);
267 		gop1.addPlayer(3);
268 		ll.add(gop1);
269 		db.store(ll);
270 		lpg = gop1.getPlayerGroups();
271 		assertEquals(2, ll.size());
272 		gop1 = ll.get(1);
273 		assertEquals(s_group2, gop1.getGroupName());
274 		gop1.getPlayerGroups().get(0).setTagForDelete();
275 		gop1.setDirty(true);
276 		db.store(ll);
277 		ll = db.getGroupsOfPlayers();
278 		gop1 = ll.get(1);
279 		db.shutdown();
280 		assertEquals(s_group2, gop1.getGroupName());
281 		assertEquals(1, gop1.getPlayerGroups().size());
282 	}
283 
284 	/**
285 	 * Test method for {@link com.buckosoft.fibs.BuckoFIBS.db.HibernateUtils#unwindProperties(java.lang.String)}.
286 	 */
287 	@Test
288 	public void testUnwindProperties() {
289 		String s = HibernateUtil.unwindProperties("test");
290 		assertEquals("test", s);
291 		s = HibernateUtil.getUserDataDirectory();
292 		assertNotNull(s);
293 	}
294 }