You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2016/10/21 14:23:49 UTC

[3/5] shiro git commit: Updated Spring hibernate example to use hibernate 4

Updated Spring hibernate example to use hibernate 4

Updated Spring to latest 3.x (3.2.17) and Spring boot to 1.4.1
Added basic IT for hibernate example


Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/67d340ef
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/67d340ef
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/67d340ef

Branch: refs/heads/master
Commit: 67d340ef13c689cb0581c2e0bf90b21fbeae6eb0
Parents: 4e8feab
Author: Brian Demers <bd...@apache.org>
Authored: Thu Oct 20 11:14:25 2016 -0400
Committer: Brian Demers <bd...@apache.org>
Committed: Thu Oct 20 18:01:37 2016 -0400

----------------------------------------------------------------------
 pom.xml                                         | 11 +--
 samples/spring-hibernate/pom.xml                | 81 +++++++++-----------
 .../shiro/samples/sprhib/dao/HibernateDao.java  |  3 +-
 .../apache/shiro/samples/sprhib/model/Role.java |  2 +-
 .../main/webapp/WEB-INF/applicationContext.xml  | 22 ++----
 .../src/main/webapp/WEB-INF/jsp/login.jsp       |  2 +-
 .../src/main/webapp/WEB-INF/web.xml             |  2 +-
 .../shiro/samples/ContainerIntegrationIT.java   | 62 +++++++++++++++
 8 files changed, 117 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index d27c8d2..d466e22 100644
--- a/pom.xml
+++ b/pom.xml
@@ -91,8 +91,8 @@
              modules' OSGi metadata: -->
         <quartz.version>1.6.1</quartz.version>
         <slf4j.version>1.7.21</slf4j.version>
-        <spring.version>3.1.0.RELEASE</spring.version>
-        <spring-boot.version>1.4.0.RELEASE</spring-boot.version>
+        <spring.version>3.2.17.RELEASE</spring.version>
+        <spring-boot.version>1.4.1.RELEASE</spring-boot.version>
         <guice.version>3.0</guice.version>
 
         <!-- Test 3rd-party dependencies: -->
@@ -100,6 +100,7 @@
         <gmaven.version>1.3</gmaven.version>
         <groovy.version>2.4.7</groovy.version>
         <junit.version>4.12</junit.version>
+        <hibernate.version>4.2.21.Final</hibernate.version>
         <!-- so we can mock static methods in 3rd party libraries that sometimes don't use proper interfaces
              ahem, hazelcast, ahem... -->
         <powermock.version>1.6.5</powermock.version>
@@ -949,8 +950,8 @@
             <dependency>
                 <!-- Used for sample applications only - not required for the framework: -->
                 <groupId>org.hibernate</groupId>
-                <artifactId>hibernate</artifactId>
-                <version>3.2.6.ga</version>
+                <artifactId>hibernate-core</artifactId>
+                <version>${hibernate.version}</version>
                 <optional>true</optional>
                 <exclusions>
                     <exclusion>
@@ -1013,7 +1014,7 @@
             <dependency>
                 <groupId>org.hibernate</groupId>
                 <artifactId>hibernate-annotations</artifactId>
-                <version>3.2.1.ga</version>
+                <version>3.5.6-Final</version>
                 <optional>true</optional>
             </dependency>
             <dependency>

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/pom.xml
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/pom.xml b/samples/spring-hibernate/pom.xml
index 88e06dd..c8ec313 100644
--- a/samples/spring-hibernate/pom.xml
+++ b/samples/spring-hibernate/pom.xml
@@ -32,49 +32,6 @@
     <name>Apache Shiro :: Samples :: Spring-Hibernate</name>
     <packaging>war</packaging>
 
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.mortbay.jetty</groupId>
-                <artifactId>maven-jetty-plugin</artifactId>
-                <version>${jetty.version}</version>
-                <!-- <configuration>
-                    <contextPath>/shirosprhib</contextPath>
-                    <connectors>
-                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
-                            <port>9080</port>
-                            <maxIdleTime>60000</maxIdleTime>
-                        </connector>
-                    </connectors>
-                    <requestLog implementation="org.mortbay.jetty.NCSARequestLog">
-                        <filename>./target/yyyy_mm_dd.request.log</filename>
-                        <retainDays>90</retainDays>
-                        <append>true</append>
-                        <extended>false</extended>
-                        <logTimeZone>GMT</logTimeZone>
-                    </requestLog>
-                    <systemProperties>
-                        <systemProperty>
-                            <name>org.apache.commons.logging.Log</name>
-                            <value>org.apache.commons.logging.impl.SimpleLog</value>
-                        </systemProperty>
-                        <systemProperty>
-                            <name>java.util.logging.config.file</name>
-                            <value>./target/test-classes/logging.properties</value>
-                        </systemProperty>
-                    </systemProperties>
-                </configuration>
-                <dependencies>
-                    <dependency>
-                        <groupId>hsqldb</groupId>
-                        <artifactId>hsqldb</artifactId>
-                        <version>${hsqldb.version}</version>
-                    </dependency>
-                </dependencies> -->
-            </plugin>
-        </plugins>
-    </build>
-
     <dependencies>
         <dependency>
             <groupId>org.apache.shiro</groupId>
@@ -110,7 +67,7 @@
         </dependency>
         <dependency>
             <groupId>org.hibernate</groupId>
-            <artifactId>hibernate</artifactId>
+            <artifactId>hibernate-core</artifactId>
             <scope>compile</scope>
             <optional>false</optional>
         </dependency>
@@ -135,6 +92,12 @@
             <scope>runtime</scope>
         </dependency>
         <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-ehcache</artifactId>
+            <version>${hibernate.version}</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
         </dependency>
@@ -146,6 +109,36 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>net.sourceforge.htmlunit</groupId>
+            <artifactId>htmlunit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>apache-jsp</artifactId>
+            <version>${jetty.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>jstl</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>apache-jstl</artifactId>
+            <version>${jetty.version}</version>
+            <type>pom</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.shiro.integrationtests</groupId>
+            <artifactId>shiro-its-support</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/dao/HibernateDao.java
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/dao/HibernateDao.java b/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/dao/HibernateDao.java
index 3a91f7d..340bd2a 100644
--- a/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/dao/HibernateDao.java
+++ b/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/dao/HibernateDao.java
@@ -21,7 +21,6 @@ package org.apache.shiro.samples.sprhib.dao;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.orm.hibernate3.SessionFactoryUtils;
 
 /**
  * Convenience superclass for DAOs that contains annotations for injecting the session factory
@@ -37,7 +36,7 @@ public abstract class HibernateDao {
     }
 
     public Session getSession() {
-        return SessionFactoryUtils.getSession(this.sessionFactory, true);
+        return sessionFactory.getCurrentSession();
     }    
 
 }

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/model/Role.java
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/model/Role.java b/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/model/Role.java
index 9115239..4c40d31 100644
--- a/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/model/Role.java
+++ b/samples/spring-hibernate/src/main/java/org/apache/shiro/samples/sprhib/model/Role.java
@@ -81,7 +81,7 @@ public class Role {
         this.description = description;
     }
 
-    @CollectionOfElements
+    @ElementCollection(targetClass=String.class)
     @JoinTable(name="roles_permissions")
     @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
     public Set<String> getPermissions() {

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/main/webapp/WEB-INF/applicationContext.xml
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/main/webapp/WEB-INF/applicationContext.xml b/samples/spring-hibernate/src/main/webapp/WEB-INF/applicationContext.xml
index f852ec3..abcdb5e 100644
--- a/samples/spring-hibernate/src/main/webapp/WEB-INF/applicationContext.xml
+++ b/samples/spring-hibernate/src/main/webapp/WEB-INF/applicationContext.xml
@@ -51,32 +51,26 @@
     </bean>
 
     <!-- Hibernate SessionFactory -->
-    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
+    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"/>
-        <!-- Because we're using an in-memory database for demo purposes (which is lost every time the app
-             shuts down), we have to ensure that the HSQLDB DDL is run each time the app starts.  The
-             DDL is auto-generated based on the *.hbm.xml mapping definitions below. -->
-        <property name="schemaUpdate" value="true"/>
         <!-- Scan packages for JPA annotations -->
         <property name="packagesToScan" value="org.apache.shiro.samples.sprhib.model"/>
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                 <prop key="hibernate.jdbc.fetch_size">100</prop>
-                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
+
+                <!-- Because we're using an in-memory database for demo purposes (which is lost every time the app
+                     shuts down), we have to ensure that the HSQLDB DDL is run each time the app starts.  The
+                     DDL is auto-generated based on the *.hbm.xml mapping definitions below. -->
+                <prop key="hibernate.hbm2ddl.auto">update</prop>
+                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
             </props>
         </property>
-        <property name="eventListeners">
-            <map>
-                <entry key="merge">
-                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
-                </entry>
-            </map>
-        </property>
     </bean>
 
     <!-- Transaction support beans -->
-    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
+    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
 

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/main/webapp/WEB-INF/jsp/login.jsp
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/main/webapp/WEB-INF/jsp/login.jsp b/samples/spring-hibernate/src/main/webapp/WEB-INF/jsp/login.jsp
index 7c16c17..54e284e 100644
--- a/samples/spring-hibernate/src/main/webapp/WEB-INF/jsp/login.jsp
+++ b/samples/spring-hibernate/src/main/webapp/WEB-INF/jsp/login.jsp
@@ -29,7 +29,7 @@
         <div class="title">Apache Shiro Sample App - Login</div>
 
         <div class="content">
-            <form:form modelAttribute="loginCommand">
+            <form:form modelAttribute="loginCommand" name="loginForm">
 
                 <form:errors path="*" element="div" cssClass="errors"/>
 

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml b/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml
index 905bc54..3340478 100644
--- a/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml
+++ b/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml
@@ -56,7 +56,7 @@
  -  =================================================================== -->
     <filter>
         <filter-name>openSessionInViewFilter</filter-name>
-        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
+        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
     </filter>
 
     <!-- Shiro Filter is defined in the spring application context: -->

http://git-wip-us.apache.org/repos/asf/shiro/blob/67d340ef/samples/spring-hibernate/src/test/java/org/apache/shiro/samples/ContainerIntegrationIT.java
----------------------------------------------------------------------
diff --git a/samples/spring-hibernate/src/test/java/org/apache/shiro/samples/ContainerIntegrationIT.java b/samples/spring-hibernate/src/test/java/org/apache/shiro/samples/ContainerIntegrationIT.java
new file mode 100644
index 0000000..12c2247
--- /dev/null
+++ b/samples/spring-hibernate/src/test/java/org/apache/shiro/samples/ContainerIntegrationIT.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.shiro.samples;
+
+import com.gargoylesoftware.htmlunit.ElementNotFoundException;
+import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.HtmlForm;
+import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import org.apache.shiro.testing.web.AbstractContainerIT;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+
+public class ContainerIntegrationIT extends AbstractContainerIT {
+
+    protected final WebClient webClient = new WebClient();
+
+    @Before
+    public void logOut() throws IOException {
+        // Make sure we are logged out
+        final HtmlPage homePage = webClient.getPage(getBaseUri());
+        try {
+            homePage.getAnchorByHref("/s/logout").click();
+        }
+        catch (ElementNotFoundException e) {
+            //Ignore
+        }
+    }
+
+    @Test
+    public void logIn() throws FailingHttpStatusCodeException, IOException, InterruptedException {
+
+        HtmlPage page = webClient.getPage(getBaseUri() + "s/login");
+        HtmlForm form = page.getFormByName("loginForm");
+        form.<HtmlInput>getInputByName("username").setValueAttribute("admin");
+        form.<HtmlInput>getInputByName("password").setValueAttribute("admin");
+        page = form.<HtmlInput>getInputByValue("Login").click();
+        // This'll throw an expection if not logged in
+        page.getAnchorByHref("/s/logout");
+    }
+}