1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo;
24
25 import java.util.Random;
26
27 /***
28 * @author MarcusT
29 */
30 public final class ProfileDocumentProxyTest extends BaseDocumentProxyTest {
31
32 private final Random random;
33
34 private String profileName;
35 private String profileKey;
36
37 /***
38 * @param name the name of the test
39 */
40 public ProfileDocumentProxyTest(String name) {
41 super(name);
42 random = new Random();
43 }
44
45 /***
46 * Tests a profile document to return its name.
47 */
48 public void testGetNameOfProfile() {
49 System.out.println("-> testGetNameOfProfile");
50 String profileNameG = "GN" + System.currentTimeMillis();
51 String profileKeyG = "GN" + System.currentTimeMillis();
52 DProfileDocument doc = getDatabase().getProfileDocument(profileNameG, profileKeyG);
53
54 String key = doc.getNameOfProfile();
55 assertEquals("The keys should be equal.", profileKeyG, key);
56
57 }
58
59 /***
60 * Tests a profile document to return its key.
61 */
62 public void testGetKey() {
63 System.out.println("-> testGetKey");
64
65 String profileNameG = "G" + System.currentTimeMillis();
66 String profileKeyG = "G" + System.currentTimeMillis();
67 DProfileDocument doc = getDatabase().getProfileDocument(profileNameG, profileKeyG);
68
69 String key = doc.getKey();
70 assertEquals("The keys should be equal.", profileKeyG, key);
71
72 }
73
74 /***
75 * Tests a profile document to its ability to save itself.
76 *
77 */
78 public void testSave() {
79 System.out.println("-> testSave");
80
81 String profileNameS = "B" + System.currentTimeMillis();
82 String profileKeyS = "Key" + System.currentTimeMillis();
83 DProfileDocument doc = getDatabase().getProfileDocument(profileNameS, profileKeyS);
84 String someValue = "someValueForTestSave";
85 doc.appendItemValue(getItemName(), someValue);
86 doc.save();
87
88 DProfileDocument received = getDatabase().getProfileDocument(profileNameS, profileKeyS);
89 String receivedValue = received.getItemValueString(getItemName());
90 assertEquals(
91 "The Document received from the database should contain an item, but does not.",
92 someValue,
93 receivedValue);
94
95 assertTrue("The Document received should be equal to the previously saved one.", doc.equals(received));
96
97 doc.remove(true);
98 }
99
100 /***
101 * Tests save(boolean).
102 */
103 public void testSaveWithOneArgument() {
104 System.out.println("-> testSaveWithOneArgument");
105
106 String profileNameE = "S_bool_" + System.currentTimeMillis();
107 String profileKeyE = "K_S_bool" + System.currentTimeMillis();
108 DProfileDocument doc = getDatabase().getProfileDocument(profileNameE, profileKeyE);
109
110 String someValue = "someValueForTestSave";
111 doc.appendItemValue(getItemName(), someValue);
112 boolean saved = doc.save(true);
113 assertTrue("The save action should work properly.", saved);
114
115 doc.appendItemValue(getItemName() + getItemName(), someValue);
116 saved = doc.save(false);
117 assertTrue("The unforced save action should work properly.", saved);
118
119 DProfileDocument received = getDatabase().getProfileDocument(profileNameE, profileKeyE);
120 assertNotNull("The received document should exist.", received);
121 String receivedValue = received.getItemValueString(getItemName());
122 assertEquals(
123 "The Document received from the database should contain an item, but does not.",
124 someValue, receivedValue);
125
126 String receivedValue1 = received.getItemValueString(getItemName() + getItemName());
127 assertEquals(
128 "The Document received from the database should contain a second item, but does not.",
129 someValue, receivedValue1);
130
131 assertTrue("The Document received should be equal to the previously saved one.", doc.equals(received));
132
133 doc.remove(true);
134 }
135
136 /***
137 * Tests save(boolean, boolean).
138 */
139 public void testSaveWithTwoArguments() {
140 System.out.println("-> testSaveWithTwoArguments");
141
142 String profileNameE = "S_boolbool_" + System.currentTimeMillis();
143 String profileKeyE = "K_S_boolbool" + System.currentTimeMillis();
144 DProfileDocument doc = getDatabase().getProfileDocument(profileNameE, profileKeyE);
145
146 String someValue = "someValueForTestSave";
147 doc.appendItemValue(getItemName(), someValue);
148 boolean saved = doc.save(true, false);
149 assertTrue("The save action should work properly.", saved);
150
151 doc.appendItemValue(getItemName() + getItemName(), someValue);
152 saved = doc.save(true, true);
153 assertTrue("The unforced save action should work properly.", saved);
154
155 DProfileDocument received = getDatabase().getProfileDocument(profileNameE, profileKeyE);
156 assertNotNull("The received document should exist.", received);
157 String receivedValue = received.getItemValueString(getItemName());
158 assertEquals(
159 "The Document received from the database should contain an item, but does not.",
160 someValue, receivedValue);
161
162 String receivedValue1 = received.getItemValueString(getItemName() + getItemName());
163 assertEquals(
164 "The Document received from the database should contain a second item, but does not.",
165 someValue, receivedValue1);
166
167 assertTrue("The Document received should be equal to the previously saved one.", doc.equals(received));
168
169 doc.remove(true);
170 }
171
172 /***
173 * Tests a profile document on its equality to others.
174 *
175 */
176 public void testEquals() {
177 System.out.println("-> testEquals");
178
179 String profileNameE = "A" + System.currentTimeMillis();
180 String profileKeyE = "K" + System.currentTimeMillis();
181 DProfileDocument doc = getDatabase().getProfileDocument(profileNameE, profileKeyE);
182 doc.save();
183 DProfileDocument receivedDoc = getDatabase().getProfileDocument(profileNameE, profileKeyE);
184 assertEquals("Documents should be equal but are not.", doc, receivedDoc);
185
186 DBaseDocument anotherOne = createDBaseDocument();
187 assertTrue("Documents should NOT be equal but are.", !doc.equals(anotherOne));
188
189 doc.remove(true);
190 }
191
192 /***
193 * @see de.bea.domingo.BaseDocumentProxyTest#createDBaseDocument()
194 * {@inheritDoc}
195 */
196 protected DBaseDocument createDBaseDocument() {
197 profileName = createStringKey();
198 profileKey = createStringKey();
199 return getDatabase().getProfileDocument(profileName, profileKey);
200 }
201
202 /***
203 * Creates a unique key by current time in milliseconds and a concatenated
204 * random number string from 0 to 10000.
205 * @return A String that should suit as unique key
206 */
207 private String createStringKey() {
208 return "" + System.currentTimeMillis() + random.nextInt(10000);
209 }
210
211 /***
212 * Sets up the proper test environment for each test again.
213 * Sets: profileName, profileKey
214 */
215 public void setUp() {
216 baseSetUp();
217 profileName = "someProfileName";
218 profileKey = "someProfileKey";
219 }
220 }