1 package org.codehaus.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.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;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import javax.jdo.PersistenceManager;
41 import javax.jdo.PersistenceManagerFactory;
43 import java.util.Map.Entry;
44 import java.util.Properties;
47 * Test the Mailer class.
49 @RunWith( SpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
51 public class MailGeneratorTest
55 @Named(value = "mailGenerator#velocity")
56 private MailGenerator generator;
59 @Named(value = "mailGenerator#custom-url")
60 private MailGenerator customGenerator;
63 @Named(value = "userSecurityPolicy")
64 private UserSecurityPolicy policy;
67 @Named(value = "keyManager#memory")
68 private KeyManager keyManager;
71 @Named(value = "jdoFactory#users")
72 DefaultConfigurableJdoFactory jdoFactory;
74 private Logger log = LoggerFactory.getLogger( getClass() );
80 CacheManager.getInstance().clearAll();
83 jdoFactory.setPassword( "" );
85 jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
87 jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); //$NON-NLS-1$ //$NON-NLS-2$
89 jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); //$NON-NLS-1$ //$NON-NLS-2$
91 Properties properties = jdoFactory.getProperties();
93 for ( Entry<Object, Object> entry : properties.entrySet() )
95 System.setProperty( (String) entry.getKey(), (String) entry.getValue() );
98 SchemaTool.createSchemaTables( new URL[] { getClass()
99 .getResource( "/org/codehaus/plexus/redback/keys/jdo/package.jdo" ) }, new URL[] {}, null, false, null ); //$NON-NLS-1$
101 log.info( "jdoFactory driverName {} " , jdoFactory.getDriverName() );
103 PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory();
105 assertNotNull( pmf );
107 PersistenceManager pm = pmf.getPersistenceManager();
114 public void testGeneratePasswordResetMail()
115 throws KeyManagerException
117 AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
118 policy.getUserValidationSettings().getEmailValidationTimeout() );
120 String content = generator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
122 assertNotNull( content );
123 assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
127 public void testGenerateAccountValidationMail()
128 throws KeyManagerException
130 AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
131 policy.getUserValidationSettings().getEmailValidationTimeout() );
133 String content = generator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
135 assertNotNull( content );
136 assertTrue( content.indexOf( '$' ) == -1 ); // make sure everything is properly populate
140 public void testGenerateAccountValidationMailCustomUrl()
143 AuthenticationKey authkey = keyManager.createKey( "username", "New User Email Validation",
144 policy.getUserValidationSettings().getEmailValidationTimeout() );
146 String content = customGenerator.generateMail( "newAccountValidationEmail", authkey, "baseUrl" );
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
154 public void testGeneratePasswordResetMailCustomUrl()
157 AuthenticationKey authkey = keyManager.createKey( "username", "Password Reset Request",
158 policy.getUserValidationSettings().getEmailValidationTimeout() );
160 String content = customGenerator.generateMail( "passwordResetEmail", authkey, "baseUrl" );
162 assertNotNull( content );
164 log.info( "mail content " + content );
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