]> source.dussan.org Git - archiva.git/blob
d5a3f765e7b2f24abc26899265d4c6b2976ee2fe
[archiva.git] /
1 package org.codehaus.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.keys.AuthenticationKey;
25 import org.apache.archiva.redback.keys.KeyManager;
26 import org.apache.archiva.redback.policy.UserSecurityPolicy;
27 import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory;
28 import org.apache.archiva.redback.keys.KeyManagerException;
29 import org.jpox.SchemaTool;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import javax.jdo.PersistenceManager;
41 import javax.jdo.PersistenceManagerFactory;
42 import java.net.URL;
43 import java.util.Map.Entry;
44 import java.util.Properties;
45
46 /**
47  * Test the Mailer class.
48  */
49 @RunWith( SpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
51 public class MailGeneratorTest
52     extends TestCase
53 {
54     @Inject
55     @Named(value = "mailGenerator#velocity")
56     private MailGenerator generator;
57
58     @Inject
59     @Named(value = "mailGenerator#custom-url")
60     private MailGenerator customGenerator;
61
62     @Inject
63     @Named(value = "userSecurityPolicy")
64     private UserSecurityPolicy policy;
65
66     @Inject
67     @Named(value = "keyManager#memory")
68     private KeyManager keyManager;
69
70     @Inject
71     @Named(value = "jdoFactory#users")
72     DefaultConfigurableJdoFactory jdoFactory;
73
74     private Logger log = LoggerFactory.getLogger( getClass() );
75
76     @Before
77     public void setUp()
78         throws Exception
79     {
80         CacheManager.getInstance().clearAll();
81         super.setUp();
82
83         jdoFactory.setPassword( "" );
84
85         jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
86
87         jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
88
89         jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); //$NON-NLS-1$ //$NON-NLS-2$
90         
91         Properties properties = jdoFactory.getProperties();
92
93         for ( Entry<Object, Object> entry : properties.entrySet() )
94         {
95             System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
96         }
97
98         SchemaTool.createSchemaTables( new URL[] { getClass()
99             .getResource( "/org/codehaus/plexus/redback/keys/jdo/package.jdo" ) }, new URL[] {}, null, false, null ); //$NON-NLS-1$
100
101         log.info( "jdoFactory driverName {} " , jdoFactory.getDriverName() );
102
103         PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
104
105         assertNotNull( pmf );
106
107         PersistenceManager pm = pmf.getPersistenceManager();
108
109         pm.close();        
110         
111     }
112
113     @Test
114     public void testGeneratePasswordResetMail()
115         throws KeyManagerException
116     {
117         AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
118                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
119
120         String content = generator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
121
122         assertNotNull( content );
123         assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
124     }
125
126     @Test
127     public void testGenerateAccountValidationMail()
128         throws KeyManagerException
129     {
130         AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
131                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
132
133         String content = generator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
134
135         assertNotNull( content );
136         assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
137     }
138
139     @Test
140     public void testGenerateAccountValidationMailCustomUrl()
141         throws Exception
142     {
143         AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
144                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
145
146         String content = customGenerator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
147
148         assertNotNull( content );
149         assertTrue( content.indexOf( "baseUrl" ) == -1 ); // make sure everything is properly populate
150         assertTrue( content.indexOf( "MY_APPLICATION_URL/security" ) > 0 ); // make sure everything is properly populate
151     }
152
153     @Test
154     public void testGeneratePasswordResetMailCustomUrl()
155         throws Exception
156     {
157         AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
158                                                           policy.getUserValidationSettings().getEmailValidationTimeout() );
159
160         String content = customGenerator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
161
162         assertNotNull( content );
163         
164         log.info( "mail content " + content );
165         
166         assertTrue( content.indexOf( "baseUrl" ) == -1 ); // make sure everything is properly populate
167         assertTrue( content.indexOf( "MY_APPLICATION_URL/security" ) > 0 ); // make sure everything is properly populate
168     }
169 }