/* Copyright (c) 2013-2014 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.geotools.cli.porcelain;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import jline.UnsupportedTerminal;
import jline.console.ConsoleReader;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.locationtech.geogig.api.Platform;
import org.locationtech.geogig.cli.CommandFailedException;
import org.locationtech.geogig.cli.GeogigCLI;
import org.mockito.exceptions.base.MockitoException;
/**
*
*/
public class PGDescribeTest extends Assert {
@Rule
public ExpectedException exception = ExpectedException.none();
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private GeogigCLI cli;
@Before
public void setUp() throws Exception {
ConsoleReader consoleReader = new ConsoleReader(System.in, System.out,
new UnsupportedTerminal());
cli = new GeogigCLI(consoleReader);
setUpGeogig(cli);
}
@After
public void tearDown() throws Exception {
cli.close();
}
@Test
public void testDescribe() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "table1";
describeCommand.dataStoreFactory = TestHelper.createTestFactory();
describeCommand.run(cli);
}
@Test
public void testDescribeHelp() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.help = true;
describeCommand.run(cli);
}
@Test
public void testInvalidDatabaseParams() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.commonArgs.host = "nonexistent";
describeCommand.table = "table1";
exception.expect(CommandFailedException.class);
describeCommand.run(cli);
}
@Test
public void testDescribeNonexistentTable() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "nonexistent";
describeCommand.dataStoreFactory = TestHelper.createTestFactory();
exception.expect(CommandFailedException.class);
describeCommand.run(cli);
}
@Test
public void testNoTable() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "";
describeCommand.dataStoreFactory = TestHelper.createTestFactory();
exception.expect(CommandFailedException.class);
describeCommand.run(cli);
}
@Test
public void testNullDataStore() throws Exception {
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "table1";
describeCommand.dataStoreFactory = TestHelper.createNullTestFactory();
exception.expect(CommandFailedException.class);
describeCommand.run(cli);
}
@Test
public void testDescribeException() throws Exception {
ConsoleReader consoleReader = new ConsoleReader(System.in, System.out,
new UnsupportedTerminal());
GeogigCLI mockCli = spy(new GeogigCLI(consoleReader));
setUpGeogig(mockCli);
when(mockCli.getConsole()).thenThrow(new MockitoException("Exception"));
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "table1";
describeCommand.dataStoreFactory = TestHelper.createTestFactory();
exception.expect(MockitoException.class);
describeCommand.run(mockCli);
}
@Test
public void testFlushException() throws Exception {
ConsoleReader consoleReader = spy(new ConsoleReader(System.in, System.out,
new UnsupportedTerminal()));
GeogigCLI testCli = new GeogigCLI(consoleReader);
setUpGeogig(testCli);
doThrow(new IOException("Exception")).when(consoleReader).flush();
PGDescribe describeCommand = new PGDescribe();
describeCommand.table = "table1";
describeCommand.dataStoreFactory = TestHelper.createTestFactory();
exception.expect(Exception.class);
describeCommand.run(testCli);
}
private void setUpGeogig(GeogigCLI cli) throws Exception {
final File userhome = tempFolder.newFolder("mockUserHomeDir");
final File workingDir = tempFolder.newFolder("mockWorkingDir");
tempFolder.newFolder("mockWorkingDir/.geogig");
final Platform platform = mock(Platform.class);
when(platform.pwd()).thenReturn(workingDir);
when(platform.getUserHome()).thenReturn(userhome);
cli.setPlatform(platform);
}
}