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.nio.charset.StandardCharsets;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Properties;
29
30 import org.eclipse.aether.internal.impl.TrackingFileManager;
31 import org.eclipse.aether.internal.test.util.TestFileUtils;
32 import org.junit.Test;
33
34
35
36 public class DefaultTrackingFileManagerTest
37 {
38
39 @Test
40 public void testRead()
41 throws Exception
42 {
43 TrackingFileManager tfm = new DefaultTrackingFileManager();
44
45 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
46 Properties props = tfm.read( propFile );
47
48 assertNotNull( props );
49 assertEquals( String.valueOf( props ), 2, props.size() );
50 assertEquals( "value1", props.get( "key1" ) );
51 assertEquals( "value2", props.get( "key2" ) );
52
53 assertTrue( "Leaked file: " + propFile, propFile.delete() );
54
55 props = tfm.read( propFile );
56 assertNull( String.valueOf( props ), props );
57 }
58
59 @Test
60 public void testReadNoFileLeak()
61 throws Exception
62 {
63 TrackingFileManager tfm = new DefaultTrackingFileManager();
64
65 for ( int i = 0; i < 1000; i++ )
66 {
67 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
68 assertNotNull( tfm.read( propFile ) );
69 assertTrue( "Leaked file: " + propFile, propFile.delete() );
70 }
71 }
72
73 @Test
74 public void testUpdate()
75 throws Exception
76 {
77 TrackingFileManager tfm = new DefaultTrackingFileManager();
78
79
80 File propFile = TestFileUtils.createTempFile( "key1=value1\nkey2 : value2\n".getBytes( StandardCharsets.UTF_8 ), 1000 );
81
82 Map<String, String> updates = new HashMap<>();
83 updates.put( "key1", "v" );
84 updates.put( "key2", null );
85
86 tfm.update( propFile, updates );
87
88 Properties props = tfm.read( propFile );
89
90 assertNotNull( props );
91 assertEquals( String.valueOf( props ), 1, props.size() );
92 assertEquals( "v", props.get( "key1" ) );
93 assertNull( String.valueOf( props.get( "key2" ) ), props.get( "key2" ) );
94 }
95
96 @Test
97 public void testUpdateNoFileLeak()
98 throws Exception
99 {
100 TrackingFileManager tfm = new DefaultTrackingFileManager();
101
102 Map<String, String> updates = new HashMap<>();
103 updates.put( "k", "v" );
104
105 for ( int i = 0; i < 1000; i++ )
106 {
107 File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
108 assertNotNull( tfm.update( propFile, updates ) );
109 assertTrue( "Leaked file: " + propFile, propFile.delete() );
110 }
111 }
112
113 }