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.FileTap;
045import cascading.tap.local.PartitionTap;
046import cascading.tap.partition.Partition;
047import cascading.tuple.Fields;
048import org.apache.commons.io.FileUtils;
049
050/**
051 * Class LocalPlatform is automatically loaded and injected into a {@link cascading.PlatformTestCase} instance
052 * so that all *PlatformTest classes can be tested against the Cascading local mode planner.
053 */
054public class LocalPlatform extends TestPlatform
055  {
056  private Properties properties = new Properties();
057
058  @Override
059  public void setUp() throws IOException
060    {
061    properties.putAll( getGlobalProperties() );
062    }
063
064  @Override
065  public Map<Object, Object> getProperties()
066    {
067    return PropertyUtil.asFlatMap( properties );
068    }
069
070  @Override
071  public void tearDown()
072    {
073    }
074
075  @Override
076  public void copyFromLocal( String inputFile ) throws IOException
077    {
078    }
079
080  @Override
081  public void copyToLocal( String outputFile ) throws IOException
082    {
083    }
084
085  @Override
086  public boolean remoteExists( String outputFile ) throws IOException
087    {
088    return new File( outputFile ).exists();
089    }
090
091  @Override
092  public boolean remoteRemove( String outputFile, boolean recursive ) throws IOException
093    {
094    if( !remoteExists( outputFile ) )
095      return true;
096
097    File file = new File( outputFile );
098
099    if( !recursive || !file.isDirectory() )
100      return file.delete();
101
102    try
103      {
104      FileUtils.deleteDirectory( file );
105      }
106    catch( IOException exception )
107      {
108      return false;
109      }
110
111    return !file.exists();
112    }
113
114  @Override
115  public FlowProcess getFlowProcess()
116    {
117    return new LocalFlowProcess( FlowSession.NULL, PropertyUtil.createProperties( getProperties(), null ) );
118    }
119
120  @Override
121  public FlowConnector getFlowConnector( Map<Object, Object> properties )
122    {
123    return new LocalFlowConnector( properties );
124    }
125
126  @Override
127  public Tap getTap( Scheme scheme, String filename, SinkMode mode )
128    {
129    return new FileTap( scheme, filename, mode );
130    }
131
132  @Override
133  public Tap getTextFile( Fields sourceFields, Fields sinkFields, String filename, SinkMode mode )
134    {
135    if( sourceFields == null )
136      return new FileTap( new TextLine(), filename, mode );
137
138    return new FileTap( new TextLine( sourceFields, sinkFields ), filename, mode );
139    }
140
141  @Override
142  public Tap getDelimitedFile( Fields fields, boolean hasHeader, String delimiter, String quote, Class[] types, String filename, SinkMode mode )
143    {
144    return new FileTap( new TextDelimited( fields, hasHeader, delimiter, quote, types ), filename, mode );
145    }
146
147  @Override
148  public Tap getDelimitedFile( Fields fields, boolean skipHeader, boolean writeHeader, String delimiter, String quote, Class[] types, String filename, SinkMode mode )
149    {
150    return new FileTap( new TextDelimited( fields, skipHeader, writeHeader, delimiter, quote, types ), filename, mode );
151    }
152
153  @Override
154  public Tap getDelimitedFile( String delimiter, String quote, FieldTypeResolver fieldTypeResolver, String filename, SinkMode mode )
155    {
156    return new FileTap( new TextDelimited( true, new DelimitedParser( delimiter, quote, fieldTypeResolver ) ), filename, mode );
157    }
158
159  @Override
160  public Tap getPartitionTap( Tap sink, Partition partition, int openThreshold )
161    {
162    return new PartitionTap( (FileTap) sink, partition, openThreshold );
163    }
164
165  @Override
166  public Scheme getTestConfigDefScheme()
167    {
168    return new LocalConfigDefScheme( new Fields( "line" ) );
169    }
170
171  @Override
172  public Scheme getTestFailScheme()
173    {
174    return new LocalFailScheme( new Fields( "line" ) );
175    }
176
177  @Override
178  public Comparator getLongComparator( boolean reverseSort )
179    {
180    return new TestLongComparator( reverseSort );
181    }
182
183  @Override
184  public Comparator getStringComparator( boolean reverseSort )
185    {
186    return new TestStringComparator( reverseSort );
187    }
188
189  @Override
190  public String getHiddenTemporaryPath()
191    {
192    return null;
193    }
194  }