    001/*
    002 * Copyright (c) 2016 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
    003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    004 *
    005 * Project and contact information: http://www.cascading.org/
    006 *
    007 * This file is part of the Cascading project.
    008 *
    009 * Licensed under the Apache License, Version 2.0 (the "License");
    010 * you may not use this file except in compliance with the License.
    011 * You may obtain a copy of the License at
    012 *
    013 *     http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing, software
    016 * distributed under the License is distributed on an "AS IS" BASIS,
    017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    018 * See the License for the specific language governing permissions and
    019 * limitations under the License.
    020 */
    021
    022package cascading.platform.local;
    023
    024import java.io.File;
    025import java.io.IOException;
    026import java.util.Comparator;
    027import java.util.Map;
    028import java.util.Properties;
    029
    030import cascading.flow.FlowConnector;
    031import cascading.flow.FlowProcess;
    032import cascading.flow.FlowSession;
    033import cascading.flow.local.LocalFlowConnector;
    034import cascading.flow.local.LocalFlowProcess;
    035import cascading.platform.TestPlatform;
    036import cascading.property.PropertyUtil;
    037import cascading.scheme.Scheme;
    038import cascading.scheme.local.TextDelimited;
    039import cascading.scheme.local.TextLine;
    040import cascading.scheme.util.DelimitedParser;
    041import cascading.scheme.util.FieldTypeResolver;
    042import cascading.tap.SinkMode;
    043import cascading.tap.Tap;
    044import cascading.tap.local.DirTap;
    045import cascading.tap.local.FileTap;
    046import cascading.tap.local.PartitionTap;
    047import cascading.tap.partition.Partition;
    048import cascading.tuple.Fields;
    049import org.apache.commons.io.FileUtils;
    050
    051/**
    052 * Class LocalPlatform is automatically loaded and injected into a {@link cascading.PlatformTestCase} instance
    053 * so that all *PlatformTest classes can be tested against the Cascading local mode planner.
    054 */
    055public class LocalPlatform extends TestPlatform
    056  {
    057  private Properties properties = new Properties();
    058
    059  @Override
    060  public void setUp() throws IOException
    061    {
    062    properties.putAll( getGlobalProperties() );
    063    }
    064
    065  @Override
    066  public Map<Object, Object> getProperties()
    067    {
    068    return PropertyUtil.asFlatMap( properties );
    069    }
    070
    071  @Override
    072  public void tearDown()
    073    {
    074    }
    075
    076  @Override
    077  public void copyFromLocal( String inputFile ) throws IOException
    078    {
    079    }
    080
    081  @Override
    082  public void copyToLocal( String outputFile ) throws IOException
    083    {
    084    }
    085
    086  @Override
    087  public boolean remoteExists( String outputFile ) throws IOException
    088    {
    089    return new File( outputFile ).exists();
    090    }
    091
    092  @Override
    093  public boolean remoteRemove( String outputFile, boolean recursive ) throws IOException
    094    {
    095    if( !remoteExists( outputFile ) )
    096      return true;
    097
    098    File file = new File( outputFile );
    099
    100    if( !recursive || !file.isDirectory() )
    101      return file.delete();
    102
    103    try
    104      {
    105      FileUtils.deleteDirectory( file );
    106      }
    107    catch( IOException exception )
    108      {
    109      return false;
    110      }
    111
    112    return !file.exists();
    113    }
    114
    115  @Override
    116  public FlowProcess getFlowProcess()
    117    {
    118    return new LocalFlowProcess( FlowSession.NULL, PropertyUtil.createProperties( getProperties(), null ) );
    119    }
    120
    121  @Override
    122  public FlowConnector getFlowConnector( Map<Object, Object> properties )
    123    {
    124    return new LocalFlowConnector( properties );
    125    }
    126
    127  @Override
    128  public Tap getTap( Scheme scheme, String filename, SinkMode mode )
    129    {
    130    return new FileTap( scheme, filename, mode );
    131    }
    132
    133  @Override
    134  public Tap getTextFile( Fields sourceFields, Fields sinkFields, String filename, SinkMode mode )
    135    {
    136    if( sourceFields == null )
    137      return new DirTap( new TextLine(), filename, mode );
    138
    139    return new DirTap( new TextLine( sourceFields, sinkFields ), filename, mode );
    140    }
    141
    142  @Override
    143  public Tap getDelimitedFile( Fields fields, boolean hasHeader, String delimiter, String quote, Class[] types, String filename, SinkMode mode )
    144    {
    145    return new DirTap( new TextDelimited( fields, hasHeader, delimiter, quote, types ), filename, mode );
    146    }
    147
    148  @Override
    149  public Tap getDelimitedFile( Fields fields, boolean skipHeader, boolean writeHeader, String delimiter, String quote, Class[] types, String filename, SinkMode mode )
    150    {
    151    return new DirTap( new TextDelimited( fields, skipHeader, writeHeader, delimiter, quote, types ), filename, mode );
    152    }
    153
    154  @Override
    155  public Tap getDelimitedFile( String delimiter, String quote, FieldTypeResolver fieldTypeResolver, String filename, SinkMode mode )
    156    {
    157    return new DirTap( new TextDelimited( true, new DelimitedParser( delimiter, quote, fieldTypeResolver ) ), filename, mode );
    158    }
    159
    160  @Override
    161  public Tap getPartitionTap( Tap sink, Partition partition, int openThreshold )
    162    {
    163    return new PartitionTap( (FileTap) sink, partition, openThreshold );
    164    }
    165
    166  @Override
    167  public Scheme getTestConfigDefScheme()
    168    {
    169    return new LocalConfigDefScheme( new Fields( "line" ) );
    170    }
    171
    172  @Override
    173  public Scheme getTestFailScheme()
    174    {
    175    return new LocalFailScheme( new Fields( "line" ) );
    176    }
    177
    178  @Override
    179  public Comparator getLongComparator( boolean reverseSort )
    180    {
    181    return new TestLongComparator( reverseSort );
    182    }
    183
    184  @Override
    185  public Comparator getStringComparator( boolean reverseSort )
    186    {
    187    return new TestStringComparator( reverseSort );
    188    }
    189
    190  @Override
    191  public String getHiddenTemporaryPath()
    192    {
    193    return null;
    194    }
    195  }