[hornetq-commits] JBoss hornetq SVN: r9524 - in trunk: tests/src/org/hornetq/tests/integration/persistence and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Aug 10 22:16:35 EDT 2010


Author: clebert.suconic at jboss.com
Date: 2010-08-10 22:16:35 -0400 (Tue, 10 Aug 2010)
New Revision: 9524

Added:
   trunk/tests/src/org/hornetq/tests/integration/persistence/ExportFormatTest.java
Modified:
   trunk/src/main/org/hornetq/core/journal/impl/ExportJournal.java
   trunk/src/main/org/hornetq/core/journal/impl/ImportJournal.java
Log:
Adding test to validate export/import format between versions

Modified: trunk/src/main/org/hornetq/core/journal/impl/ExportJournal.java
===================================================================
--- trunk/src/main/org/hornetq/core/journal/impl/ExportJournal.java	2010-08-10 21:05:17 UTC (rev 9523)
+++ trunk/src/main/org/hornetq/core/journal/impl/ExportJournal.java	2010-08-11 02:16:35 UTC (rev 9524)
@@ -72,18 +72,32 @@
                                     String journalSuffix,
                                     int minFiles,
                                     int fileSize,
-                                    String fileOutpu) throws Exception
+                                    String fileOutput) throws Exception
    {
-      NIOSequentialFileFactory nio = new NIOSequentialFileFactory(directory);
-
-      JournalImpl journal = new JournalImpl(fileSize, minFiles, 0, 0, nio, journalPrefix, journalSuffix, 1);
       
-      FileOutputStream fileOut = new FileOutputStream(new File(fileOutpu));
+      FileOutputStream fileOut = new FileOutputStream(new File(fileOutput));
 
       BufferedOutputStream buffOut = new BufferedOutputStream(fileOut);
 
       PrintStream out = new PrintStream(buffOut);
+      
+      exportJournal(directory, journalPrefix, journalSuffix, minFiles, fileSize, out);
+      
+      out.close();
+   }
 
+   
+   public static void exportJournal(String directory,
+                                    String journalPrefix,
+                                    String journalSuffix,
+                                    int minFiles,
+                                    int fileSize,
+                                    PrintStream out) throws Exception
+   {
+      NIOSequentialFileFactory nio = new NIOSequentialFileFactory(directory);
+
+      JournalImpl journal = new JournalImpl(fileSize, minFiles, 0, 0, nio, journalPrefix, journalSuffix, 1);
+
       List<JournalFile> files = journal.orderFiles();
 
       for (JournalFile file : files)
@@ -92,8 +106,6 @@
 
          exportJournalFile(out, nio, file);
       }
-
-      out.close();
    }
 
    /**

Modified: trunk/src/main/org/hornetq/core/journal/impl/ImportJournal.java
===================================================================
--- trunk/src/main/org/hornetq/core/journal/impl/ImportJournal.java	2010-08-10 21:05:17 UTC (rev 9523)
+++ trunk/src/main/org/hornetq/core/journal/impl/ImportJournal.java	2010-08-11 02:16:35 UTC (rev 9524)
@@ -16,7 +16,9 @@
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -76,7 +78,29 @@
                                     int fileSize,
                                     String fileInput) throws Exception
    {
+      FileInputStream fileInputStream = new FileInputStream(new File(fileInput));
+      importJournal(directory, journalPrefix, journalSuffix, minFiles, fileSize, fileInputStream);
 
+   }
+   public static void importJournal(String directory,
+                                    String journalPrefix,
+                                    String journalSuffix,
+                                    int minFiles,
+                                    int fileSize,
+                                    InputStream stream) throws Exception
+   {
+      Reader reader = new InputStreamReader(stream);
+      importJournal(directory, journalPrefix, journalSuffix, minFiles, fileSize, reader);
+   }
+
+   public static void importJournal(String directory,
+                                    String journalPrefix,
+                                    String journalSuffix,
+                                    int minFiles,
+                                    int fileSize,
+                                    Reader reader) throws Exception
+   {
+
       File journalDir = new File(directory);
 
       journalDir.mkdirs();
@@ -95,19 +119,17 @@
       // The journal is empty, as we checked already. Calling load just to initialize the internal data
       journal.loadInternalOnly();
 
-      FileInputStream fileInputStream = new FileInputStream(new File(fileInput));
+      BufferedReader buffReader = new BufferedReader(reader);
 
-      BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
-
       String line;
 
       HashMap<Long, AtomicInteger> txCounters = new HashMap<Long, AtomicInteger>();
 
       long lineNumber = 0;
-      
+
       Map<Long, JournalRecord> journalRecords = journal.getRecords();
 
-      while ((line = reader.readLine()) != null)
+      while ((line = buffReader.readLine()) != null)
       {
          lineNumber++;
          String splitLine[] = line.split(",");
@@ -161,7 +183,7 @@
             else if (operation.equals("DeleteRecord"))
             {
                long id = parseLong("id", lineProperties);
-               
+
                // If not found it means the append/update records were reclaimed already
                if (journalRecords.get((Long)id) != null)
                {
@@ -238,7 +260,7 @@
             System.err.println("Error at line " + lineNumber + ", operation=" + operation + " msg = " + ex.getMessage());
          }
       }
-      
+
       journal.stop();
    }
 

Added: trunk/tests/src/org/hornetq/tests/integration/persistence/ExportFormatTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/integration/persistence/ExportFormatTest.java	                        (rev 0)
+++ trunk/tests/src/org/hornetq/tests/integration/persistence/ExportFormatTest.java	2010-08-11 02:16:35 UTC (rev 9524)
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2010 Red Hat, Inc.
+ * Red Hat 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.
+ */
+
+package org.hornetq.tests.integration.persistence;
+
+import java.io.StringReader;
+
+import org.hornetq.api.core.client.ClientConsumer;
+import org.hornetq.api.core.client.ClientMessage;
+import org.hornetq.api.core.client.ClientProducer;
+import org.hornetq.api.core.client.ClientSession;
+import org.hornetq.api.core.client.ClientSessionFactory;
+import org.hornetq.core.journal.impl.ExportJournal;
+import org.hornetq.core.journal.impl.ImportJournal;
+import org.hornetq.core.server.HornetQServer;
+import org.hornetq.tests.util.ServiceTestBase;
+
+/**
+ * A ExportFormatTest
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ *
+ *
+ */
+public class ExportFormatTest extends ServiceTestBase
+{
+
+   // Constants -----------------------------------------------------
+
+   // Attributes ----------------------------------------------------
+
+   // Case the format was changed, and the change was agreed, use _testCreateFormat to recreate this field
+   String bindingsFile = "#File,JournalFileImpl: (hornetq-bindings-1.bindings id = 1, recordID = 1)\n" + "operation at AddRecord,id at 2,userRecordType at 24,length at 8,isUpdate at false,data at AAAAAH____8=\n"
+                         + "operation at AddRecord,id at 0,userRecordType at 23,length at 16,isUpdate at false,data at jhbeVKTqEd-gYwAi-v8IyA==\n"
+                         + "operation at AddRecord,id at 4,userRecordType at 21,length at 17,isUpdate at false,data at AAAABEEAMQAAAAAEQQAxAAA=\n"
+                         + "operation at AddRecord,id at 109,userRecordType at 24,length at 8,isUpdate at false,data at AAAAAAAAAG0=\n"
+                         + "#File,JournalFileImpl: (hornetq-bindings-2.bindings id = 2, recordID = 2)\n";
+
+   // Case the format was changed, and the change was agreed, use _testCreateFormat to recreate this field
+   String journalFile = "#File,JournalFileImpl: (hornetq-data-1.hq id = 1, recordID = 1)\n" 
+                        + "operation at AddRecordTX,txID at 3,id at 6,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAABgEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rANMEAQAAAAEAAAAGawBlAHkABgAAAAA=\n"
+                        + "operation at UpdateTX,txID at 3,id at 6,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecordTX,txID at 3,id at 7,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAABwEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rANcEAQAAAAEAAAAGawBlAHkABgAAAAE=\n"
+                        + "operation at UpdateTX,txID at 3,id at 7,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecordTX,txID at 3,id at 8,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAACAEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rANcEAQAAAAEAAAAGawBlAHkABgAAAAI=\n"
+                        + "operation at UpdateTX,txID at 3,id at 8,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecordTX,txID at 3,id at 9,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAACQEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rANcEAQAAAAEAAAAGawBlAHkABgAAAAM=\n"
+                        + "operation at UpdateTX,txID at 3,id at 9,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecordTX,txID at 3,id at 10,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAACgEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rANcEAQAAAAEAAAAGawBlAHkABgAAAAQ=\n"
+                        + "operation at UpdateTX,txID at 3,id at 10,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at Commit,txID at 3,numberOfRecords at 10\n"
+                        + "operation at AddRecord,id at 14,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAADgEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rAOEEAQAAAAEAAAAGawBlAHkABgAAAAU=\n"
+                        + "operation at Update,id at 14,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecord,id at 15,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAADwEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rAOQEAQAAAAEAAAAGawBlAHkABgAAAAY=\n"
+                        + "operation at Update,id at 15,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecord,id at 16,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAAEAEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rAOUEAQAAAAEAAAAGawBlAHkABgAAAAc=\n"
+                        + "operation at Update,id at 16,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecord,id at 17,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAAEQEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rAOcEAQAAAAEAAAAGawBlAHkABgAAAAg=\n"
+                        + "operation at Update,id at 17,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "operation at AddRecord,id at 18,userRecordType at 31,length at 65,isUpdate at false,data at AAAAEQAAAE4AAAAAAAAAEgEAAAAEQQAxAAAA_wAAAAAAAAAAAAABKl7rAOgEAQAAAAEAAAAGawBlAHkABgAAAAk=\n"
+                        + "operation at Update,id at 18,userRecordType at 32,length at 8,isUpdate at true,data at AAAAAAAAAAQ=\n"
+                        + "#File,JournalFileImpl: (hornetq-data-2.hq id = 2, recordID = 2)\n";
+
+   // Static --------------------------------------------------------
+
+   // Constructors --------------------------------------------------
+
+   // Public --------------------------------------------------------
+
+   protected void tearDown() throws Exception
+   {
+
+   }
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+   }
+
+   public void _testCreateFormat() throws Exception
+   {
+      HornetQServer server = createServer(true);
+      server.start();
+
+      ClientSessionFactory factory = createInVMFactory();
+      ClientSession session = factory.createSession(false, false, false);
+      session.createQueue("A1", "A1");
+
+      ClientProducer producer = session.createProducer("A1");
+      for (int i = 0; i < 5; i++)
+      {
+         ClientMessage msg = session.createMessage(true);
+         msg.putIntProperty("key", i);
+         producer.send(msg);
+      }
+      session.commit();
+
+      session.close();
+
+      session = factory.createSession(false, true, true);
+
+      producer = session.createProducer("A1");
+
+      for (int i = 5; i < 10; i++)
+      {
+         ClientMessage msg = session.createMessage(true);
+         msg.putIntProperty("key", i);
+         producer.send(msg);
+      }
+
+      server.stop();
+
+      System.out.println("copy & paste the following as bindingsFile:");
+
+      ExportJournal.exportJournal(getBindingsDir(), "hornetq-bindings", "bindings", 2, 1048576, System.out);
+
+      System.out.println("copy & paste the following as dataFile:");
+
+      ExportJournal.exportJournal(getJournalDir(), "hornetq-data", "hq", 2, 102400, System.out);
+   }
+
+   public void testConsumeFromFormat() throws Exception
+   {
+      ImportJournal.importJournal(getJournalDir(), "hornetq-data", "hq", 2, 102400, new StringReader(journalFile));
+      ImportJournal.importJournal(getBindingsDir(),
+                                  "hornetq-bindings",
+                                  "bindings",
+                                  2,
+                                  1048576,
+                                  new StringReader(bindingsFile));
+
+      HornetQServer server = createServer(true);
+      server.start();
+
+      ClientSessionFactory factory = createInVMFactory();
+      ClientSession session = factory.createSession();
+      session.start();
+
+      ClientConsumer consumer = session.createConsumer("A1");
+      for (int i = 0; i < 10; i++)
+      {
+         ClientMessage msg = consumer.receive(5000);
+         assertNotNull(msg);
+         msg.acknowledge();
+         assertEquals(i, msg.getIntProperty("key").intValue());
+      }
+
+      session.commit();
+
+      server.stop();
+   }
+
+   // Package protected ---------------------------------------------
+
+   // Protected -----------------------------------------------------
+
+   // Private -------------------------------------------------------
+
+   // Inner classes -------------------------------------------------
+
+}



More information about the hornetq-commits mailing list