/gator/tags/gator-1.2.1/gator-core/src/test/java/edu/vt/middleware/gator/validation/UniqueNameValidatorTest.java

http://vt-middleware.googlecode.com/ · Java · 164 lines · 115 code · 21 blank · 28 comment · 0 complexity · 8235efd75f9d280c533f5ee328ce3d88 MD5 · raw file

  1. /*
  2. $Id: UniqueNameValidatorTest.java 1421 2010-06-25 16:43:59Z dfisher $
  3. Copyright (C) 2009-2010 Virginia Tech.
  4. All rights reserved.
  5. SEE LICENSE FOR MORE INFORMATION
  6. Author: Middleware Services
  7. Email: middleware@vt.edu
  8. Version: $Revision: 1421 $
  9. Updated: $Date: 2010-06-25 18:43:59 +0200 (Fri, 25 Jun 2010) $
  10. */
  11. package edu.vt.middleware.gator.validation;
  12. import edu.vt.middleware.gator.AppenderConfig;
  13. import edu.vt.middleware.gator.CategoryConfig;
  14. import edu.vt.middleware.gator.ClientConfig;
  15. import edu.vt.middleware.gator.Config;
  16. import edu.vt.middleware.gator.ConfigManager;
  17. import edu.vt.middleware.gator.PermissionConfig;
  18. import edu.vt.middleware.gator.ProjectConfig;
  19. import edu.vt.middleware.gator.UnitTestHelper;
  20. import org.junit.Assert;
  21. import org.junit.Test;
  22. import org.junit.runner.RunWith;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.test.context.ContextConfiguration;
  25. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  26. /**
  27. * Unit test for {@link UniqueNameValidator} classs.
  28. *
  29. * @author Middleware Services
  30. * @version $Revision: 1421 $
  31. */
  32. @RunWith(SpringJUnit4ClassRunner.class)
  33. @ContextConfiguration(locations = {"/test-applicationContext.xml"})
  34. public class UniqueNameValidatorTest
  35. {
  36. @Autowired
  37. private ConfigManager manager;
  38. /**
  39. * Test method for {@link UniqueNameValidator#isValid(Config,
  40. * ConstraintValidatorContext)}.
  41. */
  42. @Test
  43. public void testIsValid()
  44. {
  45. final ProjectConfig project1 = UnitTestHelper.createProject(
  46. "p1",
  47. "a1",
  48. "a2",
  49. "client1",
  50. "client2",
  51. "cat1",
  52. "cat2");
  53. project1.addPermission(
  54. new PermissionConfig("a", PermissionConfig.parsePermissions("rwd")));
  55. project1.addPermission(
  56. new PermissionConfig("b", PermissionConfig.parsePermissions("rwd")));
  57. final ProjectConfig project2 = UnitTestHelper.createProject(
  58. "p2",
  59. "a1",
  60. "a2",
  61. "client1",
  62. "client2",
  63. "cat1",
  64. "cat2");
  65. project2.addPermission(
  66. new PermissionConfig("a", PermissionConfig.parsePermissions("rwd")));
  67. project2.addPermission(
  68. new PermissionConfig("b", PermissionConfig.parsePermissions("rwd")));
  69. manager.save(project1);
  70. manager.save(project2);
  71. // Project name validation
  72. final ProjectConfig goodProject = UnitTestHelper.createProject(
  73. "good",
  74. "a1",
  75. "a2",
  76. "client1",
  77. "client2",
  78. "cat1",
  79. "cat2");
  80. Assert.assertTrue(validate(goodProject));
  81. final ProjectConfig dupeProject = UnitTestHelper.createProject(
  82. "p2",
  83. "a1",
  84. "a2",
  85. "client1",
  86. "client2",
  87. "cat1",
  88. "cat2");
  89. Assert.assertFalse(validate(dupeProject));
  90. project2.setName("p1");
  91. Assert.assertFalse(validate(project2));
  92. // Appender name validation
  93. final AppenderConfig goodAppender = UnitTestHelper.createAppender("foo");
  94. goodAppender.setProject(project1);
  95. Assert.assertTrue(validate(goodAppender));
  96. final AppenderConfig dupeAppender = UnitTestHelper.createAppender("a2");
  97. dupeAppender.setProject(project1);
  98. Assert.assertFalse(validate(dupeAppender));
  99. final AppenderConfig existingAppender = project1.getAppender("a1");
  100. existingAppender.setName("a2");
  101. Assert.assertFalse(validate(existingAppender));
  102. // Category name validation
  103. final CategoryConfig goodCategory = UnitTestHelper.createCategory("foo");
  104. goodCategory.setProject(project1);
  105. Assert.assertTrue(validate(goodCategory));
  106. final CategoryConfig dupeCategory = UnitTestHelper.createCategory("cat2");
  107. dupeCategory.setProject(project1);
  108. Assert.assertFalse(validate(dupeCategory));
  109. final CategoryConfig existingCategory = project1.getCategory("cat1");
  110. existingCategory.setName("cat2");
  111. Assert.assertFalse(validate(existingCategory));
  112. // Client name validation
  113. final ClientConfig goodClient = UnitTestHelper.createClient("foo");
  114. goodClient.setProject(project1);
  115. Assert.assertTrue(validate(goodClient));
  116. final ClientConfig dupeClient = UnitTestHelper.createClient("client2");
  117. dupeClient.setProject(project1);
  118. Assert.assertFalse(validate(dupeClient));
  119. final ClientConfig existingClient = project1.getClient("client1");
  120. existingClient.setName("client2");
  121. Assert.assertFalse(validate(existingClient));
  122. // Permission name validation
  123. final PermissionConfig goodPerm = new PermissionConfig("foo", 1);
  124. goodPerm.setProject(project1);
  125. Assert.assertTrue(validate(goodPerm));
  126. final PermissionConfig dupePerm = new PermissionConfig("a", 1);
  127. dupePerm.setProject(project1);
  128. Assert.assertFalse(validate(dupePerm));
  129. final PermissionConfig existingPerm = project1.getPermission("a");
  130. existingPerm.setName("b");
  131. Assert.assertFalse(validate(existingPerm));
  132. }
  133. private boolean validate(Config c)
  134. {
  135. final UniqueNameValidator validator = new UniqueNameValidator();
  136. validator.setConfigManager(manager);
  137. validator.initialize(c.getClass().getAnnotation(UniqueName.class));
  138. return validator.isValid(c, null);
  139. }
  140. }