1 package org.apache.maven.plugin.surefire.booterclient;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.booter.BooterDeserializer;
23 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
24 import org.apache.maven.surefire.booter.Classpath;
25 import org.apache.maven.surefire.booter.ClasspathConfiguration;
26 import org.apache.maven.surefire.booter.PropertiesWrapper;
27 import org.apache.maven.surefire.booter.ProviderConfiguration;
28 import org.apache.maven.surefire.booter.StartupConfiguration;
29 import org.apache.maven.surefire.booter.SystemPropertyManager;
30 import org.apache.maven.surefire.report.ReporterConfiguration;
31 import org.apache.maven.surefire.testset.DirectoryScannerParameters;
32 import org.apache.maven.surefire.testset.TestArtifactInfo;
33 import org.apache.maven.surefire.testset.TestRequest;
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Properties;
41
42 import junit.framework.TestCase;
43
44
45
46
47
48
49 public class BooterDeserializerStartupConfigurationTest
50 extends TestCase
51 {
52 private final ClasspathConfiguration classpathConfiguration = createClasspathConfiguration();
53
54 private final String aTest = "aTest";
55
56 public void testProvider()
57 throws IOException
58 {
59 assertEquals( "com.provider", getReloadedStartupConfiguration().getProviderClassName() );
60 }
61
62 public void testClassPathConfiguration()
63 throws IOException
64 {
65 ClasspathConfiguration reloadedClasspathConfiguration =
66 getReloadedStartupConfiguration().getClasspathConfiguration();
67 assertEquals( classpathConfiguration, reloadedClasspathConfiguration );
68 }
69
70 private void assertEquals( ClasspathConfiguration expectedConfiguration, ClasspathConfiguration actualConfiguration )
71 {
72 assertEquals( expectedConfiguration.getTestClasspath().getClassPath(),
73 actualConfiguration.getTestClasspath().getClassPath() );
74 Properties propertiesForExpectedConfiguration = getPropertiesForClasspathConfiguration( expectedConfiguration );
75 Properties propertiesForActualConfiguration = getPropertiesForClasspathConfiguration( actualConfiguration );
76 assertEquals( propertiesForExpectedConfiguration, propertiesForActualConfiguration );
77 }
78
79 private Properties getPropertiesForClasspathConfiguration( ClasspathConfiguration configuration )
80 {
81 Properties properties = new Properties();
82 configuration.setForkProperties( new PropertiesWrapper( properties ));
83 return properties;
84 }
85
86 public void testClassLoaderConfiguration()
87 throws IOException
88 {
89 assertFalse( getReloadedStartupConfiguration().isManifestOnlyJarRequestedAndUsable() );
90 }
91
92 public void testClassLoaderConfigurationTrues()
93 throws IOException
94 {
95 final StartupConfiguration testStartupConfiguration =
96 getTestStartupConfiguration( getManifestOnlyJarForkConfiguration() );
97 boolean current = testStartupConfiguration.isManifestOnlyJarRequestedAndUsable();
98 assertEquals( current, saveAndReload( testStartupConfiguration ).isManifestOnlyJarRequestedAndUsable() );
99 }
100
101 private ClasspathConfiguration createClasspathConfiguration()
102 {
103 Classpath testClassPath = new Classpath( Arrays.asList( new String[]{"CP1" , "CP2" } ) );
104 Classpath providerClasspath = new Classpath( Arrays.asList( new String[]{"SP1" , "SP2" } ) );
105 return new ClasspathConfiguration( testClassPath, providerClasspath, true, true );
106 }
107
108 public static ClassLoaderConfiguration getSystemClassLoaderConfiguration()
109 throws IOException
110 {
111 return new ClassLoaderConfiguration( true, false );
112 }
113
114 public static ClassLoaderConfiguration getManifestOnlyJarForkConfiguration()
115 throws IOException
116 {
117 return new ClassLoaderConfiguration( true, true );
118 }
119
120
121 private StartupConfiguration getReloadedStartupConfiguration()
122 throws IOException
123 {
124 ClassLoaderConfiguration classLoaderConfiguration = getSystemClassLoaderConfiguration();
125 return saveAndReload( getTestStartupConfiguration( classLoaderConfiguration ) );
126 }
127
128 private StartupConfiguration saveAndReload( StartupConfiguration startupConfiguration )
129 throws IOException
130 {
131 final ForkConfiguration forkConfiguration = ForkConfigurationTest.getForkConfiguration();
132 Properties props = new Properties();
133 BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, props );
134 booterSerializer.serialize( getProviderConfiguration(), startupConfiguration, aTest );
135 final File propsTest =
136 SystemPropertyManager.writePropertiesFile( props, forkConfiguration.getTempDirectory(), "propsTest", true );
137 BooterDeserializer booterDeserializer = new BooterDeserializer( new FileInputStream( propsTest ) );
138 return booterDeserializer.getProviderConfiguration();
139 }
140
141 private ProviderConfiguration getProviderConfiguration()
142 throws IOException
143 {
144
145 File cwd = new File( "." );
146 DirectoryScannerParameters directoryScannerParameters =
147 new DirectoryScannerParameters( cwd, new ArrayList(), new ArrayList(), Boolean.TRUE, "hourly" );
148 ReporterConfiguration reporterConfiguration =
149 new ReporterConfiguration( new ArrayList(), cwd, Boolean.TRUE, null );
150 String aUserRequestedTest = "aUserRequestedTest";
151 String aUserRequestedTestMethod = "aUserRequestedTestMethod";
152 TestRequest testSuiteDefinition =
153 new TestRequest( Arrays.asList( getSuiteXmlFileStrings() ), getTestSourceDirectory(), aUserRequestedTest,
154 aUserRequestedTestMethod );
155 return new ProviderConfiguration( directoryScannerParameters, true, reporterConfiguration,
156 new TestArtifactInfo( "5.0", "ABC" ), testSuiteDefinition, new Properties(),
157 aTest );
158 }
159
160 private StartupConfiguration getTestStartupConfiguration( ClassLoaderConfiguration classLoaderConfiguration )
161 {
162 return new StartupConfiguration( "com.provider", classpathConfiguration, classLoaderConfiguration, false, false,
163 false );
164 }
165
166 private File getTestSourceDirectory()
167 {
168 return new File( "TestSrc" );
169 }
170
171 private Object[] getSuiteXmlFileStrings()
172 {
173 return new Object[]{ "A1", "A2" };
174 }
175 }