db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1529777 - in /db/torque/torque4/trunk/torque-generator/src: main/java/org/apache/torque/generator/configuration/ main/java/org/apache/torque/generator/control/ test/java/org/apache/torque/generator/control/
Date Mon, 07 Oct 2013 07:35:52 GMT
Author: tfischer
Date: Mon Oct  7 07:35:51 2013
New Revision: 1529777

URL: http://svn.apache.org/r1529777
Log:
TORQUE-292 use single file for checksums

Added:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Checksums.java
    db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/ChecksumsTest.java
Modified:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/ControllerState.java
    db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/PropertyToJavaGenerationTest.java

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java?rev=1529777&r1=1529776&r2=1529777&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
Mon Oct  7 07:35:51 2013
@@ -106,6 +106,12 @@ public class UnitConfiguration
     private boolean runOnlyOnSourceChange = false;
 
     /**
+     * The name of the template set. Is determined from the packaging
+     * and the location of the control file.
+     */
+    private String templateSetName;
+
+    /**
      * Returns the configuration of the outlets in this generation unit.
      *
      * @return the outlet configuration, not null.
@@ -583,6 +589,26 @@ public class UnitConfiguration
     }
 
     /**
+     * Returns the name of the template set.
+     *
+     * @return the name of the template set.
+     */
+    public String getTemplateSetName()
+    {
+        return templateSetName;
+    }
+
+    /**
+     * Sets the name of the template set.
+     *
+     * @param name the name of the template set.
+     */
+    public void setTemplateSetName(final String name)
+    {
+        this.templateSetName = name;
+    }
+
+    /**
      * Checks whether the unit configuration is fully initialized.
      *
      * @return true if the unit configuration is fully initialized,
@@ -597,7 +623,8 @@ public class UnitConfiguration
             || loglevel == null
             || configurationHandlers == null
             || entityReferences == null
-            || !overrideSourceProviderInitialized)
+            || !overrideSourceProviderInitialized
+            || templateSetName == null)
         {
             return false;
         }
@@ -618,6 +645,8 @@ public class UnitConfiguration
                 .append(outputDirectoryMap)
                 .append(", loglevel=")
                 .append(loglevel)
+                .append(", name=")
+                .append(templateSetName)
                 .append(")");
         return result.toString();
     }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java?rev=1529777&r1=1529776&r2=1529777&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
Mon Oct  7 07:35:51 2013
@@ -100,6 +100,20 @@ class UnitConfigurationReader
         unitConfiguration.setClassLoader(unitDescriptor.getClassLoader());
         unitConfiguration.setRunOnlyOnSourceChange(
                 unitDescriptor.isRunOnlyOnSourceChange());
+        StringBuilder name = new StringBuilder()
+                .append(unitDescriptor.getPackaging().toString())
+                .append(":");
+        if (unitDescriptor.getProjectPaths().getConfigurationPackage() != null)
+        {
+            name.append(
+                    unitDescriptor.getProjectPaths().getConfigurationPackage());
+        }
+        else
+        {
+            name.append(
+                    unitDescriptor.getProjectPaths().getConfigurationPath());
+        }
+        unitConfiguration.setTemplateSetName(name.toString());
 
         ConfigurationProvider configurationProvider
             = createConfigurationProvider(unitDescriptor);

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Checksums.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Checksums.java?rev=1529777&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Checksums.java
(added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Checksums.java
Mon Oct  7 07:35:51 2013
@@ -0,0 +1,259 @@
+package org.apache.torque.generator.control;
+
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Stores Checksums and dates of the checksums, keyed by a String.
+ * @version $Id: $
+ *
+ */
+public class Checksums
+{
+    /** Array containing the 16 hex characters. */
+    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
+
+    /** The file encoding for the lastChanges file. */
+    private static final String FILE_ENCODING = "ISO-8859-1";
+
+    /** The '0' character as byte. */
+    private static final byte ZERO_LITERAL_AS_BYTE = (byte) '0';
+
+    /** The '9' character as byte. */
+    private static final byte NINE_LITERAL_AS_BYTE = (byte) '9';
+
+    /** The base for characters > 9 in a hex string. */
+    private static final byte CHAR_BASE = (byte) 55;
+
+    /** Bits per character of a hexadecimal number. */
+    private static final int HEX_BITS_PER_CHAR = 4;
+
+    /** The largest hex two character number. */
+    private static final int HEX_SMALLEST_TWOCHAR_NUMBER = 0x10;
+
+    /** The smallest hex one character number. */
+    private static final int HEX_LARGEST_ONECHAR_NUMBER = 0x0F;
+
+    /** The largest hex two character number. */
+    private static final int HEX_LARGEST_TWOCHAR_NUMBER = 0xFF;
+
+    /** The separator between tokens in the written file. */
+    private static final String SEPARATOR = "-";
+
+    /** The checksums, keyed by the name of the checked entity. */
+    private final Map<String, byte[]> checksums
+            = new HashMap<String, byte[]>();
+
+    /** The modification dates, keyed by the name of the checked entity. */
+    private final Map<String, Date> modificationDates
+            = new HashMap<String, Date>();
+
+    public byte[] getChecksum(final String name)
+    {
+        return checksums.get(name);
+    }
+
+    public Checksums setChecksum(
+            final String name,
+            final byte[] checksum)
+    {
+        if (checksum == null)
+        {
+            throw new NullPointerException("checksum must not be null");
+        }
+        checksums.put(name, checksum);
+        return this;
+    }
+
+    public Map<String, byte[]> getChecksums()
+    {
+        return Collections.unmodifiableMap(checksums);
+    }
+
+    public Date getModificationDate(final String name)
+    {
+        return modificationDates.get(name);
+    }
+
+    public Checksums setModificationDate(
+            final String name,
+            final Date modificationDate)
+    {
+        if (modificationDate == null)
+        {
+            throw new NullPointerException("modificationDate must not be null");
+        }
+        modificationDates.put(name, modificationDate);
+        return this;
+    }
+
+    public Map<String, Date> getModificationDates()
+    {
+        return Collections.unmodifiableMap(modificationDates);
+    }
+
+    public Checksums writeToFile(final File toWriteTo)
+            throws IOException
+    {
+        Set<String> keys = new HashSet<String>();
+        keys.addAll(checksums.keySet());
+        keys.addAll(modificationDates.keySet());
+        StringBuilder content = new StringBuilder();
+        for (String key : keys)
+        {
+            Date modificationDate = modificationDates.get(key);
+            if (modificationDate != null)
+            {
+                content.append(modificationDate.getTime());
+            }
+            content.append(SEPARATOR);
+            byte[] checksum = checksums.get(key);
+            if (checksum != null)
+            {
+                for (int j = 0; j < checksum.length; j++)
+                {
+                    int v = checksum[j] & HEX_LARGEST_TWOCHAR_NUMBER;
+                    content.append(HEX_ARRAY[v >>> HEX_BITS_PER_CHAR])
+                            .append(HEX_ARRAY[v & HEX_LARGEST_ONECHAR_NUMBER]);
+                }
+            }
+            content.append(SEPARATOR).append(key).append("\n");
+        }
+        FileUtils.writeStringToFile(
+                toWriteTo,
+                content.toString(),
+                FILE_ENCODING);
+        return this;
+    }
+
+    public Checksums readFromFile(final File toReadFrom)
+            throws IOException
+    {
+        checksums.clear();
+        modificationDates.clear();
+
+        if (!toReadFrom.exists())
+        {
+            // nothing to read
+            return this;
+        }
+
+        String content = FileUtils.readFileToString(
+                toReadFrom,
+                FILE_ENCODING);
+        for (String line : StringUtils.split(content, "\n"))
+        {
+            if (StringUtils.isBlank(line))
+            {
+                continue;
+            }
+            StringTokenizer tokenizer = new StringTokenizer(
+                    line,
+                    SEPARATOR,
+                    true);
+
+            String datestring;
+            String checksumString;
+            String name;
+            try
+            {
+                datestring = tokenizer.nextToken();
+                if (SEPARATOR.equals(datestring))
+                {
+                    datestring = null;
+                }
+                else if (!(SEPARATOR.equals(tokenizer.nextToken())))
+                {
+                    throw new IOException("invalid line (no separator after date)"
+                            + line);
+                }
+
+                checksumString = tokenizer.nextToken();
+                if (SEPARATOR.equals(checksumString))
+                {
+                    checksumString = null;
+                }
+                else if (!(SEPARATOR.equals(tokenizer.nextToken())))
+                {
+                    throw new IOException("invalid line (no separator after checksum)"
+                            + line);
+                }
+                name = tokenizer.nextToken("");
+            }
+            catch (NoSuchElementException e)
+            {
+                throw new IOException("invalid line (not enough separators)"
+                        + line);
+            }
+
+            if (datestring != null)
+            {
+                Date modificationDate = new Date(Long.parseLong(datestring));
+                modificationDates.put(name, modificationDate);
+            }
+            if (checksumString != null)
+            {
+                byte[] checksum = new byte[checksumString.length() / 2];
+                boolean last = false;
+                int currentByte = 0;
+                int i = 0;
+                for (char hexValue : checksumString.toCharArray())
+                {
+                    byte read;
+                    if (hexValue > NINE_LITERAL_AS_BYTE)
+                    {
+                        read = (byte) (hexValue - CHAR_BASE);
+                    }
+                    else
+                    {
+                        read = (byte) (hexValue - ZERO_LITERAL_AS_BYTE);
+                    }
+                    if (!last)
+                    {
+                        currentByte = HEX_SMALLEST_TWOCHAR_NUMBER * read;
+                        last = true;
+                    }
+                    else
+                    {
+                        currentByte = currentByte + read;
+                        checksum[i] = (byte) currentByte;
+                        i++;
+                        last = false;
+                    }
+                }
+                checksums.put(name, checksum);
+            }
+        }
+        return this;
+    }
+}

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java?rev=1529777&r1=1529776&r2=1529777&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/Controller.java
Mon Oct  7 07:35:51 2013
@@ -33,7 +33,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.log4j.PropertyConfigurator;
@@ -71,14 +70,11 @@ public class Controller
     private static Log log = LogFactory.getLog(Controller.class);
 
     /**
-     * The subdirectory in the work directory where last source changes
+     * The file in the work directory where last source changes
      * are stored.
      */
-    public static final String LAST_SOURCE_CHANGE_CACHE_SUBDIR
-            = "last-source-changes";
-
-    /** The suffix for written checksum files. */
-    public static final String CHECKSUM_SUFFIX = ".checksum";
+    public static final String LAST_SOURCE_CHANGE_CACHE_FILE
+            = "last-source-changes.checksums";
 
     /** The content of checksum files if no checksum can be computed. */
     public static final String NO_CHECKSUM_CONTENT
@@ -112,8 +108,8 @@ public class Controller
      * The key is the absolute path to the source file, the value the result
      * of the sourceModified check.
      */
-    private final Map<String,Boolean> sourceModifiedCache
-            = new HashMap<String,Boolean>();
+    private final Map<String, Boolean> sourceModifiedCache
+            = new HashMap<String, Boolean>();
 
     /**
      * Executes the controller action.
@@ -207,6 +203,25 @@ public class Controller
         unitConfiguration.getLoglevel().apply();
         log.debug("processGenerationUnit() : Loglevel applied.");
         controllerState.setUnitConfiguration(unitConfiguration);
+
+        File sourceChecksumsFile = new File(
+                unitConfiguration.getCacheDirectory(),
+                LAST_SOURCE_CHANGE_CACHE_FILE);
+        if (unitConfiguration.isRunOnlyOnSourceChange())
+        {
+            try
+            {
+                controllerState.getLastGeneratedSourceChecksums().readFromFile(
+                        sourceChecksumsFile);
+            }
+            catch (IOException e)
+            {
+                throw new GeneratorException("could not read "
+                        + "LastSourceChange checksum file"
+                        + sourceChecksumsFile.getAbsolutePath(),
+                    e);
+            }
+        }
         final List<Output> outputList = unitConfiguration.getOutputList();
         for (final Output output : outputList)
         {
@@ -215,6 +230,21 @@ public class Controller
                     controllerState,
                     unitConfiguration);
         }
+        if (unitConfiguration.isRunOnlyOnSourceChange())
+        {
+            try
+            {
+                controllerState.getThisGenerationSourceChecksums().writeToFile(
+                        sourceChecksumsFile);
+            }
+            catch (IOException e)
+            {
+                throw new GeneratorException("could not write "
+                        + "LastSourceChange checksum file"
+                        + sourceChecksumsFile.getAbsolutePath(),
+                    e);
+            }
+        }
     }
 
     /**
@@ -258,17 +288,13 @@ public class Controller
         {
             final Source source = sourceProvider.next();
             if (!unitConfiguration.isRunOnlyOnSourceChange()
-                    || checkSourceModified(source, unitConfiguration))
+                    || checkSourceModified(source, controllerState, unitConfiguration))
             {
                 processSourceInOutput(
                         source,
                         output,
                         controllerState,
                         unitConfiguration);
-                if (unitConfiguration.isRunOnlyOnSourceChange())
-                {
-                    writeLastModified(source, unitConfiguration);
-                }
             }
         }
         controllerState.setSourceProvider(null);
@@ -627,6 +653,7 @@ public class Controller
      */
     private boolean checkSourceModified(
             final Source source,
+            final ControllerState controllerState,
             final UnitConfiguration unitConfiguration)
     {
         File sourceFile = source.getSourceFile();
@@ -636,10 +663,12 @@ public class Controller
                     + "source file cannot be determined, return true");
             return true;
         }
-        String absoluteSourcePath = sourceFile.getAbsolutePath();
-        if (sourceModifiedCache.get(absoluteSourcePath) != null)
+        String sourceChangeKey = getSourceChangeKey(
+                unitConfiguration,
+                sourceFile);
+        if (sourceModifiedCache.get(sourceChangeKey) != null)
         {
-            return sourceModifiedCache.get(absoluteSourcePath);
+            return sourceModifiedCache.get(sourceChangeKey);
         }
 
         Date sourceLastModified = source.getLastModified();
@@ -648,121 +677,60 @@ public class Controller
             log.debug("checkSourceModified(): "
                     + "lastModified date of source cannot be determined, "
                     + "return true");
-            sourceModifiedCache.put(absoluteSourcePath, true);
+            sourceModifiedCache.put(sourceChangeKey, true);
             return true;
         }
-        File lastChangesDir = new File(
-                unitConfiguration.getCacheDirectory(),
-                LAST_SOURCE_CHANGE_CACHE_SUBDIR);
-        File lastChangesFile = new File(
-                lastChangesDir,
-                sourceFile.getName() + CHECKSUM_SUFFIX);
-        if (!lastChangesFile.exists())
+        controllerState.getThisGenerationSourceChecksums().setModificationDate(
+                sourceChangeKey, sourceLastModified);
+
+        byte[] sourceChecksum = source.getContentChecksum();
+        controllerState.getThisGenerationSourceChecksums().setChecksum(
+                sourceChangeKey, sourceChecksum);
+
+        Date lastGenerationTime
+                = controllerState.getLastGeneratedSourceChecksums()
+                    .getModificationDate(sourceChangeKey);
+        if (lastGenerationTime == null)
         {
             log.debug("checkSourceModified(): "
-                    + "lastChanges file does not exist, return true");
-            sourceModifiedCache.put(absoluteSourcePath, true);
+                    + "lastGenerationTime does not exist, return true");
+            sourceModifiedCache.put(sourceChangeKey, true);
             return true;
         }
-        if (lastChangesFile.lastModified() < sourceLastModified.getTime())
+        if (lastGenerationTime.before(sourceLastModified))
         {
             log.debug("checkSourceModified(): "
-                    + "lastChanges file was last changed before source ("
-                    + new Date(lastChangesDir.lastModified())
+                    + "lastGenerationTime was before source was modified ("
+                    + lastGenerationTime
                     + " < "
                     + sourceLastModified
                     + "), return true");
-            sourceModifiedCache.put(absoluteSourcePath, true);
+            sourceModifiedCache.put(sourceChangeKey, true);
             return true;
         }
-        byte[] lastChangesContent = null;
-        try
-        {
-            lastChangesContent = FileUtils.readFileToByteArray(lastChangesFile);
-        }
-        catch (IOException e)
-        {
-            log.warn("checkSourceModified(): "
-                    + "could not access File "
-                    + lastChangesFile.getAbsolutePath()
-                    + ", return true");
-            sourceModifiedCache.put(absoluteSourcePath, true);
-            return true;
-        }
-        if (!Arrays.equals(lastChangesContent, source.getContentChecksum()))
+        byte[] lastGeneratedChecksum
+                = controllerState.getLastGeneratedSourceChecksums()
+                    .getChecksum(sourceChangeKey);
+        if (!Arrays.equals(lastGeneratedChecksum, sourceChecksum))
         {
             log.debug("checkSourceModified(): "
                     + " different checksum, return true");
-            sourceModifiedCache.put(absoluteSourcePath, true);
+            sourceModifiedCache.put(sourceChangeKey, true);
             return true;
         }
         log.debug("checkSourceModified() : returning false");
-        sourceModifiedCache.put(absoluteSourcePath, false);
+        sourceModifiedCache.put(sourceChangeKey, false);
         return false;
     }
 
-    /**
-     * Writes the last modification time of a source to the work directory.
-     *
-     * @param source the source which modification time should be written,
-     *        not null.
-     * @param unitConfigurationthe configuration of the unit of generation,
-     *        not null.
-     */
-    private void writeLastModified(
-            final Source source,
-            final UnitConfiguration unitConfiguration)
+    private String getSourceChangeKey(
+            final UnitConfiguration unitConfiguration, final File sourceFile)
     {
-        File sourceFile = source.getSourceFile();
-        if (sourceFile == null)
-        {
-            log.debug("writeLastModified(): "
-                    + "source file cannot be determined, do nothing");
-            return;
-        }
-        File lastChangesDir = new File(
-                unitConfiguration.getCacheDirectory(),
-                LAST_SOURCE_CHANGE_CACHE_SUBDIR);
-        if (!lastChangesDir.exists())
-        {
-            boolean dirCreationSuccessfull = lastChangesDir.mkdirs();
-            if (!dirCreationSuccessfull)
-            {
-                log.warn("could not create directory(): "
-                        + lastChangesDir.getAbsolutePath()
-                        + ", do nothing");
-                return;
-            }
-        }
-        File lastChangesFile = new File(
-                lastChangesDir,
-                sourceFile.getName() + CHECKSUM_SUFFIX);
-        byte[] contentChecksum = source.getContentChecksum();
-        try
-        {
-            if (contentChecksum != null)
-            {
-                FileUtils.writeByteArrayToFile(
-                        lastChangesFile,
-                        source.getContentChecksum());
-            }
-            else
-            {
-                FileUtils.writeStringToFile(
-                        lastChangesFile,
-                        NO_CHECKSUM_CONTENT,
-                        "ISO-8859-1");
-                log.debug("writeLastModified(): "
-                        + "checksum written to file "
-                        + lastChangesFile.getAbsolutePath());
-            }
-        }
-        catch (IOException e)
-        {
-            log.warn("could not write to file(): "
-                    + lastChangesFile,
-                e);
-            return;
-        }
+        String sourceChangeCacheKey
+                = unitConfiguration.getTemplateSetName()
+                    + ":"
+                    + sourceFile.getAbsolutePath();
+        return sourceChangeCacheKey;
     }
+
 }

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/ControllerState.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/ControllerState.java?rev=1529777&r1=1529776&r2=1529777&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/ControllerState.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/control/ControllerState.java
Mon Oct  7 07:35:51 2013
@@ -102,6 +102,20 @@ public class ControllerState
     private Namespace outletNamespace;
 
     /**
+     * The checksums of the source files for the last generation run,
+     * not null.
+     */
+    private final Checksums lastGeneratedSourceChecksums
+            = new Checksums();
+
+    /**
+     * The checksums of the source files for this generation run,
+     * not null.
+     */
+    private final Checksums thisGenerationSourceChecksums
+            = new Checksums();
+
+    /**
      * Returns the source provider which is currently in use.
      *
      * @return the current source provider.
@@ -116,7 +130,7 @@ public class ControllerState
      *
      * @param sourceProvider the current source provider.
      */
-    public void setSourceProvider(SourceProvider sourceProvider)
+    public void setSourceProvider(final SourceProvider sourceProvider)
     {
         this.sourceProvider = sourceProvider;
     }
@@ -137,7 +151,7 @@ public class ControllerState
      *
      * @param output the output which is currently processed.
      */
-    void setOutput(Output output)
+    void setOutput(final Output output)
     {
         this.output = output;
     }
@@ -163,7 +177,7 @@ public class ControllerState
      * @param outlet the outlet to be added to the stack of outlets,
      *        not null.
      */
-    public void pushOutlet(Outlet outlet)
+    public void pushOutlet(final Outlet outlet)
     {
         if (outlet == null)
         {
@@ -201,7 +215,7 @@ public class ControllerState
      * @param sourceElement the new current source model object, or null
      *        to remove the current source mdoel object.
      */
-    public void setModel(Object model)
+    public void setModel(final Object model)
     {
         this.model = model;
     }
@@ -223,7 +237,7 @@ public class ControllerState
      * @param modelRoot the the root object of the current source,
      *        or null to remove the current root object.
      */
-    public void setModelRoot(Object modelRoot)
+    public void setModelRoot(final Object modelRoot)
     {
         this.modelRoot = modelRoot;
     }
@@ -246,7 +260,7 @@ public class ControllerState
      *        (i.e. the outlet which produces the whole content),
      *        or null to remove the reference.
      */
-    void setRootOutletReference(OutletReference rootOutletReference)
+    void setRootOutletReference(final OutletReference rootOutletReference)
     {
         this.rootOutletReference = rootOutletReference;
     }
@@ -258,7 +272,7 @@ public class ControllerState
      * @param namespace the namespace of the outlet which is currently
      *        active, or null to remove the name space.
      */
-    void setOutletNamespace(Namespace namespace)
+    void setOutletNamespace(final Namespace namespace)
     {
         outletNamespace = namespace;
     }
@@ -284,7 +298,7 @@ public class ControllerState
      * @return The value of the option, or null if no option with that name
      *         is visible from the given namespace.
      */
-    public Object getOption(String name)
+    public Object getOption(final String name)
     {
         Options options = unitConfiguration.getOptions();
         QualifiedName qualifiedName = getQualifiedName(name);
@@ -310,7 +324,7 @@ public class ControllerState
      *
      * @throws NullPointerException if optionName is null.
      */
-    public Object getOption(OptionName optionName)
+    public Object getOption(final OptionName optionName)
     {
         return getOption(optionName.getName());
     }
@@ -325,7 +339,7 @@ public class ControllerState
      * @return The value of the option as boolean, or false if no option
      *         with that name is visible from the given namespace,
      */
-    public boolean getBooleanOption(String name)
+    public boolean getBooleanOption(final String name)
     {
         Object option = getOption(name);
         if (option == null)
@@ -349,7 +363,7 @@ public class ControllerState
      *
      * @throws NullPointerExeption if optionName is null.
      */
-    public boolean getBooleanOption(OptionName optionName)
+    public boolean getBooleanOption(final OptionName optionName)
     {
         return getBooleanOption(optionName.getName());
     }
@@ -364,7 +378,7 @@ public class ControllerState
      * @return The value of the option as boolean, or false if no option
      *         with that name is visible from the given namespace,
      */
-    public String getStringOption(String name)
+    public String getStringOption(final String name)
     {
         Object option = getOption(name);
         if (option == null)
@@ -387,7 +401,7 @@ public class ControllerState
      *
      * @throws NullPointerExeption if optionName is null.
      */
-    public String getStringOption(OptionName optionName)
+    public String getStringOption(final OptionName optionName)
     {
         return getStringOption(optionName.getName());
     }
@@ -424,7 +438,7 @@ public class ControllerState
      * @throws NullPointerException if name is null
      * @throws IllegalArgumentException if name is no valid qualifiedName.
      */
-    public QualifiedName getQualifiedName(String name)
+    public QualifiedName getQualifiedName(final String name)
     {
         QualifiedName qualifiedName = new QualifiedName(
                 name,
@@ -450,7 +464,7 @@ public class ControllerState
      * @param outputFilePath the currently generated file, or null to remove
      *        the current output file.
      */
-    void setOutputFile(File outputFilePath)
+    void setOutputFile(final File outputFilePath)
     {
         this.outputFile = outputFilePath;
     }
@@ -474,7 +488,7 @@ public class ControllerState
      * @param sourceFile the current source file, or null to remove the
      *        source file.
      */
-    public void setSourceFile(File sourceFile)
+    public void setSourceFile(final File sourceFile)
     {
         this.sourceFile = sourceFile;
     }
@@ -496,11 +510,31 @@ public class ControllerState
      * @param unitConfiguration the configuration of the currently processed
      *        generation unit.
      */
-    public void setUnitConfiguration(UnitConfiguration unitConfiguration)
+    public void setUnitConfiguration(final UnitConfiguration unitConfiguration)
     {
         this.unitConfiguration = unitConfiguration;
     }
 
+    /**
+     * Returns the checksums of the source files for the last generation run.
+     *
+     * @return the checksums, not null.
+     */
+    public Checksums getLastGeneratedSourceChecksums()
+    {
+        return lastGeneratedSourceChecksums;
+    }
+
+    /**
+     * Returns the checksums of the source files for this generation run.
+     *
+     * @return the checksums, not null.
+     */
+    public Checksums getThisGenerationSourceChecksums()
+    {
+        return thisGenerationSourceChecksums;
+    }
+
     @Override
     public String toString()
     {

Added: db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/ChecksumsTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/ChecksumsTest.java?rev=1529777&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/ChecksumsTest.java
(added)
+++ db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/ChecksumsTest.java
Mon Oct  7 07:35:51 2013
@@ -0,0 +1,143 @@
+package org.apache.torque.generator.control;
+
+/*
+ * 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.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.torque.generator.BaseTest;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the class LastChangesContent.
+ *
+ * @version $Id: $
+ */
+public class ChecksumsTest extends BaseTest
+{
+    private static final File CHECKSUM_DIR
+            = new File("target/test/lastChangesContent");
+
+    private static final File CHECKSUM_FILE
+            = new File(CHECKSUM_DIR, "checksum");
+
+    @Before
+    public void cleanUp()
+    {
+        if (CHECKSUM_FILE.exists())
+        {
+            CHECKSUM_FILE.delete();
+        }
+        if (CHECKSUM_DIR.exists())
+        {
+            CHECKSUM_DIR.delete();
+        }
+    }
+
+    @Test
+    public void testWriteToFile() throws Exception
+    {
+        // prepare
+        Checksums lastChangesContent = new Checksums();
+        lastChangesContent.setChecksum(
+                "name",
+                new byte[] {0, 100, -100, 1, -1});
+        lastChangesContent.setChecksum(
+                "nameChecksumOnly",
+                new byte[] {127});
+        lastChangesContent.setModificationDate(
+                "name",
+                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(
+                        "2000-01-01 17:13:16"));
+        lastChangesContent.setModificationDate(
+                "name - ModDateOnly",
+                new Date(12345L));
+        assertFalse(CHECKSUM_DIR.exists());
+        assertFalse(CHECKSUM_FILE.exists());
+
+        // execute
+        lastChangesContent.writeToFile(CHECKSUM_FILE);
+
+        // verify
+        assertTrue(CHECKSUM_FILE.exists());
+        String fileContent = FileUtils.readFileToString(
+                CHECKSUM_FILE,
+                "ISO-8859-1");
+        // order of lines not fixed, therefore check substrings.
+        // expected is
+        // "12345--nameModDateOnly\n"
+        // + "946743196000-00649C01FF-name\n"
+        // + "-7F-nameChecksumOnly"
+        // where line order may be changed.
+        String expectedLine1 = "946743196000-00649C01FF-name\n";
+        String expectedLine2 = "-7F-nameChecksumOnly\n";
+        String expectedLine3 = "12345--name - ModDateOnly\n";
+        assertEquals(expectedLine1.length()
+                    + expectedLine2.length()
+                    + expectedLine3.length(),
+                fileContent.length());
+        assertTrue(fileContent.contains(expectedLine1));
+        assertTrue(fileContent.contains(expectedLine2));
+        assertTrue(fileContent.contains(expectedLine3));
+    }
+
+    @Test
+    public void testReadFromFile() throws Exception
+    {
+        // prepare
+        Checksums lastChangesContent = new Checksums();
+        FileUtils.writeStringToFile(
+                CHECKSUM_FILE,
+                "946743196000-00649C01FF-name\n"
+                    + "-7F-nameChecksumOnly\n"
+                    +"12345--name - ModDateOnly\n",
+                "ISO-8859-1");
+        // check that existing entries are cleared
+        lastChangesContent.setChecksum("a", new byte[] {1});
+        lastChangesContent.setModificationDate("b", new Date());
+
+        // execute
+        lastChangesContent.readFromFile(CHECKSUM_FILE);
+
+        // verify
+        assertEquals(2, lastChangesContent.getChecksums().size());
+        assertArrayEquals(
+                new byte[] {0, 100, -100, 1, -1},
+                lastChangesContent.getChecksum("name"));
+        assertArrayEquals(
+                new byte[] {127},
+                lastChangesContent.getChecksum("nameChecksumOnly"));
+        assertEquals(
+                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(
+                        "2000-01-01 17:13:16"),
+                lastChangesContent.getModificationDate("name"));
+        assertEquals(
+                new Date(12345L),
+                lastChangesContent.getModificationDate("name - ModDateOnly"));
+    }
+}

Modified: db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/PropertyToJavaGenerationTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/PropertyToJavaGenerationTest.java?rev=1529777&r1=1529776&r2=1529777&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/PropertyToJavaGenerationTest.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/control/PropertyToJavaGenerationTest.java
Mon Oct  7 07:35:51 2013
@@ -29,6 +29,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.torque.generator.BaseTest;
 import org.apache.torque.generator.configuration.UnitDescriptor;
 import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
@@ -227,13 +228,18 @@ public class PropertyToJavaGenerationTes
             = new File(targetDir, "Properties.properties");
         assertTrue(propertiesResultFile.exists());
 
-        // change checksum file
+        // change checksum in checksums file
         File checksumFile = new File(
                 cacheDir,
-                "last-source-changes/propertiesData.properties.checksum");
-        long checksumFileLastModified = checksumFile.lastModified();
-        FileUtils.writeStringToFile(checksumFile, "abc", "ISO-8859-1");
-        assertTrue(checksumFile.setLastModified(checksumFileLastModified));
+                "last-source-changes.checksums");
+        String checksumContent = FileUtils.readFileToString(checksumFile, "ISO-8859-1");
+        // check contains only one line
+        assertEquals(1, StringUtils.countMatches(checksumContent, "\n"));
+        int firstMinusPos = checksumContent.indexOf("-");
+        int secondMinusPos = checksumContent.indexOf("-", firstMinusPos + 1);
+        String checksum = checksumContent.substring(firstMinusPos, secondMinusPos + 1);
+        String changedChecksumContent = checksumContent.replace(checksum, "-AA-");
+        FileUtils.writeStringToFile(checksumFile, changedChecksumContent, "ISO-8859-1");
 
         // delete target so we can see if generation runs again
         assertTrue(propertiesResultFile.delete());



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message