NetwareFTPEntryParserTest.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.net.ftp.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Calendar;

import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileEntryParser;
import org.junit.jupiter.api.Test;

/**
 */
public class NetwareFTPEntryParserTest extends AbstractFTPParseTest {

    private static final String[] badsamples = { "a [-----F--] SCION_SYS                         512 Apr 13 23:52 SYS",
            "d [----AF--]          0                        512 10-04-2001 _ADMIN" };

    private static final String[] goodsamples = { "d [-----F--] SCION_SYS                         512 Apr 13 23:52 SYS",
            "d [----AF--]          0                        512 Feb 22 17:32 _ADMIN", "d [-W---F--] SCION_VOL2                        512 Apr 13 23:12 VOL2",
            "- [RWCEAFMS] rwinston                        19968 Mar 12 15:20 Executive Summary.doc",
            "d [RWCEAFMS] rwinston                          512 Nov 24  2005 Favorites" };

    @Override
    protected String[] getBadListing() {
        return badsamples;
    }

    @Override
    protected String[] getGoodListing() {
        return goodsamples;
    }

    @Override
    protected FTPFileEntryParser getParser() {
        return new NetwareFTPEntryParser();
    }

    @Override
    @Test
    public void testDefaultPrecision() {
        testPrecision("d [RWCEAFMS] rwinston                          512 Nov 24  2005 Favorites", CalendarUnit.DAY_OF_MONTH);
    }

    @Override
    @Test
    public void testParseFieldsOnDirectory() throws Exception {
        final String reply = "d [-W---F--] testUser                        512 Apr 13 23:12 testFile";
        final FTPFile f = getParser().parseFTPEntry(reply);

        assertNotNull(f, "Could not parse file");
        assertEquals("testFile", f.getName());
        assertEquals(512L, f.getSize());
        assertEquals("testUser", f.getUser());
        assertTrue(f.isDirectory(), "Directory flag is not set!");

        final Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, 3);
        cal.set(Calendar.DAY_OF_MONTH, 13);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 12);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.YEAR, f.getTimestamp().get(Calendar.YEAR));

        assertEquals(df.format(cal.getTime()), df.format(f.getTimestamp().getTime()));

    }

    @Override
    @Test
    public void testParseFieldsOnFile() throws Exception {
        final String reply = "- [R-CEAFMS] rwinston                        19968 Mar 12 15:20 Document name with spaces.doc";

        final FTPFile f = getParser().parseFTPEntry(reply);

        assertNotNull(f, "Could not parse file");
        assertEquals("Document name with spaces.doc", f.getName());
        assertEquals(19968L, f.getSize());
        assertEquals("rwinston", f.getUser());
        assertTrue(f.isFile(), "File flag is not set!");

        assertTrue(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
        assertFalse(f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    }

    @Override
    @Test
    public void testRecentPrecision() {
        testPrecision("- [RWCEAFMS] rwinston                        19968 Mar 12 15:20 Executive Summary.doc", CalendarUnit.MINUTE);
    }

}