KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > ArtifactsPublisherTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers;
38
39 import java.io.File JavaDoc;
40
41 import org.apache.tools.ant.Project;
42
43 import junit.framework.TestCase;
44 import net.sourceforge.cruisecontrol.CruiseControlException;
45
46 /**
47  * @author Jeffrey Fredrick
48  */

49 public class ArtifactsPublisherTest extends TestCase {
50     
51     private ArtifactsPublisher publisher;
52     private File JavaDoc tempFile;
53     
54     protected void setUp() throws Exception JavaDoc {
55         publisher = new ArtifactsPublisher();
56         tempFile = File.createTempFile("temp", ".tmp");
57         tempFile.deleteOnExit();
58     }
59
60     protected void tearDown() throws Exception JavaDoc {
61         publisher = null;
62         if (tempFile.exists()) {
63             tempFile.delete();
64         }
65         tempFile = null;
66     }
67
68     public void testShouldPublish() {
69         //By default, should publish on both broken and successful builds.
70
assertTrue(publisher.shouldPublish(true));
71         assertTrue(publisher.shouldPublish(false));
72
73         //Set "publishOnFailure" to true should be the same result.
74
publisher.setPublishOnFailure(true);
75         assertTrue(publisher.shouldPublish(true));
76         assertTrue(publisher.shouldPublish(false));
77
78         //Set "publishOnFailure" to false should NOT publish on a broken build.
79
publisher.setPublishOnFailure(false);
80         assertTrue(publisher.shouldPublish(true));
81         assertFalse(publisher.shouldPublish(false));
82     }
83
84     public void testPublishDirectory() {
85         File JavaDoc tempDir = tempFile.getParentFile();
86         Project project = new Project();
87         publisher.setDir(tempFile.getAbsolutePath());
88         try {
89             publisher.publishDirectory(project, tempDir);
90             fail();
91         } catch (CruiseControlException expected) {
92             String JavaDoc message = expected.getMessage();
93             assertTrue(message.startsWith("target directory "));
94         }
95     }
96
97     public void testPublishFileWhenTargetFileNotExist() {
98         File JavaDoc tempDir = tempFile.getParentFile();
99         publisher.setFile(tempFile.getName());
100         try {
101             publisher.publishFile(tempDir);
102             fail();
103         } catch (CruiseControlException expected) {
104             String JavaDoc message = expected.getMessage();
105             assertTrue(message.startsWith("target file "));
106         }
107     }
108
109     public void testValidate() {
110         try {
111             publisher.validate();
112             fail();
113         } catch (CruiseControlException expected) {
114             assertNotNull(expected);
115         }
116        
117         publisher.setDest("foo");
118         publisher.setDir("bar");
119         publisher.setFile("baz");
120         
121         try {
122             publisher.validate();
123             fail();
124         } catch (CruiseControlException expected) {
125             assertNotNull(expected);
126         }
127         
128         publisher.setFile(null);
129         try {
130             publisher.validate();
131         } catch (CruiseControlException notExpected) {
132             fail();
133         }
134
135         publisher.setDir(null);
136         publisher.setFile("baz");
137         try {
138             publisher.validate();
139         } catch (CruiseControlException notExpected) {
140             fail();
141         }
142
143         publisher.setDest(null);
144         try {
145             publisher.validate();
146             fail();
147         } catch (CruiseControlException expected) {
148             assertNotNull(expected);
149         }
150     }
151
152     public void testGetDestinationDirectory() {
153         String JavaDoc tempDir = tempFile.getParent();
154         publisher.setDest(tempDir);
155         String JavaDoc timestamp = "20040102030405";
156         File JavaDoc destinationDir = publisher.getDestinationDirectory(timestamp);
157         String JavaDoc expected = tempDir + File.separatorChar + timestamp;
158         assertEquals(expected, destinationDir.getPath());
159
160         final String JavaDoc subdir = "subdir";
161         publisher.setSubdirectory(subdir);
162         destinationDir = publisher.getDestinationDirectory(timestamp);
163         expected = tempDir + File.separatorChar + timestamp + File.separatorChar + subdir;
164         assertEquals(expected, destinationDir.getPath());
165
166     }
167 }
168
Popular Tags