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