1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import org.eclipse.aether.internal.impl.TrackingFileManager;
33 import org.eclipse.aether.internal.test.util.TestFileUtils;
34 import org.junit.Test;
35
36
37
38 public class TrackingFileManagerTest
39 {
40
41 @Test
42 public void testRead()
43 throws Exception
44 {
45 TrackingFileManager tfm = new TrackingFileManager();
46
47 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
48 Properties props = tfm.read( propFile );
49
50 assertNotNull( props );
51 assertEquals( String.valueOf( props ), 2, props.size() );
52 assertEquals( "value1", props.get( "key1" ) );
53 assertEquals( "value2", props.get( "key2" ) );
54
55 assertTrue( "Leaked file: " + propFile, propFile.delete() );
56
57 props = tfm.read( propFile );
58 assertNull( String.valueOf( props ), props );
59 }
60
61 @Test
62 public void testReadNoFileLeak()
63 throws Exception
64 {
65 TrackingFileManager tfm = new TrackingFileManager();
66
67 for ( int i = 0; i < 1000; i++ )
68 {
69 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
70 assertNotNull( tfm.read( propFile ) );
71 assertTrue( "Leaked file: " + propFile, propFile.delete() );
72 }
73 }
74
75 @Test
76 public void testUpdate()
77 throws Exception
78 {
79 TrackingFileManager tfm = new TrackingFileManager();
80
81
82 File propFile = TestFileUtils.createTempFile( "key1=value1\nkey2 : value2\n".getBytes( "UTF-8" ), 1000 );
83
84 Map<String, String> updates = new HashMap<String, String>();
85 updates.put( "key1", "v" );
86 updates.put( "key2", null );
87
88 tfm.update( propFile, updates );
89
90 Properties props = tfm.read( propFile );
91
92 assertNotNull( props );
93 assertEquals( String.valueOf( props ), 1, props.size() );
94 assertEquals( "v", props.get( "key1" ) );
95 assertNull( String.valueOf( props.get( "key2" ) ), props.get( "key2" ) );
96 }
97
98 @Test
99 public void testUpdateNoFileLeak()
100 throws Exception
101 {
102 TrackingFileManager tfm = new TrackingFileManager();
103
104 Map<String, String> updates = new HashMap<String, String>();
105 updates.put( "k", "v" );
106
107 for ( int i = 0; i < 1000; i++ )
108 {
109 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
110 assertNotNull( tfm.update( propFile, updates ) );
111 assertTrue( "Leaked file: " + propFile, propFile.delete() );
112 }
113 }
114
115 @Test
116 public void testLockingOnCanonicalPath()
117 throws Exception
118 {
119 final TrackingFileManager tfm = new TrackingFileManager();
120
121 final File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
122
123 final List<Throwable> errors = Collections.synchronizedList( new ArrayList<Throwable>() );
124
125 Thread[] threads = new Thread[4];
126 for ( int i = 0; i < threads.length; i++ )
127 {
128 String path = propFile.getParent();
129 for ( int j = 0; j < i; j++ )
130 {
131 path += "/.";
132 }
133 path += "/" + propFile.getName();
134 final File file = new File( path );
135
136 threads[i] = new Thread()
137 {
138 public void run()
139 {
140 try
141 {
142 for ( int i = 0; i < 1000; i++ )
143 {
144 assertNotNull( tfm.read( file ) );
145 }
146 }
147 catch ( Throwable e )
148 {
149 errors.add( e );
150 }
151 }
152 };
153 }
154
155 for ( Thread thread1 : threads )
156 {
157 thread1.start();
158 }
159
160 for ( Thread thread : threads )
161 {
162 thread.join();
163 }
164
165 assertEquals( Collections.emptyList(), errors );
166 }
167
168 }