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 30da27f..ab44bb3 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 accf960..9a49ffb 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 82e1fa8..4bc7621 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 999d834..f467bc5 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 9fcd389..1c784a9 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 0b08ba3..7e1a7c5 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 1f1776f..4ad8fd7 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 4d8bd3e..5eb58d5 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 2eae8b6..fb5cd2e 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 56cd615..c7a12a3 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 c3ca077..34b38bf 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 b437e77..451e4dc 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 4e39230..054edff 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 622ce0f..37bf67d 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>

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.