001/*
002 * #%L
003 * Netarchivesuite - common - test
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.common.webinterface;
025
026import java.io.BufferedReader;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.PrintWriter;
030import java.io.UnsupportedEncodingException;
031import java.net.MalformedURLException;
032import java.net.URL;
033import java.util.Enumeration;
034import java.util.EventListener;
035import java.util.HashMap;
036import java.util.Locale;
037import java.util.Map;
038import java.util.Set;
039import javax.el.ELContext;
040import javax.servlet.AsyncContext;
041import javax.servlet.DispatcherType;
042import javax.servlet.Filter;
043import javax.servlet.FilterRegistration;
044import javax.servlet.RequestDispatcher;
045import javax.servlet.Servlet;
046import javax.servlet.ServletConfig;
047import javax.servlet.ServletContext;
048import javax.servlet.ServletException;
049import javax.servlet.ServletInputStream;
050import javax.servlet.ServletOutputStream;
051
052import javax.servlet.ServletRegistration;
053import javax.servlet.ServletRequest;
054import javax.servlet.ServletResponse;
055import javax.servlet.SessionCookieConfig;
056import javax.servlet.SessionTrackingMode;
057import javax.servlet.descriptor.JspConfigDescriptor;
058import javax.servlet.http.HttpSession;
059import javax.servlet.jsp.JspWriter;
060import javax.servlet.jsp.PageContext;
061import javax.servlet.jsp.el.ExpressionEvaluator;
062import javax.servlet.jsp.el.VariableResolver;
063
064import dk.netarkivet.common.exceptions.NotImplementedException;
065import dk.netarkivet.common.utils.FileUtils;
066import dk.netarkivet.testutils.TestFileUtils;
067import dk.netarkivet.testutils.preconfigured.ReloadSettings;
068
069/**
070 * A TestCase subclass specifically tailored to test webinterface classes, primarily the classes in
071 * dk.netarkivet.harvester.webinterface: HarvestStatusTester, EventHarvestTester, DomainDefinitionTester,
072 * ScheduleDefinitionTester, SnapshotHarvestDefinitionTester but also
073 * dk.netarkivet.archive.webinterface.BitpreserveFileStatusTester
074 */
075@SuppressWarnings({"rawtypes", "deprecation"})
076public class WebinterfaceTestCase {
077    ReloadSettings rs = new ReloadSettings();
078
079    public void setUp() throws Exception {
080        rs.setUp();
081        TestFileUtils.copyDirectoryNonCVS(TestInfo.ORIGINALS_DIR, TestInfo.WORKING_DIR);
082    }
083
084    public void tearDown() throws Exception {
085        FileUtils.removeRecursively(TestInfo.WORKING_DIR);
086        rs.tearDown();
087    }
088
089    /**
090     * A dummy class implementing only the methods for getting parameters. A single setter method is provided to set the
091     * parameter map.
092     */
093    public static class TestServletRequest implements ServletRequest {
094        Map<String, Object> attributes = new HashMap<String, Object>();
095        Map<String, String[]> parameterMap = new HashMap<String, String[]>();
096
097        public void setParameterMap(Map<String, String[]> parameterMap) {
098            this.parameterMap = parameterMap;
099        }
100
101        public Object getAttribute(String string) {
102            throw new NotImplementedException("Not implemented");
103        }
104
105        public Enumeration getAttributeNames() {
106            throw new NotImplementedException("Not implemented");
107        }
108
109        public String getCharacterEncoding() {
110            throw new NotImplementedException("Not implemented");
111        }
112
113        public void setCharacterEncoding(String string) throws UnsupportedEncodingException {
114            throw new NotImplementedException("Not implemented");
115        }
116
117        public int getContentLength() {
118            throw new NotImplementedException("Not implemented");
119        }
120
121        @Override
122        public long getContentLengthLong() {
123            return 0;
124        }
125
126        public String getContentType() {
127            throw new NotImplementedException("Not implemented");
128        }
129
130        public ServletInputStream getInputStream() throws IOException {
131            throw new NotImplementedException("Not implemented");
132        }
133
134        public String getParameter(String string) {
135            String[] val = parameterMap.get(string);
136            if (val == null) {
137                return null;
138            }
139            return val[0];
140        }
141
142        public Enumeration getParameterNames() {
143            throw new NotImplementedException("Not implemented");
144        }
145
146        public String[] getParameterValues(String string) {
147            return parameterMap.get(string);
148        }
149
150        public Map getParameterMap() {
151            return parameterMap;
152        }
153
154        public String getProtocol() {
155            throw new NotImplementedException("Not implemented");
156        }
157
158        public String getScheme() {
159            throw new NotImplementedException("Not implemented");
160        }
161
162        public String getServerName() {
163            throw new NotImplementedException("Not implemented");
164        }
165
166        public int getServerPort() {
167            throw new NotImplementedException("Not implemented");
168        }
169
170        public BufferedReader getReader() throws IOException {
171            throw new NotImplementedException("Not implemented");
172        }
173
174        public String getRemoteAddr() {
175            throw new NotImplementedException("Not implemented");
176        }
177
178        public String getRemoteHost() {
179            throw new NotImplementedException("Not implemented");
180        }
181
182        public void setAttribute(String string, Object object) {
183            attributes.put(string, object);
184        }
185
186        public void removeAttribute(String string) {
187            throw new NotImplementedException("Not implemented");
188        }
189
190        public Locale getLocale() {
191            throw new NotImplementedException("Not implemented");
192        }
193
194        public Enumeration getLocales() {
195            throw new NotImplementedException("Not implemented");
196        }
197
198        public boolean isSecure() {
199            throw new NotImplementedException("Not implemented");
200        }
201
202        public RequestDispatcher getRequestDispatcher(String string) {
203            throw new NotImplementedException("Not implemented");
204        }
205
206        public String getRealPath(String string) {
207            throw new NotImplementedException("Not implemented");
208        }
209
210        public int getRemotePort() {
211            return 0;
212        }
213
214        public String getLocalName() {
215            return null;
216        }
217
218        public String getLocalAddr() {
219            return null;
220        }
221
222        public int getLocalPort() {
223            return 0;
224        }
225
226        @Override
227        public ServletContext getServletContext() {
228            return null;
229        }
230
231        @Override
232        public AsyncContext startAsync() throws IllegalStateException {
233            return null;
234        }
235
236        @Override
237        public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
238                throws IllegalStateException {
239            return null;
240        }
241
242        @Override
243        public boolean isAsyncStarted() {
244            return false;
245        }
246
247        @Override
248        public boolean isAsyncSupported() {
249            return false;
250        }
251
252        @Override
253        public AsyncContext getAsyncContext() {
254            return null;
255        }
256
257        @Override
258        public DispatcherType getDispatcherType() {
259            return null;
260        }
261
262    }
263
264    public static class TestPageContext extends PageContext {
265        private final ServletRequest request;
266        private JspWriter out;
267        private final Locale locale;
268
269        public TestPageContext(ServletRequest request) {
270            this.request = request;
271            this.locale = new Locale("en");
272        }
273
274        public TestPageContext(ServletRequest request, JspWriter out, Locale locale) {
275            this.request = request;
276            this.out = out;
277            this.locale = locale;
278        }
279
280        public void initialize(Servlet servlet, ServletRequest servletRequest, ServletResponse servletResponse,
281                String string, boolean b, int i, boolean b1) throws IOException, IllegalStateException,
282                IllegalArgumentException {
283        }
284
285        public void release() {
286        }
287
288        public HttpSession getSession() {
289            return null;
290        }
291
292        public Object getPage() {
293            return null;
294        }
295
296        public ServletRequest getRequest() {
297            return request;
298        }
299
300        public ServletResponse getResponse() {
301            return new ServletResponse() {
302
303                public String getCharacterEncoding() {
304                    return null; 
305                }
306
307                public String getContentType() {
308                    return null; 
309                }
310
311                public ServletOutputStream getOutputStream() throws IOException {
312                    return null; 
313                }
314
315                public PrintWriter getWriter() throws IOException {
316                    return null; 
317                }
318
319                public void setCharacterEncoding(String string) {
320                }
321
322                public void setContentLength(int i) {
323                }
324
325                @Override
326                public void setContentLengthLong(long l) {
327
328                }
329
330                public void setContentType(String string) {
331                }
332
333                public void setBufferSize(int i) {
334                }
335
336                public int getBufferSize() {
337                    return 0;
338                }
339
340                public void flushBuffer() throws IOException {
341                }
342
343                public void resetBuffer() {
344                }
345
346                public boolean isCommitted() {
347                    return false;
348                }
349
350                public void reset() {
351                }
352
353                public void setLocale(Locale locale) {
354                    // To change body of implemented methods use File | Settings
355                    // | File Templates.
356                }
357
358                public Locale getLocale() {
359                    return locale;
360                }
361            };
362        }
363
364        public Exception getException() {
365            return null;
366        }
367
368        public ServletConfig getServletConfig() {
369            return null;
370        }
371
372        public ServletContext getServletContext() {
373            return new ServletContext() {
374                public String getContextPath() {
375                    return null;
376                }
377
378                public ServletContext getContext(String string) {
379                    return null; 
380                }
381
382                public int getMajorVersion() {
383                    return 0; 
384                }
385
386                public int getMinorVersion() {
387                    return 0; 
388                }
389
390                @Override
391                public int getEffectiveMajorVersion() {
392                    return 0;
393                }
394
395                @Override
396                public int getEffectiveMinorVersion() {
397                    return 0;
398                }
399
400                public String getMimeType(String string) {
401                    return null;
402                }
403
404                public Set getResourcePaths(String string) {
405                    return null; 
406                }
407
408                public URL getResource(String string) throws MalformedURLException {
409                    return null; 
410                }
411
412                public InputStream getResourceAsStream(String string) {
413                    return null; 
414                }
415
416                public RequestDispatcher getRequestDispatcher(String string) {
417                    return new RequestDispatcher() {
418
419                        public void forward(ServletRequest servletRequest, ServletResponse servletResponse)
420                                throws ServletException, IOException {
421                        }
422
423                        public void include(ServletRequest servletRequest, ServletResponse servletResponse)
424                                throws ServletException, IOException {
425                     
426                        }
427                    };
428                }
429
430                public RequestDispatcher getNamedDispatcher(String string) {
431                    return null; 
432                }
433
434                public Servlet getServlet(String string) throws ServletException {
435                    return null; 
436                }
437
438                public Enumeration getServlets() {
439                    return null; 
440                }
441
442                public Enumeration getServletNames() {
443                    return null; 
444                }
445
446                public void log(String string) {
447                   
448                }
449
450                public void log(Exception exception, String string) {
451                   
452                }
453
454                public void log(String string, Throwable throwable) {
455                   
456                }
457
458                public String getRealPath(String string) {
459                    return null; 
460                }
461
462                public String getServerInfo() {
463                    return null; 
464                }
465
466                public String getInitParameter(String string) {
467                    return null; 
468                }
469
470                public Enumeration getInitParameterNames() {
471                    return null; 
472                }
473
474                @Override
475                public boolean setInitParameter(String s, String s2) {
476                    return false;
477                }
478
479                public Object getAttribute(String string) {
480                    return null; 
481                }
482
483                public Enumeration getAttributeNames() {
484                    return null; 
485                }
486
487                public void setAttribute(String string, Object object) {
488                }
489
490                public void removeAttribute(String string) {
491                }
492
493                public String getServletContextName() {
494                    return null;
495                }
496
497                @Override
498                public ServletRegistration.Dynamic addServlet(String s, String s2) {
499                    return null;
500                }
501
502                @Override
503                public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) {
504                    return null;
505                }
506
507                @Override
508                public ServletRegistration.Dynamic addServlet(String s, Class<? extends Servlet> aClass) {
509                    return null;
510                }
511
512                @Override
513                public <T extends Servlet> T createServlet(Class<T> tClass) throws ServletException {
514                    return null;
515                }
516
517                @Override
518                public ServletRegistration getServletRegistration(String s) {
519                    return null;
520                }
521
522                @Override
523                public Map<String, ? extends ServletRegistration> getServletRegistrations() {
524                    return null;
525                }
526
527                @Override
528                public FilterRegistration.Dynamic addFilter(String s, String s2) {
529                    return null;
530                }
531
532                @Override
533                public FilterRegistration.Dynamic addFilter(String s, Filter filter) {
534                    return null;
535                }
536
537                @Override
538                public FilterRegistration.Dynamic addFilter(String s, Class<? extends Filter> aClass) {
539                    return null;
540                }
541
542                @Override
543                public <T extends Filter> T createFilter(Class<T> tClass) throws ServletException {
544                    return null;
545                }
546
547                @Override
548                public FilterRegistration getFilterRegistration(String s) {
549                    return null;
550                }
551
552                @Override
553                public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
554                    return null;
555                }
556
557                @Override
558                public SessionCookieConfig getSessionCookieConfig() {
559                    return null;
560                }
561
562                @Override
563                public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
564
565                }
566
567                @Override
568                public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
569                    return null;
570                }
571
572                @Override
573                public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
574                    return null;
575                }
576
577                @Override
578                public void addListener(String s) {
579
580                }
581
582                @Override
583                public <T extends EventListener> void addListener(T t) {
584
585                }
586
587                @Override
588                public void addListener(Class<? extends EventListener> aClass) {
589
590                }
591
592                @Override
593                public <T extends EventListener> T createListener(Class<T> tClass) throws ServletException {
594                    return null;
595                }
596
597                @Override
598                public JspConfigDescriptor getJspConfigDescriptor() {
599                    return null;
600                }
601
602                @Override
603                public ClassLoader getClassLoader() {
604                    return null;
605                }
606
607                @Override
608                public void declareRoles(String... strings) {
609
610                }
611
612                @Override
613                public String getVirtualServerName() {
614                    return null;
615                }
616
617            };
618        }
619
620        public void forward(String string) throws ServletException, IOException {
621        }
622
623        public void include(String string) throws ServletException, IOException {
624        }
625
626        public void include(String string, boolean b) throws ServletException, IOException {
627        }
628
629        public void handlePageException(Exception exception) throws ServletException, IOException {
630        }
631
632        public void handlePageException(Throwable throwable) throws ServletException, IOException {
633        }
634
635        public void setAttribute(String string, Object object) {
636        }
637
638        public void setAttribute(String string, Object object, int i) {
639        }
640
641        public Object getAttribute(String string) {
642            return null; 
643        }
644
645        public Object getAttribute(String string, int i) {
646            return null; 
647        }
648
649        public Object findAttribute(String string) {
650            return null; 
651        }
652
653        public void removeAttribute(String string) {
654        }
655
656        public void removeAttribute(String string, int i) {
657        }
658
659        public int getAttributesScope(String string) {
660            return 0; 
661        }
662
663        public Enumeration<String> getAttributeNamesInScope(int i) {
664            return null; 
665        }
666
667        public JspWriter getOut() {
668            return out;
669        }
670
671        public ExpressionEvaluator getExpressionEvaluator() {
672            return null; 
673        }
674
675        public VariableResolver getVariableResolver() {
676            return null; 
677        }
678
679                @Override
680                public ELContext getELContext() {
681                        return null;
682                }
683    }
684
685    public static PageContext getDummyPageContext(final Locale l, final ServletRequest request) {
686        return new PageContext() {
687            public void initialize(Servlet servlet, ServletRequest servletRequest, ServletResponse servletResponse,
688                    String string, boolean b, int i, boolean b1) throws IOException, IllegalStateException,
689                    IllegalArgumentException {
690            }
691
692            public void release() {
693            }
694
695            public HttpSession getSession() {
696                return null;
697            }
698
699            public Object getPage() {
700                return null;
701            }
702
703            public ServletRequest getRequest() {
704                return request;
705            }
706
707            public ServletResponse getResponse() {
708                return new ServletResponse() {
709                    public String getCharacterEncoding() {
710                        return null;
711                    }
712
713                    public String getContentType() {
714                        return null;
715                    }
716
717                    public ServletOutputStream getOutputStream() throws IOException {
718                        return null;
719                    }
720
721                    public PrintWriter getWriter() throws IOException {
722                        return null;
723                    }
724
725                    public void setCharacterEncoding(String string) {
726                    }
727
728                    public void setContentLength(int i) {
729                    }
730
731                    @Override
732                    public void setContentLengthLong(long l) {
733
734                    }
735
736                    public void setContentType(String string) {
737                    }
738
739                    public void setBufferSize(int i) {
740                    }
741
742                    public int getBufferSize() {
743                        return 0;
744                    }
745
746                    public void flushBuffer() throws IOException {
747                    }
748
749                    public void resetBuffer() {
750                    }
751
752                    public boolean isCommitted() {
753                        return false;
754                    }
755
756                    public void reset() {
757                    }
758
759                    public void setLocale(Locale locale) {
760                    }
761
762                    public Locale getLocale() {
763                        return l;
764                    }
765                };
766            }
767
768            public Exception getException() {
769                return null;
770            }
771
772            public ServletConfig getServletConfig() {
773                return null;
774            }
775
776            public ServletContext getServletContext() {
777                return null;
778            }
779
780            public void forward(String string) throws ServletException, IOException {
781            }
782
783            public void include(String string) throws ServletException, IOException {
784            }
785
786            public void include(String string, boolean b) throws ServletException, IOException {
787            }
788
789            public void handlePageException(Exception exception) throws ServletException, IOException {
790            }
791
792            public void handlePageException(Throwable throwable) throws ServletException, IOException {
793            }
794
795            public void setAttribute(String string, Object object) {
796            }
797
798            public void setAttribute(String string, Object object, int i) {
799            }
800
801            public Object getAttribute(String string) {
802                return null;
803            }
804
805            public Object getAttribute(String string, int i) {
806                return null;
807            }
808
809            public Object findAttribute(String string) {
810                return null;
811            }
812
813            public void removeAttribute(String string) {
814            }
815
816            public void removeAttribute(String string, int i) {
817            }
818
819            public int getAttributesScope(String string) {
820                return 0;
821            }
822
823            public Enumeration<String> getAttributeNamesInScope(int i) {
824                return null;
825            }
826
827            public JspWriter getOut() {
828                return null;
829            }
830
831            public ExpressionEvaluator getExpressionEvaluator() {
832                return null;
833            }
834
835            public VariableResolver getVariableResolver() {
836                return null;
837            }
838
839                        @Override
840                        public ELContext getELContext() {
841                                return null;
842                        }
843        };
844    }
845}