svn commit: r17994 - trunk/src/argouml-app: src/org/argouml/profile/internal tests/org/argouml tests/org/argouml/profile tests/org/argouml/profile/internal

Luis Sergio Oliveira <[email protected]>
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: euluis
Date: 2010-02-11 15:48:44-0800
New Revision: 17994

Modified:
   trunk/src/argouml-app/src/org/argouml/profile/internal/DependencyResolver.java
   trunk/src/argouml-app/tests/org/argouml/FileHelper.java
   trunk/src/argouml-app/tests/org/argouml/profile/ProfileMother.java
   trunk/src/argouml-app/tests/org/argouml/profile/TestProfileMother.java
   trunk/src/argouml-app/tests/org/argouml/profile/internal/TestDependencyResolver.java
   trunk/src/argouml-app/tests/org/argouml/profile/internal/TestProfileManagerImpl.java

Log:
issue 4997: fixed an error in DependencyResolver and improved the tests.

Modified: trunk/src/argouml-app/src/org/argouml/profile/internal/DependencyResolver.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/profile/internal/DependencyResolver.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/profile/internal/DependencyResolver.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/profile/internal/DependencyResolver.java	2010-02-11 15:48:44-0800
@@ -13,6 +13,8 @@
 
 package org.argouml.profile.internal;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Collection;
 
@@ -31,7 +33,12 @@
             DependencyResolver.class);
 
     private DependencyChecker<T> checker;
-    private Collection<T> unresolvedItems;
+    /**
+     * WARNING: only to be used from outside classes by tests.
+     * This is the state-full part of the algorithm, storing the unresolved
+     * items between resolve methods calls.
+     */
+    Collection<T> unresolvedItems;
 
     /**
      * Create a dependency resolver and initialize it with the associated
@@ -53,7 +60,9 @@
         if (unresolvedItems.isEmpty()) {
             return;
         }
-        resolve(new HashSet<T>());
+        final Collection<T> items = Collections.unmodifiableCollection(
+            new ArrayList<T>());
+        resolve(items);
     }
 
     /**
@@ -72,6 +81,7 @@
         }
         Collection<T> resolved = internalResolve(allUnresolvedItems);
         allUnresolvedItems.removeAll(resolved);
+        unresolvedItems.clear();
         unresolvedItems.addAll(allUnresolvedItems);
         if (!unresolvedItems.isEmpty()) {
             LOG.warn(items2Msg(

Modified: trunk/src/argouml-app/tests/org/argouml/FileHelper.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/FileHelper.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/FileHelper.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/FileHelper.java	2010-02-11 15:48:44-0800
@@ -123,4 +123,24 @@
     public static File createTempDirectory() throws IOException {
         return createTempDirectory(DEFAULT_TEMP_DIR_PREFIX);
     }
+
+    /**
+     * @param fileToMove the original file that must be moved.
+     * @param filePrefix the prefix of the new file name.
+     * @param fileSuffix the suffix (or extension) of the file.
+     *        NOTE: should contain "." if it is intended to be an extension.
+     * @param directoryPrefix the prefix of the new directory name.
+     * @return the new {@link File}.
+     * @throws IOException
+     */
+    public static File moveFileToNewTempDirectory(File fileToMove,
+            String filePrefix, String fileSuffix, String directoryPrefix)
+            throws IOException {
+        File directory = createTempDirectory(directoryPrefix);
+        File newFile = File.createTempFile(filePrefix, fileSuffix, directory);
+        boolean renamed = fileToMove.renameTo(newFile);
+        assert renamed : "Renaming of " + fileToMove + " to "
+            + newFile + " failed.";
+        return newFile;
+    }
 }

Modified: trunk/src/argouml-app/tests/org/argouml/profile/ProfileMother.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/profile/ProfileMother.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/profile/ProfileMother.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/profile/ProfileMother.java	2010-02-11 15:48:44-0800
@@ -45,8 +45,12 @@
 import static org.argouml.model.Model.getFacade;
 import static org.argouml.model.Model.getModelManagementFactory;
 
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -205,7 +209,7 @@
      * @throws IOException if the new profile file creation fails.
      * @throws UmlException if the model subsystem throws.
      */
-    public File createXmiDependentProfile(File profileFromWhichDependsFile,
+    File createXmiDependentProfile(File profileFromWhichDependsFile,
             DependencyCreator dependencyCreator,
             File profilesDir, String dependentProfileFilenamePrefix)
         throws IOException, UmlException {
@@ -245,7 +249,7 @@
      * @throws IOException if file IO causes errors.
      * @throws UmlException if the manipulation of models causes errors.
      */
-    public List<File> createProfileFilePairWithSecondDependingOnFirstThroughXmi()
+    List<File> createProfileFilePairWith2ndDependingOn1stViaXmi()
             throws IOException, UmlException {
         File profilesDir = FileHelper.createTempDirectory();
         final File baseFile = File.createTempFile(
@@ -273,4 +277,80 @@
         return new ArrayList<File>() { { add(baseFile); add(dependentFile); }
         };
     }
+
+    /**
+     * Creates two profiles with one depending of the other. Saves the two
+     * profiles in different temporary directories and returns the associated
+     * {@link File Files}, being that the second XMI file depends on the first.
+     * 
+     * Ensures that both profiles aren't remembered by the model sub-system
+     * by renaming them.
+     *
+     * @return A list of two files, the second File contains an XMI that
+     *         depends of the first.
+     * @throws IOException if file IO causes errors.
+     * @throws UmlException if the manipulation of models causes errors.
+     */
+    public List<File> createUnloadedProfilePairWith2ndDependingOn1stViaXmi()
+            throws IOException, UmlException {
+        List<File> profileFiles =
+            createProfileFilePairWith2ndDependingOn1stViaXmi();
+        File baseProfileFile = profileFiles.get(0);
+        String baseProfileFileName = baseProfileFile.getName();
+        // ensure that model subsystem implementation doesn't remember the
+        // profiles by changing their names and directories
+        baseProfileFile = FileHelper.moveFileToNewTempDirectory(
+            baseProfileFile, "new-base-profile", ".xmi",
+            ProfileMother.class.getCanonicalName());
+        File dependentProfileFile = profileFiles.get(1);
+        replaceStringInFile(dependentProfileFile, baseProfileFileName,
+            baseProfileFile.getName());
+        dependentProfileFile = FileHelper.moveFileToNewTempDirectory(
+            dependentProfileFile, "new-dependent-profile", ".xmi",
+            ProfileMother.class.getCanonicalName());
+        profileFiles.clear();
+        profileFiles.add(baseProfileFile);
+        profileFiles.add(dependentProfileFile);
+        return profileFiles;
+    }
+
+    /**
+     * The regular expression occurrences in file are replaced by
+     * replacement {@link String}.
+     * @param file the file in which to replace.
+     * @param regex the regular expression to replace.
+     * @param replacement the replacement {@link String}.
+     * @throws IOException if IO operations throw.
+     */
+    static public void replaceStringInFile(File file, String regex,
+            String replacement) throws IOException {
+        StringBuffer fileContents = new StringBuffer();
+        BufferedReader reader = null;
+        String fileContents2 = null;
+        try {
+            reader = new BufferedReader(new FileReader(file));
+            String line = "";
+            while (null != (line = reader.readLine())) {
+                fileContents.append(line);
+                fileContents.append("\n");
+            }
+            fileContents2 = fileContents.toString();
+            fileContents2 = fileContents2.replaceAll(regex, replacement);
+        } finally {
+            if (reader != null) {
+                reader.close();
+            }
+        }
+        if (fileContents2 != null && file.delete() && file.createNewFile()) {
+            BufferedWriter writer = null;
+            try {
+                writer = new BufferedWriter(new FileWriter(file));
+                writer.append(fileContents2);
+            } finally {
+                if (writer != null) {
+                    writer.close();
+                }
+            }
+        }
+    }
 }

Modified: trunk/src/argouml-app/tests/org/argouml/profile/TestProfileMother.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/profile/TestProfileMother.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/profile/TestProfileMother.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/profile/TestProfileMother.java	2010-02-11 15:48:44-0800
@@ -207,8 +207,8 @@
             1, dependentProfileModelTopElements.size());
     }
 
-    private void assertStringInLineOfFile(String failureMsg, String str, File file)
-            throws IOException {
+    private void assertStringInLineOfFile(String failureMsg, String str,
+            File file) throws IOException {
         BufferedReader fileReader = new BufferedReader(new FileReader(file));
         try {
             String line = "";
@@ -226,13 +226,14 @@
     }
     
     /**
-     * Test {@link ProfileMother#createProfileFilePairWithSecondDependingOnFirstThroughXmi()}.
+     * Test {@link ProfileMother#createProfileFilePairWith2ndDependingOn1stViaXmi()}.
      * @throws IOException when file IO goes wrong...
      * @throws UmlException when UML manipulation goes wrong...
      */
-    public void testCreateProfilePairWithSecondDependingOnFirstThroughXmi() throws IOException, UmlException {
+    public void testCreateProfilePairWith2ndDependingOn1stViaXmi()
+            throws IOException, UmlException {
         List<File> profilesFiles =
-            mother.createProfileFilePairWithSecondDependingOnFirstThroughXmi();
+            mother.createProfileFilePairWith2ndDependingOn1stViaXmi();
         assertEquals("Should contain two elements.", 2, profilesFiles.size());
         File baseFile = profilesFiles.get(0);
         File dependentFile = profilesFiles.get(1);

Modified: trunk/src/argouml-app/tests/org/argouml/profile/internal/TestDependencyResolver.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/profile/internal/TestDependencyResolver.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/profile/internal/TestDependencyResolver.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/profile/internal/TestDependencyResolver.java	2010-02-11 15:48:44-0800
@@ -49,6 +49,8 @@
         };
         resolver = new DependencyResolver<String>(checker2);
         resolver.resolve();
+        assertTrue("There should be no unresolved items.",
+            resolver.unresolvedItems.isEmpty());
     }
 
     /**
@@ -82,6 +84,8 @@
         assertEquals(3, checker.calls);
         assertTrue("All the items should have been resolved.",
             checker.resolved.containsAll(items));
+        assertTrue("There should be no unresolved items.",
+            resolver.unresolvedItems.isEmpty());
     }
 
     /**
@@ -108,6 +112,8 @@
         resolver.resolve(items);
         assertTrue("All the items should have been resolved.",
             checker.resolved.containsAll(items));
+        assertTrue("There should be no unresolved items.",
+            resolver.unresolvedItems.isEmpty());
     }
 
     /**
@@ -135,6 +141,8 @@
         assertTrue("All the items should have been resolved.",
             checker.resolved.containsAll(items1)
             && checker.resolved.containsAll(items2));
+        assertTrue("There should be no unresolved items.",
+            resolver.unresolvedItems.isEmpty());
     }
 
     /**
@@ -172,6 +180,8 @@
         resolver.resolve();
         assertTrue("All the items should have been resolved.",
             checker.resolved.containsAll(items));
+        assertTrue("There should be no unresolved items.",
+            resolver.unresolvedItems.isEmpty());
     }
 }
 

Modified: trunk/src/argouml-app/tests/org/argouml/profile/internal/TestProfileManagerImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/profile/internal/TestProfileManagerImpl.java?view=diff&pathrev=17994&r1=17993&r2=17994
==============================================================================
--- trunk/src/argouml-app/tests/org/argouml/profile/internal/TestProfileManagerImpl.java	(original)
+++ trunk/src/argouml-app/tests/org/argouml/profile/internal/TestProfileManagerImpl.java	2010-02-11 15:48:44-0800
@@ -39,11 +39,7 @@
 
 package org.argouml.profile.internal;
 
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -55,7 +51,6 @@
 
 import junit.framework.TestCase;
 
-import org.argouml.FileHelper;
 import org.argouml.model.InitializeModel;
 import org.argouml.model.UmlException;
 import org.argouml.profile.Profile;
@@ -165,7 +160,7 @@
         int numRegisteredBefore = registeredProfiles.size();
         ProfileMother mother = new ProfileMother();
         List<File> profileFiles =
-            mother.createProfileFilePairWithSecondDependingOnFirstThroughXmi();
+            mother.createUnloadedProfilePairWith2ndDependingOn1stViaXmi();
         Collections.reverse(profileFiles);
         
         ProfileManagerImpl managerImpl = (ProfileManagerImpl) manager;
@@ -191,27 +186,16 @@
             throws IOException, UmlException {
         List<Profile> registeredProfiles = manager.getRegisteredProfiles();
         int numRegisteredBefore = registeredProfiles.size();
+
         ProfileMother mother = new ProfileMother();
         List<File> profileFiles =
-            mother.createProfileFilePairWithSecondDependingOnFirstThroughXmi();
-        
-        File baseProfileFile = profileFiles.get(0);
-        String baseProfileFileName = baseProfileFile.getName();
-        File baseProfileDirectory = FileHelper.createTempDirectory(
-            getClass().getCanonicalName());
-        String newBaseProfileFileName = "new-base-profile.xmi";
-        File newBaseProfileFile = new File(baseProfileDirectory,
-            newBaseProfileFileName);
-        assertTrue(baseProfileFile.renameTo(newBaseProfileFile));
-        baseProfileFile = newBaseProfileFile;
-        
-        ProfileManagerImpl managerImpl = (ProfileManagerImpl) manager;
-        
+            mother.createUnloadedProfilePairWith2ndDependingOn1stViaXmi();
         File dependentProfileFile = profileFiles.get(1);
-        replaceStringInFile(dependentProfileFile, baseProfileFileName,
-            newBaseProfileFileName);
+        File baseProfileFile = profileFiles.get(0);
         ArrayList<File> dependentProfileList = new ArrayList<File>();
         dependentProfileList.add(dependentProfileFile);
+        
+        ProfileManagerImpl managerImpl = (ProfileManagerImpl) manager;
         managerImpl.loadProfiles(dependentProfileList);
         assertEquals("We should have exaclty the same number of registered "
             + "profiles as in the begining.",
@@ -220,40 +204,7 @@
         ArrayList<File> baseProfileList = new ArrayList<File>();
         baseProfileList.add(baseProfileFile);
         managerImpl.loadProfiles(baseProfileList);
-        
         assertEquals("Now we should have two more registered profiles.",
             numRegisteredBefore + 2, manager.getRegisteredProfiles().size());
     }
-
-    private void replaceStringInFile(File file, String regex,
-            String replacement) throws IOException {
-        StringBuffer fileContents = new StringBuffer();
-        BufferedReader reader = null;
-        String fileContents2 = null;
-        try {
-            reader = new BufferedReader(new FileReader(file));
-            String line = "";
-            while (null != (line = reader.readLine())) {
-                fileContents.append(line);
-                fileContents.append("\n");
-            }
-            fileContents2 = fileContents.toString();
-            fileContents2 = fileContents2.replaceAll(regex, replacement);
-        } finally {
-            if (reader != null) {
-                reader.close();
-            }
-        }
-        if (fileContents2 != null && file.delete() && file.createNewFile()) {
-            BufferedWriter writer = null;
-            try {
-                writer = new BufferedWriter(new FileWriter(file));
-                writer.append(fileContents2);
-            } finally {
-                if (writer != null) {
-                    writer.close();
-                }
-            }
-        }
-    }
 }

------------------------------------------------------
http://argouml.tigris.org/ds/viewMessage.do?dsForumId=5905&dsMessageId=2446959

To unsubscribe from this discussion, e-mail: [[email protected]].
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.