1   package org.gnomekr.potron.service;
2   
3   import static org.gnomekr.potron.util.PotronConstants.BEAN_ID_PROJECT_MANAGER;
4   
5   import java.io.PrintWriter;
6   import java.io.Reader;
7   import java.io.StringReader;
8   import java.io.StringWriter;
9   import java.util.Date;
10  import java.util.List;
11  import java.util.Map;
12  
13  import org.gnomekr.potron.data.LanguageTeam;
14  import org.gnomekr.potron.data.Project;
15  import org.gnomekr.potron.data.Template;
16  
17  /***
18   * ProjectManagerTest.java
19   * @author Xavier Cho
20   * @version $Revision 1.1 $ $Date: 2005/09/11 05:49:45 $
21   */
22  public class ProjectManagerTest extends AbstractHibernateSpringContextTests {
23  
24      /***
25       * @throws Exception 
26       */
27      public void testProjectPersistence() throws Exception {
28          IProjectManager manager = (IProjectManager) applicationContext
29                  .getBean(BEAN_ID_PROJECT_MANAGER);
30          assertNotNull(manager);
31  
32          Project project = new Project("__test_project__");
33  
34          project.setRegisteredDate(new Date(System.currentTimeMillis()));
35          project.setName("Test Project");
36          project.setVersion("1.0");
37          project.setHomepage("homepage");
38          project.setDescription("description");
39  
40          manager.createProject(project);
41  
42          flushCurrentSession();
43  
44          int count = jdbcTemplate.queryForInt(
45                  "select count(*) from POTRON_PROJECT where project_id=?",
46                  new String[] {
47                      project.getId()
48                  });
49  
50          assertEquals(1, count);
51  
52          try {
53              manager.createProject(project);
54              flushCurrentSession();
55  
56              assertTrue("Should have thrown ProjectExistsException here.", false);
57          } catch (ProjectExistsException e) {
58          }
59  
60          Project project2 = manager.getProject(project.getId());
61  
62          assertNotNull(project2);
63          assertEquals(project2, project);
64  
65          project.setName("Test Project2");
66  
67          manager.updateProject(project);
68          flushCurrentSession();
69  
70          String name = (String) jdbcTemplate.queryForObject(
71                  "select name from POTRON_PROJECT where project_id=?",
72                  new String[] {
73                      project.getId()
74                  },
75                  String.class);
76  
77          assertEquals("Test Project2", name);
78  
79          List<Project> projects = manager.getProjects();
80          assertNotNull(projects);
81          assertTrue(projects.contains(project));
82  
83          manager.deleteProject(project.getId());
84          flushCurrentSession();
85  
86          count = jdbcTemplate.queryForInt(
87                  "select count(*) from POTRON_PROJECT where project_id=?",
88                  new String[] {
89                      project.getId()
90                  });
91  
92          assertEquals(0, count);
93      }
94  
95      /***
96       * @throws Exception 
97       */
98      public void testLanguageTeamPersistence() throws Exception {
99          IProjectManager manager = (IProjectManager) applicationContext
100                 .getBean(BEAN_ID_PROJECT_MANAGER);
101         assertNotNull(manager);
102 
103         LanguageTeam team = new LanguageTeam();
104 
105         team.setRegisteredDate(new Date(System.currentTimeMillis()));
106         team.setName("Test Team");
107         team.setContact("contact");
108         team.setPrefix("prefix");
109         team.setEncoding("encoding");
110 
111         manager.addLanguageTeam(team);
112 
113         flushCurrentSession();
114 
115         int count = jdbcTemplate.queryForInt(
116                 "select count(*) from POTRON_TEAM where team_id=?",
117                 new Long[] {
118                     team.getId()
119                 });
120 
121         assertEquals(1, count);
122 
123         LanguageTeam team2 = manager.getLanguageTeam(team.getId());
124 
125         assertNotNull(team2);
126         assertEquals(team2, team);
127 
128         team.setName("Test Team2");
129 
130         manager.updateLanguageTeam(team);
131         flushCurrentSession();
132 
133         String name = (String) jdbcTemplate.queryForObject(
134                 "select name from POTRON_TEAM where team_id=?",
135                 new Long[] {
136                     team.getId()
137                 },
138                 String.class);
139 
140         assertEquals("Test Team2", name);
141 
142         List<LanguageTeam> teams = manager.getLanguageTeams();
143         assertNotNull(teams);
144         assertTrue(teams.contains(team));
145 
146         manager.removeLanguageTeam(team.getId());
147         flushCurrentSession();
148 
149         count = jdbcTemplate.queryForInt(
150                 "select count(*) from POTRON_TEAM where team_id=?",
151                 new Long[] {
152                     team.getId()
153                 });
154 
155         assertEquals(0, count);
156     }
157 
158     /***
159      * @throws Exception 
160      */
161     public void testTemplatePersistence() throws Exception {
162         IProjectManager manager = (IProjectManager) applicationContext
163                 .getBean(BEAN_ID_PROJECT_MANAGER);
164         assertNotNull(manager);
165 
166         StringWriter writer = new StringWriter();
167         PrintWriter out = new PrintWriter(writer);
168 
169         out.println("# SOME DESCRIPTIVE TITLE.");
170         out.println("# Copyright (C) YEAR Free Software Foundation, Inc.");
171         out.println("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.");
172         out.println("#");
173         out.println("#, fuzzy");
174         out.println("msgid \"\"");
175         out.println("msgstr \"\"");
176         out.println("\"POT-Creation-Date: 2001-02-09 01:25+0100//n\"");
177         out.println();
178         out.println("#: gpl.xml:15");
179         out.println("#, no-c-format");
180         out.println("#, fuzzy");
181         out.println("msgid \"Free Software Foundation, Inc.\"");
182         out.println("msgstr \"test2\"");
183 
184         Project project = new Project("__test_project__");
185 
186         project.setRegisteredDate(new Date(System.currentTimeMillis()));
187         project.setName("Test Project");
188         project.setVersion("1.0");
189         project.setHomepage("homepage");
190         project.setDescription("description");
191 
192         manager.createProject(project);
193 
194         Reader reader = new StringReader(writer.toString());
195 
196         long id = manager.addTemplate(
197                 project.getId(),
198                 "test template",
199                 "description",
200                 reader);
201 
202         flushCurrentSession();
203 
204         int count = jdbcTemplate.queryForInt(
205                 "select count(*) from POTRON_TEMPLATE where template_id=?",
206                 new Long[] {
207                     id
208                 });
209 
210         assertEquals(1, count);
211 
212         String comment = (String) jdbcTemplate.queryForObject(
213                 "select comment from POTRON_TEMPLATE where template_id=?",
214                 new Long[] {
215                     id
216                 },
217                 String.class);
218 
219         assertNotNull(comment);
220         assertTrue(comment.startsWith("# SOME DESCRIPTIVE TITLE."));
221 
222         List headers = jdbcTemplate.queryForList(
223                 "select * from POTRON_TEMPL_HEADER where template_id=?",
224                 new Long[] {
225                     id
226                 });
227 
228         assertNotNull(headers);
229         assertEquals(1, headers.size());
230 
231         Map values = (Map) headers.get(0);
232         assertEquals("POT-Creation-Date", values.get("name"));
233         assertEquals("2001-02-09 01:25+0100", values.get("value"));
234 
235         List entries = jdbcTemplate.queryForList(
236                 "select * from POTRON_TEMPL_ENTRY where template_id=?",
237                 new Long[] {
238                     id
239                 });
240 
241         assertNotNull(entries);
242         assertEquals(1, entries.size());
243 
244         values = (Map) entries.get(0);
245 
246         assertEquals("gpl.xml:15", values.get("entry_refs"));
247         assertEquals("#, no-c-format", values.get("comment"));
248         assertEquals("Free Software Foundation, Inc.", values.get("entry_name"));
249 
250         Template template = manager.getTemplate(id);
251 
252         assertNotNull(template);
253         assertEquals("test template", template.getName());
254 
255         template.setName("test template 2");
256         manager.updateTemplate(
257                 template.getId(),
258                 template.getName(),
259                 template.getDescription(),
260                 template.getComment());
261 
262         flushCurrentSession();
263 
264         String name = (String) jdbcTemplate.queryForObject(
265                 "select name from POTRON_TEMPLATE where template_id=?",
266                 new Long[] {
267                     id
268                 },
269                 String.class);
270 
271         assertNotNull(name);
272         assertEquals(template.getName(), name);
273 
274         getCurrentSession().evict(project);
275 
276         project = manager.getProject(project.getId());
277 
278         assertTrue(project.getTemplates().contains(template));
279 
280         project.getTemplates().remove(template);
281 
282         manager.updateProject(project);
283 
284         flushCurrentSession();
285 
286         count = jdbcTemplate.queryForInt(
287                 "select count(*) from POTRON_TEMPLATE where template_id=?",
288                 new Long[] {
289                     id
290                 });
291 
292         assertEquals(0, count);
293     }
294 }