Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPR-9541: Provide PersistenceExceptionTranslator for EclipseLink exceptions #102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;

/**
* {@link PersistenceExceptionTranslator} capable of translating {@link EclipseLinkException}
* instances to Spring's {@link DataAccessException} hierarchy.
*
* @author Jan Stamer
* @since 3.2
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
*/
public class EclipseLinkExceptionTranslator implements PersistenceExceptionTranslator {

public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof EclipseLinkException) {
return convertEclipseLinkAccessException((EclipseLinkException) ex);
}
return null;
}

/**
* Convert the given EclipseLinkException to an appropriate exception from
* the {@code org.springframework.dao} hierarchy.
* @param ex EclipseLinkException that occurred
* @return a corresponding DataAccessException
* @see SessionFactoryUtils#convertEclipseLinkAccessException
*/
protected DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) {
return EclipseLinkUtils.convertEclipseLinkAccessException(ex);
}

}
@@ -0,0 +1,42 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.UncategorizedDataAccessException;

/**
* EclipseLink-specific subclass of UncategorizedDataAccessException, for
* EclipseLink system errors that do not match any concrete
* <code>org.springframework.dao</code> exceptions.
*
* @author Jan Stamer
* @since 3.2
* @see EclipseLinkUtils#convertEclipseLinkAccessException(EclipseLinkException)
*/
public class EclipseLinkSystemException extends UncategorizedDataAccessException {

/**
* Create a new HibernateSystemException, wrapping an arbitrary
* HibernateException.
* @param cause the HibernateException thrown
*/
public EclipseLinkSystemException(EclipseLinkException cause) {
super(cause != null ? cause.getMessage() : null, cause);
}

}
@@ -0,0 +1,42 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.springframework.dao.DataAccessException;

/**
* Helper class featuring methods for Eclipse Link. Also provides support for
* exception translation.
*
* @author Jan Stamer
* @since 3.2
*/
public abstract class EclipseLinkUtils {

/**
* Convert the given EclipseLinkException to an appropriate exception from
* the <code>org.springframework.dao</code> hierarchy.
* @param ex EclipseLinkException that occured
* @return the corresponding DataAccessException instance
* @see EclipseLinkExceptionTranslator#convertEclipseLinkAccessException(EclipseLinkException)
*/
public static DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) {
return new EclipseLinkSystemException(ex);
}

}
@@ -0,0 +1,41 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import junit.framework.TestCase;

import org.eclipse.persistence.exceptions.DatabaseException;
import org.springframework.dao.DataAccessException;

/**
* @author Jan Stamer
* @since 3.2
*/
public class EclipseLinkExceptionTranslatorTests extends TestCase {

public void testWithWrongException() {
EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator();
assertNull(exceptionTranslator.translateExceptionIfPossible(new IllegalArgumentException()));
}

public void testWithEclipseLinkException() {
EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator();
assertNotNull(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected()));
assertTrue(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException);
}

}
@@ -0,0 +1,52 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import junit.framework.TestCase;

import org.eclipse.persistence.exceptions.DatabaseException;
import org.hibernate.HibernateException;

/**
* @author Jan Stamer
* @since 3.2
*/
public class EclipseLinkSytemExceptionTests extends TestCase {

public void testWithNull() {
EclipseLinkSystemException exception = new EclipseLinkSystemException(null);
assertNull(exception.getCause());
assertNull(exception.getMessage());
}

public void testCreateWithCause() {
DatabaseException dbExceptionWithCause = new DatabaseException("my custom exception cause") {
};
EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause);
assertEquals(dbExceptionWithCause, elSystemException.getCause());
assertTrue(elSystemException.getMessage().contains("my custom exception cause"));
}

public void testCreateWithNullCause() throws HibernateException {
DatabaseException dbExceptionWithCause = new DatabaseException((String) null) {
};
EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause);
assertEquals(dbExceptionWithCause, elSystemException.getCause());
assertTrue(elSystemException.getMessage().contains("null"));
}

}
@@ -0,0 +1,38 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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.springframework.orm.eclipselink;

import junit.framework.TestCase;

import org.eclipse.persistence.exceptions.DatabaseException;
import org.springframework.dao.DataAccessException;

/**
* @author Jan Stamer
* @since 3.2
*/
public class EclipseLinkUtilsTests extends TestCase {

public void testWithNull() {
assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(null) instanceof DataAccessException);
}

public void testWithEclipseLinkException() {
assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException);
}

}