01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.plugins.trigger;
19:
20: import java.io.File;
21: import java.util.Date;
22:
23: import junit.framework.TestCase;
24:
25: import org.apache.ivy.core.event.resolve.StartResolveEvent;
26: import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
27: import org.apache.ivy.core.module.id.ModuleRevisionId;
28: import org.apache.ivy.util.FileUtil;
29: import org.apache.ivy.util.Message;
30: import org.apache.ivy.util.MockMessageLogger;
31:
32: public class LogTriggerTest extends TestCase {
33: private static final String LINE_SEPARATOR = System
34: .getProperty("line.separator");
35:
36: private StartResolveEvent ev;
37: private LogTrigger trigger;
38: private File testDir;
39:
40: protected void setUp() {
41: ev = new StartResolveEvent(DefaultModuleDescriptor
42: .newBasicInstance(ModuleRevisionId.parse("o#A;1"),
43: new Date()), new String[] { "c" });
44: trigger = new LogTrigger();
45: trigger.setEvent(ev.getName());
46: testDir = new File("build/test/trigger");
47: testDir.mkdirs();
48: }
49:
50: protected void tearDown() throws Exception {
51: FileUtil.forceDelete(testDir);
52: }
53:
54: public void testMessage() throws Exception {
55: trigger
56: .setMessage("msg: ${organisation} ${module} ${revision}");
57:
58: MockMessageLogger mockLogger = new MockMessageLogger();
59: Message.setDefaultLogger(mockLogger);
60: trigger.progress(ev);
61:
62: mockLogger.assertLogInfoContains("msg: o A 1");
63: }
64:
65: public void testFile() throws Exception {
66: trigger
67: .setMessage("msg: ${organisation} ${module} ${revision}");
68: File f = new File(testDir, "test.log");
69: trigger.setFile(f.getPath());
70:
71: trigger.progress(ev);
72:
73: assertTrue(f.exists());
74: assertEquals("msg: o A 1" + LINE_SEPARATOR, FileUtil
75: .readEntirely(f));
76:
77: trigger.progress(ev);
78:
79: assertEquals("msg: o A 1" + LINE_SEPARATOR + "msg: o A 1"
80: + LINE_SEPARATOR, FileUtil.readEntirely(f));
81: }
82:
83: public void testFileNoAppend() throws Exception {
84: trigger
85: .setMessage("msg: ${organisation} ${module} ${revision}");
86: File f = new File(testDir, "test.log");
87: trigger.setFile(f.getPath());
88: trigger.setAppend(false);
89:
90: trigger.progress(ev);
91: trigger.progress(ev);
92:
93: assertTrue(f.exists());
94: assertEquals("msg: o A 1" + LINE_SEPARATOR, FileUtil
95: .readEntirely(f));
96: }
97: }
|