View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.transaction.file;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.commons.transaction.util.CommonsLoggingLogger;
33  import org.apache.commons.transaction.util.FileHelper;
34  import org.apache.commons.transaction.util.LoggerFacade;
35  
36  /**
37   * Tests for FileResourceManager.
38   * 
39   * @version $Id: FileResourceManagerTest.java 493628 2007-01-07 01:42:48Z joerg $
40   */
41  public class FileResourceManagerVirtualAdminCommandsTest extends TestCase {
42  
43      private static final Log log = LogFactory
44              .getLog(FileResourceManagerVirtualAdminCommandsTest.class.getName());
45  
46      private static final LoggerFacade sLogger = new CommonsLoggingLogger(log);
47  
48      private static final String ADMIN_COMMAND_PREFIX = "/extremelyUnlikelyPrefixForAdminCommands";
49  
50      private static final String STORE = "tmp/store";
51  
52      private static final String WORK = "tmp/work";
53  
54      private static final String ENCODING = "ISO-8859-15";
55  
56      private static final String[] INITIAL_FILES = new String[] { STORE + "/olli/Hubert6",
57              STORE + "/olli/Hubert" };
58  
59      protected static final long TIMEOUT = Long.MAX_VALUE;
60  
61      private static void removeRec(String dirPath) {
62          FileHelper.removeRec(new File(dirPath));
63      }
64  
65      private static final void createFiles(String[] filePaths) {
66          createFiles(filePaths, null, null);
67      }
68  
69      private static final void createFiles(String[] filePaths, String[] contents, String dirPath) {
70          for (int i = 0; i < filePaths.length; i++) {
71              String filePath = filePaths[i];
72              File file;
73              if (dirPath != null) {
74                  file = new File(new File(dirPath), filePath);
75              } else {
76                  file = new File(filePath);
77              }
78              file.getParentFile().mkdirs();
79              try {
80                  file.delete();
81                  file.createNewFile();
82                  String content = null;
83                  if (contents != null && contents.length > i) {
84                      content = contents[i];
85                  }
86                  if (content != null) {
87                      FileOutputStream stream = new FileOutputStream(file);
88                      stream.write(contents[i].getBytes(ENCODING));
89                      stream.close();
90                  }
91              } catch (IOException e) {
92              }
93          }
94      }
95  
96      private static void reset() {
97          removeRec(STORE);
98          removeRec(WORK);
99          new File(STORE).mkdirs();
100         new File(WORK).mkdirs();
101     }
102 
103     private static void createInitialFiles() {
104         createFiles(INITIAL_FILES);
105     }
106 
107     public static VirtualAdminCommandsFileResourceManager createFRM() {
108         return new VirtualAdminCommandsFileResourceManager(STORE, WORK, false, sLogger, true) {
109             public void setDirty(Object txId, Throwable t) {
110                 dirty = true;
111             }
112 
113         };
114     }
115 
116     public static Test suite() {
117         TestSuite suite = new TestSuite(FileResourceManagerVirtualAdminCommandsTest.class);
118         return suite;
119     }
120 
121     public static void main(java.lang.String[] args) {
122         junit.textui.TestRunner.run(suite());
123     }
124 
125     public FileResourceManagerVirtualAdminCommandsTest(String testName) {
126         super(testName);
127     }
128 
129     public void testRecover() throws Throwable {
130         sLogger.logInfo("Checking recover() admin command");
131         reset();
132         createInitialFiles();
133         VirtualAdminCommandsFileResourceManager frm = createFRM();
134         frm.setVirtualAdminPath(ADMIN_COMMAND_PREFIX);
135         InputStream is = frm.readResource(ADMIN_COMMAND_PREFIX + "recover");
136         BufferedReader bf = new BufferedReader(new InputStreamReader(is));
137         String line = bf.readLine();
138         assertEquals(
139                 line,
140                 "Command recover failed with the following message: Recovery is possible in started or starting resource manager only: System error");
141         frm.start();
142         frm.setDirty(null, null);
143         frm.startTransaction("newTx");
144         boolean failed = false;
145         try {
146         frm.commitTransaction("newTx");
147         } catch (ResourceManagerException rme) {
148             failed = true;
149         }
150         assertTrue(failed);
151         is = frm.readResource(ADMIN_COMMAND_PREFIX + "recover");
152         bf = new BufferedReader(new InputStreamReader(is));
153         line = bf.readLine();
154         assertEquals(
155                 line,
156                 "Command recover terminated successfully");
157         frm.startTransaction("newTx");
158         failed = false;
159         try {
160             frm.commitTransaction("newTx");
161             } catch (ResourceManagerException rme) {
162                 failed = true;
163             }
164             assertFalse(failed);
165     }
166 }