Example usage for java.util Enumeration Enumeration

List of usage examples for java.util Enumeration Enumeration

Introduction

In this page you can find the example usage for java.util Enumeration Enumeration.

Prototype

Enumeration

Source Link

Usage

From source file:com.siberhus.web.ckeditor.servlet.MultipartServletRequest.java

@Override
public Enumeration<String> getParameterNames() {
    final Iterator<String> params = paramMap.keySet().iterator();
    return new Enumeration<String>() {
        @Override// w w w  .  j a  v a2 s .  c om
        public boolean hasMoreElements() {
            return params.hasNext();
        }

        @Override
        public String nextElement() {
            return params.next();
        }
    };
}

From source file:org.openo.nfvo.resmanagement.common.util.request.RequestUtilTest.java

@SuppressWarnings("rawtypes")
@Test/*from   w  ww.  j  av a 2  s . co m*/
public void testGetAllJsonRequestBodyRequestBodyIsNull() {
    HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {

        @Mocked
        ServletInputStream input;

        @Mock
        public ServletInputStream getInputStream() throws IOException {
            return input;
        }

        @Mock
        public Enumeration getHeaderNames() {
            return new Enumeration() {

                List<String> a = Arrays.asList(new String[] { "1", "2" });

                @Override
                public boolean hasMoreElements() {
                    return false;
                }

                @Override
                public Object nextElement() {
                    return null;
                }

            };
        }

    }.getMockInstance();
    new MockUp<RequestUtil>() {

        @Mock
        public JSONObject getJsonRequestBody(HttpServletRequest context) {
            return null;
        }
    };
    JSONObject result = RequestUtil.getAllJsonRequestBody(context);
    JSONObject expectedResult = new JSONObject();
    expectedResult.put("header", new HashMap<String, String>());
    assertEquals(expectedResult, result);
}

From source file:org.codice.opendj.embedded.server.LDAPManagerTest.java

private BundleContext createMockContext(final File dataFolderPath) {
    Bundle mockBundle = Mockito.mock(Bundle.class);
    Mockito.when(mockBundle.findEntries(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean()))
            .then(new Answer<Enumeration<URL>>() {

                @Override//from  ww w.  ja va2  s . c  o m
                public Enumeration<URL> answer(InvocationOnMock invocation) throws Throwable {

                    Object[] arguments = invocation.getArguments();
                    String path = arguments[0].toString();
                    String filePattern = arguments[1].toString();
                    boolean recurse = (Boolean) arguments[2];
                    final URL url = this.getClass().getResource(path);
                    File pathFile = null;
                    try {
                        pathFile = new File(url.toURI());
                    } catch (URISyntaxException e) {
                        throw new RuntimeException("Unable to resolve file path", e);
                    }
                    final File[] files = pathFile.listFiles((FileFilter) new WildcardFileFilter(filePattern));
                    Enumeration<URL> enumer = new Enumeration<URL>() {
                        int place = 0;

                        List<File> urlList = Arrays.asList(files);

                        @Override
                        public boolean hasMoreElements() {
                            return place < urlList.size();
                        }

                        @Override
                        public URL nextElement() {
                            File file = urlList.get(place++);
                            try {
                                return file.toURL();
                            } catch (MalformedURLException e) {
                                throw new RuntimeException("Unable to convert to URL", e);
                            }
                        }
                    };
                    return enumer;
                }

            });
    Mockito.when(mockBundle.getResource(Mockito.anyString())).then(new Answer<URL>() {

        @Override
        public URL answer(InvocationOnMock invocation) throws Throwable {
            return this.getClass().getResource((String) invocation.getArguments()[0]);
        }

    });
    BundleContext mockContext = Mockito.mock(BundleContext.class);
    Mockito.when(mockContext.getDataFile(Mockito.anyString())).then(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            String filename = invocation.getArguments()[0].toString();
            if (dataFolderPath != null) {
                return new File(dataFolderPath + "/" + filename);
            } else {
                return null;
            }
        }

    });
    Mockito.when(mockContext.getBundle()).thenReturn(mockBundle);

    return mockContext;
}

From source file:ddf.ldap.embedded.server.LDAPManagerTest.java

private BundleContext createMockContext(final File dataFolderPath) {
    Bundle mockBundle = Mockito.mock(Bundle.class);
    Mockito.when(mockBundle.findEntries(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean()))
            .then(new Answer<Enumeration<URL>>() {

                @Override//w w w  .  j  ava2 s .  c  om
                public Enumeration<URL> answer(InvocationOnMock invocation) throws Throwable {

                    Object[] arguments = invocation.getArguments();
                    String path = arguments[0].toString();
                    String filePattern = arguments[1].toString();
                    boolean recurse = (Boolean) arguments[2];
                    final URL url = this.getClass().getResource(path);
                    File pathFile = null;
                    try {
                        pathFile = new File(url.toURI());
                    } catch (URISyntaxException e) {
                        throw new RuntimeException("Unable to resolve file path", e);
                    }
                    final File[] files = pathFile.listFiles((FileFilter) new WildcardFileFilter(filePattern));
                    Enumeration<URL> enumer = new Enumeration<URL>() {
                        int place = 0;
                        List<File> urlList = Arrays.asList(files);

                        @Override
                        public boolean hasMoreElements() {
                            return place < urlList.size();
                        }

                        @Override
                        public URL nextElement() {
                            File file = urlList.get(place++);
                            try {
                                return file.toURL();
                            } catch (MalformedURLException e) {
                                throw new RuntimeException("Unable to convert to URL", e);
                            }
                        }
                    };
                    return enumer;
                }

            });
    Mockito.when(mockBundle.getResource(Mockito.anyString())).then(new Answer<URL>() {

        @Override
        public URL answer(InvocationOnMock invocation) throws Throwable {
            return this.getClass().getResource((String) invocation.getArguments()[0]);
        }

    });
    BundleContext mockContext = Mockito.mock(BundleContext.class);
    Mockito.when(mockContext.getDataFile(Mockito.anyString())).then(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            String filename = invocation.getArguments()[0].toString();
            if (dataFolderPath != null) {
                return new File(dataFolderPath + "/" + filename);
            } else {
                return null;
            }
        }

    });
    Mockito.when(mockContext.getBundle()).thenReturn(mockBundle);

    return mockContext;
}

From source file:pt.webdetails.cpf.Util.java

public static <T> Enumeration<T> toEnumeration(final T[] array) {
    return new Enumeration<T>() {
        int idx = 0;

        public boolean hasMoreElements() {
            return idx < array.length;
        }//from www  .j  a  v a2 s .  c o  m

        @Override
        public T nextElement() {
            return array[idx++];
        }
    };
}

From source file:org.alfresco.opencmis.CMISHttpServletRequest.java

@SuppressWarnings("unchecked")
@Override/*from   w ww. j a  va2 s.c  om*/
public Enumeration getAttributeNames() {
    Enumeration e = httpReq.getAttributeNames();
    List attrNames = new ArrayList();
    while (e.hasMoreElements()) {
        attrNames.add(e.nextElement());
    }
    attrNames.add(Dispatcher.BASE_URL_ATTRIBUTE);
    final Iterator it = attrNames.iterator();

    return new Enumeration() {
        public boolean hasMoreElements() {
            return it.hasNext();
        }

        public Object nextElement() {
            return it.next();
        }
    };
}

From source file:net.wastl.webmail.storage.simple.SimpleStorage.java

public Enumeration getUsers(String domain) {
    String path = parent.getProperty("webmail.data.path") + System.getProperty("file.separator") + domain
            + System.getProperty("file.separator");

    File f = new File(path);
    if (f.canRead() && f.isDirectory()) {
        final String[] files = f.list(new FilenameFilter() {
            public boolean accept(File file, String s) {
                if (s.endsWith(".xml")) {
                    return true;
                } else {
                    return false;
                }/*  w  w w  . j a v  a  2 s  .  com*/
            }
        });
        return new Enumeration() {
            int i = 0;

            public boolean hasMoreElements() {
                return i < files.length;
            }

            public Object nextElement() {
                int cur = i++;
                return files[cur].substring(0, files[cur].length() - 4);
            }
        };
    } else {
        log.warn("SimpleStorage: Could not list files in directory " + path);
        return new Enumeration() {
            public boolean hasMoreElements() {
                return false;
            }

            public Object nextElement() {
                return null;
            }
        };
    }
}

From source file:xworker.http.MultiPartRequest.java

public Enumeration getParameterNames() {
    final Iterator keys = parmeters.keySet().iterator();
    return new Enumeration() {

        public boolean hasMoreElements() {
            return keys.hasNext();
        }// w  w w. j  a v a  2  s .  c om

        public Object nextElement() {
            return keys.next();
        }

    };
}

From source file:org.openo.nfvo.resmanagement.common.util.request.RequestUtilTest.java

@SuppressWarnings("rawtypes")
@Test/* w w w .ja  v  a2  s. c  o  m*/
public void testGetContextHeader() {
    HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() {

        @Mock
        public String getHeader(String name) {
            return "1";
        }

        @Mock
        public Enumeration getHeaderNames() {
            return new Enumeration() {

                List<String> a = Arrays.asList(new String[] { "1", "2" });

                int count = 1;

                @Override
                public boolean hasMoreElements() {
                    if (count == 1) {
                        count += 1;
                        return true;
                    } else
                        return false;
                }

                @Override
                public Object nextElement() {
                    return "1";
                }

            };
        }

    }.getMockInstance();
    new MockUp<RequestUtil>() {

        @Mock
        public JSONObject getJsonRequestBody(HttpServletRequest context) {
            return null;
        }
    };
    JSONObject result = RequestUtil.getAllJsonRequestBody(context);
    JSONObject expectedResult = new JSONObject();
    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "1");
    expectedResult.put("header", map);
    assertEquals(expectedResult, result);
}

From source file:org.amplafi.jawr.maven.JawrMojo.java

private void setupJawrConfig(ServletConfig config, ServletContext context, final Map<String, Object> attributes,
        final Response respData) {
    expect(config.getServletContext()).andReturn(context).anyTimes();
    expect(config.getServletName()).andReturn("maven-jawr-plugin").anyTimes();

    context.log(isA(String.class));
    expectLastCall().anyTimes();/*from  w  w w.jav  a  2 s. com*/

    expect(context.getResourcePaths(isA(String.class))).andAnswer(new IAnswer<Set>() {

        public Set<String> answer() throws Throwable {
            final Set<String> set = new HashSet<String>();

            // hack to disallow orphan bundles
            Exception e = new Exception();
            for (StackTraceElement trace : e.getStackTrace()) {
                if (trace.getClassName().endsWith("OrphanResourceBundlesMapper")) {
                    return set;
                }
            }

            String path = (String) EasyMock.getCurrentArguments()[0];
            File file = new File(getRootPath() + path);

            if (file.exists() && file.isDirectory()) {
                for (String one : file.list()) {
                    set.add(path + one);
                }
            }

            return set;
        }

    }).anyTimes();

    expect(context.getResourceAsStream(isA(String.class))).andAnswer(new IAnswer<InputStream>() {

        public InputStream answer() throws Throwable {
            String path = (String) EasyMock.getCurrentArguments()[0];
            File file = new File(getRootPath(), path);
            return new FileInputStream(file);
        }

    }).anyTimes();

    expect(context.getAttribute(isA(String.class))).andAnswer(new IAnswer<Object>() {

        public Object answer() throws Throwable {
            return attributes.get(EasyMock.getCurrentArguments()[0]);
        }

    }).anyTimes();

    context.setAttribute(isA(String.class), isA(Object.class));
    expectLastCall().andAnswer(new IAnswer<Object>() {

        public Object answer() throws Throwable {
            String key = (String) EasyMock.getCurrentArguments()[0];
            Object value = EasyMock.getCurrentArguments()[1];
            attributes.put(key, value);
            return null;
        }

    }).anyTimes();

    expect(config.getInitParameterNames()).andReturn(new Enumeration<String>() {

        public boolean hasMoreElements() {
            return false;
        }

        public String nextElement() {
            return null;
        }

    }).anyTimes();

    expect(config.getInitParameter(JawrConstant.TYPE_INIT_PARAMETER)).andAnswer(new IAnswer<String>() {
        public String answer() throws Throwable {
            return respData == null ? null : respData.getType();
        }
    }).anyTimes();

    expect(config.getInitParameter("configLocation")).andReturn(getConfigLocation()).anyTimes();
    expect(config.getInitParameter("configPropertiesSourceClass")).andReturn(null).anyTimes();
}