]> source.dussan.org Git - archiva.git/blob
e8f2817411e3986eef10ef5b2ca0f3f3e0775480
[archiva.git] /
1 package org.apache.archiva.redback.integration.mail;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import junit.framework.TestCase;
23 import net.sf.ehcache.CacheManager;
24 import org.apache.archiva.redback.integration.mail.MailGenerator;
25 import org.apache.archiva.redback.keys.AuthenticationKey;
26 import org.apache.archiva.redback.keys.KeyManager;
27 import org.apache.archiva.redback.policy.UserSecurityPolicy;
28 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
29 import org.apache.archiva.redback.keys.KeyManagerException;
30 import org.jpox.SchemaTool;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import javax.jdo.PersistenceManager;
42 import javax.jdo.PersistenceManagerFactory;
43 import java.net.URL;
44 import java.util.Map.Entry;
45 import java.util.Properties;
46
47 /**
48  * Test the Mailer class.
49  */
50 @RunWith( SpringJUnit4ClassRunner.class )
51 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
52 public class MailGeneratorTest
53     extends TestCase
54 {
55     @Inject
56     @Named(value = "mailGenerator#velocity")
57     private MailGenerator generator;
58
59     @Inject
60     @Named(value = "mailGenerator#custom-url")
61     private MailGenerator customGenerator;
62
63     @Inject
64     @Named(value = "userSecurityPolicy")
65     private UserSecurityPolicy policy;
66
67     @Inject
68     @Named(value = "keyManager#memory")
69     private KeyManager keyManager;
70
71     @Inject
72     @Named(value = "jdoFactory#users")
73     DefaultConfigurableJdoFactory jdoFactory;
74
75     private Logger log = LoggerFactory.getLogger( getClass() );
76
77     @Before
78     public void setUp()
79         throws Exception
80     {
81         CacheManager.getInstance().clearAll();
82         super.setUp();
83
84         jdoFactory.setPassword( "" );
85
86         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
87
88         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
89
90         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); //$NON-NLS-1$ //$NON-NLS-2$
91         
92         Properties properties = jdoFactory.getProperties();
93
94         for ( Entry<Object, Object> entry : properties.entrySet() )
95         {
96             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
97         }
98
99         SchemaTool.createSchemaTables( new URL[] { getClass()
100             .getResource( "/org/codehaus/plexus/redback/keys/jdo/package.jdo" ) }, new URL[] {}, null, false, null ); //$NON-NLS-1$
101
102         log.info( "jdoFactory driverName {} " , jdoFactory.getDriverName() );
103
104         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
105
106         assertNotNull( pmf );
107
108         PersistenceManager pm = pmf.getPersistenceManager();
109
110         pm.close();        
111         
112     }
113
114     @Test
115     public void testGeneratePasswordResetMail()
116         throws KeyManagerException
117     {
118         AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
119                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
120
121         String content = generator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
122
123         assertNotNull( content );
124         assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
125     }
126
127     @Test
128     public void testGenerateAccountValidationMail()
129         throws KeyManagerException
130     {
131         AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
132                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
133
134         String content = generator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
135
136         assertNotNull( content );
137         assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
138     }
139
140     @Test
141     public void testGenerateAccountValidationMailCustomUrl()
142         throws Exception
143     {
144         AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
145                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
146
147         String content = customGenerator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
148
149         assertNotNull( content );
150         assertTrue( content.indexOf( "baseUrl" ) == -1 ); // make sure everything is properly populate
151         assertTrue( content.indexOf( "MY_APPLICATION_URL/security" ) > 0 ); // make sure everything is properly populate
152     }
153
154     @Test
155     public void testGeneratePasswordResetMailCustomUrl()
156         throws Exception
157     {
158         AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
159                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
160
161         String content = customGenerator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
162
163         assertNotNull( content );
164         
165         log.info( "mail content " + content );
166         
167         assertTrue( content.indexOf( "baseUrl" ) == -1 ); // make sure everything is properly populate
168         assertTrue( content.indexOf( "MY_APPLICATION_URL/security" ) > 0 ); // make sure everything is properly populate
169     }
170 }