aboutsummaryrefslogtreecommitdiffstats
path: root/test/net/java/sip/communicator/slick/fileaccess/TestFailSafeTransaction.java
blob: 36872879b2d12ebe1a920d5befeeb10a02997a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package net.java.sip.communicator.slick.fileaccess;

import java.io.*;

import junit.framework.*;

import org.jitsi.service.fileaccess.*;
import org.osgi.framework.*;

/**
 * Tests for the fail safe transactions
 * 
 * @author Benoit Pradelle
 */
public class TestFailSafeTransaction
    extends TestCase
{
    /**
     * The Service that we will be testing.
     */
    private FileAccessService fileAccessService = null;
    
    /**
     * Test data to write in the original file
     */
    private static final String origData = "this is a test for the fail safe "
        + "transaction ability in SIP Communicator";
    
    /**
     * Test data to add to the file
     */
    private static final String addedData = " which is the greatest IM client "
        + "in the world !";
    
    /**
     * Test data to never write in the file
     */
    private static final String wrongData = "all the file is damaged now !";
    
    /**
     * The base for the name of the temp file
     */
    private static String tempName = "wzsxedcrfv" + System.currentTimeMillis();
    
    /**
     * Standart constructor.
     * 
     * @param name 
     */
    public TestFailSafeTransaction(String name)
    {
        super(name);
        BundleContext context = FileAccessServiceLick.bc;
        ServiceReference ref = context
                .getServiceReference(FileAccessService.class.getName());
        this.fileAccessService = (FileAccessService) context.getService(ref);
    }
    /**
     * Tests the commit operation
     */
    public void testCommit() {
        try {
            // setup a temp file
            File temp = File.createTempFile(tempName + "a", null);
            FileOutputStream out = new FileOutputStream(temp);
            
            out.write(origData.getBytes());
            
            // write a modification during a transaction
            FailSafeTransaction trans = this.fileAccessService
                                            .createFailSafeTransaction(temp);
            trans.beginTransaction();
            
            out.write(addedData.getBytes());
            
            trans.commit();
            
            out.close();
            
            // test if the two writes are ok
            // file length
            assertEquals("the file hasn't the right size after a commit",
                    temp.length(),
                    origData.length() + addedData.length());
            
            FileInputStream in = new FileInputStream(temp);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            in.close();
            String content = new String(buffer);
            
            // file content
            assertEquals("the file content isn't correct",
                    origData + addedData,
                    content);
            
            temp.delete();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }   
    }
    
    /**
     * Tests the rollback operation
     */
    public void testRollback() {
        try {
            // setup a temp file
            File temp = File.createTempFile(tempName + "b", null);
            FileOutputStream out = new FileOutputStream(temp);
            byte[] origDataBytes = origData.getBytes();
            
            out.write(origDataBytes);
            out.flush();
            
            // write a modification during a transaction
            FailSafeTransaction trans = this.fileAccessService
                                            .createFailSafeTransaction(temp);
            trans.beginTransaction();
            
            out.write(wrongData.getBytes());
            out.flush();
            
            trans.rollback();
            
            out.close();
            
            // test if the two writes are ok
            // file length
            assertEquals("the file hasn't the right size after a commit",
                    temp.length(),
                    origDataBytes.length);
            
            FileInputStream in = new FileInputStream(temp);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            in.close();
            String content = new String(buffer);
            
            // file content
            assertEquals("the file content isn't correct",
                    origData,
                    content);
            
            temp.delete();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
    
    /**
     * Tests if the file is commited when we start a new transaction
     */
    public void testCommitOnReOpen() {
        try {
            // setup a temp file
            File temp = File.createTempFile(tempName + "c", null);
            FileOutputStream out = new FileOutputStream(temp);
            
            out.write(origData.getBytes());
            
            // write a modification during a transaction
            FailSafeTransaction trans = this.fileAccessService
                                            .createFailSafeTransaction(temp);
            trans.beginTransaction();
            
            out.write(addedData.getBytes());
            
            // this transaction isn't closed, it should commit the changes
            trans.beginTransaction();
            
            // just to be sure to clean everything
            // the rollback must rollback nothing
            trans.rollback();
            
            out.close();
            
            // test if the two writes are ok
            // file length
            assertEquals("the file hasn't the right size after a commit",
                    temp.length(),
                    origData.length() + addedData.length());
            
            FileInputStream in = new FileInputStream(temp);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            in.close();
            String content = new String(buffer);
            
            // file content
            assertEquals("the file content isn't correct",
                    origData + addedData,
                    content);
            
            temp.delete();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
    
    /**
     * Tests if the file is rollback-ed if the transaction is never closed
     */
    public void testRollbackOnFailure() {
        try {
            // setup a temp file
            File temp = File.createTempFile(tempName + "d", null);
            FileOutputStream out = new FileOutputStream(temp);
            byte[] origDataBytes = origData.getBytes();
            
            out.write(origDataBytes);
            out.flush();
            
            // write a modification during a transaction
            FailSafeTransaction trans = this.fileAccessService
                                            .createFailSafeTransaction(temp);
            FailSafeTransaction trans2 = this.fileAccessService
                                            .createFailSafeTransaction(temp);
            trans.beginTransaction();
            
            out.write(wrongData.getBytes());
            out.flush();
            
            // we suppose here that SC crashed without closing properly the 
            // transaction. When it restarts, the modification must have been
            // rollback-ed
            
            trans2.restoreFile();
            
            out.close();
            
            // test if the two writes are ok
            // file length
            assertEquals("the file hasn't the right size after a commit",
                    temp.length(),
                    origDataBytes.length);
            
            FileInputStream in = new FileInputStream(temp);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            in.close();
            String content = new String(buffer);
            
            // file content
            assertEquals("the file content isn't correct",
                    origData,
                    content);
            
            temp.delete();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
}