BUG 2839: Config: remove dependencies on commons-io 55/21855/4
authorTomas Cere <tcere@cisco.com>
Tue, 2 Jun 2015 14:36:57 +0000 (16:36 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 3 Jul 2015 08:38:47 +0000 (08:38 +0000)
Migrated commons-io users to guava and
custom solutions when there were no alternatives in guava.

Change-Id: I399030df1fbd7ea6e40b2591346802068e32f681
Signed-off-by: Tomas Cere <tcere@cisco.com>
14 files changed:
opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java
opendaylight/config/config-persister-directory-xml-adapter/pom.xml
opendaylight/config/logback-config/pom.xml
opendaylight/config/logback-config/src/test/java/org/opendaylight/controller/config/yang/logback/config/LogbackModuleWithInitialConfigurationTest.java
opendaylight/config/logback-config/src/test/java/org/opendaylight/controller/config/yang/logback/config/LogbackWithXmlConfigModuleTest.java
opendaylight/config/yang-jmx-generator-plugin/pom.xml
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGenerator.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/java/GeneratedObject.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/AbstractGeneratorTest.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/module/AbstractGeneratedObjectTest.java
opendaylight/config/yang-test-plugin/pom.xml
opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java
opendaylight/md-sal/sal-binding-it/pom.xml

index 30da27f1a1dee6951d05fe59b96b81e319d936c6..ab44bb34958da1d22fed964b8ed701a2a7427c24 100644 (file)
@@ -14,6 +14,8 @@ import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 
 import com.google.common.base.Preconditions;
+import java.io.File;
+import java.io.IOException;
 import java.lang.management.ManagementFactory;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
@@ -288,4 +290,27 @@ public abstract class AbstractConfigTest extends
         return (T) proxy;
     }
 
+    /**
+     * removes contents of the directory
+     * @param dir to be cleaned
+     * @throws IOException
+     */
+    protected void cleanDirectory(File dir) throws IOException {
+        if (!dir.isDirectory()) {
+            throw new IllegalStateException("dir must be a directory");
+        }
+
+        final File[] files = dir.listFiles();
+        if (files == null) {
+            throw new IOException("Failed to list contents of " + dir);
+        }
+
+        for (File file : files) {
+            if (file.isDirectory()) {
+                cleanDirectory(dir);
+            }
+            file.delete();
+        }
+    }
+
 }
index accf9600b4c384fbaf722939f4b526b40a09934f..9a49ffbed49907af03ff1af14e55ae808939da0d 100644 (file)
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
     </dependency>
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
@@ -77,7 +73,6 @@
             <Provide-Capability>org.opendaylight.controller.config.persister.storage.adapter</Provide-Capability>
             <Import-Package>com.google.common.base,
                             com.google.common.io,
-                            org.apache.commons.io,
                             org.opendaylight.controller.config.persist.api,
                             org.slf4j,
                             com.google.common.collect,
index 82e1fa8ea33ff7f02c2b06957279f6b58cf04334..4bc76211c68b8b3fdfbbafb064241251afac636f 100644 (file)
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
     </dependency>
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
index 999d8346e6c4dc27d675c31d43b3d9ce3b8ba4ef..f467bc504dc1f97bfdae3a2e104201dd39684b87 100644 (file)
@@ -28,7 +28,6 @@ import javax.management.InstanceNotFoundException;
 import javax.management.JMX;
 import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
-import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.config.api.ConflictingVersionException;
@@ -151,8 +150,9 @@ public class LogbackModuleWithInitialConfigurationTest extends AbstractConfigTes
         configurator.setContext(lc);
         configurator.doConfigure("src/test/resources/simple_config_logback.xml");
         File f = new File("target/it");
-        if (f.exists())
-            FileUtils.cleanDirectory(f);
+        if (f.exists()) {
+            cleanDirectory(f);
+        }
         ch.qos.logback.classic.Logger logger = lc.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
         ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent> fileAppender = (ch.qos.logback.core.rolling.RollingFileAppender<ILoggingEvent>) logger
                 .getAppender("VARLOGFILE");
index 9fcd38976ea4f1a166cbd5dfc43ea6a31a8a81ac..1c784a938d7293ac413aeb15789eceddc345e09e 100644 (file)
@@ -22,7 +22,6 @@ import javax.management.InstanceNotFoundException;
 import javax.management.JMX;
 import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
-import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.config.api.ConflictingVersionException;
@@ -50,7 +49,7 @@ public class LogbackWithXmlConfigModuleTest extends AbstractConfigTest {
         configurator.doConfigure("src/test/resources/simple_config_logback.xml");
         File f = new File("target/it");
         if (f.exists())
-            FileUtils.cleanDirectory(f);
+            cleanDirectory(f);
     }
 
     /**
index 0b08ba3f5d19f88594ff3764f34b85dd7da4643e..7e1a7c52fa580e566b9423608a4539aa403a6f66 100644 (file)
       <artifactId>guava</artifactId>
     </dependency>
 
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-    </dependency>
-
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
index 1f1776f0a5f86ffa1f229de339a8463ebcb2a9b0..4ad8fd79c46fa8ad76088a2ef6ec3ea2289d5e5d 100644 (file)
@@ -12,8 +12,10 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import com.google.common.io.Files;
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -22,7 +24,6 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.apache.commons.io.FileUtils;
 import org.apache.maven.project.MavenProject;
 import org.opendaylight.controller.config.spi.ModuleFactory;
 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
@@ -173,8 +174,7 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware {
             serviceLoaderFile.getParentFile().mkdirs();
             try {
                 serviceLoaderFile.createNewFile();
-                FileUtils.write(serviceLoaderFile,
-                        fullyQualifiedNamesOfFactories.toString());
+                Files.write(fullyQualifiedNamesOfFactories.toString(), serviceLoaderFile, StandardCharsets.UTF_8);
             } catch (IOException e) {
                 String message = "Cannot write to " + serviceLoaderFile;
                 LOG.error(message);
index 4d8bd3e5b46935e1efaa89ac7a2d6f1df0dfb7dd..5eb58d5a1e5e910c08ef96f97c06b1218b027a64 100644 (file)
@@ -4,10 +4,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.Maps;
+import com.google.common.io.Files;
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.Map.Entry;
-import org.apache.commons.io.FileUtils;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
 
 public class GeneratedObject {
@@ -30,8 +31,10 @@ public class GeneratedObject {
 
     public Optional<Entry<FullyQualifiedName,File>> persist(File srcDirectory, boolean overwrite) throws IOException {
         File dstFile = fqn.toFile(srcDirectory);
-        if (overwrite == true || dstFile.exists() == false) {
-            FileUtils.write(dstFile, content);
+        if (overwrite || !dstFile.exists()) {
+            Files.createParentDirs(dstFile);
+            Files.touch(dstFile);
+            Files.write(content, dstFile, StandardCharsets.UTF_8);
             return Optional.of(Maps.immutableEntry(fqn, dstFile));
         } else {
             return Optional.absent();
@@ -60,11 +63,8 @@ public class GeneratedObject {
 
         GeneratedObject that = (GeneratedObject) o;
 
-        if (!fqn.equals(that.fqn)) {
-            return false;
-        }
+        return fqn.equals(that.fqn);
 
-        return true;
     }
 
     @Override
index 2eae8b6b0954fbc44a2ef54b81f247895e419394..fb5cd2e9899cdbe96540ff322ed3f5c4c5d50f1b 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
 
 import java.io.File;
-import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.opendaylight.controller.config.yangjmxgenerator.AbstractYangTest;
 
@@ -24,7 +23,21 @@ public abstract class AbstractGeneratorTest extends AbstractYangTest {
 
     @Before
     public void cleanUpDirectory() throws Exception {
-        FileUtils.deleteDirectory(generatorOutputPath);
+        deleteFolder(generatorOutputPath);
+    }
+
+    public void deleteFolder(File folder) {
+        File[] files = folder.listFiles();
+        if (files != null) {
+            for (File f: files) {
+                if (f.isDirectory()) {
+                    deleteFolder(f);
+                } else {
+                    f.delete();
+                }
+            }
+        }
+        folder.delete();
     }
 
 }
index 56cd615a39c5d5b77e63e0418acdcd35624dacb5..c7a12a391738d901c174c8ac6124bf4880cdea4a 100644 (file)
@@ -18,6 +18,7 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
@@ -27,6 +28,7 @@ import com.google.common.io.Files;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -38,7 +40,6 @@ import java.util.Map;
 import java.util.Set;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import org.apache.commons.io.FileUtils;
 import org.apache.maven.project.MavenProject;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.compiler.IProblem;
@@ -324,8 +325,8 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
                 generatedResourcesDir, "META-INF", "services",
                 ModuleFactory.class.getName());
         assertThat(moduleFactoryFile.exists(), is(true));
-        Set<String> lines = Sets.newHashSet(FileUtils
-                .readLines(moduleFactoryFile));
+        Set<String> lines = Sets.newHashSet(Files
+                .readLines(moduleFactoryFile, StandardCharsets.UTF_8));
         Set<String> expectedLines = Sets.newHashSet(//
                 PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
                         + ".threads.java.EventBusModuleFactory",//
index c3ca077bcc940100c7064497d0ae12ece992488f..34b38bfa50572db2b36d82eada4193c22a4192b7 100644 (file)
@@ -11,14 +11,15 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin.module;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import com.google.common.io.Files;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import net.sourceforge.pmd.lang.Parser;
 import net.sourceforge.pmd.lang.ParserOptions;
 import net.sourceforge.pmd.lang.ast.Node;
 import net.sourceforge.pmd.lang.java.Java17Parser;
-import org.apache.commons.io.FileUtils;
 import org.opendaylight.controller.config.yangjmxgenerator.plugin.AbstractGeneratorTest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -34,7 +35,7 @@ public class AbstractGeneratedObjectTest extends AbstractGeneratorTest {
 
     protected Node parse(File dstFile) throws IOException {
         assertNotNull(dstFile);
-        LOG.debug(FileUtils.readFileToString(dstFile));
+        LOG.debug(Files.toString(dstFile, StandardCharsets.UTF_8));
         Parser parser = new Java17Parser(new ParserOptions());
         return parser.parse(dstFile.toString(), new FileReader(dstFile));
     }
index b437e77b9d7a115527ad06b5cc740f6f3716b61f..451e4dce57df144af97be8ee8880ac7be7af567a 100644 (file)
   <description>Remove generated source files, after new files generation, implementation is inserted.</description>
 
   <dependencies>
-    <dependency>
-      <groupId>commons-io</groupId>
-      <artifactId>commons-io</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-plugin-api</artifactId>
index 4e392304a065ea776b3abe3c743ad50d33e04c00..054edffe68a77aa3d4e87ec15a986f79cadaa78c 100644 (file)
@@ -7,10 +7,11 @@
  */
 package org.opendaylight.controller.config.yang.test.plugin;
 
+import com.google.common.io.Files;
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.regex.Pattern;
-import org.apache.commons.io.FileUtils;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -46,7 +47,7 @@ public class ProcessSources extends AbstractMojo{
             if (sourceFile.getName().endsWith(".java")) {
                 String sourceContent;
                 try {
-                    sourceContent = FileUtils.readFileToString(sourceFile);
+                    sourceContent = Files.toString(sourceFile, StandardCharsets.UTF_8);
                 } catch (IOException e) {
                     getLog().error(String.format("Cannot read %s", sourceFile.getAbsolutePath()), e);
                     continue;
@@ -56,7 +57,7 @@ public class ProcessSources extends AbstractMojo{
                     if (stubFile.exists()) {
                         String stubContent = null;
                         try {
-                            stubContent = FileUtils.readFileToString(stubFile);
+                            stubContent = Files.toString(stubFile, StandardCharsets.UTF_8);
                         } catch (IOException e) {
                             getLog().error(String.format("Cannot read %s", stubFile.getAbsolutePath()), e);
                         }
@@ -70,7 +71,7 @@ public class ProcessSources extends AbstractMojo{
 
                 // replace the file content
                 try {
-                    FileUtils.write(sourceFile, sourceContent);
+                    Files.write(sourceContent, sourceFile, StandardCharsets.UTF_8);
                 } catch (IOException e) {
                     getLog().error(String.format("Cannot write %s", sourceFile.getAbsolutePath()), e);
                 }
index 622ce0f9a35a003de4811bbbb3d3103b07740140..37bf67d1935a5841c82a4a350232cc716bc578eb 100644 (file)
       <artifactId>pax-exam-link-mvn</artifactId>
       <scope>compile</scope>
     </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>log4j-over-slf4j</artifactId>