1 package org.apache.archiva.redback.integration.mail;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import javax.jdo.PersistenceManager;
42 import javax.jdo.PersistenceManagerFactory;
44 import java.util.Map.Entry;
45 import java.util.Properties;
48 * Test the Mailer class.
50 @RunWith( SpringJUnit4ClassRunner.class )
51 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
52 public class MailGeneratorTest
56 @Named(value = "mailGenerator#velocity")
57 private MailGenerator generator;
60 @Named(value = "mailGenerator#custom-url")
61 private MailGenerator customGenerator;
64 @Named(value = "userSecurityPolicy")
65 private UserSecurityPolicy policy;
68 @Named(value = "keyManager#memory")
69 private KeyManager keyManager;
72 @Named(value = "jdoFactory#users")
73 DefaultConfigurableJdoFactory jdoFactory;
75 private Logger log = LoggerFactory.getLogger( getClass() );
81 CacheManager.getInstance().clearAll();
84 jdoFactory.setPassword( "" );
86 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
88 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
90 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); //$NON-NLS-1$ //$NON-NLS-2$
92 Properties properties = jdoFactory.getProperties();
94 for ( Entry<Object, Object> entry : properties.entrySet() )
96 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
99 SchemaTool.createSchemaTables( new URL[] { getClass()
100 .getResource( "/org/codehaus/plexus/redback/keys/jdo/package.jdo" ) }, new URL[] {}, null, false, null ); //$NON-NLS-1$
102 log.info( "jdoFactory driverName {} " , jdoFactory.getDriverName() );
104 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
106 assertNotNull( pmf );
108 PersistenceManager pm = pmf.getPersistenceManager();
115 public void testGeneratePasswordResetMail()
116 throws KeyManagerException
118 AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
119 policy.getUserValidationSettings().getEmailValidationTimeout() );
121 String content = generator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
123 assertNotNull( content );
124 assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
128 public void testGenerateAccountValidationMail()
129 throws KeyManagerException
131 AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
132 policy.getUserValidationSettings().getEmailValidationTimeout() );
134 String content = generator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
136 assertNotNull( content );
137 assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
141 public void testGenerateAccountValidationMailCustomUrl()
144 AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
145 policy.getUserValidationSettings().getEmailValidationTimeout() );
147 String content = customGenerator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
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
155 public void testGeneratePasswordResetMailCustomUrl()
158 AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
159 policy.getUserValidationSettings().getEmailValidationTimeout() );
161 String content = customGenerator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
163 assertNotNull( content );
165 log.info( "mail content " + content );
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