Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.uberfire.io;
 
 import java.io.File;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import org.junit.Test;
 import  org.uberfire.java.nio.base.AbstractBasicFileAttributeView;
 import  org.uberfire.java.nio.base.AbstractPath;
 import  org.uberfire.java.nio.base.AttrHolder;
 import  org.uberfire.java.nio.base.NeedsPreloadedAttrs;
 import  org.uberfire.java.nio.channels.SeekableByteChannel;
 import  org.uberfire.java.nio.file.FileAlreadyExistsException;
 import  org.uberfire.java.nio.file.OpenOption;
 import  org.uberfire.java.nio.file.Path;
 import  org.uberfire.java.nio.file.attribute.BasicFileAttributeView;
 import  org.uberfire.java.nio.file.attribute.BasicFileAttributes;
 import  org.uberfire.java.nio.file.attribute.FileAttribute;
 import  org.uberfire.java.nio.file.attribute.FileTime;
 
 import static org.junit.Assert.*;
 import static org.uberfire.java.nio.base.dotfiles.DotFileUtils.*;

 
 public abstract class CommonIOServiceDotFileTest {
 
     protected final Date dateValue = new Date();
 
     protected static final List<FiletempFiles = new ArrayList<File>();
 
     @Test
     public void testFile() throws IOException {
         final Path path = getFilePath();
         ioService().writepath"ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
                                @Override
                                public String name() {
                                    return "custom";
                                }
 
                                @Override
                                public Object value() {
                                    return ;
                                }
                            }, new FileAttribute<String>() {
                                @Override
                                public String name() {
                                    return "int.hello";
                                }
 
                                @Override
                                public String value() {
                                    return "world";
                                }
                            }, new FileAttribute<Integer>() {
                                @Override
                                public String name() {
                                    return "int";
                                }
 
                                @Override
                                public Integer value() {
                                    return 10;
                                }
                            }
                          );
 
         Map<StringObjectattrs = ioService().readAttributespath );
 
         assertEqualstestFileAttrSize1(), attrs.size() );
         assertTrueattrs.containsKey"int.hello" ) );
         assertTrueattrs.containsKey"custom" ) );
         assertTrueattrs.containsKey"int" ) );
        assertEquals( 10, attrs.get"int" ) );
        assertEqualsattrs.get"custom" ) );
        assertEquals"world"attrs.get"int.hello" ) );
        if ( path instanceof AttrHolder ) {
            ( (AttrHolder) path ).getAttrStorage().clear();
        }
        attrs = ioService().readAttributespath );
        assertEquals( 10, attrs.get"int" ) );
        assertEqualsattrs.get"custom" ) );
        assertEquals"world"attrs.get"int.hello" ) );
        final Map<StringObjectattrsValue = ioService().readAttributespath );
        assertEqualstestFileAttrSize2(), attrsValue.size() );
        ioService().setAttributespathnew FileAttribute<Object>() {
            @Override
            public String name() {
                return "my_new_key";
            }
            @Override
            public Object value() {
                return null;
            }
        } );
        final Map<StringObjectattrsValue2 = ioService().readAttributespath );
        assertEqualstestFileAttrSize3(), attrsValue2.size() );
        assertFalseattrsValue2.containsKey"my_new_key" ) );
        ioService().deletepath );
        ioService().writepath"ooooo!" );
        final Map<StringObjectattrsClean = ioService().readAttributespath );
        assertEqualstestFileAttrSize4(), attrsClean.size() );
    }
    protected abstract int testFileAttrSize4();
    protected abstract int testFileAttrSize3();
    protected abstract int testFileAttrSize2();
    protected abstract int testFileAttrSize1();
    @Test
    public void testDirectory() throws IOException {
        final Path path = getDirectoryPath();
        ioService().createDirectorypathnew FileAttribute<Object>() {
                                         @Override
                                         public String name() {
                                             return "custom";
                                         }
                                         @Override
                                         public Object value() {
                                             return ;
                                         }
                                     }, new FileAttribute<String>() {
                                         @Override
                                         public String name() {
                                             return "int.hello";
                                         }
                                         @Override
                                         public String value() {
                                             return "world";
                                         }
                                     }, new FileAttribute<Integer>() {
                                         @Override
                                         public String name() {
                                             return "int";
                                         }
                                         @Override
                                         public Integer value() {
                                             return 10;
                                         }
                                     }
                                   );
        Map<StringObjectattrs = ioService().readAttributespath );
        assertEqualstestDirectoryAttrSize1(), attrs.size() );
        assertTrueattrs.containsKey"int.hello" ) );
        assertTrueattrs.containsKey"custom" ) );
        assertTrueattrs.containsKey"int" ) );
        assertEquals( 10, attrs.get"int" ) );
        assertEqualsattrs.get"custom" ) );
        assertEquals"world"attrs.get"int.hello" ) );
        if ( path instanceof AttrHolder ) {
            ( (AttrHolder) path ).getAttrStorage().clear();
        }
        attrs = ioService().readAttributespath );
        assertEquals( 10, attrs.get"int" ) );
        assertEqualsattrs.get"custom" ) );
        assertEquals"world"attrs.get"int.hello" ) );
        ioService().deletepath );
        ioService().createDirectorypath );
        final Map<StringObjectattrsClean = ioService().readAttributespath );
        assertEqualstestDirectoryAttrSize4(), attrsClean.size() );
    }
    protected abstract int testDirectoryAttrSize4();
    protected abstract int testDirectoryAttrSize3();
    protected abstract int testDirectoryAttrSize2();
    protected abstract int testDirectoryAttrSize1();
    @Test
    public void testDelete() throws IOException {
        final Path dir = getDirectoryPath();
        ioService().createDirectorydirnew FileAttribute<Object>() {
                                         @Override
                                         public String name() {
                                             return "custom";
                                         }
                                         @Override
                                         public Object value() {
                                             return ;
                                         }
                                     }, new FileAttribute<String>() {
                                         @Override
                                         public String name() {
                                             return "int.hello";
                                         }
                                         @Override
                                         public String value() {
                                             return "world";
                                         }
                                     }, new FileAttribute<Integer>() {
                                         @Override
                                         public String name() {
                                             return "int";
                                         }
                                         @Override
                                         public Integer value() {
                                             return 10;
                                         }
                                     }
                                   );
        assertTrueioService().existsdir ) );
        assertTrueioService().exists( dot( dir ) ) );
        ioService().deletedir );
        assertFalseioService().existsdir ) );
        assertFalseioService().exists( dot( dir ) ) );
        final Path file = getFilePath();
        ioService().writefile"ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "custom";
            }
            @Override
            public Object value() {
                return ;
            }
        } );
        assertTrueioService().existsfile ) );
        assertTrueioService().exists( dot( file ) ) );
        assertFalse( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
        ioService().deletefile );
        assertTrue( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
        assertFalseioService().existsfile ) );
        assertFalseioService().exists( dot( file ) ) );
    }
    @Test
    public void testCopyFile() {
        final Path sfile = getFilePath();
        final Path tfile = getTargetPath();
        ioService().deleteIfExistssfile );
        ioService().deleteIfExiststfile );
        ioService().writesfile"wow", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "custom";
            }
            @Override
            public Object value() {
                return ;
            }
        } );
        assertTrueioService().existssfile ) );
        assertTrueioService().exists( dot( sfile ) ) );
        assertFalseioService().existstfile ) );
        assertFalseioService().exists( dot( tfile ) ) );
        ioService().copysfiletfile );
        assertTrueioService().existssfile ) );
        assertTrueioService().exists( dot( sfile ) ) );
        assertTrueioService().existstfile ) );
        assertTrueioService().exists( dot( tfile ) ) );
    }
    @Test
    public void createDirectories() {
        final Path dir = getComposedDirectoryPath();
        assertFalseioService().existsdir ) );
        ioService().createDirectoriesdirnew FileAttribute<Object>() {
            @Override
            public String name() {
                return "custom";
            }
            @Override
            public Object value() {
                return ;
            }
        } );
        assertTrueioService().existsdir ) );
        assertTrueioService().existsdir.getParent() ) );
        assertNotNullioService().existsdir.getParent().getFileName() ) );
        Map<StringObjectattrs = ioService().readAttributesdir );
        assertEqualscreateDirectoriesAttrSize(), attrs.size() );
        ioService().deletedir );
        ioService().existsdir.getParent() );
    }
    protected abstract int createDirectoriesAttrSize();
    @Test
    public void testDeleteIfExistis() throws IOException {
        final Path dir = getDirectoryPath();
        ioService().deleteIfExistsdir );
        ioService().createDirectorydirnew FileAttribute<Object>() {
                                         @Override
                                         public String name() {
                                             return "custom";
                                         }
                                         @Override
                                         public Object value() {
                                             return ;
                                         }
                                     }, new FileAttribute<String>() {
                                         @Override
                                         public String name() {
                                             return "int.hello";
                                         }
                                         @Override
                                         public String value() {
                                             return "world";
                                         }
                                     }, new FileAttribute<Integer>() {
                                         @Override
                                         public String name() {
                                             return "int";
                                         }
                                         @Override
                                         public Integer value() {
                                             return 10;
                                         }
                                     }
                                   );
        assertTrueioService().deleteIfExistsdir ) );
        assertFalseioService().deleteIfExistsdir ) );
        final Path file = getFilePath();
        ioService().deleteIfExistsfile );
        ioService().writefile"ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "custom";
            }
            @Override
            public Object value() {
                return ;
            }
        } );
        assertFalse( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
        assertTrueioService().deleteIfExistsfile ) );
        assertTrue( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
        assertFalseioService().deleteIfExistsfile ) );
    }
    @Test
    public void testNewByteChannel() throws IOException {
        final Path file = getFilePath();
        ioService().deleteIfExistsfile );
        assertFalseioService().existsfile ) );
        final SeekableByteChannel sbc = ioService().newByteChannelfile, Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "custom";
            }
            @Override
            public Object value() {
                return ;
            }
        } );
        sbc.write( ByteBuffer.wrap"helloWorld!".getBytes() ) );
        sbc.close();
        assertTrueioService().existsfile ) );
        Map<StringObjectattrs = ioService().readAttributesfile );
        assertEqualstestNewByteChannelAttrSize(), attrs.size() );
        try {
            ioService().newByteChannelfile, Collections.<OpenOption>emptySet() );
            fail"FileAlreadyExistsException expected" );
        } catch ( FileAlreadyExistsException ex ) {
        }
        ioService().deletefile );
        ioService().newByteChannelfile, Collections.<OpenOption>emptySet() ).close();
        assertTrueioService().deleteIfExistsfile ) );
    }
    protected abstract int testNewByteChannelAttrSize();
    @Test
    public void testGetAttribute() {
        final Path file = getFilePath();
        ioService().deleteIfExistsfile );
        ioService().writefile"ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "dcore.author";
            }
            @Override
            public Object value() {
                return "AuthorName";
            }
        } );
        assertNotNullioService().getAttributefile"dcore:dcore.author" ) );
        assertNullioService().getAttributefile"dcore:dcore.not_here" ) );
        assertNotNullioService().getAttributefile"dcore.author" ) );
        assertNullioService().getAttributefile"something" ) );
        ( (AttrHolder) file ).getAttrStorage().clear();
        assertNotNullioService().getAttributefile"dcore:dcore.author" ) );
        assertNullioService().getAttributefile"dcore:dcore.not_here" ) );
        assertNotNullioService().getAttributefile"dcore.author" ) );
        assertNullioService().getAttributefile"something" ) );
    }
    @Test
    public void testGetAttributeView() {
        final Path file = getFilePath();
        ioService().deleteIfExistsfile );
        ioService().writefile"ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() {
            @Override
            public String name() {
                return "dcore.author";
            }
            @Override
            public Object value() {
                return "AuthorName";
            }
        } );
        assertNotNullioService().getFileAttributeViewfile, BasicFileAttributeView.class ) );
        assertNullioService().getFileAttributeViewfileMyAttrsView.class ) );
        assertNotNullioService().getFileAttributeViewfileXDublinCoreView.class ) );
        final DublinCoreAttributes attr = ioService().getFileAttributeViewfileXDublinCoreView.class ).readAttributes();
        assertEquals"AuthorName"attr.getAuthor() );
        ( (AttrHolder) file ).getAttrStorage().clear();
        assertNotNullioService().getFileAttributeViewfile, BasicFileAttributeView.class ) );
        assertNullioService().getFileAttributeViewfileMyAttrsView.class ) );
        assertNotNullioService().getFileAttributeViewfileXDublinCoreView.class ) );
    }
    public abstract Path getFilePath();
    public abstract Path getTargetPath();
    public abstract Path getDirectoryPath();
    public abstract Path getComposedDirectoryPath();
    public static File createTempDirectory()
            throws IOException {
        final File temp = File.createTempFile"temp", Long.toString( System.nanoTime() ) );
        if ( !( temp.delete() ) ) {
            throw new IOException"Could not delete temp file: " + temp.getAbsolutePath() );
        }
        if ( !( temp.mkdir() ) ) {
            throw new IOException"Could not create temp directory: " + temp.getAbsolutePath() );
        }
        .addtemp );
        return temp;
    }
    private static interface MyAttrsView extends BasicFileAttributeView {
    }
    public static class XDublinCoreView extends AbstractBasicFileAttributeView<AbstractPath>
            implements NeedsPreloadedAttrs {
        private BasicFileAttributes attrs = null;
        public XDublinCoreViewfinal AbstractPath path ) {
            superpath );
        }
        @Override
        public <T extends BasicFileAttributes> T readAttributes() throws org.uberfire.java.nio.IOException {
            if (  == null ) {
                final BasicFileAttributes basicAtts = ( (BasicFileAttributeView) path.getAttrView( BasicFileAttributeView.class ) ).readAttributes();
                 = new DublinCoreAttributesbasicAtts, (String) path.getAttrStorage().getContent().get( "dcore.author" ) );
            }
            return (T) ;
        }
        @Override
        public Class<? extends BasicFileAttributeView>[] viewTypes() {
            return new Class[]{ XDublinCoreView.class };
        }
    }
    public static class DublinCoreAttributes implements BasicFileAttributes {
        private final BasicFileAttributes attributes;
        private final String author;
        private DublinCoreAttributesfinal BasicFileAttributes attributes,
                                      final String author ) {
            this. = attributes;
            this. = author;
        }
        public String getAuthor() {
            return ;
        }
        @Override
        public FileTime lastModifiedTime() {
            return .lastModifiedTime();
        }
        @Override
        public FileTime lastAccessTime() {
            return .lastAccessTime();
        }
        @Override
        public FileTime creationTime() {
            return .creationTime();
        }
        @Override
        public boolean isRegularFile() {
            return .isRegularFile();
        }
        @Override
        public boolean isDirectory() {
            return .isDirectory();
        }
        @Override
        public boolean isSymbolicLink() {
            return .isSymbolicLink();
        }
        @Override
        public boolean isOther() {
            return .isOther();
        }
        @Override
        public long size() {
            return .size();
        }
        @Override
        public Object fileKey() {
            return .fileKey();
        }
    }
    @AfterClass
    public static void cleanup() {
        for ( final File tempFile :  ) {
            FileUtils.deleteQuietlytempFile );
        }
    }
    protected static IOService ioService = null;
    public IOService ioService() {
        if (  == null ) {
             = new IOServiceDotFileImpl();
        }
        return ;
    }
New to GrepCode? Check out our FAQ X