Commit 8435ad9b authored by Matija Obreza's avatar Matija Obreza
Browse files

Tests and refactoring for getClassPkId(String)

parent 88449b5c
...@@ -48,6 +48,6 @@ public interface ClassPKService { ...@@ -48,6 +48,6 @@ public interface ClassPKService {
* @param className * @param className
* @return * @return
*/ */
Long getClassPkId(String className) throws ClassNotFoundException; Long getClassPkId(String className);
} }
...@@ -89,25 +89,23 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -89,25 +89,23 @@ public class ClassPKServiceImpl implements ClassPKService {
} }
@Override @Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW) @Transactional
public Long getClassPkId(String classname) throws ClassNotFoundException { public Long getClassPkId(String classname) {
Long id = null; Long id = classPkRepository.getClassPkId(classname);
try {
id = classPkRepository.getClassPkId(classname); if (id == null) {
if (id == null) { try {
throw new Exception(); Class<?> aClass = Class.forName(classname);
return getClassPk(aClass).getId();
} catch (ClassNotFoundException e) {
return null;
} }
} catch (Exception e) { } else {
Class aClass = Class.forName(classname); return id;
ClassPK classPK = createNewClassPkForClass(aClass);
id = classPK.getId();
} }
return id;
} }
private ClassPK createNewClassPkForClass(Class<?> aClass) { private ClassPK createNewClassPkForClass(Class<?> aClass) {
ClassPK classPk = new ClassPK(); ClassPK classPk = new ClassPK();
classPk.setClassname(aClass.getName()); classPk.setClassname(aClass.getName());
classPk.setShortName(generateShortPkName(aClass.getSimpleName().toLowerCase())); classPk.setShortName(generateShortPkName(aClass.getSimpleName().toLowerCase()));
......
...@@ -48,4 +48,34 @@ public class ClassPKServiceTest extends ServiceTest { ...@@ -48,4 +48,34 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk2.getClassname(), equalTo(Long.class.getName())); assertThat(cpk2.getClassname(), equalTo(Long.class.getName()));
assertThat(cpk.getId(), equalTo(cpk2.getId())); assertThat(cpk.getId(), equalTo(cpk2.getId()));
} }
@Test
public void testGetClassPkId() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, equalTo(cpk.getId()));
}
@Test
public void testCreateClassPkId() {
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, not(nullValue()));
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(id, equalTo(cpk.getId()));
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
}
/**
* Test that classPk is not created for classnames that don't exist
*/
@Test
public void testNoCreateClassPkForCNFE() {
final Long id = classPkService.getClassPkId("this.class.does.not.Exist");
assertThat(id, nullValue());
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment