Project

General

Profile

Revision 7839

add simple test for the IndexEventDAO class -- adding, removing, listing events in the DB table. https://projects.ecoinformatics.org/ecoinfo/issues/5944

View differences:

test/edu/ucsb/nceas/metacat/index/IndexEventDAOTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
package edu.ucsb.nceas.metacat.index;
25

  
26
import java.sql.SQLException;
27
import java.util.Calendar;
28
import java.util.Set;
29

  
30
import org.dataone.service.types.v1.Event;
31
import org.dataone.service.types.v1.Identifier;
32

  
33
import edu.ucsb.nceas.MCTestCase;
34
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
35

  
36
/**
37
 * @author jones
38
 * 
39
 *         Test class for the Version class.
40
 */
41
public class IndexEventDAOTest extends MCTestCase {
42

  
43
	private IndexEvent event = null;
44

  
45
	/*
46
	 * @see TestCase#setUp()
47
	 */
48
	protected void setUp() throws Exception {
49
		super.setUp();
50

  
51
		// initialize the event
52
		event = new IndexEvent();
53
		event.setAction(Event.CREATE);
54
		event.setDate(Calendar.getInstance().getTime());
55
		event.setDescription("Testing DAO");
56
		Identifier pid = new Identifier();
57
		pid.setValue("IndexEventDAOTest." + System.currentTimeMillis());
58
		event.setIdentifier(pid);
59
	}
60

  
61
	/**
62
	 * Test saving
63
	 */
64
	public void testSave() {
65
		try {
66
			// save
67
			IndexEventDAO.getInstance().add(event);
68
			// lookup
69
			IndexEvent savedEvent = IndexEventDAO.getInstance().get(event.getIdentifier());
70
			// check
71
			assertEquals(event.getIdentifier(), savedEvent.getIdentifier());
72
			assertEquals(event.getAction(), savedEvent.getAction());
73
			assertEquals(event.getDate(), savedEvent.getDate());
74
			assertEquals(event.getDescription(), savedEvent.getDescription());
75
		} catch (Exception e) {
76
			e.printStackTrace();
77
			fail("Could not save; " + e.getMessage());
78
		} finally {
79
			// try to clean up as best we can
80
			try {
81
				IndexEventDAO.getInstance().remove(event.getIdentifier());
82
			} catch (SQLException e) {
83
				// TODO Auto-generated catch block
84
				e.printStackTrace();
85
			}
86
		}
87
	}
88

  
89
	/**
90
	 * Test removing
91
	 */
92
	public void testRemove() {
93
		try {
94
			// save
95
			IndexEventDAO.getInstance().add(event);
96
			// remove
97
			IndexEventDAO.getInstance().remove(event.getIdentifier());
98
			// check
99
			IndexEvent savedEvent = IndexEventDAO.getInstance().get(event.getIdentifier());
100
			assertNull(savedEvent);
101

  
102
		} catch (Exception e) {
103
			e.printStackTrace();
104
			fail("Could not test removal; " + e.getMessage());
105
		}
106
	}
107
	
108
	/**
109
	 * Test listing
110
	 */
111
	public void testList() {
112
		try {
113
			
114
			// get the count
115
			Set<Identifier> allIdentifiers = IndexEventDAO.getInstance().getAllIdentifiers();
116
			int originalSize = allIdentifiers.size();
117
			
118
			// get one
119
			if (allIdentifiers != null && !allIdentifiers.isEmpty()) {
120
				IndexEvent existingEvent = IndexEventDAO.getInstance().get(allIdentifiers.iterator().next());
121
				assertNotNull(existingEvent);
122
			}
123
			// add one
124
			IndexEventDAO.getInstance().add(event);
125
			
126
			// get the count again
127
			int newSize = IndexEventDAO.getInstance().getAllIdentifiers().size();
128
			assertEquals(originalSize+1, newSize);
129
			
130
			// clean up
131
			IndexEventDAO.getInstance().remove(event.getIdentifier());
132
			
133

  
134
		} catch (Exception e) {
135
			e.printStackTrace();
136
			fail("Could not test removal; " + e.getMessage());
137
		}
138
	}
139

  
140
}
0 141

  
src/edu/ucsb/nceas/metacat/index/IndexEventDAO.java
118 118

  
119 119
				event = new IndexEvent();
120 120
				event.setIdentifier(identifier);
121
				event.setAction(Event.valueOf(action));
121
				event.setAction(Event.convert(action));
122 122
				event.setDate(timestamp);
123 123
				event.setDescription(description);
124 124
			}

Also available in: Unified diff