Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9fc05f521451bee13304cb8cbfeb0bca27afce32 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2012, 2015 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tests.tcf.filesystem.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.TimeoutException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.tcf.core.concurrent.Rendezvous;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.CacheManager;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.FileState;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.PersistenceManager;
import org.eclipse.tcf.te.tcf.filesystem.core.model.CacheState;

public class StateManagerTest extends UtilsTestBase {

	public void testCacheStateConsistent() throws Exception {
		cleanUp();
		writeFileContent("hello,world!"); //$NON-NLS-1$
		updateCache(testFile);
		CacheState cacheState = testFile.getCacheState();
		assertEquals(CacheState.consistent, cacheState);
		Thread.sleep(5000L);
	}

	public void testCacheStateModified() throws Exception {
		cleanUp();
		writeFileContent("hello,world!"); //$NON-NLS-1$
		updateCache(testFile);
	    File file = CacheManager.getCacheFile(testFile);
	    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
	    writer.write("hello, test!"); //$NON-NLS-1$
	    writer.close();
		updateCacheState();
		CacheState cacheState = testFile.getCacheState();
	    assertEquals(CacheState.modified, cacheState);
		Thread.sleep(5000L);
	}

	private void updateCacheState() throws TimeoutException {
	    FileState digest = PersistenceManager.getInstance().getFileDigest(testFile);
		final Rendezvous rendezvous = new Rendezvous();
		digest.updateState(new Callback(){
			@Override
            protected void internalDone(Object caller, IStatus status) {
				rendezvous.arrive();
            }
		});
		rendezvous.waiting(10000);
    }

	private void refreshCacheState() throws TimeoutException {
		final Rendezvous rendezvous = new Rendezvous();
		testFile.operationRefresh(true).runInJob(new Callback(){
			@Override
            protected void internalDone(Object caller, IStatus status) {
				rendezvous.arrive();
            }
		});
		rendezvous.waiting(10000);
	}


	public void testCacheStateOutdated() throws Exception {
		cleanUp();
		writeFileContent("hello,world!"); //$NON-NLS-1$
		updateCache(testFile);
		Thread.sleep(2000L);
		writeFileContent("hello,test!"); //$NON-NLS-1$
		refreshCacheState();
		CacheState cacheState = testFile.getCacheState();
		assertEquals(CacheState.outdated, cacheState);
		Thread.sleep(5000L);
	}

	private void cleanUp() {
		File cacheFile = CacheManager.getCacheFile(testFile);
		if(cacheFile.exists()) {
			cacheFile.delete();
		}
		PersistenceManager.getInstance().removeFileDigest(testFile.getLocationURI());
	}

	public void testCacheStateConflict() throws Exception {
		cleanUp();
		writeFileContent("hello,world!"); //$NON-NLS-1$
		updateCache(testFile);
		writeFileContent("hello,test!"); //$NON-NLS-1$
		refreshCacheState();
		Thread.sleep(2000L);
	    File file = CacheManager.getCacheFile(testFile);
	    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
	    writer.write("hello, earth!"); //$NON-NLS-1$
	    writer.close();
		updateCacheState();
		CacheState cacheState = testFile.getCacheState();
		assertEquals(CacheState.conflict, cacheState);
		Thread.sleep(5000L);
	}
}

Back to the top