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