001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.io.File;
025import java.nio.charset.StandardCharsets;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.Properties;
029
030import org.eclipse.aether.internal.impl.TrackingFileManager;
031import org.eclipse.aether.internal.test.util.TestFileUtils;
032import org.junit.Test;
033
034/**
035 */
036public class DefaultTrackingFileManagerTest
037{
038
039    @Test
040    public void testRead()
041        throws Exception
042    {
043        TrackingFileManager tfm = new DefaultTrackingFileManager();
044
045        File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
046        Properties props = tfm.read( propFile );
047
048        assertNotNull( props );
049        assertEquals( String.valueOf( props ), 2, props.size() );
050        assertEquals( "value1", props.get( "key1" ) );
051        assertEquals( "value2", props.get( "key2" ) );
052
053        assertTrue( "Leaked file: " + propFile, propFile.delete() );
054
055        props = tfm.read( propFile );
056        assertNull( String.valueOf( props ), props );
057    }
058
059    @Test
060    public void testReadNoFileLeak()
061        throws Exception
062    {
063        TrackingFileManager tfm = new DefaultTrackingFileManager();
064
065        for ( int i = 0; i < 1000; i++ )
066        {
067            File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" );
068            assertNotNull( tfm.read( propFile ) );
069            assertTrue( "Leaked file: " + propFile, propFile.delete() );
070        }
071    }
072
073    @Test
074    public void testUpdate()
075        throws Exception
076    {
077        TrackingFileManager tfm = new DefaultTrackingFileManager();
078
079        // NOTE: The excessive repetitions are to check the update properly truncates the file
080        File propFile = TestFileUtils.createTempFile( "key1=value1\nkey2 : value2\n".getBytes( StandardCharsets.UTF_8 ), 1000 );
081
082        Map<String, String> updates = new HashMap<>();
083        updates.put( "key1", "v" );
084        updates.put( "key2", null );
085
086        tfm.update( propFile, updates );
087
088        Properties props = tfm.read( propFile );
089
090        assertNotNull( props );
091        assertEquals( String.valueOf( props ), 1, props.size() );
092        assertEquals( "v", props.get( "key1" ) );
093        assertNull( String.valueOf( props.get( "key2" ) ), props.get( "key2" ) );
094    }
095
096    @Test
097    public void testUpdateNoFileLeak()
098        throws Exception
099    {
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}