1 package org.gnomekr.potron.service;
2
3 import static org.gnomekr.potron.util.PotronConstants.BEAN_ID_PROJECT_MANAGER;
4 import static org.gnomekr.potron.util.PotronConstants.BEAN_ID_TRANSLATION_MANAGER;
5
6 import java.io.PrintWriter;
7 import java.io.Reader;
8 import java.io.StringReader;
9 import java.io.StringWriter;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.gnomekr.potron.data.LanguageTeam;
15 import org.gnomekr.potron.data.Project;
16 import org.gnomekr.potron.data.TranslatedEntry;
17 import org.gnomekr.potron.data.Translation;
18
19 /***
20 * TranslationManagerTest.java
21 * @author iolo
22 * @version $Revision 1.1 $ $Date: 2005/08/28 11:47:15 $
23 */
24 public class TranslationManagerTest extends AbstractHibernateSpringContextTests {
25
26 public void testTranslationPersistence() throws Exception {
27 IProjectManager projectManager = (IProjectManager) applicationContext
28 .getBean(BEAN_ID_PROJECT_MANAGER);
29 assertNotNull(projectManager);
30
31 ITranslationManager manager = (ITranslationManager) applicationContext
32 .getBean(BEAN_ID_TRANSLATION_MANAGER);
33 assertNotNull(manager);
34
35 StringWriter writer = new StringWriter();
36 PrintWriter out = new PrintWriter(writer);
37
38 out.println("# SOME DESCRIPTIVE TITLE.");
39 out.println("# Copyright (C) YEAR Free Software Foundation, Inc.");
40 out.println("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.");
41 out.println("#");
42 out.println("#, fuzzy");
43 out.println("msgid \"\"");
44 out.println("msgstr \"\"");
45 out.println("\"POT-Creation-Date: 2001-02-09 01:25+0100//n\"");
46 out.println();
47 out.println("#: gpl.xml:15");
48 out.println("#, no-c-format");
49 out.println("#, fuzzy");
50 out.println("msgid \"Free Software Foundation, Inc.\"");
51 out.println("msgstr \"test2\"");
52 out.println();
53 out.println("#: gpl.xml:16");
54 out.println("#, no-c-format");
55 out.println("#, fuzzy");
56 out.println("msgid \"test3\"");
57 out.println("msgstr \"test3\"");
58
59 Project project = new Project("__test_project__");
60
61 project.setRegisteredDate(new Date(System.currentTimeMillis()));
62 project.setName("Test Project");
63 project.setVersion("1.0");
64 project.setHomepage("homepage");
65 project.setDescription("description");
66
67 projectManager.createProject(project);
68
69 Reader reader = new StringReader(writer.toString());
70
71 long templateId = projectManager.addTemplate(
72 project.getId(),
73 "test template",
74 "description",
75 reader);
76
77 LanguageTeam team = new LanguageTeam();
78
79 team.setRegisteredDate(new Date(System.currentTimeMillis()));
80 team.setName("Test Team");
81 team.setContact("contact");
82 team.setPrefix("prefix");
83 team.setEncoding("encoding");
84
85 StringWriter writer2 = new StringWriter();
86 PrintWriter out2 = new PrintWriter(writer2);
87
88 out2.println("# SOME DESCRIPTIVE TITLE.");
89 out2.println("# Copyright (C) YEAR Free Software Foundation, Inc.");
90 out2.println("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.");
91 out2.println("#");
92 out2.println("#, fuzzy");
93 out2.println("msgid \"\"");
94 out2.println("msgstr \"\"");
95 out2.println("\"POT-Creation-Date: 2001-02-09 01:25+0100//n\"");
96 out2.println();
97 out2.println("#: gpl.xml:15");
98 out2.println("#, no-c-format");
99 out2.println("#, fuzzy");
100 out2.println("msgid \"Free Software Foundation, Inc.\"");
101 out2.println("msgstr \"not yet translated\"");
102 out2.println();
103 out2.println("#: gpl.xml:16");
104 out2.println("#, no-c-format");
105 out2.println("msgid \"test3\"");
106 out2.println("msgstr \"translated!\"");
107
108 Reader reader2 = new StringReader(writer2.toString());
109
110 Translation translation = new Translation();
111 translation.setName("test");
112 translation.setLanguageTeam(team);
113 translation.setTemplate(projectManager.getTemplate(templateId));
114
115 long id = manager.addTranslation(translation, reader2);
116
117 flushCurrentSession();
118
119 int count = jdbcTemplate.queryForInt(
120 "select count(*) from POTRON_TRANS where trans_id=?",
121 new Long[] {
122 id
123 });
124
125 assertEquals(1, count);
126
127 String comment = (String) jdbcTemplate.queryForObject(
128 "select note from POTRON_TRANS where trans_id=?",
129 new Long[] {
130 id
131 },
132 String.class);
133
134 assertNotNull(comment);
135 assertTrue(comment.startsWith("# SOME DESCRIPTIVE TITLE."));
136
137 List headers = jdbcTemplate.queryForList(
138 "select * from POTRON_TRANS_HEADER where template_id=?",
139 new Long[] {
140 id
141 });
142
143 assertNotNull(headers);
144 assertEquals(1, headers.size());
145
146 Map values = (Map) headers.get(0);
147 assertEquals("POT-Creation-Date", values.get("name"));
148 assertEquals("2001-02-09 01:25+0100", values.get("value"));
149
150 List entries = jdbcTemplate.queryForList(
151 "select * from POTRON_TRANS_ENTRY where trans_id=?",
152 new Long[] {
153 id
154 });
155
156 assertNotNull(entries);
157 assertEquals(2, entries.size());
158
159 values = (Map) entries.get(0);
160
161 assertNull(values.get("checked_date"));
162 assertNull(values.get("modified_date"));
163
164 List<TranslatedEntry> list = manager.getFuzzyEntries(id, false);
165 TranslatedEntry tran0 = list.get(0);
166 assertEquals(tran0.getTranslatedString(), "not yet translated");
167 assertTrue(tran0.isFuzzy());
168
169
170 manager.exportTranslation(id, new PrintWriter(System.out));
171
172
173 long entry_id = tran0.getId();
174 TranslatedEntry tran0_co = manager.checkOutEntry(entry_id);
175 assertNotNull(tran0_co);
176 assertNotNull(tran0_co.getEntry());
177 assertEquals(
178 tran0_co.getEntry().getKey(),
179 "Free Software Foundation, Inc.");
180
181 TranslatedEntry tran0_co_fail = null;
182 try {
183 tran0_co_fail = manager.checkOutEntry(entry_id);
184 } catch (Exception e) {
185 assertTrue((e instanceof AlreadyCheckedOutException));
186 }
187 assertNull(tran0_co_fail);
188
189 manager.checkInEntry(tran0_co_fail.getId(), "test", false);
190 }
191 }