Author: krosenvold
Date: Sat Jun 20 05:43:20 2015
New Revision: 1686524
URL: http://svn.apache.org/r1686524
Log:
Fixed style comment from mailing list. Added small testcase for Java7Support
Converted to commons-style indentation
Added:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/Java7SupportTest.java
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Java7Support.java
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Java7Support.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/Java7Support.java?rev=1686524&r1=1686523&r2=1686524&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Java7Support.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Java7Support.java
Sat Jun 20 05:43:20 2015
@@ -27,11 +27,10 @@ import java.lang.reflect.Method;
/**
* Java7 feature detection and reflection based feature access.
- *
+ * <p/>
* Taken from maven-shared-utils, only for private usage until we go full java7
*/
-class Java7Support
-{
+class Java7Support {
private static final boolean IS_JAVA7;
@@ -53,146 +52,109 @@ class Java7Support
private static Object emptyFileAttributes;
- static
- {
+ static {
boolean isJava7x = true;
- try
- {
+ try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
- Class<?> files = cl.loadClass( "java.nio.file.Files" );
- Class<?> path = cl.loadClass( "java.nio.file.Path" );
- Class<?> fa = cl.loadClass(
"java.nio.file.attribute.FileAttribute" );
- Class<?> linkOption = cl.loadClass( "java.nio.file.LinkOption" );
- isSymbolicLink = files.getMethod( "isSymbolicLink", path );
- delete = files.getMethod( "delete", path );
- readSymlink = files.getMethod( "readSymbolicLink", path );
-
- emptyFileAttributes = Array.newInstance( fa, 0 );
- final Object o = emptyFileAttributes;
- createSymlink = files.getMethod( "createSymbolicLink", path, path,
o.getClass() );
- emptyLinkOpts = Array.newInstance( linkOption, 0 );
- exists = files.getMethod( "exists", path, emptyLinkOpts.getClass()
);
- toPath = File.class.getMethod( "toPath" );
- toFile = path.getMethod( "toFile" );
- }
- catch ( ClassNotFoundException e )
- {
+ Class<?> files = cl.loadClass("java.nio.file.Files");
+ Class<?> path = cl.loadClass("java.nio.file.Path");
+ Class<?> fa =
cl.loadClass("java.nio.file.attribute.FileAttribute");
+ Class<?> linkOption = cl.loadClass("java.nio.file.LinkOption");
+ isSymbolicLink = files.getMethod("isSymbolicLink", path);
+ delete = files.getMethod("delete", path);
+ readSymlink = files.getMethod("readSymbolicLink", path);
+
+ emptyFileAttributes = Array.newInstance(fa, 0);
+ createSymlink = files.getMethod("createSymbolicLink", path, path,
emptyFileAttributes.getClass());
+ emptyLinkOpts = Array.newInstance(linkOption, 0);
+ exists = files.getMethod("exists", path, emptyLinkOpts.getClass());
+ toPath = File.class.getMethod("toPath");
+ toFile = path.getMethod("toFile");
+ } catch (ClassNotFoundException e) {
isJava7x = false;
- }
- catch ( NoSuchMethodException e )
- {
+ } catch (NoSuchMethodException e) {
isJava7x = false;
}
IS_JAVA7 = isJava7x;
}
- public static boolean isSymLink( File file )
- {
- try
- {
- Object path = toPath.invoke( file );
- return (Boolean) isSymbolicLink.invoke( null, path );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e );
+ public static boolean isSymLink(File file) {
+ try {
+ Object path = toPath.invoke(file);
+ return (Boolean) isSymbolicLink.invoke(null, path);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
}
}
- public static File readSymbolicLink( File symlink )
- throws IOException
- {
- try
- {
- Object path = toPath.invoke( symlink );
- Object resultPath = readSymlink.invoke( null, path );
- return (File) toFile.invoke( resultPath );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
- throw new RuntimeException( e );
+ public static File readSymbolicLink(File symlink)
+ throws IOException {
+ try {
+ Object path = toPath.invoke(symlink);
+ Object resultPath = readSymlink.invoke(null, path);
+ return (File) toFile.invoke(resultPath);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
}
}
- public static boolean exists( File file )
- throws IOException
- {
- try
- {
- Object path = toPath.invoke( file );
- final Object invoke = exists.invoke( null, path, emptyLinkOpts );
+ public static boolean exists(File file)
+ throws IOException {
+ try {
+ Object path = toPath.invoke(file);
+ final Object invoke = exists.invoke(null, path, emptyLinkOpts);
return (Boolean) invoke;
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
throw (RuntimeException) e.getTargetException();
}
}
- public static File createSymbolicLink( File symlink, File target )
- throws IOException
- {
- try
- {
- if ( !exists( symlink ) )
- {
- Object link = toPath.invoke( symlink );
- Object path = createSymlink.invoke( null, link, toPath.invoke(
target ), emptyFileAttributes );
- return (File) toFile.invoke( path );
+ public static File createSymbolicLink(File symlink, File target)
+ throws IOException {
+ try {
+ if (!exists(symlink)) {
+ Object link = toPath.invoke(symlink);
+ Object path = createSymlink.invoke(null, link,
toPath.invoke(target), emptyFileAttributes);
+ return (File) toFile.invoke(path);
}
return symlink;
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
final Throwable targetException = e.getTargetException();
throw (IOException) targetException;
}
}
+
/**
* Performs a nio delete
+ *
* @param file the file to delete
* @throws IOException
*/
- public static void delete( File file )
- throws IOException
- {
- try
- {
- Object path = toPath.invoke( file );
- delete.invoke( null, path );
- }
- catch ( IllegalAccessException e )
- {
- throw new RuntimeException( e );
- }
- catch ( InvocationTargetException e )
- {
+ public static void delete(File file)
+ throws IOException {
+ try {
+ Object path = toPath.invoke(file);
+ delete.invoke(null, path);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
throw (IOException) e.getTargetException();
}
}
- public static boolean isAtLeastJava7()
- {
+ public static boolean isAtLeastJava7() {
return IS_JAVA7;
}
Added:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/Java7SupportTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/Java7SupportTest.java?rev=1686524&view=auto
==============================================================================
---
commons/proper/io/trunk/src/test/java/org/apache/commons/io/Java7SupportTest.java
(added)
+++
commons/proper/io/trunk/src/test/java/org/apache/commons/io/Java7SupportTest.java
Sat Jun 20 05:43:20 2015
@@ -0,0 +1,53 @@
+package org.apache.commons.io;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.io.File;
+
+import org.junit.Test;
+
+public class Java7SupportTest {
+ @Test
+ public void testIsSymLink()
+ throws Exception {
+
+ File file = new File(".");
+ if (Java7Support.isAtLeastJava7()) {
+ assertFalse(Java7Support.isSymLink(file));
+ }
+ }
+
+ @Test
+ public void createAndReadSymlink()
+ throws Exception {
+
+ File file = new File("target/fzz");
+ if (Java7Support.isAtLeastJava7()) {
+ Java7Support.createSymbolicLink(file, new File("../target"));
+
+ final File file1 = Java7Support.readSymbolicLink(file);
+ assertEquals("target", file1.getName());
+ Java7Support.delete(file);
+ }
+ }
+
+}