SAIL-232: AppliedPatch.hbm.xml

- Basic CREATE script for core tables: AlfrescoCreate-3.3-RepoTables.sql
 - Removed Hibernate control of alf_applied_patch


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@18101 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2010-01-18 16:39:18 +00:00
parent 7f24c8c4e7
commit b96d174e1f
26 changed files with 941 additions and 696 deletions

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch;
import java.util.Date;
/**
* Applied patch bean
*
* @author Derek Hulley
* @since 3.3
*/
public class AppliedPatch
{
private String id;
private String description;
private int fixesFromSchema;
private int fixesToSchema;
private int targetSchema;
private int appliedToSchema;
private String appliedToServer;
private Date appliedOnDate;
private boolean wasExecuted;
private boolean succeeded;
private String report;
/**
* Default constructor
*/
public AppliedPatch()
{
}
/**
* Construct an instance from another patch info-provider
*/
public AppliedPatch(AppliedPatch appliedPatch)
{
this.id = appliedPatch.getId();
this.description = appliedPatch.getDescription();
this.fixesFromSchema = appliedPatch.getFixesFromSchema();
this.fixesToSchema = appliedPatch.getFixesToSchema();
this.targetSchema = appliedPatch.getTargetSchema();
this.appliedToSchema = appliedPatch.getAppliedToSchema();
this.appliedToServer = appliedPatch.getAppliedToServer();
this.appliedOnDate = appliedPatch.getAppliedOnDate();
this.wasExecuted = appliedPatch.getWasExecuted();
this.succeeded = appliedPatch.getSucceeded();
this.report = appliedPatch.getReport();
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public int getFixesFromSchema()
{
return fixesFromSchema;
}
public void setFixesFromSchema(int fixesFromSchema)
{
this.fixesFromSchema = fixesFromSchema;
}
public int getFixesToSchema()
{
return fixesToSchema;
}
public void setFixesToSchema(int fixesToSchema)
{
this.fixesToSchema = fixesToSchema;
}
public int getTargetSchema()
{
return targetSchema;
}
public void setTargetSchema(int targetSchema)
{
this.targetSchema = targetSchema;
}
public int getAppliedToSchema()
{
return appliedToSchema;
}
public void setAppliedToSchema(int appliedToSchema)
{
this.appliedToSchema = appliedToSchema;
}
public String getAppliedToServer()
{
return appliedToServer;
}
public void setAppliedToServer(String appliedToServer)
{
this.appliedToServer = appliedToServer;
}
public Date getAppliedOnDate()
{
return appliedOnDate;
}
public void setAppliedOnDate(Date appliedOnDate)
{
this.appliedOnDate = appliedOnDate;
}
public boolean getWasExecuted()
{
return wasExecuted;
}
public void setWasExecuted(boolean wasExecuted)
{
this.wasExecuted = wasExecuted;
}
public boolean getSucceeded()
{
return succeeded;
}
public void setSucceeded(boolean succeeded)
{
this.succeeded = succeeded;
}
public String getReport()
{
return report;
}
public void setReport(String report)
{
this.report = report;
}
}

View File

@@ -1,91 +0,0 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch;
import java.util.Date;
import java.util.List;
import org.alfresco.repo.domain.AppliedPatch;
/**
* Provides data access support for patch persistence.
*
* @since 1.2
* @author Derek Hulley
*/
public interface PatchDaoService
{
/**
* Creates and saves a new instance of the patch. This will not have all the mandatory
* properties set - only the ID.
*
* @param id the unique key
* @return Returns a new instance that can be manipulated
*/
public AppliedPatch newAppliedPatch(String id);
/**
* Retrieve an existing patch
*
* @param id the patch unique ID
* @return Returns the patch instance or null if one has not been persisted
*/
public AppliedPatch getAppliedPatch(String id);
/**
* Detaches the given instance from the persistence engine. This will
* ensure that any changes made to the java object do not get persisted,
* allowing the objects to be passed out to external clients without any
* concern of their lifecycle.
*
* @param appliedPatch the object to detach from persistence
*/
public void detach(AppliedPatch appliedPatch);
/**
* Get a list of all applied patches
*
* @return Returns a list of all applied patches
*/
public List<AppliedPatch> getAppliedPatches();
/**
* Get a list of all patches applied between the given dates.
*
* @param from the lower date limit or null to ignore
* @param to the upper date limit or null to ignore
* @return Returns applied patches for the date range, but also patches without
* a date
*/
public List<AppliedPatch> getAppliedPatches(Date from, Date to);
/**
* Update the patch <i>applied on</i> date.
*
* @param id the patch ID
* @param appliedOnDate the date applied
*/
public void setAppliedOnDate(String id, Date appliedOnDate);
}

View File

@@ -90,7 +90,7 @@ public class PatchExecuter extends AbstractLifecycleBean
Date after = new Date(System .currentTimeMillis() + 20000L); // 20 seconds ahead
// get all the patches executed in the time
List<PatchInfo> appliedPatches = patchService.getPatches(before, after);
List<AppliedPatch> appliedPatches = patchService.getPatches(before, after);
// don't report anything if nothing was done
if (appliedPatches.size() == 0)
@@ -101,7 +101,7 @@ public class PatchExecuter extends AbstractLifecycleBean
{
boolean succeeded = true;
// list all patches applied, including failures
for (PatchInfo patchInfo : appliedPatches)
for (AppliedPatch patchInfo : appliedPatches)
{
if (!patchInfo.getWasExecuted())
{

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch;
import java.util.Date;
/**
* Data on applied patches
*
* @author Derek Hulley
*/
public interface PatchInfo
{
public String getId();
public String getDescription();
public int getFixesFromSchema();
public int getFixesToSchema();
public int getTargetSchema();
public int getAppliedToSchema();
public String getAppliedToServer();
public Date getAppliedOnDate();
public boolean getWasExecuted();
public boolean getSucceeded();
public String getReport();
}

View File

@@ -27,8 +27,6 @@ package org.alfresco.repo.admin.patch;
import java.util.Date;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
/**
* Manages patches applied against the repository.
* <p>
@@ -73,5 +71,5 @@ public interface PatchService
* the end date of the search, or null to g
* @return Returns all applied patches (successful or not)
*/
public List<PatchInfo> getPatches(Date fromDate, Date toDate);
public List<AppliedPatch> getPatches(Date fromDate, Date toDate);
}

View File

@@ -32,8 +32,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.extensions.surf.util.I18NUtil;
import org.alfresco.repo.domain.AppliedPatch;
import org.alfresco.repo.domain.patch.AppliedPatchDAO;
import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.admin.PatchException;
@@ -42,6 +41,7 @@ import org.alfresco.service.descriptor.Descriptor;
import org.alfresco.service.descriptor.DescriptorService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.surf.util.I18NUtil;
/**
@@ -68,7 +68,7 @@ public class PatchServiceImpl implements PatchService
private DescriptorService descriptorService;
private TransactionServiceImpl transactionService;
private RuleService ruleService;
private PatchDaoService patchDaoService;
private AppliedPatchDAO appliedPatchDAO;
private List<Patch> patches;
public PatchServiceImpl()
@@ -86,9 +86,9 @@ public class PatchServiceImpl implements PatchService
this.transactionService = transactionService;
}
public void setPatchDaoService(PatchDaoService patchDaoService)
public void setAppliedPatchDAO(AppliedPatchDAO appliedPatchDAO)
{
this.patchDaoService = patchDaoService;
this.appliedPatchDAO = appliedPatchDAO;
}
public void setRuleService(RuleService ruleService)
@@ -139,7 +139,7 @@ public class PatchServiceImpl implements PatchService
// construct a list of executed patches by ID (also check the date)
Map<String, AppliedPatch> appliedPatchesById = new HashMap<String, AppliedPatch>(23);
List<AppliedPatch> appliedPatches = patchDaoService.getAppliedPatches();
List<AppliedPatch> appliedPatches = appliedPatchDAO.getAppliedPatches();
for (final AppliedPatch appliedPatch : appliedPatches)
{
appliedPatchesById.put(appliedPatch.getId(), appliedPatch);
@@ -153,7 +153,8 @@ public class PatchServiceImpl implements PatchService
public Date execute() throws Throwable
{
Date now = new Date();
patchDaoService.setAppliedOnDate(appliedPatch.getId(), now);
appliedPatch.setAppliedOnDate(now);
appliedPatchDAO.updateAppliedPatch(appliedPatch);
return now;
}
};
@@ -255,7 +256,7 @@ public class PatchServiceImpl implements PatchService
" Patch: " + patch);
}
// get the patch from the DAO
AppliedPatch appliedPatch = patchDaoService.getAppliedPatch(patch.getId());
AppliedPatch appliedPatch = appliedPatchDAO.getAppliedPatch(patch.getId());
// We bypass the patch if it was executed successfully
if (appliedPatch != null && !forcePatch)
{
@@ -316,9 +317,17 @@ public class PatchServiceImpl implements PatchService
String server = (serverDescriptor.getVersion() + " - " + serverDescriptor.getEdition());
// create or update the record of execution
boolean create = true;
if (appliedPatch == null)
{
appliedPatch = patchDaoService.newAppliedPatch(patch.getId());
appliedPatch = new AppliedPatch();
appliedPatch.setId(patch.getId());
create = true;
}
else
{
// Update it
create = false;
}
// fill in the record's details
String patchDescription = I18NUtil.getMessage(patch.getDescription());
@@ -337,6 +346,15 @@ public class PatchServiceImpl implements PatchService
appliedPatch.setSucceeded(success); // whether or not the patch succeeded
appliedPatch.setWasExecuted(applies); // whether or not the patch was executed
appliedPatch.setReport(report); // additional, human-readable, status
// Update or create the entry
if (create)
{
appliedPatchDAO.createAppliedPatch(appliedPatch);
}
else
{
appliedPatchDAO.updateAppliedPatch(appliedPatch);
}
// done
if (logger.isDebugEnabled())
@@ -359,7 +377,7 @@ public class PatchServiceImpl implements PatchService
for (Patch alternative : alternatives)
{
// If the patch was executed, then this one was effectively executed
AppliedPatch appliedAlternative = patchDaoService.getAppliedPatch(alternative.getId());
AppliedPatch appliedAlternative = appliedPatchDAO.getAppliedPatch(alternative.getId());
if (appliedAlternative != null && appliedAlternative.getSucceeded())
{
return alternative.getId();
@@ -392,7 +410,7 @@ public class PatchServiceImpl implements PatchService
}
@SuppressWarnings("unchecked")
public List<PatchInfo> getPatches(Date fromDate, Date toDate)
public List<AppliedPatch> getPatches(Date fromDate, Date toDate)
{
if (fromDate == null)
{
@@ -402,14 +420,9 @@ public class PatchServiceImpl implements PatchService
{
toDate = INFINITE_DATE;
}
List<? extends PatchInfo> appliedPatches = patchDaoService.getAppliedPatches(fromDate, toDate);
// disconnect each of these
for (PatchInfo appliedPatch : appliedPatches)
{
patchDaoService.detach((AppliedPatch)appliedPatch);
}
List<? extends AppliedPatch> appliedPatches = appliedPatchDAO.getAppliedPatches(fromDate, toDate);
// done
return (List<PatchInfo>) appliedPatches;
return (List<AppliedPatch>) appliedPatches;
}
/**

View File

@@ -1,198 +1,188 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.domain.AppliedPatch;
import org.alfresco.repo.security.authentication.AuthenticationContext;
import org.alfresco.service.cmr.admin.PatchException;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @see org.alfresco.repo.admin.patch.Patch
* @see org.alfresco.repo.admin.patch.AbstractPatch
* @see org.alfresco.repo.admin.patch.PatchService
*
* @author Derek Hulley
*/
public class PatchTest extends TestCase
{
private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private NamespaceService namespaceService;
private NodeService nodeService;
private SearchService searchService;
private AuthenticationContext authenticationContext;
private PatchService patchService;
private PatchDaoService patchDaoComponent;
public PatchTest(String name)
{
super(name);
}
public void setUp() throws Exception
{
transactionService = (TransactionService) ctx.getBean("transactionComponent");
namespaceService = (NamespaceService) ctx.getBean("namespaceService");
nodeService = (NodeService) ctx.getBean("nodeService");
searchService = (SearchService) ctx.getBean("searchService");
authenticationContext = (AuthenticationContext) ctx.getBean("authenticationContext");
patchService = (PatchService) ctx.getBean("PatchService");
patchDaoComponent = (PatchDaoService) ctx.getBean("patchDaoComponent");
// get the patches to play with
patchService.registerPatch((Patch)ctx.getBean("patch.sample.02"));
patchService.registerPatch((Patch)ctx.getBean("patch.sample.01"));
}
public void testSetup() throws Exception
{
assertNotNull(transactionService);
assertNotNull(patchService);
assertNotNull(patchDaoComponent);
}
private SamplePatch constructSamplePatch(boolean mustFail)
{
SamplePatch patch = new SamplePatch(mustFail, transactionService);
patch.setNamespaceService(namespaceService);
patch.setNodeService(nodeService);
patch.setSearchService(searchService);
patch.setAuthenticationContext(authenticationContext);
// done
return patch;
}
public void testSimplePatchSuccess() throws Exception
{
Patch patch = constructSamplePatch(false);
String report = patch.apply();
// check that the report was generated
assertEquals("Patch report incorrect", SamplePatch.MSG_SUCCESS, report);
}
public void testPatchReapplication()
{
// successfully apply a patch
Patch patch = constructSamplePatch(false);
patch.apply();
// check that the patch cannot be reapplied
try
{
patch.apply();
fail("AbstractPatch failed to prevent reapplication");
}
catch (AlfrescoRuntimeException e)
{
// expected
}
// apply an unsuccessful patch
patch = constructSamplePatch(true);
try
{
patch.apply();
fail("Failed patch didn't throw PatchException");
}
catch (PatchException e)
{
// expected
}
// repeat
try
{
patch.apply();
fail("Reapplication of failed patch didn't throw PatchException");
}
catch (PatchException e)
{
// expected
}
}
public void testApplyOutstandingPatches() throws Exception
{
// apply outstanding patches
boolean success = patchService.applyOutstandingPatches();
assertTrue(success);
// get applied patches
List<AppliedPatch> appliedPatches = patchDaoComponent.getAppliedPatches();
// check that the patch application was recorded
boolean found01 = false;
boolean found02 = false;
for (AppliedPatch appliedPatch : appliedPatches)
{
if (appliedPatch.getId().equals("Sample01"))
{
found01 = true;
assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
}
else if (appliedPatch.getId().equals("Sample02"))
{
found02 = true;
assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
}
}
assertTrue("Sample 01 not in list of applied patches", found01);
assertTrue("Sample 02 not in list of applied patches", found02);
}
public void testGetPatchesByDate() throws Exception
{
// ensure that there are some applied patches
testApplyOutstandingPatches();
// get the number of applied patches
List<AppliedPatch> appliedPatches = patchDaoComponent.getAppliedPatches();
assertTrue("Expected at least 2 applied patches", appliedPatches.size() >= 2);
// now requery using null dates
List<PatchInfo> appliedPatchesAllDates = patchService.getPatches(null, null);
assertEquals("Applied patches by all dates doesn't match all applied patches",
appliedPatches.size(), appliedPatchesAllDates.size());
// make sure that the objects are not connected to the persistence layer
PatchInfo disconnectedObject = appliedPatchesAllDates.get(0);
AppliedPatch persistedObject = patchDaoComponent.getAppliedPatch(disconnectedObject.getId());
assertNotSame("Instances should not be shared between evicted and cached objects",
disconnectedObject, persistedObject);
// perform another query with dates that should return no results
List<PatchInfo> appliedPatchesFutureDates = patchService.getPatches(new Date(), new Date());
assertEquals("Query returned results for dates when no patches should exist", 0, appliedPatchesFutureDates.size());
}
}
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.security.authentication.AuthenticationContext;
import org.alfresco.service.cmr.admin.PatchException;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @see org.alfresco.repo.admin.patch.Patch
* @see org.alfresco.repo.admin.patch.AbstractPatch
* @see org.alfresco.repo.admin.patch.PatchService
*
* @author Derek Hulley
*/
public class PatchTest extends TestCase
{
private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private NamespaceService namespaceService;
private NodeService nodeService;
private SearchService searchService;
private AuthenticationContext authenticationContext;
private PatchService patchService;
public PatchTest(String name)
{
super(name);
}
public void setUp() throws Exception
{
transactionService = (TransactionService) ctx.getBean("transactionComponent");
namespaceService = (NamespaceService) ctx.getBean("namespaceService");
nodeService = (NodeService) ctx.getBean("nodeService");
searchService = (SearchService) ctx.getBean("searchService");
authenticationContext = (AuthenticationContext) ctx.getBean("authenticationContext");
patchService = (PatchService) ctx.getBean("PatchService");
// get the patches to play with
patchService.registerPatch((Patch)ctx.getBean("patch.sample.02"));
patchService.registerPatch((Patch)ctx.getBean("patch.sample.01"));
}
public void testSetup() throws Exception
{
assertNotNull(transactionService);
assertNotNull(patchService);
}
private SamplePatch constructSamplePatch(boolean mustFail)
{
SamplePatch patch = new SamplePatch(mustFail, transactionService);
patch.setNamespaceService(namespaceService);
patch.setNodeService(nodeService);
patch.setSearchService(searchService);
patch.setAuthenticationContext(authenticationContext);
// done
return patch;
}
public void testSimplePatchSuccess() throws Exception
{
Patch patch = constructSamplePatch(false);
String report = patch.apply();
// check that the report was generated
assertEquals("Patch report incorrect", SamplePatch.MSG_SUCCESS, report);
}
public void testPatchReapplication()
{
// successfully apply a patch
Patch patch = constructSamplePatch(false);
patch.apply();
// check that the patch cannot be reapplied
try
{
patch.apply();
fail("AbstractPatch failed to prevent reapplication");
}
catch (AlfrescoRuntimeException e)
{
// expected
}
// apply an unsuccessful patch
patch = constructSamplePatch(true);
try
{
patch.apply();
fail("Failed patch didn't throw PatchException");
}
catch (PatchException e)
{
// expected
}
// repeat
try
{
patch.apply();
fail("Reapplication of failed patch didn't throw PatchException");
}
catch (PatchException e)
{
// expected
}
}
public void testApplyOutstandingPatches() throws Exception
{
// apply outstanding patches
boolean success = patchService.applyOutstandingPatches();
assertTrue(success);
// get applied patches
List<AppliedPatch> appliedPatches = patchService.getPatches(null, null);
// check that the patch application was recorded
boolean found01 = false;
boolean found02 = false;
for (AppliedPatch appliedPatch : appliedPatches)
{
if (appliedPatch.getId().equals("Sample01"))
{
found01 = true;
assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
}
else if (appliedPatch.getId().equals("Sample02"))
{
found02 = true;
assertTrue("Patch info didn't indicate success: " + appliedPatch, appliedPatch.getSucceeded());
}
}
assertTrue("Sample 01 not in list of applied patches", found01);
assertTrue("Sample 02 not in list of applied patches", found02);
}
public void testGetPatchesByDate() throws Exception
{
// ensure that there are some applied patches
testApplyOutstandingPatches();
// get the number of applied patches
List<AppliedPatch> appliedPatches = patchService.getPatches(null, null);
assertTrue("Expected at least 2 applied patches", appliedPatches.size() >= 2);
// now requery using null dates
List<AppliedPatch> appliedPatchesAllDates = patchService.getPatches(null, null);
assertEquals("Applied patches by all dates doesn't match all applied patches",
appliedPatches.size(), appliedPatchesAllDates.size());
// perform another query with dates that should return no results
List<AppliedPatch> appliedPatchesFutureDates = patchService.getPatches(new Date(), new Date());
assertEquals("Query returned results for dates when no patches should exist", 0, appliedPatchesFutureDates.size());
}
}

View File

@@ -1,137 +0,0 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin.patch.hibernate;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.admin.patch.PatchDaoService;
import org.alfresco.repo.domain.AppliedPatch;
import org.alfresco.repo.domain.hibernate.AppliedPatchImpl;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Hibernate-specific implementation for managing patch persistence.
*
* @since 1.2
* @author Derek Hulley
*/
public class HibernatePatchDaoServiceImpl extends HibernateDaoSupport implements PatchDaoService
{
public static final String QUERY_GET_ALL_APPLIED_PATCHES = "patch.GetAllAppliedPatches";
public static final String QUERY_GET_APPLIED_PATCHES_BY_DATE = "patch.GetAppliedPatchesByDate";
public AppliedPatch newAppliedPatch(String id)
{
// check for existence
AppliedPatch existing = getAppliedPatch(id);
if (existing != null)
{
throw new AlfrescoRuntimeException("An applied patch already exists: \n" +
" id: " + id);
}
// construct a new one
AppliedPatchImpl patch = new AppliedPatchImpl();
patch.setId(id);
// save this in hibernate
getHibernateTemplate().save(patch);
// done
return patch;
}
public AppliedPatch getAppliedPatch(String id)
{
AppliedPatch patch = (AppliedPatch) getHibernateTemplate().get(AppliedPatchImpl.class, id);
// done
return patch;
}
public void detach(AppliedPatch appliedPatch)
{
getSession().evict(appliedPatch);
}
/**
* @see #QUERY_GET_ALL_APPLIED_PATCHES
*/
@SuppressWarnings("unchecked")
public List<AppliedPatch> getAppliedPatches()
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernatePatchDaoServiceImpl.QUERY_GET_ALL_APPLIED_PATCHES);
return query.list();
}
};
List<AppliedPatch> queryResults = (List) getHibernateTemplate().execute(callback);
// done
return queryResults;
}
/**
* @see #QUERY_GET_APPLIED_PATCHES_BY_DATE
*/
@SuppressWarnings("unchecked")
public List<AppliedPatch> getAppliedPatches(final Date fromDate, final Date toDate)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernatePatchDaoServiceImpl.QUERY_GET_ALL_APPLIED_PATCHES);
return query.list();
}
};
List<AppliedPatch> queryResults = (List) getHibernateTemplate().execute(callback);
// eliminate results that are out of range
Iterator<AppliedPatch> iterator = queryResults.iterator();
while (iterator.hasNext())
{
AppliedPatch appliedPatch = iterator.next();
Date appliedOnDate = appliedPatch.getAppliedOnDate();
if (appliedOnDate != null &&
(fromDate.compareTo(appliedOnDate) >= 0 || toDate.compareTo(appliedOnDate) <= 0))
{
// it is out of range
iterator.remove();
}
}
// done
return queryResults;
}
public void setAppliedOnDate(String id, Date appliedOnDate)
{
AppliedPatch patch = (AppliedPatch) getHibernateTemplate().get(AppliedPatchImpl.class, id);
patch.setAppliedOnDate(appliedOnDate);
}
}