~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      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
/* $Id: ListenerTestCase.java 727407 2008-12-17 15:05:45Z jeremias $ */
 
19
 
 
20
package org.apache.xmlgraphics.ps.dsc;
 
21
 
 
22
import java.io.IOException;
 
23
import java.io.InputStream;
 
24
import java.util.Map;
 
25
 
 
26
import junit.framework.TestCase;
 
27
 
 
28
import org.apache.commons.io.IOUtils;
 
29
 
 
30
import org.apache.xmlgraphics.ps.DSCConstants;
 
31
import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
 
32
import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel;
 
33
import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
 
34
 
 
35
/**
 
36
 * Tests the listener functionality on the DSC parser.
 
37
 */
 
38
public class ListenerTestCase extends TestCase {
 
39
 
 
40
    /**
 
41
     * Tests {@link DSCParser#setFilter(DSCFilter)}.
 
42
     * @throws Exception if an error occurs
 
43
     */
 
44
    public void testFilter() throws Exception {
 
45
        InputStream in = getClass().getResourceAsStream("test1.txt");
 
46
        try {
 
47
            DSCParser parser = new DSCParser(in);
 
48
            parser.setFilter(new DSCFilter() {
 
49
 
 
50
                public boolean accept(DSCEvent event) {
 
51
                    //Filter out all non-DSC comments
 
52
                    return !event.isComment();
 
53
                }
 
54
 
 
55
            });
 
56
            while (parser.hasNext()) {
 
57
                DSCEvent event = parser.nextEvent();
 
58
 
 
59
                if (parser.getCurrentEvent().isComment()) {
 
60
                    fail("Filter failed. Comment found.");
 
61
                }
 
62
            }
 
63
        } finally {
 
64
            IOUtils.closeQuietly(in);
 
65
        }
 
66
    }
 
67
 
 
68
    /**
 
69
     * Tests listeners on DSCParser.
 
70
     * @throws Exception if an error occurs
 
71
     */
 
72
    public void testListeners() throws Exception {
 
73
        InputStream in = getClass().getResourceAsStream("test1.txt");
 
74
        try {
 
75
            final Map results = new java.util.HashMap();
 
76
            DSCParser parser = new DSCParser(in);
 
77
 
 
78
            //Filter the prolog
 
79
            parser.addListener(new DSCListener() {
 
80
                public void processEvent(DSCEvent event, DSCParser parser)
 
81
                        throws IOException, DSCException {
 
82
                    if (event.isDSCComment()) {
 
83
                        DSCComment comment = event.asDSCComment();
 
84
                        if (DSCConstants.BEGIN_PROLOG.equals(comment.getName())) {
 
85
                            //Skip until end of prolog
 
86
                            while (parser.hasNext()) {
 
87
                                DSCEvent e = parser.nextEvent();
 
88
                                if (e.isDSCComment()) {
 
89
                                    if (DSCConstants.END_PROLOG.equals(
 
90
                                            e.asDSCComment().getName())) {
 
91
                                        parser.next();
 
92
                                        break;
 
93
                                    }
 
94
                                }
 
95
 
 
96
                            }
 
97
                        }
 
98
                    }
 
99
                }
 
100
            });
 
101
 
 
102
            //Listener for the language level
 
103
            parser.addListener(new DSCListener() {
 
104
                public void processEvent(DSCEvent event, DSCParser parser)
 
105
                        throws IOException, DSCException {
 
106
                    if (event instanceof DSCCommentLanguageLevel) {
 
107
                        DSCCommentLanguageLevel level = (DSCCommentLanguageLevel)event;
 
108
                        results.put("level", new Integer(level.getLanguageLevel()));
 
109
                    }
 
110
                }
 
111
            });
 
112
            int count = 0;
 
113
            while (parser.hasNext()) {
 
114
                DSCEvent event = parser.nextEvent();
 
115
                System.out.println(event);
 
116
                count++;
 
117
            }
 
118
            assertEquals(12, count);
 
119
            assertEquals(new Integer(1), results.get("level"));
 
120
        } finally {
 
121
            IOUtils.closeQuietly(in);
 
122
        }
 
123
    }
 
124
 
 
125
}