001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.transaction.file; 018 019import java.io.BufferedReader; 020import java.io.File; 021import java.io.FileOutputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.InputStreamReader; 025 026import junit.framework.Test; 027import junit.framework.TestCase; 028import junit.framework.TestSuite; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.apache.commons.transaction.util.CommonsLoggingLogger; 033import org.apache.commons.transaction.util.FileHelper; 034import org.apache.commons.transaction.util.LoggerFacade; 035 036/** 037 * Tests for FileResourceManager. 038 * 039 * @version $Id: FileResourceManagerTest.java 493628 2007-01-07 01:42:48Z joerg $ 040 */ 041public class FileResourceManagerVirtualAdminCommandsTest extends TestCase { 042 043 private static final Log log = LogFactory 044 .getLog(FileResourceManagerVirtualAdminCommandsTest.class.getName()); 045 046 private static final LoggerFacade sLogger = new CommonsLoggingLogger(log); 047 048 private static final String ADMIN_COMMAND_PREFIX = "/extremelyUnlikelyPrefixForAdminCommands"; 049 050 private static final String STORE = "tmp/store"; 051 052 private static final String WORK = "tmp/work"; 053 054 private static final String ENCODING = "ISO-8859-15"; 055 056 private static final String[] INITIAL_FILES = new String[] { STORE + "/olli/Hubert6", 057 STORE + "/olli/Hubert" }; 058 059 protected static final long TIMEOUT = Long.MAX_VALUE; 060 061 private static void removeRec(String dirPath) { 062 FileHelper.removeRec(new File(dirPath)); 063 } 064 065 private static final void createFiles(String[] filePaths) { 066 createFiles(filePaths, null, null); 067 } 068 069 private static final void createFiles(String[] filePaths, String[] contents, String dirPath) { 070 for (int i = 0; i < filePaths.length; i++) { 071 String filePath = filePaths[i]; 072 File file; 073 if (dirPath != null) { 074 file = new File(new File(dirPath), filePath); 075 } else { 076 file = new File(filePath); 077 } 078 file.getParentFile().mkdirs(); 079 try { 080 file.delete(); 081 file.createNewFile(); 082 String content = null; 083 if (contents != null && contents.length > i) { 084 content = contents[i]; 085 } 086 if (content != null) { 087 FileOutputStream stream = new FileOutputStream(file); 088 stream.write(contents[i].getBytes(ENCODING)); 089 stream.close(); 090 } 091 } catch (IOException e) { 092 } 093 } 094 } 095 096 private static void reset() { 097 removeRec(STORE); 098 removeRec(WORK); 099 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}