View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.ci.config.load;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.hamcrest.Matchers.notNullValue;
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.File;
25  import java.util.List;
26  
27  import org.jdom.Document;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.xml.sax.InputSource;
31  
32  import de.smartics.ci.config.load.HudsonJobConfig;
33  import de.smartics.ci.config.load.HudsonJobConfigLoader;
34  import de.smartics.ci.config.load.HudsonJobConfigurationLoaderConfig;
35  import de.smartics.ci.config.load.LoaderPlan;
36  import de.smartics.ci.config.load.LoaderPlanLoader;
37  import de.smartics.ci.config.load.LocationManager;
38  import de.smartics.ci.config.maven.MavenConfig;
39  import de.smartics.ci.config.projects.Project;
40  import de.smartics.ci.config.test.JDomTestUtils;
41  import de.smartics.ci.config.test.MavenConfigBuilder;
42  import de.smartics.testdoc.annotations.Uut;
43  import de.smartics.util.lang.NullArgumentException;
44  import de.smartics.util.test.io.FileTestUtils;
45  
46  /**
47   * Tests {@link HudsonJobConfigLoader}.
48   */
49  public class HudsonJobConfigLoaderTest
50  {
51    // ********************************* Fields *********************************
52  
53    // --- constants ------------------------------------------------------------
54  
55    // --- members --------------------------------------------------------------
56  
57    @Uut
58    private HudsonJobConfigLoader uut;
59  
60    private LocationManager locationManager;
61  
62    private MavenConfig mavenConfig;
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- prepare --------------------------------------------------------------
69  
70    @Before
71    public void setUp()
72    {
73      locationManager = new LocationManager();
74      final File globalHudsonConfigs =
75          FileTestUtils.getFileFromRelativeResource(Project.class,
76              Project.GLOBAL_RESOURCES);
77      locationManager.addLocation(globalHudsonConfigs);
78  
79      final HudsonJobConfigurationLoaderConfig loaderConfig =
80          createLoaderConfig();
81      mavenConfig = MavenConfigBuilder.a().build();
82  
83      uut = new HudsonJobConfigLoader(locationManager, loaderConfig);
84    }
85  
86    // --- helper ---------------------------------------------------------------
87  
88    private static HudsonJobConfigurationLoaderConfig createLoaderConfig()
89    {
90      final HudsonJobConfigurationLoaderConfig.Builder builder =
91          new HudsonJobConfigurationLoaderConfig.Builder();
92      builder.withAddScmElementWithFullContent(true)
93          .withRemoveCascadingChildrenNamesElement(false)
94          .withRemoveCascadingJobPropertiesElement(false);
95      final HudsonJobConfigurationLoaderConfig loaderConfig = builder.build();
96      return loaderConfig;
97    }
98  
99    // --- tests ----------------------------------------------------------------
100 
101   @Test(expected = NullArgumentException.class)
102   public void rejectsNullPlan() throws Exception
103   {
104     uut.load(null);
105   }
106 
107   @Test(expected = IllegalArgumentException.class)
108   public void rejectsEmptyPlan() throws Exception
109   {
110     final LoaderPlan emptyPlan = new LoaderPlan("id", mavenConfig);
111     uut.load(emptyPlan);
112   }
113 
114   @Test
115   public void loadProjectWithOverridingConfigs() throws Exception
116   {
117     final File projectDir =
118         FileTestUtils.getFileFromRelativeResource(Project.class,
119             Project.SINGLE_PROJECT_USING_POM_DEFAULTS);
120     locationManager.addLocation(projectDir);
121 
122     final LoaderPlanLoader loader = new LoaderPlanLoader(mavenConfig);
123 
124     final InputSource ciConfigXml = locationManager.open("ci-config.xml");
125     final List<LoaderPlan> plans = loader.load(ciConfigXml);
126     final LoaderPlan plan = plans.get(0);
127 
128     final Document hudsonConfigXml = uut.load(plan);
129     final String result = JDomTestUtils.toString(hudsonConfigXml);
130     final String expected =
131         JDomTestUtils.read(Project.class,
132             "001-single-project-using-pom-defaults.xml");
133     assertEquals(expected, result);
134   }
135 
136   @Test
137   public void loadProjectToConfigInstance() throws Exception
138   {
139     final File projectDir =
140         FileTestUtils.getFileFromRelativeResource(Project.class,
141             Project.SINGLE_PROJECT_USING_POM_DEFAULTS);
142     locationManager.addLocation(projectDir);
143 
144     final LoaderPlanLoader loader = new LoaderPlanLoader(mavenConfig);
145 
146     final InputSource ciConfigXml = locationManager.open("ci-config.xml");
147     final List<LoaderPlan> plans = loader.load(ciConfigXml);
148     final LoaderPlan plan = plans.get(0);
149 
150     final HudsonJobConfig hudsonConfig = uut.loadHudsonConfig(plan);
151     assertThat(hudsonConfig.getId(), is(equalTo("smartics-test-utils")));
152     assertThat(hudsonConfig.getConfigXml(), is(notNullValue()));
153   }
154 }