/*
* Copyright 2010 netling project <http://netling.org>
*
* Licensed 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
*
* http://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.
*
* This file may incorporate work covered by the following copyright and
* permission notice:
*
* 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
*
* http://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.netling.ftp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import org.netling.SocketClient;
public class FTPTest {
FTP ftp = new FTP();
BufferedWriter writer;
StringWriter output;
BufferedReader reader;
@Before
public void setUp() {
output = new StringWriter();
writer = new BufferedWriter(output);
ftp.setControlOutput(writer);
}
@Test
public void testStrictTest() {
String line = "200 OK";
assertFalse(ftp.strictCheck(line, "200"));
line = "This is a test line";
assertTrue(ftp.strictCheck(line, "200"));
}
@Test
public void testLenientTest() {
String line = "abc";
assertTrue(ftp.lenientCheck(line));
line = "abc-1234";
assertTrue(ftp.lenientCheck(line));
line = "426-blahblah";
assertTrue(ftp.lenientCheck(line));
line = "200 OK";
assertFalse(ftp.lenientCheck(line));
}
@Test
public void testUser() throws IOException {
String response = "331 User OK";
setupResponse(response);
ftp.user("rory");
assertEquals(output.toString(), FTPCommand.USER.command() + " rory" + SocketClient.NETASCII_EOL);
assertEquals(ftp.getReplyCode(), 331);
assertTrue(FTPReply.isPositiveIntermediate(ftp.getReplyCode()));
assertEquals(ftp.getReplyString(), response + SocketClient.NETASCII_EOL);
}
@Test
public void testPass() throws IOException {
String response = "230 Logged In Ok";
setupResponse(response);
ftp.pass("mypassword");
assertEquals(output.toString(), FTPCommand.PASS + " mypassword" + SocketClient.NETASCII_EOL);
assertEquals(ftp.getReplyCode(), 230);
assertTrue(FTPReply.isPositiveCompletion(ftp.getReplyCode()));
assertEquals(ftp.getReplyString(), response + SocketClient.NETASCII_EOL);
}
private void setupResponse(String response) {
reader = new BufferedReader(new StringReader(response));
ftp.setControlInput(reader);
}
}