001    /*
002     * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.platform.local;
022    
023    import java.io.File;
024    import java.io.IOException;
025    import java.util.Comparator;
026    import java.util.Map;
027    import java.util.Properties;
028    
029    import cascading.flow.FlowConnector;
030    import cascading.flow.FlowProcess;
031    import cascading.flow.FlowSession;
032    import cascading.flow.local.LocalFlowConnector;
033    import cascading.flow.local.LocalFlowProcess;
034    import cascading.platform.TestPlatform;
035    import cascading.scheme.Scheme;
036    import cascading.scheme.local.TextDelimited;
037    import cascading.scheme.local.TextLine;
038    import cascading.scheme.util.DelimitedParser;
039    import cascading.scheme.util.FieldTypeResolver;
040    import cascading.tap.SinkMode;
041    import cascading.tap.Tap;
042    import cascading.tap.local.FileTap;
043    import cascading.tap.local.PartitionTap;
044    import cascading.tap.local.TemplateTap;
045    import cascading.tap.partition.Partition;
046    import cascading.tuple.Fields;
047    import org.apache.commons.io.FileUtils;
048    
049    /**
050     * Class LocalPlatform is automatically loaded and injected into a {@link cascading.PlatformTestCase} instance
051     * so that all *PlatformTest classes can be tested against the Cascading local mode planner.
052     */
053    public class LocalPlatform extends TestPlatform
054      {
055      private Properties properties = new Properties();
056    
057      {
058      properties.putAll( getGlobalProperties() );
059      }
060    
061      @Override
062      public void setUp() throws IOException
063        {
064        }
065    
066      @Override
067      public Map<Object, Object> getProperties()
068        {
069        return new Properties( 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, (Properties) getProperties() );
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 FileTap( new TextLine(), filename, mode );
139    
140        return new FileTap( 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 FileTap( 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 FileTap( 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 FileTap( new TextDelimited( true, new DelimitedParser( delimiter, quote, fieldTypeResolver ) ), filename, mode );
159        }
160    
161      @Override
162      public Tap getTemplateTap( Tap sink, String pathTemplate, int openThreshold )
163        {
164        return new TemplateTap( (FileTap) sink, pathTemplate, openThreshold );
165        }
166    
167      @Override
168      public Tap getTemplateTap( Tap sink, String pathTemplate, Fields fields, int openThreshold )
169        {
170        return new TemplateTap( (FileTap) sink, pathTemplate, fields, openThreshold );
171        }
172    
173      @Override
174      public Tap getPartitionTap( Tap sink, Partition partition, int openThreshold )
175        {
176        return new PartitionTap( (FileTap) sink, partition, openThreshold );
177        }
178    
179      @Override
180      public Scheme getTestConfigDefScheme()
181        {
182        return new LocalConfigDefScheme( new Fields( "line" ) );
183        }
184    
185      @Override
186      public Scheme getTestFailScheme()
187        {
188        return new LocalFailScheme( new Fields( "line" ) );
189        }
190    
191      @Override
192      public Comparator getLongComparator( boolean reverseSort )
193        {
194        return new TestLongComparator( reverseSort );
195        }
196    
197      @Override
198      public Comparator getStringComparator( boolean reverseSort )
199        {
200        return new TestStringComparator( reverseSort );
201        }
202    
203      @Override
204      public String getHiddenTemporaryPath()
205        {
206        return null;
207        }
208      }