View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.File;
25  import java.nio.charset.StandardCharsets;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  import org.eclipse.aether.internal.impl.TrackingFileManager;
34  import org.eclipse.aether.internal.test.util.TestFileUtils;
35  import org.junit.Test;
36  
37  /**
38   */
39  public class TrackingFileManagerTest
40  {
41  
42      @Test
43      public void testRead()
44          throws Exception
45      {
46          TrackingFileManager tfm = new TrackingFileManager();
47  
48          File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
49          Properties props = tfm.read( propFile );
50  
51          assertNotNull( props );
52          assertEquals( String.valueOf( props ), 2, props.size() );
53          assertEquals( "value1", props.get( "key1" ) );
54          assertEquals( "value2", props.get( "key2" ) );
55  
56          assertTrue( "Leaked file: " + propFile, propFile.delete() );
57  
58          props = tfm.read( propFile );
59          assertNull( String.valueOf( props ), props );
60      }
61  
62      @Test
63      public void testReadNoFileLeak()
64          throws Exception
65      {
66          TrackingFileManager tfm = new TrackingFileManager();
67  
68          for ( int i = 0; i < 1000; i++ )
69          {
70              File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
71              assertNotNull( tfm.read( propFile ) );
72              assertTrue( "Leaked file: " + propFile, propFile.delete() );
73          }
74      }
75  
76      @Test
77      public void testUpdate()
78          throws Exception
79      {
80          TrackingFileManager tfm = new TrackingFileManager();
81  
82          // NOTE: The excessive repetitions are to check the update properly truncates the file
83          File propFile = TestFileUtils.createTempFile( "key1=value1\nkey2 : value2\n".getBytes( StandardCharsets.UTF_8 ), 1000 );
84  
85          Map<String, String> updates = new HashMap<String, String>();
86          updates.put( "key1", "v" );
87          updates.put( "key2", null );
88  
89          tfm.update( propFile, updates );
90  
91          Properties props = tfm.read( propFile );
92  
93          assertNotNull( props );
94          assertEquals( String.valueOf( props ), 1, props.size() );
95          assertEquals( "v", props.get( "key1" ) );
96          assertNull( String.valueOf( props.get( "key2" ) ), props.get( "key2" ) );
97      }
98  
99      @Test
100     public void testUpdateNoFileLeak()
101         throws Exception
102     {
103         TrackingFileManager tfm = new TrackingFileManager();
104 
105         Map<String, String> updates = new HashMap<String, String>();
106         updates.put( "k", "v" );
107 
108         for ( int i = 0; i < 1000; i++ )
109         {
110             File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
111             assertNotNull( tfm.update( propFile, updates ) );
112             assertTrue( "Leaked file: " + propFile, propFile.delete() );
113         }
114     }
115 
116     @Test
117     public void testLockingOnCanonicalPath()
118         throws Exception
119     {
120         final TrackingFileManager tfm = new TrackingFileManager();
121 
122         final File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
123 
124         final List<Throwable> errors = Collections.synchronizedList( new ArrayList<Throwable>() );
125 
126         Thread[] threads = new Thread[4];
127         for ( int i = 0; i < threads.length; i++ )
128         {
129             String path = propFile.getParent();
130             for ( int j = 0; j < i; j++ )
131             {
132                 path += "/.";
133             }
134             path += "/" + propFile.getName();
135             final File file = new File( path );
136 
137             threads[i] = new Thread()
138             {
139                 public void run()
140                 {
141                     try
142                     {
143                         for ( int i = 0; i < 1000; i++ )
144                         {
145                             assertNotNull( tfm.read( file ) );
146                         }
147                     }
148                     catch ( Throwable e )
149                     {
150                         errors.add( e );
151                     }
152                 }
153             };
154         }
155 
156         for ( Thread thread1 : threads )
157         {
158             thread1.start();
159         }
160 
161         for ( Thread thread : threads )
162         {
163             thread.join();
164         }
165 
166         assertEquals( Collections.emptyList(), errors );
167     }
168 
169 }