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.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          // NOTE: The excessive repetitions are to check the update properly truncates the file
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 }