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;
039
040import javax.el.ELContext;
041import javax.servlet.AsyncContext;
042import javax.servlet.DispatcherType;
043import javax.servlet.Filter;
044import javax.servlet.FilterRegistration;
045import javax.servlet.RequestDispatcher;
046import javax.servlet.Servlet;
047import javax.servlet.ServletConfig;
048import javax.servlet.ServletContext;
049import javax.servlet.ServletException;
050import javax.servlet.ServletInputStream;
051import javax.servlet.ServletOutputStream;
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    public static class TestPageContext extends PageContext {
264        private final ServletRequest request;
265        private JspWriter out;
266        private final Locale locale;
267
268        public TestPageContext(ServletRequest request) {
269            this.request = request;
270            this.locale = new Locale("en");
271        }
272
273        public TestPageContext(ServletRequest request, JspWriter out, Locale locale) {
274            this.request = request;
275            this.out = out;
276            this.locale = locale;
277        }
278
279        public void initialize(Servlet servlet, ServletRequest servletRequest, ServletResponse servletResponse,
280                String string, boolean b, int i, boolean b1) throws IOException, IllegalStateException,
281                IllegalArgumentException {
282            // To change body of implemented methods use File | Settings | File
283            // Templates.
284        }
285
286        public void release() {
287            // To change body of implemented methods use File | Settings | File
288            // Templates.
289        }
290
291        public HttpSession getSession() {
292            return null; // To change body of implemented methods use File |
293            // Settings | File Templates.
294        }
295
296        public Object getPage() {
297            return null; // To change body of implemented methods use File |
298            // Settings | File Templates.
299        }
300
301        public ServletRequest getRequest() {
302            return request;
303        }
304
305        public ServletResponse getResponse() {
306            return new ServletResponse() {
307
308                public String getCharacterEncoding() {
309                    return null; // To change body of implemented methods use
310                    // File | Settings | File Templates.
311                }
312
313                public String getContentType() {
314                    return null; // To change body of implemented methods use
315                    // File | Settings | File Templates.
316                }
317
318                public ServletOutputStream getOutputStream() throws IOException {
319                    return null; // To change body of implemented methods use
320                    // File | Settings | File Templates.
321                }
322
323                public PrintWriter getWriter() throws IOException {
324                    return null; // To change body of implemented methods use
325                    // File | Settings | File Templates.
326                }
327
328                public void setCharacterEncoding(String string) {
329                    // To change body of implemented methods use File | Settings
330                    // | File Templates.
331                }
332
333                public void setContentLength(int i) {
334                    // To change body of implemented methods use File | Settings
335                    // | File Templates.
336                }
337
338                @Override
339                public void setContentLengthLong(long l) {
340
341                }
342
343                public void setContentType(String string) {
344                    // To change body of implemented methods use File | Settings
345                    // | File Templates.
346                }
347
348                public void setBufferSize(int i) {
349                    // To change body of implemented methods use File | Settings
350                    // | File Templates.
351                }
352
353                public int getBufferSize() {
354                    return 0; // To change body of implemented methods use File
355                    // | Settings | File Templates.
356                }
357
358                public void flushBuffer() throws IOException {
359                    // To change body of implemented methods use File | Settings
360                    // | File Templates.
361                }
362
363                public void resetBuffer() {
364                    // To change body of implemented methods use File | Settings
365                    // | File Templates.
366                }
367
368                public boolean isCommitted() {
369                    return false; // To change body of implemented methods use
370                    // File | Settings | File Templates.
371                }
372
373                public void reset() {
374                    // To change body of implemented methods use File | Settings
375                    // | File Templates.
376                }
377
378                public void setLocale(Locale locale) {
379                    // To change body of implemented methods use File | Settings
380                    // | File Templates.
381                }
382
383                public Locale getLocale() {
384                    return locale;
385                }
386            };
387        }
388
389        public Exception getException() {
390            return null; // To change body of implemented methods use File |
391            // Settings | File Templates.
392        }
393
394        public ServletConfig getServletConfig() {
395            return null; // To change body of implemented methods use File |
396            // Settings | File Templates.
397        }
398
399        public ServletContext getServletContext() {
400            return new ServletContext() {
401                public String getContextPath() {
402                    return null; // To change body of implemented methods use
403                    // File | Settings | File Templates.
404                }
405
406                public ServletContext getContext(String string) {
407                    return null; // To change body of implemented methods use
408                    // File | Settings | File Templates.
409                }
410
411                public int getMajorVersion() {
412                    return 0; // To change body of implemented methods use File
413                    // | Settings | File Templates.
414                }
415
416                public int getMinorVersion() {
417                    return 0; // To change body of implemented methods use File
418                    // | Settings | File Templates.
419                }
420
421                @Override
422                public int getEffectiveMajorVersion() {
423                    return 0;
424                }
425
426                @Override
427                public int getEffectiveMinorVersion() {
428                    return 0;
429                }
430
431                public String getMimeType(String string) {
432                    return null; // To change body of implemented methods use
433                    // File | Settings | File Templates.
434                }
435
436                public Set getResourcePaths(String string) {
437                    return null; // To change body of implemented methods use
438                    // File | Settings | File Templates.
439                }
440
441                public URL getResource(String string) throws MalformedURLException {
442                    return null; // To change body of implemented methods use
443                    // File | Settings | File Templates.
444                }
445
446                public InputStream getResourceAsStream(String string) {
447                    return null; // To change body of implemented methods use
448                    // File | Settings | File Templates.
449                }
450
451                public RequestDispatcher getRequestDispatcher(String string) {
452                    return new RequestDispatcher() {
453
454                        public void forward(ServletRequest servletRequest, ServletResponse servletResponse)
455                                throws ServletException, IOException {
456                        }
457
458                        public void include(ServletRequest servletRequest, ServletResponse servletResponse)
459                                throws ServletException, IOException {
460                            // To change body of implemented methods use File |
461                            // Settings | File Templates.
462                        }
463                    };
464                }
465
466                public RequestDispatcher getNamedDispatcher(String string) {
467                    return null; // To change body of implemented methods use
468                    // File | Settings | File Templates.
469                }
470
471                public Servlet getServlet(String string) throws ServletException {
472                    return null; // To change body of implemented methods use
473                    // File | Settings | File Templates.
474                }
475
476                public Enumeration getServlets() {
477                    return null; // To change body of implemented methods use
478                    // File | Settings | File Templates.
479                }
480
481                public Enumeration getServletNames() {
482                    return null; // To change body of implemented methods use
483                    // File | Settings | File Templates.
484                }
485
486                public void log(String string) {
487                    // To change body of implemented methods use File | Settings
488                    // | File Templates.
489                }
490
491                public void log(Exception exception, String string) {
492                    // To change body of implemented methods use File | Settings
493                    // | File Templates.
494                }
495
496                public void log(String string, Throwable throwable) {
497                    // To change body of implemented methods use File | Settings
498                    // | File Templates.
499                }
500
501                public String getRealPath(String string) {
502                    return null; // To change body of implemented methods use
503                    // File | Settings | File Templates.
504                }
505
506                public String getServerInfo() {
507                    return null; // To change body of implemented methods use
508                    // File | Settings | File Templates.
509                }
510
511                public String getInitParameter(String string) {
512                    return null; // To change body of implemented methods use
513                    // File | Settings | File Templates.
514                }
515
516                public Enumeration getInitParameterNames() {
517                    return null; // To change body of implemented methods use
518                    // File | Settings | File Templates.
519                }
520
521                @Override
522                public boolean setInitParameter(String s, String s2) {
523                    return false;
524                }
525
526                public Object getAttribute(String string) {
527                    return null; // To change body of implemented methods use
528                    // File | Settings | File Templates.
529                }
530
531                public Enumeration getAttributeNames() {
532                    return null; // To change body of implemented methods use
533                    // File | Settings | File Templates.
534                }
535
536                public void setAttribute(String string, Object object) {
537                    // To change body of implemented methods use File | Settings
538                    // | File Templates.
539                }
540
541                public void removeAttribute(String string) {
542                    // To change body of implemented methods use File | Settings
543                    // | File Templates.
544                }
545
546                public String getServletContextName() {
547                    return null; // To change body of implemented methods use
548                    // File | Settings | File Templates.
549                }
550
551                @Override
552                public ServletRegistration.Dynamic addServlet(String s, String s2) {
553                    return null;
554                }
555
556                @Override
557                public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) {
558                    return null;
559                }
560
561                @Override
562                public ServletRegistration.Dynamic addServlet(String s, Class<? extends Servlet> aClass) {
563                    return null;
564                }
565
566                @Override
567                public <T extends Servlet> T createServlet(Class<T> tClass) throws ServletException {
568                    return null;
569                }
570
571                @Override
572                public ServletRegistration getServletRegistration(String s) {
573                    return null;
574                }
575
576                @Override
577                public Map<String, ? extends ServletRegistration> getServletRegistrations() {
578                    return null;
579                }
580
581                @Override
582                public FilterRegistration.Dynamic addFilter(String s, String s2) {
583                    return null;
584                }
585
586                @Override
587                public FilterRegistration.Dynamic addFilter(String s, Filter filter) {
588                    return null;
589                }
590
591                @Override
592                public FilterRegistration.Dynamic addFilter(String s, Class<? extends Filter> aClass) {
593                    return null;
594                }
595
596                @Override
597                public <T extends Filter> T createFilter(Class<T> tClass) throws ServletException {
598                    return null;
599                }
600
601                @Override
602                public FilterRegistration getFilterRegistration(String s) {
603                    return null;
604                }
605
606                @Override
607                public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
608                    return null;
609                }
610
611                @Override
612                public SessionCookieConfig getSessionCookieConfig() {
613                    return null;
614                }
615
616                @Override
617                public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
618
619                }
620
621                @Override
622                public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
623                    return null;
624                }
625
626                @Override
627                public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
628                    return null;
629                }
630
631                @Override
632                public void addListener(String s) {
633
634                }
635
636                @Override
637                public <T extends EventListener> void addListener(T t) {
638
639                }
640
641                @Override
642                public void addListener(Class<? extends EventListener> aClass) {
643
644                }
645
646                @Override
647                public <T extends EventListener> T createListener(Class<T> tClass) throws ServletException {
648                    return null;
649                }
650
651                @Override
652                public JspConfigDescriptor getJspConfigDescriptor() {
653                    return null;
654                }
655
656                @Override
657                public ClassLoader getClassLoader() {
658                    return null;
659                }
660
661                @Override
662                public void declareRoles(String... strings) {
663
664                }
665
666                @Override
667                public String getVirtualServerName() {
668                    return null;
669                }
670            };
671        }
672
673        public void forward(String string) throws ServletException, IOException {
674            // To change body of implemented methods use File | Settings | File
675            // Templates.
676        }
677
678        public void include(String string) throws ServletException, IOException {
679            // To change body of implemented methods use File | Settings | File
680            // Templates.
681        }
682
683        public void include(String string, boolean b) throws ServletException, IOException {
684            // To change body of implemented methods use File | Settings | File
685            // Templates.
686        }
687
688        public void handlePageException(Exception exception) throws ServletException, IOException {
689            // To change body of implemented methods use File | Settings | File
690            // Templates.
691        }
692
693        public void handlePageException(Throwable throwable) throws ServletException, IOException {
694            // To change body of implemented methods use File | Settings | File
695            // Templates.
696        }
697
698        public void setAttribute(String string, Object object) {
699            // To change body of implemented methods use File | Settings | File
700            // Templates.
701        }
702
703        public void setAttribute(String string, Object object, int i) {
704            // To change body of implemented methods use File | Settings | File
705            // Templates.
706        }
707
708        public Object getAttribute(String string) {
709            return null; // To change body of implemented methods use File |
710            // Settings | File Templates.
711        }
712
713        public Object getAttribute(String string, int i) {
714            return null; // To change body of implemented methods use File |
715            // Settings | File Templates.
716        }
717
718        public Object findAttribute(String string) {
719            return null; // To change body of implemented methods use File |
720            // Settings | File Templates.
721        }
722
723        public void removeAttribute(String string) {
724            // To change body of implemented methods use File | Settings | File
725            // Templates.
726        }
727
728        public void removeAttribute(String string, int i) {
729            // To change body of implemented methods use File | Settings | File
730            // Templates.
731        }
732
733        public int getAttributesScope(String string) {
734            return 0; // To change body of implemented methods use File |
735            // Settings | File Templates.
736        }
737
738        public Enumeration<String> getAttributeNamesInScope(int i) {
739            return null; // To change body of implemented methods use File |
740            // Settings | File Templates.
741        }
742
743        public JspWriter getOut() {
744            return out;
745        }
746
747        public ExpressionEvaluator getExpressionEvaluator() {
748            return null; // To change body of implemented methods use File |
749            // Settings | File Templates.
750        }
751
752        public VariableResolver getVariableResolver() {
753            return null; // To change body of implemented methods use File |
754            // Settings | File Templates.
755        }
756
757        public ELContext getELContext() {
758            return null; // To change body of implemented methods use File |
759            // Settings | File Templates.
760        }
761    }
762
763    public static PageContext getDummyPageContext(final Locale l, final ServletRequest request) {
764        return new PageContext() {
765            public void initialize(Servlet servlet, ServletRequest servletRequest, ServletResponse servletResponse,
766                    String string, boolean b, int i, boolean b1) throws IOException, IllegalStateException,
767                    IllegalArgumentException {
768            }
769
770            public void release() {
771            }
772
773            public HttpSession getSession() {
774                return null;
775            }
776
777            public Object getPage() {
778                return null;
779            }
780
781            public ServletRequest getRequest() {
782                return request;
783            }
784
785            public ServletResponse getResponse() {
786                return new ServletResponse() {
787                    public String getCharacterEncoding() {
788                        return null;
789                    }
790
791                    public String getContentType() {
792                        return null;
793                    }
794
795                    public ServletOutputStream getOutputStream() throws IOException {
796                        return null;
797                    }
798
799                    public PrintWriter getWriter() throws IOException {
800                        return null;
801                    }
802
803                    public void setCharacterEncoding(String string) {
804                    }
805
806                    public void setContentLength(int i) {
807                    }
808
809                    @Override
810                    public void setContentLengthLong(long l) {
811
812                    }
813
814                    public void setContentType(String string) {
815                    }
816
817                    public void setBufferSize(int i) {
818                    }
819
820                    public int getBufferSize() {
821                        return 0;
822                    }
823
824                    public void flushBuffer() throws IOException {
825                    }
826
827                    public void resetBuffer() {
828                    }
829
830                    public boolean isCommitted() {
831                        return false;
832                    }
833
834                    public void reset() {
835                    }
836
837                    public void setLocale(Locale locale) {
838                    }
839
840                    public Locale getLocale() {
841                        return l;
842                    }
843                };
844            }
845
846            public Exception getException() {
847                return null;
848            }
849
850            public ServletConfig getServletConfig() {
851                return null;
852            }
853
854            public ServletContext getServletContext() {
855                return null;
856            }
857
858            public void forward(String string) throws ServletException, IOException {
859            }
860
861            public void include(String string) throws ServletException, IOException {
862            }
863
864            public void include(String string, boolean b) throws ServletException, IOException {
865            }
866
867            public void handlePageException(Exception exception) throws ServletException, IOException {
868            }
869
870            public void handlePageException(Throwable throwable) throws ServletException, IOException {
871            }
872
873            public void setAttribute(String string, Object object) {
874            }
875
876            public void setAttribute(String string, Object object, int i) {
877            }
878
879            public Object getAttribute(String string) {
880                return null;
881            }
882
883            public Object getAttribute(String string, int i) {
884                return null;
885            }
886
887            public Object findAttribute(String string) {
888                return null;
889            }
890
891            public void removeAttribute(String string) {
892            }
893
894            public void removeAttribute(String string, int i) {
895            }
896
897            public int getAttributesScope(String string) {
898                return 0;
899            }
900
901            public Enumeration<String> getAttributeNamesInScope(int i) {
902                return null;
903            }
904
905            public JspWriter getOut() {
906                return null;
907            }
908
909            public ExpressionEvaluator getExpressionEvaluator() {
910                return null;
911            }
912
913            public VariableResolver getVariableResolver() {
914                return null;
915            }
916
917            public ELContext getELContext() {
918                return null;
919            }
920        };
921    }
922}