Skip to main content
summaryrefslogtreecommitdiffstats
blob: 279d0150154a86c28aa9c07edc6083614dc1a81f (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
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
package org.eclipse.jetty.security;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.Assert;

import org.eclipse.jetty.http.security.Credential;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class PropertyUserStoreTest
{
	String testFileDir="target" + File.separator + "property-user-store-test";
	String testFile = testFileDir + File.separator + "users.txt";
	
	@Before
	public void before() throws Exception 
	{
		File file = new File(testFileDir);
		file.mkdirs();
		
		writeInitialUsers(testFile);
	}
	
	@After
	public void after() throws Exception
	{
		File file = new File(testFile);

		file.delete();
	}

	private void writeInitialUsers(String testFile) throws Exception
	{
		BufferedWriter writer = new BufferedWriter(new FileWriter(testFile));
		writer.append("tom: tom, roleA\n");
		writer.append("dick: dick, roleB\n");
		writer.append("harry: harry, roleA, roleB\n");
		writer.close();
	}
	
	private void writeAdditionalUser(String testFile) throws Exception
	{
		BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true));
		writer.append("skip: skip, roleA\n");
		writer.close();
	}
	
	
	@Test
	public void testPropertyUserStoreLoad() throws Exception
	{
		final AtomicInteger userCount = new AtomicInteger();
				
		PropertyUserStore store = new PropertyUserStore();
		
		store.setConfig(testFile);
				
		store.registerUserListener(new PropertyUserStore.UserListener() {
			
			public void update(String username, Credential credential, String[] roleArray) 
			{
				userCount.getAndIncrement();
			}
			
			public void remove(String username) 
			{

			}
		});		
		
		store.start();
		
		Assert.assertEquals(3, userCount.get());		
	}

	@Test
	public void testPropertyUserStoreLoadUpdateUser() throws Exception
	{
		
		final AtomicInteger userCount = new AtomicInteger();
		
		final List<String> users = new ArrayList<String>();
		
		PropertyUserStore store = new PropertyUserStore();
		store.setRefreshInterval(2);
		store.setConfig(testFile);
		
		store.registerUserListener(new PropertyUserStore.UserListener() {
			
			public void update(String username, Credential credential, String[] roleArray) 
			{
				if ( !users.contains(username))
				{
					users.add(username);
					userCount.getAndIncrement();
				}
			}
			
			public void remove(String username) 
			{
				
			}
		});
				
		store.start();

		Assert.assertEquals(3, userCount.get());
	
		store.start();
				
		Thread.sleep(2000);
		
		writeAdditionalUser(testFile);
		
		Thread.sleep(2000);
		
		Assert.assertEquals(4, userCount.get());
		Assert.assertTrue(users.contains("skip"));		
	}
	
	@Test
	public void testPropertyUserStoreLoadRemoveUser() throws Exception
	{
		writeAdditionalUser(testFile);
		
		final AtomicInteger userCount = new AtomicInteger();
		
		final List<String> users = new ArrayList<String>();
		
		PropertyUserStore store = new PropertyUserStore();
		store.setRefreshInterval(2);
		store.setConfig(testFile);
		
		store.registerUserListener(new PropertyUserStore.UserListener() {
			
			public void update(String username, Credential credential, String[] roleArray) {
				if ( !users.contains(username))
				{
					users.add(username);
					userCount.getAndIncrement();
				}
			}
			
			public void remove(String username) {
				users.remove(username);				
				userCount.getAndDecrement();
			}
		});
			
		store.start();
		
		Assert.assertEquals(4, userCount.get());
			
		Thread.sleep(2000);		
		writeInitialUsers(testFile);		
		Thread.sleep(3000);
		Assert.assertEquals(3, userCount.get());
	}
	
}

Back to the top