1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.domain.geoloc.service.fulltextsearch;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.fail;
27  import net.sf.jstester.JsTester;
28  
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import com.gisgraphy.domain.geoloc.service.errors.IoutputFormatVisitor;
34  import com.gisgraphy.domain.valueobject.Output.OutputFormat;
35  import com.gisgraphy.test.FeedChecker;
36  
37  
38  public class FulltextErrorVisitorTest  {
39      
40      private String errorMessage = "My Message";
41      IoutputFormatVisitor FulltextErrorVisitor;
42      
43  
44      @Before
45      public void onSetUp() throws Exception {
46  	FulltextErrorVisitor = new FulltextErrorVisitor(errorMessage);
47      }
48      
49      
50      @Test
51      public void fulltextErrorVisitorString() {
52  	IoutputFormatVisitor FulltextErrorVisitor = new FulltextErrorVisitor(errorMessage);
53  	Assert.assertEquals("The error message is not well set ",errorMessage, FulltextErrorVisitor.getErrorMessage());
54      }
55      
56      @Test
57      public void fulltextErrorVisitor() {
58  	IoutputFormatVisitor FulltextErrorVisitor = new FulltextErrorVisitor();
59  	Assert.assertEquals("An error message should be provided when no message is specified ",IoutputFormatVisitor.DEFAULT_ERROR_MESSAGE, FulltextErrorVisitor.getErrorMessage());
60      }
61  
62      @Test
63      public void visitXML() {
64  	    String result = FulltextErrorVisitor.visitXML(OutputFormat.XML);
65  	    FeedChecker.checkFulltextErrorXML(result,errorMessage);
66      }
67  
68  
69      @Test
70      public void visitJSON() {
71  	JsTester jsTester = null;
72  	IoutputFormatVisitor FulltextErrorVisitor = new FulltextErrorVisitor(errorMessage);
73  	String result = FulltextErrorVisitor.visitJSON(OutputFormat.JSON);
74  	try {
75  	    jsTester = new JsTester();
76  	    jsTester.onSetUp();
77  
78  	    // JsTester
79  	    jsTester.eval("evalresult= eval(" + result + ");");
80  	    String error = jsTester.eval("evalresult.responseHeader.error")
81  	    .toString();
82     
83  	    assertEquals(errorMessage, error);
84  
85  	    error = jsTester.eval("evalresult.responseHeader.status")
86  	    .toString();
87  	    Assert.assertEquals("-1.0", error);// -1.0 because it is considered as a
88  	    // float
89  
90  	} catch (Exception e) {
91  	    fail("An exception has occured " + e.getMessage());
92  	} finally {
93  	    if (jsTester != null) {
94  		jsTester.onTearDown();
95  	    }
96  
97  	}
98  
99      }
100 
101     @Test
102     public void visitPYTHON() {
103 	    String result = FulltextErrorVisitor.visitPYTHON(OutputFormat.PYTHON);
104 	    checkErrorMessageIsPresentInOutputStream(result);
105     }
106 
107 
108 
109 
110     @Test
111     public void visitRUBY() {
112 	    String result = FulltextErrorVisitor.visitRUBY(OutputFormat.RUBY);
113 	    checkErrorMessageIsPresentInOutputStream(result);
114     }
115 
116     @Test
117     public void visitPHP() {
118 	    String result = FulltextErrorVisitor.visitPHP(OutputFormat.PHP);
119 	    checkErrorMessageIsPresentInOutputStream(result);
120 	    
121     }
122 
123     @Test
124     public void visitATOM() {
125 	    String result = FulltextErrorVisitor.visitATOM(OutputFormat.ATOM);
126 	    
127 	    FeedChecker.checkFulltextErrorXML(result,errorMessage);
128     }
129 
130     @Test
131     public void visitGEORSS() {
132 	    String result = FulltextErrorVisitor.visitGEORSS(OutputFormat.GEORSS);
133 	    
134 	    FeedChecker.checkFulltextErrorXML(result,errorMessage);
135     }
136 
137     private void checkErrorMessageIsPresentInOutputStream(String result) {
138 	Assert.assertTrue("the error Message should contains the error Message", result.contains(errorMessage));
139     }
140 
141 
142    
143 
144 
145 }