diff --git a/scm-test/pom.xml b/scm-test/pom.xml index 2184f993b0..6310ddb9b9 100644 --- a/scm-test/pom.xml +++ b/scm-test/pom.xml @@ -17,6 +17,12 @@ + + org.apache.geronimo.specs + geronimo-servlet_2.5_spec + 1.2 + + sonia.scm scm-core diff --git a/scm-test/src/main/java/sonia/scm/AbstractTestBase.java b/scm-test/src/main/java/sonia/scm/AbstractTestBase.java new file mode 100644 index 0000000000..d35ac129ee --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/AbstractTestBase.java @@ -0,0 +1,123 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.junit.After; +import org.junit.Before; + +import sonia.scm.util.IOUtil; +import sonia.scm.util.MockUtil; + +import static org.junit.Assert.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +import java.util.UUID; + +/** + * + * @author Sebastian Sdorra + */ +public class AbstractTestBase +{ + + /** + * Method description + * + * + * @throws Exception + */ + @After + public void tearDownTest() throws Exception + { + try + { + preTearDown(); + } + finally + { + IOUtil.delete(tempDirectory); + } + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @throws Exception + */ + @Before + public void setUpTest() throws Exception + { + tempDirectory = new File(System.getProperty("java.io.tmpdir"), + UUID.randomUUID().toString()); + assertTrue(tempDirectory.mkdirs()); + contextProvider = MockUtil.getSCMContextProvider(tempDirectory); + postSetUp(); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @throws Exception + */ + protected void postSetUp() throws Exception {} + + /** + * Method description + * + * + * @throws Exception + */ + protected void preTearDown() throws Exception {} + + ; + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + protected SCMContextProvider contextProvider; + + /** Field description */ + private File tempDirectory; +} diff --git a/scm-test/src/main/java/sonia/scm/ManagerTestBase.java b/scm-test/src/main/java/sonia/scm/ManagerTestBase.java index 3ade2d1831..516e44b1bf 100644 --- a/scm-test/src/main/java/sonia/scm/ManagerTestBase.java +++ b/scm-test/src/main/java/sonia/scm/ManagerTestBase.java @@ -63,7 +63,7 @@ import java.util.UUID; * @param */ public abstract class ManagerTestBase + E extends Exception> extends AbstractTestBase { /** @@ -78,72 +78,29 @@ public abstract class ManagerTestBase getAdminSecurityContextProvider() + @Override + protected void preTearDown() throws Exception { - User admin = new User("scmadmin", "SCM Admin", "scmadmin@scm.org"); - - admin.setAdmin(true); - - SecurityContext context = mock(SecurityContext.class); - - when(context.getUser()).thenReturn(admin); - - Provider scp = mock(Provider.class); - - when(scp.get()).thenReturn(context); - - return scp; + manager.close(); } //~--- fields --------------------------------------------------------------- /** Field description */ protected Manager manager; - - /** Field description */ - protected SCMContextProvider provider; - - /** Field description */ - protected File tempDirectory; } diff --git a/scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java b/scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java index cc3a755dd4..5b9c65f756 100644 --- a/scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java +++ b/scm-test/src/main/java/sonia/scm/repository/RepositoryManagerTestBase.java @@ -205,7 +205,7 @@ public abstract class RepositoryManagerTestBase @Test(expected = RepositoryException.class) public void testModifyNotExisting() throws RepositoryException, IOException { - manager.modify(getTestRepository()); + manager.modify(RepositoryTestData.createHeartOfGold()); } /** @@ -278,7 +278,8 @@ public abstract class RepositoryManagerTestBase private Repository createSecondTestRepository() throws RepositoryException, IOException { - return createRepository(getSecondTestRepository()); + return createRepository( + RepositoryTestData.createHappyVerticalPeopleTransporter()); } /** @@ -293,50 +294,6 @@ public abstract class RepositoryManagerTestBase private Repository createTestRepository() throws RepositoryException, IOException { - return createRepository(getTestRepository()); - } - - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - private Repository getSecondTestRepository() - { - Repository happyVerticalPeopleTransporter = new Repository(); - - happyVerticalPeopleTransporter.setType(DummyRepositoryHandler.TYPE_NAME); - happyVerticalPeopleTransporter.setContact( - "zaphod.beeblebrox@hitchhiker.com"); - happyVerticalPeopleTransporter.setName("happyVerticalPeopleTransporter"); - happyVerticalPeopleTransporter.setDescription( - "Happy Vertical People Transporter"); - happyVerticalPeopleTransporter.setUrl( - "http://hitchhiker.com/dummy/HeartOfGold"); - - return happyVerticalPeopleTransporter; - } - - /** - * Method description - * - * - * @return - */ - private Repository getTestRepository() - { - Repository heartOfGold = new Repository(); - - heartOfGold.setType(DummyRepositoryHandler.TYPE_NAME); - heartOfGold.setContact("zaphod.beeblebrox@hitchhiker.com"); - heartOfGold.setName("HeartOfGold"); - heartOfGold.setDescription( - "Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive"); - heartOfGold.setUrl("http://hitchhiker.com/dummy/HeartOfGold"); - - return heartOfGold; + return createRepository(RepositoryTestData.createHeartOfGold()); } } diff --git a/scm-test/src/main/java/sonia/scm/repository/RepositoryTestData.java b/scm-test/src/main/java/sonia/scm/repository/RepositoryTestData.java new file mode 100644 index 0000000000..12888a7814 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/repository/RepositoryTestData.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.repository; + +/** + * + * @author Sebastian Sdorra + */ +public class RepositoryTestData +{ + + /** + * Method description + * + * + * @return + */ + public static Repository createHappyVerticalPeopleTransporter() + { + Repository happyVerticalPeopleTransporter = new Repository(); + + happyVerticalPeopleTransporter.setType(DummyRepositoryHandler.TYPE_NAME); + happyVerticalPeopleTransporter.setContact( + "zaphod.beeblebrox@hitchhiker.com"); + happyVerticalPeopleTransporter.setName("happyVerticalPeopleTransporter"); + happyVerticalPeopleTransporter.setDescription( + "Happy Vertical People Transporter"); + happyVerticalPeopleTransporter.setUrl( + "http://hitchhiker.com/dummy/HeartOfGold"); + + return happyVerticalPeopleTransporter; + } + + /** + * Method description + * + * + * @return + */ + public static Repository createHeartOfGold() + { + Repository heartOfGold = new Repository(); + + heartOfGold.setType(DummyRepositoryHandler.TYPE_NAME); + heartOfGold.setContact("zaphod.beeblebrox@hitchhiker.com"); + heartOfGold.setName("HeartOfGold"); + heartOfGold.setDescription( + "Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive"); + heartOfGold.setUrl("http://hitchhiker.com/dummy/HeartOfGold"); + + return heartOfGold; + } +} diff --git a/scm-test/src/main/java/sonia/scm/store/MemoryStore.java b/scm-test/src/main/java/sonia/scm/store/MemoryStore.java new file mode 100644 index 0000000000..c93ab0748d --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/store/MemoryStore.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.store; + +/** + * + * @author Sebastian Sdorra + * + * @param + */ +public class MemoryStore implements Store +{ + + /** + * Method description + * + * + * @return + */ + @Override + public T get() + { + return object; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param obejct + */ + @Override + public void set(T obejct) + { + this.object = obejct; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private T object; +} diff --git a/scm-test/src/main/java/sonia/scm/store/MemoryStoreFactory.java b/scm-test/src/main/java/sonia/scm/store/MemoryStoreFactory.java new file mode 100644 index 0000000000..3a00b70375 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/store/MemoryStoreFactory.java @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.store; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.SCMContextProvider; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +/** + * + * @author Sebastian Sdorra + */ +public class MemoryStoreFactory implements StoreFactory +{ + + /** + * Method description + * + * + * @throws IOException + */ + @Override + public void close() throws IOException + { + + // do nothing + } + + /** + * Method description + * + * + * @param context + */ + @Override + public void init(SCMContextProvider context) + { + + // do nothing + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param type + * @param name + * @param + * + * @return + */ + @Override + public Store getStore(Class type, String name) + { + return new MemoryStore(); + } +} diff --git a/scm-test/src/main/java/sonia/scm/user/UserManagerTestBase.java b/scm-test/src/main/java/sonia/scm/user/UserManagerTestBase.java index 1d05b6c8cb..f3594e2987 100644 --- a/scm-test/src/main/java/sonia/scm/user/UserManagerTestBase.java +++ b/scm-test/src/main/java/sonia/scm/user/UserManagerTestBase.java @@ -74,7 +74,7 @@ public abstract class UserManagerTestBase @Test public void testCreate() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); @@ -94,12 +94,12 @@ public abstract class UserManagerTestBase @Test(expected = UserAllreadyExistException.class) public void testCreateExisting() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); - User sameUser = getTestUser(); + User sameUser = UserTestData.createZaphod(); manager.create(sameUser); } @@ -114,7 +114,7 @@ public abstract class UserManagerTestBase @Test public void testDelete() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); @@ -132,7 +132,7 @@ public abstract class UserManagerTestBase @Test public void testGet() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); @@ -154,13 +154,12 @@ public abstract class UserManagerTestBase @Test public void testGetAll() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); - User trillian = new User("trillian", "Tricia McMillan", - "tricia.mcmillan@hitchhiker.com"); + User trillian = UserTestData.createTrillian(); manager.create(trillian); assertNotNull(manager.get("trillian")); @@ -228,7 +227,7 @@ public abstract class UserManagerTestBase @Test public void testModify() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); @@ -251,7 +250,7 @@ public abstract class UserManagerTestBase @Test(expected = UserException.class) public void testModifyNotExisting() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.modify(zaphod); } @@ -310,7 +309,7 @@ public abstract class UserManagerTestBase @Test public void testRefresh() throws UserException, IOException { - User zaphod = getTestUser(); + User zaphod = UserTestData.createZaphod(); manager.create(zaphod); assertNotNull(manager.get("zaphod")); @@ -334,20 +333,6 @@ public abstract class UserManagerTestBase assertEquals(user.getPassword(), otherUser.getPassword()); } - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @return - */ - private User getTestUser() - { - return new User("zaphod", "Zaphod Beeblebrox", - "zaphod.beeblebrox@hitchhiker.com"); - } - //~--- inner classes -------------------------------------------------------- /** diff --git a/scm-test/src/main/java/sonia/scm/user/UserTestData.java b/scm-test/src/main/java/sonia/scm/user/UserTestData.java new file mode 100644 index 0000000000..b7008e7746 --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/user/UserTestData.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.user; + +/** + * + * @author Sebastian Sdorra + */ +public class UserTestData +{ + + /** + * Method description + * + * + * @return + */ + public static User createAdams() + { + return new User("adams", "Douglas Adams", "douglas.adams@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createDent() + { + return new User("dent", "Arthur Dent", "arthur.dent@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createMarvin() + { + return new User("marvin", "Marvin", "paranoid.android@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createPerfect() + { + return new User("perfect", "Ford Prefect", "ford.perfect@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createSlarti() + { + return new User("slarti", "Slartibartfaß", "slartibartfass@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createTrillian() + { + return new User("trillian", "Tricia McMillan", + "tricia.mcmillan@hitchhiker.com"); + } + + /** + * Method description + * + * + * @return + */ + public static User createZaphod() + { + return new User("zaphod", "Zaphod Beeblebrox", + "zaphod.beeblebrox@hitchhiker.com"); + } +} diff --git a/scm-test/src/main/java/sonia/scm/util/MockUtil.java b/scm-test/src/main/java/sonia/scm/util/MockUtil.java new file mode 100644 index 0000000000..de93a4a3ce --- /dev/null +++ b/scm-test/src/main/java/sonia/scm/util/MockUtil.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.util; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Provider; + +import sonia.scm.SCMContextProvider; +import sonia.scm.security.SecurityContext; +import sonia.scm.user.User; + +import static org.mockito.Mockito.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Sebastian Sdorra + */ +public class MockUtil +{ + + /** + * Method description + * + * + * @return + */ + public static Provider getAdminSecurityContextProvider() + { + User admin = new User("scmadmin", "SCM Admin", "scmadmin@scm.org"); + + admin.setAdmin(true); + + SecurityContext context = mock(SecurityContext.class); + + when(context.getUser()).thenReturn(admin); + + Provider scp = mock(Provider.class); + + when(scp.get()).thenReturn(context); + + return scp; + } + + /** + * Method description + * + * + * @return + */ + public static HttpServletRequest getHttpServletRequest() + { + HttpServletRequest request = mock(HttpServletRequest.class); + + when(request.getContextPath()).thenReturn("/scm-webapp"); + + return request; + } + + /** + * Method description + * + * + * @return + */ + public static HttpServletResponse getHttpServletResponse() + { + return mock(HttpServletResponse.class); + } + + /** + * Method description + * + * + * @param directory + * + * @return + */ + public static SCMContextProvider getSCMContextProvider(File directory) + { + SCMContextProvider provider = mock(SCMContextProvider.class); + + when(provider.getBaseDirectory()).thenReturn(directory); + + return provider; + } +} diff --git a/scm-webapp/src/test/java/sonia/scm/repository/XmlRepositoryManagerTest.java b/scm-webapp/src/test/java/sonia/scm/repository/XmlRepositoryManagerTest.java index 48662341fc..98046267fe 100644 --- a/scm-webapp/src/test/java/sonia/scm/repository/XmlRepositoryManagerTest.java +++ b/scm-webapp/src/test/java/sonia/scm/repository/XmlRepositoryManagerTest.java @@ -36,6 +36,7 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- import sonia.scm.Manager; +import sonia.scm.util.MockUtil; import sonia.scm.repository.xml.XmlRepositoryManager; import sonia.scm.store.JAXBStoreFactory; import sonia.scm.store.StoreFactory; @@ -64,10 +65,10 @@ public class XmlRepositoryManagerTest extends RepositoryManagerTestBase Set handlerSet = new HashSet(); StoreFactory factory = new JAXBStoreFactory(); - factory.init(provider); + factory.init(contextProvider); handlerSet.add(new DummyRepositoryHandler(factory)); - return new XmlRepositoryManager(getAdminSecurityContextProvider(), factory, - handlerSet); + return new XmlRepositoryManager(MockUtil.getAdminSecurityContextProvider(), + factory, handlerSet); } } diff --git a/scm-webapp/src/test/java/sonia/scm/user/XmlUserManagerTest.java b/scm-webapp/src/test/java/sonia/scm/user/XmlUserManagerTest.java index 70414d7688..9873ab947b 100644 --- a/scm-webapp/src/test/java/sonia/scm/user/XmlUserManagerTest.java +++ b/scm-webapp/src/test/java/sonia/scm/user/XmlUserManagerTest.java @@ -35,6 +35,7 @@ package sonia.scm.user; //~--- non-JDK imports -------------------------------------------------------- +import sonia.scm.util.MockUtil; import sonia.scm.store.JAXBStoreFactory; import sonia.scm.store.StoreFactory; import sonia.scm.user.xml.XmlUserManager; @@ -57,8 +58,9 @@ public class XmlUserManagerTest extends UserManagerTestBase { StoreFactory factory = new JAXBStoreFactory(); - factory.init(provider); + factory.init(contextProvider); - return new XmlUserManager(getAdminSecurityContextProvider(), factory); + return new XmlUserManager(MockUtil.getAdminSecurityContextProvider(), + factory); } }